// JavaScript Document
//pageTransitions.js
//read cookie to see if page transitions are on or off:
var ie6 = navigator.userAgent.indexOf('MSIE 6');
//var cookie = readCookie();


if(!jQuery || readCookie("fading") == "off" || ie6 != -1) {
		var fading="off";
		setActiveStyleSheet("nofading");
	} else {
		var fading = "on";
		setActiveStyleSheet("fading");
	}

function setCookie(name,value,expiredays) {
	// Set the cookie to the new style sheet
		var d = new Date();
		d.setDate(d.getDate() + expiredays);
		expires = "expires=" + d.toGMTString();
		document.cookie=name + "=" + value + "; " + expires;
}

function readCookie(name) {
	
	// retrieve the cookie
	cookies=document.cookie;
	startpos = cookies.indexOf(name) + name.length+1;
	endpos = cookies.indexOf(";",startpos);
	if(endpos ==-1) endpos = cookies.length;
	
	if(startpos < name.length+1) { // if no cookie is set
		return null;
	} else {
		return cookies.substring(startpos,endpos);
	}
	
}
function setActiveStyleSheet(title) {

   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
//Fade out content on link click:
function fadeContent() {
var links = document.getElementById("nav").getElementsByTagName("a");
for (var i=0; i < links.length; i++) {
		$(links[i]).click(function() {
			var self = this;
			$("div#content").fadeOut("slow");
			if($("div#overlay2")) {
			$("div#overlay2").fadeOut("slow");
		}
			if($("div#page_title")) {
			$("div#page_title").fadeOut("slow");
		}
		if($("div.column2")) {
			$("div.column2").fadeOut("slow");
		}
		if($("div#footer")) {
			$("div#footer").fadeOut("slow");
		}
			setTimeout(function() {window.location.href=self.href;},1000);
			return false;
								});
	} // End for loop.
}

function showContent() {
	$("div.chapter").attr("style","display:none");
	$("div#page_title").fadeIn("slow");
	//$("div#imagemenu").fadeIn("normal");
	$("div.column1").fadeIn("normal");
	$("div#footer").fadeIn("normal");
}
function transition1() { // Fade pages without initial image:
$("div#page_title").fadeIn("slow");
	$("div#content").fadeIn("slow");
	//if($("div.column2")) { $("div.column2").fadeIn("slow");}
	$("div#footer").fadeIn("slow");
}
function transition2() { // Fade image in and out then fade in content
	$("div.column1").attr("style","display:none");
	//$("div#imagemenu").attr("style","display:none");
	$("div#content").fadeIn("slow");
	$("div#chapter").fadeIn("slow");
}
function transition3() {

	$("div#content").fadeIn("normal");
	setTimeout(function(){$("div#nav").fadeIn("normal");},500);

}
$(document).ready(function() {
						   
var styleswitch = document.getElementById("disable_fades");

if(fading == 'on') { // If fading is enabled:	

		if(document.getElementById("column3")) {
			//transition3();
		} else if(document.getElementById("chapter")) {
			transition2();
		} else {
			transition1();
		}						   
		fadeContent();
		if(styleswitch) {
		$("a#disable_fades").click(function() {
			setCookie("fading","off",1);
			this.firstChild.nodeValue = "Enable page fades";
			return false;
											});
			}
		
	} else { // fading is disabled:
		
		if(styleswitch) {
			styleswitch.firstChild.nodeValue = "Enable page fades";
			$("a#disable_fades").click(function() {
			setCookie("fading","on",1);
			this.firstChild.nodeValue = "Disable page fades for faster browsing";
			return false;
											});
		}
	}


});