function createCookie(name,value,days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	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);
}

var defaultSize  = '11px';
var enlargedSize = '13px';
var cookieName   = 'ovchipzeeland';

$(document).ready(function() {
	//find all <p> and <li> elements in the current page once, and store it for later usage
	var pElms = $('p'); //general
	var voorwaardenElms = $('li'); //voorwaarden
	var faqElms = $('td.question a, td.unselected a, div.answer'); //faq
	
	var x = readCookie(cookieName);
	if (x) {
		recalculateHeight( (x == 'enlargedSize' ? false : true) );
		if (x == 'enlargedSize') {
			$("#txtsmaller").removeClass("active");
			$("#txtlarger").addClass("active");
		}
		else {
			$("#txtsmaller").addClass("active");
			$("#txtlarger").removeClass("active");
		}
	}
	
	$(".item").click(function() {
		var url = $(this).find("h2:first a:first").attr("href");
		if( url != undefined )
			window.location = url;
	});
	
	$(".item").hover(
		function() { 
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	
	//onclick handlers for text size changes
	$("#txtsmaller").click(function(){
		$(this).addClass("active");
		$("#txtlarger").removeClass("active");
		recalculateHeight(true);
	});
	$("#txtlarger").click(function(){
		$(this).addClass("active");
		$("#txtsmaller").removeClass("active");
		recalculateHeight(false);
	});
	
	//recalc height of col2 because lte IE7 can't handle auto heights used in this context
	function recalculateHeight(to_default) {
		pElms.css('fontSize', (to_default ? defaultSize : enlargedSize));
		voorwaardenElms.css('fontSize', (to_default ? defaultSize : enlargedSize));
		faqElms.css('fontSize', (to_default ? defaultSize : enlargedSize));
		if ($.browser.msie && $.browser.version <= 7) {
			$("div.col2").css('height', $("div.col1").height()+'px');
		}
		//resize columns @ 'Aanvragen op locatie'
		$("div#aanvragenoplocatie div.col1 ul#infodays li").css('width', (to_default ? '120' : '140')+'px');
		$("div#aanvragenoplocatie div.col1 ul#infodays li.column2").css('margin-left', (to_default ? '120' : '140')+'px');
		$("div#aanvragenoplocatie div.col1 ul#infodays li.column3").css('margin-left', (to_default ? '300' : '360')+'px');
		$("div#aanvragenoplocatie div.col1 ul#infodays li.column4").css('margin-left', (to_default ? '400' : '470')+'px');
		if ($.browser.msie && $.browser.version <= 7) {
			$("div#aanvragenoplocatie div.col1 ul#infodays li.first").css('margin-top', (to_default ? '-225' : '-233')+'px');
			//$("div#aanvragenoplocatie div.col1 ul#infodays li.first").css('margin-top', (to_default ? '-465' : '-480')+'px');
		}
		//README: write cookie val
		createCookie(cookieName,(to_default ? 'defaultSize' : 'enlargedSize'),7);
	};

});
