$(document).ready(function() {

	/**
	* candy box functionality
	* creates the slide up and down effect on Gale homepage...Does not fire for Safari (defualt open in Safari).
	* Timer is set to 1000 milliseconds before closing, keeps the effect from "bouncing" when quickly moused-over.
	* The slideBack() function used by setTimeout() is at the bottom of this file.
	**/
	if($("#candy") && !$.browser.safari){
		var timerID = 0;
		$(".slidedown").slideUp("fast");
		$(".slideTrigger strong").css("cursor", "pointer").css("color","#0789c4"); //sets some basic styles for usability
		$("#candy").hover(function(){ if(timerID){ clearTimeout(timerID); } $(".slidedown").show("slow"); }, function(){ timerID = setTimeout( "slideBack()", 1000); });
	}

	if($.browser.safari){
		$('body').css('background-image','none');
		$('#go').val('Search');
	}
	//open any anchor with a rel="" in a new window
	if($("a[rel]")){
		$("a[rel]").click(function() {
			/*newWindow = window.open(this, "newWin", "toolbar=yes,location=yes,scrollbars=yes,resizable=yes,width=750,height=550")*/
			newWindow = window.open(this, "newWin", "width=750,height=550,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes")
			newWindow.focus();
			return false; //stop link from working normally
		});
	}
	
	//Code added for Omniture Custom Link tracking -- Updated 09/25/09
	if ($('a[rel1!=""]')){
		$("a[rel1]").click(function() {

			//Home Page promos
			if ($(this).attr('rel1') == "omnipromotrack"){	
				var cus_link_name = ' Gale Home - '+$(this).attr('title')+' - Waterfall Promo';
			}
			
			//Other links
			else if (($(this).attr('rel1') == "omnilinktrack") || ($(this).attr('rel1') == "omniexlinktrack")){	
				var cus_link_name = $(this).attr('title');				
			}
			
			//If title value exists
			if ($(this).attr('title') != ""){	
				
				//Omniture code
				var s = s_gi('thgalecom');
				s.linkTrackVars = 'None';
				s.linkTrackEvents = 'None';	
				
				s.tl($(this),'o',cus_link_name);
			}
			
			//If external link, open in new window
			if ($(this).attr('rel1') == "omniexlinktrack"){
				window.open( $(this).attr('href') );
        		return false;
			}
		});		
	}
	
	/**
	* right navigation slide down when hovered, it is used on the freeresources sections and shows the
	* left navigation which is displayed as "Free Monthly Activities" and when hovered it displays
	* the Black History, Hispanic Heritage, Poet's Corner, and Women's History sections.
	**/ 
	if($("a#freeresources_opener")){
		$("a#freeresources_opener").hover( function(){ $("#freeresources").show("slow"); }, function(){  } );
	}

	/**
	* right promotional functionality
	* creates the sliding promos in the right column on both Gale.com and Galeschools.com
	**/
	// Accordion for homepage promos
	if($("#promo_slider").accordion){
		$("#promo_slider").accordion({
			active: '.default_open',
			header: '.head',
			navigation: false,
			event: 'mouseover',
			autoheight: true,
			animated: 'easeslide'
		});
	}

	$("#searchInput").focus( function() { if($(this).val() == "Enter author, title, or keyword") { $(this).val(""); } });
	$("#searchInput").blur( function() { if($(this).val() == "") { $(this).val("Enter author, title, or keyword"); } });
	
	/**
	* Add remove the "Search for Books" text from the global search
	* box in the header of the catalog.  The search box that is run
	* by the CMS has a onFocus call. The one in the catalog does not.
	**/
	if($("#searchTextAT")){
		$("#searchTextAT").focus( function() { if($(this).val() == "Search for Books") { $(this).val(""); } });
		$("#searchTextAT").blur( function() { if($(this).val() == "") { $(this).val("Search for Books"); } });
	}
	

	$("#go").click(
	function() {
	//first - determine if a new value was entered into the search field
	x = $("#search_dropdown").val();
		if ($("#searchInput").val() != "" && $("#searchInput").val() != "Enter author, title, or keyword") {
			//determine if this is a product (catalog) search or site search
			if (x == "2") {
				//this is a site search - so we submit the form normally
				$("#search_dropdown").val("0");  //this resets dropdown
				$("#form_search").attr("method", "get");
				$("#form_search").attr("action", "http://www.gale.com/sitesearch.htm");
				$("#form_search").submit();
			}
			else if (x == "1") {
				//this is a product (catalog) search - code to COME
				$("#search_dropdown").val("0");  //this resets dropdown
				$("#form_search").submit();
			}
			else {
				//nothing was selected, so submit to catalog by default
				$("#search_dropdown").val("0");  //this resets dropdown
				$("#form_search").submit();
			}
		}
		else {
			$("#search_dropdown").val("0");  //this resets dropdown
		}
	});


	/**
	* Close all left global left-nav sub sections.
	* Slide open any sub-sections when their parent is clicked.
	**/
	if($("#account")){ $("#account").css("background","transparent url(/images/bullet_toggle_plug.png) no-repeat center left scroll"); $("#account").click( function(){ closeAllExcept(this); $("#account_sub").toggle("slow"); return false; } ); }
	if($("#infomenu")){ $("#infomenu").click( function(){ closeAllExcept(this); $("#infomenu_sub").toggle("slow"); return false; } ); }
	if($("#technical")){ $("#technical").click( function(){ closeAllExcept(this); $("#technical_sub").toggle("slow"); return false; } ); }
	if($("#event")){ $("#event").click( function(){ closeAllExcept(this); $("#event_sub").toggle("slow"); return false; } ); }

	/**
	* This function keeps the section from closing and then opening again when you click an already opened
	* section to close it.  The toggle will handle the closing of this section and the close will be skipped below.
	* Otherwise if it is a different section you are clicking on to open...all other open sections will close.
	**/
	function closeAllExcept(dontclose) {
		var id = $(dontclose).attr('id');
		if(id != 'account'){ $("#account_sub").slideUp(); }
		if(id != 'infomenu'){ $("#infomenu_sub").slideUp(); }
		if(id != 'technical'){ $("#technical_sub").slideUp(); }
		if(id != 'event'){ $("#event_sub").slideUp(); }
	}

}); // end document.ready


/**
* adjusts the left nav catalog > View Wish List link and the top nav order center links for login/logout
* of the order center.  Needed becuase servlets run in a different directory with Java and we have no way
* of knowing if they are logged in without cookies being set and read.
**/
function GetCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}


/* ########################################## */
/* Cookie Functions - (creating, edit, erase) */
/* ########################################## */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
/* ########################################## */


/**
* slides the "Areas of Interest" candy box back after 1000 milliseconds
**/
function slideBack(){
	$(".slidedown").hide("slow");
}
