var fontCookieName = "fontSize";
var fontSize = readCookie(fontCookieName);
var fontSizes = {"font-size-s": 62.5, "font-size-m": 72.5, "font-size-l": 82.5};

/* The following functions to create, read, and delete cookies
 is courtesy Peter-Paul Koch, http://www.quirksmode.org/js/cookies.html */
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);
}
/*********************************/

$(document).ready(function() {
	/* scrapped for now
	// hide all subnavigation
	$("li.nav-group ul").css("display","none");
	
	// attach mouse events to toggle subnav
	$("li.nav-group").bind("mouseenter", function() { $("li#" + this.id).toggleClass("active"); $("li#" + this.id + " ul").show(); });
	$("li.nav-group").bind("mouseleave", function() { $("li#" + this.id).toggleClass("active"); $("li#" + this.id + " ul").hide(); });
	*/
	if (!fontSize) fontSize = "font-size-s";
	setFontSize(fontSize,1);
	
	/* shadow height script for fluid layout
	resizeShadow();
	$(window).resize(function() {resizeShadow()});
	*/
	
	// attach font size events
	$("div#text-size a").click(function() {return setFontSize(this.id)});
	$("a.category-link").mouseover(function() {$(this.parentNode).toggleClass("mouseover");});
	$("a.category-link").mouseout(function() {$(this.parentNode).toggleClass("mouseover");});
	
	// exit disclaimer - attach URL into query string
	$("a.exit").each(function() {
							var url = this.href+"?URL=[URL]&title=[TITLE]";  
							this.href = url.replace("[URL]",encodeURIComponent($(this).prev()[0].href)).replace("[TITLE]",encodeURIComponent($(this).prev()[0].title));
						});
	
	$("a#email_updates").click( function() { alert("functionality is forthcoming"); });
	
});

function resizeShadow() {
	var height = $("div#footer").height() + $("div#content").height() + 6;

	$("div#right-shadow").height(height);
}

function setFontSize(size, init) {
	if (init || (size != fontSize)) {
		// change font size
		document.body.style.fontSize = fontSizes[size] + "%";
		
		// change link images
		var imagesizeLink = $("a#"+size+" img")[0];
		imagesizeLink.src = imagesizeLink.src.replace("_off","_on");
		
		if (!init) {
		var imagesizeLink = $("a#"+fontSize+" img")[0];
		imagesizeLink.src = imagesizeLink.src.replace("_on","_off");
		}
		// store fontsize
		fontSize = size;
		createCookie(fontCookieName,size);
		
		resizeShadow();
	}
	
	return false;
}