$(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
		});
	}

	if($("a#freeresources_opener")){
		$("a#freeresources_opener").hover( function(){ $("#freeresources").show("slow"); }, function(){  } );
	}

	// Accordion for homepage promos
	if($("#promo_slider").Accordion){
		$("#promo_slider").Accordion({
			active: false,
			header: '.head',
			navigation: false,
			event: 'mouseover',
			autoheight: true,
			animated: 'easeslide',
			active: '.default_open'
		});
	}

	$("#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"); } });

	$("#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 don't do anything
				$("#search_dropdown").val("0");  //this resets dropdown
				$("#form_search").submit();
			}
		}
		else {
			$("#search_dropdown").val("0");  //this resets dropdown
		}
	});

}); // 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");
}

