/**
* Stylesheet toggle variation on styleswitch stylesheet switcher.
* Built on jQuery.
* Under an CC Attribution, Share Alike License.
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
(function($)
	{
		// Local vars for toggle
		var availableStylesheets = [];
		var activeStylesheetIndex = 0;
		
		// To loop through available stylesheets
		$.stylesheetToggle = function()
		{
			activeStylesheetIndex ++;
			activeStylesheetIndex %= availableStylesheets.length;
			$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
		};	
		
		// To switch to a specific named stylesheet
		$.stylesheetSwitch = function(styleName)
		{
			$('link[rel*=style][media=screen]').each(
				function(i) 
				{
					this.disabled = true;
					if (this.getAttribute('href') == styleName) {
						this.disabled = false;
						activeStylesheetIndex = i;
					}
				}
			);
			createCookie('style', styleName, 365);
			
				if( styleName == '/sites/all/themes/ntw/style.css?l') {
					//RESPONSE FOR STANDARD THEME STYLESHEET
					eraseCookie('style');
					location.reload();
				}else if( styleName == '/sites/all/themes/ntw/accessability/highcontrast.css'){
					$('div').contents().unwrap();					
					$('div').removeAttr("style");
					$('.slide').removeAttr("style");
					$('link[rel*=style][media=all]').remove();
					
				}else if( styleName == '/sites/all/themes/ntw/accessability/plain.css'){
					$('div').contents().unwrap();					
					$('div').removeAttr("style");
					$('.slide').removeAttr("style");
					$('link[rel*=style][media=all]').remove();
									}else{
					//NO STYLESHEET
					eraseCookie('style');
					location.reload();
				}	
		};
		
		// To initialise the stylesheet with it's 
		$.stylesheetInit = function()
		{
			$('link[rel*=style][media=screen]').each(				
				function(i) 
				{					
					//alert( this.getAttribute('href') );
					
					availableStylesheets.push(this.getAttribute('href'));
				}
			);
			var c = readCookie('style');
			if (c) {
				$.stylesheetSwitch(c);
			}
		};
	}
)(jQuery);


// COOKIE FUNCTIONS http://www.quirksmode.org/js/cookies.html
// WRITE COOKIE
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=/";
}

// READ COOKIE
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;
}

// ERASE COOKIE
function eraseCookie(name)
{
	createCookie(name,"",-1);
}


// BEGIN DOCUMENT INITIALISATION
$(function(){
		// Call stylesheet init so that all stylesheet changing functions 
		// will work.
		$.stylesheetInit();
		
		// When one of the styleswitch links is clicked then switch the stylesheet to
		// the one matching the value of that links rel attribute.
		$('.styleswitch').bind(
			'click',
			function(e)
			{
				$.stylesheetSwitch(this.getAttribute('rel'));
				return false;
			}
		);
});


$(document).ready(function(){



	// GET THE COOKIE
	var $CookieValue = readCookie('font-size');

	// CHECK THE COOKIE VALUE IS NOT NULL
	if($CookieValue != null)
	{  
		var originalFontSize = $CookieValue;
		//ADJUST THE FONT SIZE BASED ON THE COOKIE VALUE
		$('html').css('font-size', originalFontSize);
	}else
	{
		// Grab the original font size or recall from cookie  
		var originalFontSize = $('html').css('font-size');
	}

	// Reset Font Size
	$("#resetFont").click(function(){
		$('html').css('font-size', '16px');
		// SET COOKIE
		//$.cookie("font-size", originalFontSize);	
		createCookie('font-size', originalFontSize, 365);
		return false;
	});
	
	// Increase Font Size
	$("#increaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.1;
		
		// SET COOKIE
		//$.cookie("font-size", newFontSize);
		createCookie('font-size', newFontSize, 365);
		
		//SET THE NEW FONT SIZE
		$('html').css('font-size', newFontSize);
		return false;
	});
	
	// Decrease Font Size
	$("#decreaseFont").click(function(){
									  
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.9;
		
		// SET COOKIE
		//$.cookie("font-size", newFontSize);	
		createCookie('font-size', newFontSize, 365);
		
		//SET THE NEW FONT SIZE
		$('html').css('font-size', newFontSize);
		return false;
	});
});


