// JavaScript Document

// Set to short style by default (for javascript browsers)
// non script browsers will get long style by default
document.write('<link href="css/data-short.css" rel="stylesheet" type="text/css">');


window.onload = function() {
	start();
	
}
window.onunload = function() {
	
	setUserStyleSheet(); //On unload set the current style sheet to the current one and put in cookie
}

function start() {
	applyUserStyleSheet(); //Gets the user style sheet from cookie
	pageSize = GetActiveStyleSheet("swap"); //needs to be global 
											//this sets up the initial page size for the larger/smaller text size buttons
											//Gets its size by working out the active style sheet
	//alert(pageSize);
	fixBg();   // Fix the background by getting size of page
	//now keep fixing the background everytime the mouse moves (no onTextSizeChange event)
	//by assigining the finction to onmouse over/out of body
	document.getElementById("body").onmouseover = function() {fixBg()};
	document.getElementById("body").onmouseout = function() {fixBg()};
	



}




function longVersion() {
	document.getElementById("data-more-info").style.display = "block"; 	// set more info to visible
	document.getElementById("more-info-link").style.display = "none";		// set more info link to invisible
}
function shortVersion() {
	document.getElementById("data-more-info").style.display = "none";	// set more info to invisible
	document.getElementById("more-info-link").style.display = "block";		// set more info link to visible
}

function fixBg() {
	
	//grab body element
	var styleF = document.getElementById("body");
	var textColor; //holds text color
	
	//check if normal browser (by checking if method exists)
	if( window.getComputedStyle ) {
		//get style using normal method
		textColor = window.getComputedStyle(styleF,null).color;
	} else { 
		//Use M$ damn way
		textColor = styleF.currentStyle.color;
	}
	
	//now check if text color is black
	
	if (textColor == "rgb(0, 0, 0)") {
		//then set bg image to nothing
		document.getElementById("body").style.backgroundImage = "none"
		document.getElementById("data").style.backgroundImage = "none"
		document.getElementById("footer").style.backgroundImage = "none"
		
	} else {
		//grab the centered page
		var fixWidth = document.getElementById("data");
		
		//get the computed width of the centered page
		var x = fixWidth.offsetWidth;
		
		//set the bg image to a filename using rendered width
		var bgImage = "url(\"img/style/bg" + x + ".gif\")";
		
		//assign the new bg to necassary elements
		document.getElementById("data").style.backgroundImage = bgImage;
		document.getElementById("body").style.backgroundImage = bgImage;
		document.getElementById("footer").style.backgroundImage = bgImage;
		
	};
}












function setActiveStyleSheet(style, mode) {
	// swaps the style sheet
	// INPUT: 	"style" can be a number or the title of a style sheet
	//			"mode" can be:	"all",  "swap",   null, nothing or ""
	//
	//	When called with "all": "style" is the number of the stylesheet to enable.
	//									The number is dertermined by the order of 
	//									the stylesheet tags, in the html.
	//									In all mode the counting starts from all style sheets
	//	When called with "swap":"style" is the number of the style sheet to enable.
	//									*BUT* the numbering only includes stylesheets
	//									that have: 	rel="alternate stylesheet" OR
	//												title="Default"
	//  When called without a mode: "style" is the "title" of the stylesheet to enable
	
	// In all cases only alternate stylesheets or title="Default" are replaced
	
	//get all the tags with "link" as a name
	var allLinks = document.getElementsByTagName("link");
	//find out how many tags there are
	var noOfLinks = allLinks.length;
	// used as an increasement for the counter of matching stylesheets
	var n = 0;
	// cycle thru each link and check if its worth using
	for (var i=0; i<noOfLinks; i++) {
		
		
			//get the relavence
			var rel = allLinks[i].getAttribute("rel");
			//get the title
			var title = allLinks[i].getAttribute("title");
			
			//check if the relevence is alternate or if the title is default
			if (rel == "alternate stylesheet" || title == "Default") {
				
				//mode selection
				if (mode == "all") {
					//check if the SS number matches the one supplied
					if (i == style) {
						// if it is dont disable it
						allLinks[i].disabled = false;
						//alert("match")
					} else {
						// disable it
						allLinks[i].disabled = true;
					};
				
				} else { 
					if (mode == "swap") {
						
						if (n == style) {
							// if it is dont disable it
							allLinks[i].disabled = false;
							//alert("match")
						} else {
							// disable it
							allLinks[i].disabled = true;
						};
						//increase n by on
						n = n + 1;
					} else {
						//alert(i);
						//alert(n);
						//check if the title is the one supplied
						if (title == style) {
							// if it is dont disable it
							allLinks[i].disabled = false;
							//alert("match")
						} else {
							// disable it
							allLinks[i].disabled = true;
						};
						
					};
				};
			};
		
	};
		
}


function GetActiveStyleSheet(mode) {
	//var mode = "all";
	var allLinks = document.getElementsByTagName("link");
	//find out how many tags there are
	var noOfLinks = allLinks.length;
	// used as an increasement for the counter of matching stylesheets
	var n = 0;
	// cycle thru each link and check if its worth using
	for (var i=0; i<noOfLinks; i++) {
			//get the relavence
			var rel = allLinks[i].getAttribute("rel");
			//get the title
			var title = allLinks[i].getAttribute("title");
			
			//check if the relevence is alternate or if the title is default
			if (rel == "alternate stylesheet" || title == "Default") {
				
				if(allLinks[i].disabled == false) {
					if (mode == "all") {
						return i;
					} else {
						if (mode == "swap"){
							return n;
						} else {
							return title;
						};
					};
				};
				n = n + 1;
			};
	};
	
}

function test22() {
	createCookie('userPageStyle','smell',7);
	alert("Done");
}

function setDefault() {
	pageSize = 0; //Needed cus otherwise bigger smallelr functions dont know new setting
	setActiveStyleSheet("Default");
	fixBg(); //Fix the bg
}	

function setPrintStyle() {
	pageSize = 0; //Needed cus otherwise bigger smallelr functions dont know new setting
	setActiveStyleSheet("Print");
	fixBg(); //Fix the bg
}	


function setSmaller() {
	if (pageSize != 0) { //check if max size has not been reached (I.E. first page size allowed)
		pageSize = pageSize - 1; //Decrease the page size by 1
		//alert(pageSize);
		setActiveStyleSheet(pageSize, "swap"); //Apply style sheet
		fixBg(); //Fix the bg
	};

}	
		
function setLarger() {
	if (pageSize != 2) { //check if max size has not been reached (I.E. last page size allowed)
		pageSize = pageSize + 1; //Increase the page size by 1
		//alert(pageSize);
		setActiveStyleSheet(pageSize, "swap"); //Apply style sheet
		fixBg(); //Fix the bg
	};

}	
function applyUserStyleSheet() {

	var userStyle = readCookie("userPageStyle"); //Get the users preffered style
	//check if it matches optional stylesheets
	if (userStyle == "Print" || userStyle == "large" || userStyle == "x-large"){
		//if it does then use the users style sheet
			//alert(userStyle);
		if (navigator.appName != "Microsoft Internet Explorer") {
			setActiveStyleSheet(userStyle); //Set the users style sheet
		} else {
			setActiveStyleSheet("Default"); //Set the users style sheet
			
		};
	} else { 
		//or set the default style sheet
		 setActiveStyleSheet("Default"); //Set the users style sheet
	};
}
function setUserStyleSheet() {

	var userStyleSheet = GetActiveStyleSheet(""); //Get active style sheet using function
	//alert(userStyleSheet);
	createCookie('userPageStyle',userStyleSheet,7); //save the style sheet as a cookie
	//alert("cooky set");
}





	
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=/";
}

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);
}
	
	


	


