/* MIKE BERGERON */
/* theme.js */
/* 2009 */

function $get(element) { return document.getElementById(element) }

applyTheme();

function applyTheme(){
	var selectedtitle=getCookie("mysheet")
	if ($get && selectedtitle!=null){ //load user chosen stylesheet from cookie if there is one stored
		setStylesheet(selectedtitle);
		setCurrent(selectedtitle);
	}
}

function setStylesheet(title){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
	var i, cacheobj, altsheets=[""];
	for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++){
		if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")){ //if this is an alternate stylesheet with title
			cacheobj.disabled = true;
			altsheets.push(cacheobj); //store reference to alt stylesheets inside array
		}
		if(cacheobj.getAttribute("title") == title){ //enable alternate stylesheet with title that matches parameter
			cacheobj.disabled = false; //enable chosen stylesheet
		}
	}
	return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
}

function chooseStyle(styletitle, days){ //Interface function to switch stylesheets plus save "title" attribute of selected stylesheet to cookie
	if ($get){
		setStylesheet(styletitle);
		setCookie("mysheet", styletitle, days);
		setCurrent(styletitle);
	}
}

function setCurrent(styletitle){
	resetCurrent();
	switch(styletitle)
	{
	case "green":
	  $get("green").className = "current";
	  break;    
	case "red":
	  $get("red").className = "current";
	  break;
	default:
	  $get("default").className = "current";
	}
}

function resetCurrent(){
	 $get("default").className = "";
	 $get("green").className = "";
	 $get("red").className = "";
}

function getCookie(Name){ 
	var regexp=new RegExp(Name+"=[^;]+", "i"); //construct regular expression to search for target name/value pair
	if(document.cookie.match(regexp)){ //if cookie found
		return document.cookie.match(regexp)[0].split("=")[1]; //return its value
		return null;
	}
}

function setCookie(name, value, days){
	var expireDate = new Date();
	var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
	setCookie(name, "css");
}