// Simple CSS template switcher using body classes
var changeSize = function(event) {
	var size;
	
	// Clear any class on the body tag
	$('body').removeClass();
	// Add the class matching the target's id, unless it's 'normal', then leave it blank
	if (event.target.id != 'normal') {
		$('body').addClass(event.target.id);
	}
	// Remove 'selected' class from all size links
	$("#resizeLinks a").removeClass();
	// Add 'selected' class to the link that was clicked
	$(event.target).addClass('selected');
	// Save the cookie
	saveCookie();
	return false;
}

var saveCookie = function() {
	var size = $(".selected")[0].id;
	createCookie("size",size,365);
}

$(function() {
	var sizeCookie = readCookie("size");
	// Set up event bindings here
	// These are the ID's of the various size changing links, they also correspond to the classes to be used
	$("#normal").bind("click", changeSize);
	$("#large").bind("click", changeSize);
	$("#xlarge").bind("click", changeSize);
	// The link used to save the current size in a cookie - for reference, we're doing this by default now.
	$("#saveCookie").bind("click", saveCookie);
	if (sizeCookie != null) {
		$("#"+sizeCookie).click();
	} else {
		$("#normal").click();
	}
});

// Cookie scripts from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	var domain = ";signiant.com"; // TODO: Make this a param
	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=/"+domain;
}

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);
}