/* ----------------------------------------------------- 

Contains elements of an original script by Mattias Sjoberg found at http://javascript.internet.com

Created March 2010 by Liam Connolly for http://www.lesliedechernatony.com

-------------------------------------------------------*/

// Global variable for cookie usage
var expDays = 30; // Remember settings for 30 days
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

// Load the default colour from cookies (do this before DOM has loaded to stop flickering)
colour();


/* ------------- Document load functions --------------*/
$(function() {
	
	// Set up navigation bar
    $("#navbar").lavaLamp({
	   fx: "linear",
	   speed: 200,
	   click: function(event, menuItem) {
	   return false;
	   }
    });

	// Load the buttons in the control panel
	$("#greenLink").click(function () {changeColourScheme('green');return false;});
	$("#blueLink").click(function () {changeColourScheme('blue');return false;});
	$("#textDecrease").click(function () {resizeText(-1);});
	$("#textIncrease").click(function () {resizeText(1);});
		
	//$('#clicker').click(function(){ $('#hiddenText').slideToggle("slow"); });
	
	/* Remove css attributes that are in place for users without
	javascript */
	$('.noscript').removeClass('noscript');
			
	// Load font size from cookies
	fontSize();
	
	imageFade(1);  // Initialise the fading images
	
	
});
/* ------------ End of document load functions ---------*/

/* Global variables for slideshow */
var imageFadeIn = 2000;
var quoteFadeIn = 1000;
var readTime = 7000;
var fadeOutTime = 2000;
var noImages = 0;

/* ------ Loop around images, fading from one to the other -------- */
function imageFade(i){
	var next = i+1;
	
	// Only count the number of images once
	if (noImages==0) noImages = $("div[class*='quotationBox']").size();
	if (i==noImages) next = 1; // Loop around

	// Reset the opacity and begin slideshow
	$(".quotationBox"+i).css("opacity","1.0");
  	$(".slideImage"+i).fadeIn(imageFadeIn, 
							  function(){
								 	 
								  $(".quotationBox"+i).fadeIn(quoteFadeIn, 
										function(){ 
										
											setTimeout("$('.quotationBox"+i+", .slideImage"+i+"').stop(true, false).fadeOut("+fadeOutTime+");imageFade("+next+");",readTime); 
							  			}); 
							  });

}

/* ------- End of slideshow fading ----------------*/

// Set the colour from cookie if necessary
function colour(){
	var favColour = GetCookie('colour');
	if (favColour != null){
		setActiveStyleSheet(favColour); 
	}
	else setActiveStyleSheet('green'); // Green is the default style
}

// Set the colours of the nav bar and css styles
function changeColourScheme(colourName){
	// Fade out all elements, then change colour and fade back in
	 $("div[class*='quotationBox']:visible:not(:animated), #navbar li.back, .colourScheme, .colourSchemeText, .colourSchemeDark, .colourHighContrast, .clickHere").fadeOut(300, function () {
			
			setActiveStyleSheet(colourName);  // Switch style sheets
			$(this).fadeIn("slow");  // Fade elements back in once done
	 });
	 
	 // Set the cookie
	SetCookie ('colour', colourName, exp);
}

function fontSize(){
	var favSize = GetCookie('fontsize');
	if (favSize != null) {
	  $("body").css("font-size",favSize + "em"); 
	  // Additionally, increase the size of the colour scheme options
	  var originalWidth = 13;  //This is the width in pixels of the circle images
	  document.getElementById('changeBlue').width = favSize * originalWidth;
	  document.getElementById('changeGreen').width = favSize * originalWidth;
	}
}

function resizeText(multiplier) {
  if (document.body.style.fontSize == "") {
     document.body.style.fontSize = "1.0em";
  }
  var newSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2);
  var fontSize = newSize + "em";
 
  
  // Additionally, increase the size of the colour scheme options
  var originalWidth = 13;  //This is the width in pixels of the circle images
  document.getElementById('changeBlue').width = newSize * originalWidth;
  document.getElementById('changeGreen').width = newSize * originalWidth;
  
  // Set the actual font size and store it in a cookie
  document.body.style.fontSize = fontSize;
  SetCookie ('fontsize', newSize, exp);
}

// Function to get cookie values
function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

// Function to get a cookie
function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

// Function to set specific cookies and values
function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

// Function to delete a specific cookie
function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
