// JavaScript Document
$(document).ready(function(){
	//The next chunk of code sets a cookie to store the readers text size choice
	function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString()+";path=/");
    };
	//Get the cookie's information
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    };
	//Erase the cookie
	function eraseCookie(name){
		setCookie(name,'',-1);
	}
	
	//On the click store the links rel value as the cookie value
	$('a.font-size').click(function(event){
		event.preventDefault();
		var ccv = getCookie('fontSize');//Get the "current cookie value" or ccv for short.
		var lv = $(this).attr('rel');//lv equals "link value".
		$('body').removeClass(ccv);//Remove the current cookie class from the body.
		//Delete the cookie.
		eraseCookie('fontSize');
		$('body').addClass(lv);//Add the new cookie class to the body.
		$('a.font-size').removeClass('active');
		$('a.font-size.'+lv).addClass('active');//Highlight the font-size selected.
		//Reset the cookie with the new value.
		setCookie('fontSize', lv, 1);
	});
	
	//Get the cookies value so it is added as a class to the body
	var cv = getCookie('fontSize');//cv equals "cookie value"
	
	//If cv has a value...
	if(cv != ''){
		//alert(cv);
		switch(cv) {
			case 'small':
				$('body').addClass('small');
				$('a.font-size.'+cv).addClass('active');
				break;
			case 'medium':
				$('body').addClass('medium');
				$('a.font-size.'+cv).addClass('active');
				break;
			case 'large':
				$('body').addClass('large');
				$('a.font-size.'+cv).addClass('active');
				break;
			default :
				$('a.font-size.medium').addClass('active');
				break;
		}
	} else {
		$('a.font-size.medium').addClass('active');
	}
});
