$(document).ready(function () {
    //set out intial text for the search box
var initialText = "Search FNSBSD";

//identify our search box by id
var searchBox = document.getElementById("edit-search-theme-form-1");

function resetSearchBox(){ //if someone leaves the search box empty it reverts back to the standard message
	if(searchBox.value == ""){
		searchBox.style.color = "#999999";
		searchBox.value = initialText;
	}
}

function focusSearchBox(){ //clear the search box of the standard message and change the font color when someone focuses on the text box
	if (searchBox.value == initialText){
		searchBox.value = "";
	}
	searchBox.style.color = "black";
}

//Set the initial text in the box
searchBox.value = initialText;

//set the initial color in the box
if(searchBox.value == initialText){
	searchBox.style.color = "#999999";
}

//add onfocus and onblur actions to the search box
searchBox.onfocus = focusSearchBox;
searchBox.onblur = resetSearchBox;
});


//THIS EXPANDS ANY ADVANCED BOOK BLOCK MAIN LINKS SO THAT THE NAVIGATION IS ALWAYS THERE
$(document).ready(function(){
	$(".block-advancedbookblocks li ul").eq(0).show();
	$(".block-advancedbookblocks ul li.parent span.parent").eq(0).hide();
	$(".block-advancedbookblocks ul li").eq(0).css('margin-left','-10px');
	
	//THIS DOES SPECIFICALLY THE BOND BLOCK
	$("#block-advancedbookblocks-60 li ul").eq(0).show();
	$("#block-advancedbookblocks-60 ul li.parent span.parent").eq(0).hide();
	$("#block-advancedbookblocks-60 ul li").eq(0).css('margin-left','-10px');
	

});
						 

//THIS HANDLES THE OPEN AND CLOSE BUTTON ON THE FLOAT ADMIN MENU
$(document).ready(function(){
	
	if (document.cookie.length>0){
	  c_name = 'float_menu_state';
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		var float_menu_state = unescape(document.cookie.substring(c_start,c_end));
		}
  	}
	
	if(float_menu_state == "closed"){
		$("#add-page a, #add-event a, #add-announcement a, #banner-gui-link a").css('display','none');
		$("#float-menu-icon img").attr('src','/custom/imgs/arrow_out.png');
		$("#float-menu").css('width','27px');
		$("#float-menu").css('height','29px');
		$("#float-menu-icon img").css('border-width','0px');
		$("#float-menu-icon img").toggleClass("float-menu-closed");
		$("#float-menu-icon img").toggleClass("float-menu-opened");
		
		
	}
						   
	$("#float-menu-icon img").click(function(){
											 	if($("#float-menu-icon img").hasClass('float-menu-opened')){
													$("#add-page a, #add-event a, #add-announcement a, #banner-gui-link a").fadeOut();
													$("#float-menu-icon img").toggleClass("float-menu-closed");
													$("#float-menu-icon img").toggleClass("float-menu-opened");
													$("#float-menu-icon img").attr('src','/custom/imgs/arrow_out.png');
													$("#float-menu-icon img").css('border-width','0px');
													$("#float-menu").animate({ 
																				width: "27px",
																				height: "29px"
																			  }, 'normal' );
													setCookie('float_menu_state','closed',1);

													
												}else{
													$("#add-page a, #add-event a, #add-announcement a, #banner-gui-link a").fadeIn();
													$("#float-menu-icon img").toggleClass("float-menu-closed");
													$("#float-menu-icon img").toggleClass("float-menu-opened");
													$("#float-menu-icon img").attr('src','/custom/imgs/cross.png');
													$("#float-menu-icon img").css('border-width','1px');
													$("#float-menu").animate({ 
																				width: "180px",
																				height: "110px"
																			  }, 'normal' );
													setCookie('float_menu_state','open',1);
												}
											 });


});


function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";	
}


function init(){
	document.banner_ready_state = "ready";
}
window.onload = init;

$(document).ready(function(){
	$('span.teacher_web_page_link a').click(function(){
		pageTracker._trackEvent('Outgoing links', 'Teacher Web Page', this.href);	
	});
	$('#teacher_web_page a').click(function(){
		pageTracker._trackEvent('Outgoing links', 'Teacher Web Page', this.href);	
	});
	
	$('#v-menu li.single-teacher a').click(function(){
		pageTracker._trackEvent('Outgoing links', 'Teacher Web Page', this.href);	
	});
	
	$('#v-menu li.multi-sites a').click(function(){
		pageTracker._trackEvent('Outgoing links', 'Teacher Web Page', this.href);	
	});
	
});

//EMAIL OBSFUCATION
$(document).ready(function(){
	$('dir').each(function(){
						   //alert(this.id);
						  $.ajax({
						   type: "POST",
						   url: "/custom/widgets/decode_email.php",
						   data: "email="+this.id,
						   dataType: 'html',
						   success: function(msg,data, id ){                
							  var split_array = msg.split('|');
							  $('#'+split_array[0]).attr('innerHTML',split_array[1]);
						   }
						 });
						   });					   
});




//VIDEO PLAYER GOOGLE ANALYTICS FUNCTIONS
var player;
var buffered = 0;
var playlist;
var media;
function playerReady(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	player = document.getElementById(obj['id']);
	player.addModelListener("STATE","FNSBSDVideoPlayerendTracker");
	player.addModelListener("TIME","FNSBSDVideoPlayertimeTracker");
	playlist = player.getPlaylist()[0].file;
	media = playlist.split('custom/video/media/');
	//alert(media[1]);
	//alert('Hello Dolly');
	//alert(getConfig());
};


//var player = document.getElementById('ply');
//alert (player.getConfig().autostart);



var howFarDidTheyGet = 0;
var finishedWatching = 0;
function FNSBSDVideoPlayerendTracker(obj){
	if(obj['newstate'] == 'COMPLETED'){
		finishedWatching = 1;
		//alert ('I watched the whole thing without falling asleep');
                pageTracker._trackEvent('Videos', media[1], 'Finish');
				//alert('Finish');
	}
	if(obj['newstate'] == 'BUFFERING'){
		buffered++;
		if(buffered > 1){
			//alert('I have had to buffer '+ buffered + ' times');
                        pageTracker._trackEvent('Videos', media[1], 'Buffered', buffered);
						pageTracker._trackEvent('Video Player', 'Buffered', media[1]);
		}
	}
}

function FNSBSDVideoPlayertimeTracker(obj){
	if(obj['position']){
		howFarDidTheyGet = obj['position'];
	}
	if(obj['position'] == 1){
		pageTracker._trackEvent('Videos', media[1], 'Start');
		//alert('Start');
		//alert ('1 sec in');
		//alert (playlist[1]);
	}
	var check_time = obj['position'] % 30;
	if(check_time == 0 &&  obj['position'] != 0){
		//alert (obj['position']+' sec in');
                pageTracker._trackEvent('Videos', media[1], ''+obj['position']+' secs'); 
				//alert(obj['position']);
	}
	
}

$(window).unload(function() {
  var tellGAhowfar = '';
  if(finishedWatching){
	  tellGAhowfar = 'Finished';
	  pageTracker._trackEvent('Video Player', media[1], 'Finished');
  }
  else if(howFarDidTheyGet){
	  tellGAhowfar = parseInt(howFarDidTheyGet)
	  pageTracker._trackEvent('Video Player', media[1], ''+parseInt(howFarDidTheyGet)+' secs');
  }
});



//THOSE SNEAKY DEVILS
$(document).ready(function(){
	$('#edit-submit').css('visibility','visible');
	$('#edit-preview').css('visibility','visible');
	$('#edit-publish').css('visibility','visible');
	$('#edit-unpublish').css('visibility','visible');
	$('#edit-delete').css('visibility','visible');

});


//MENU TITLE FIELD FUNCTIONS
$(document).ready(function(){
						   if($('#edit-field-menu-link-0-value').val() == ''){
						   		$('#edit-field-menu-link-0-value').val($('#edit-title').val());
						   }						  
						   });

$(document).ready(function(){
    if($('#edit-title').val() == ''){
      $('#edit-title').attr('onkeyup', 'menuTitleFunction();');
    }
});

function menuTitleFunction(){
  $('#edit-field-menu-link-0-value').val($('#edit-title').val());
}