///////////////////////////////////////////////////////
// Style Sheet Switcher version 1.1 Oct 10th, 2006
// Author: Dynamic Drive: http://www.dynamicdrive.com
// Usage terms: http://www.dynamicdrive.com/notice.htm
///////////////////////////////////////////////////////

// Select "manual" or "random"
var manual_or_random = "manual";

// Select default style
var defaultstyle = "themename";

// How often to switch random style?
// "eachtime", "sessiononly", or "x days (replace x with desired integer)"
// Only applicable if mode is random
var randomsetting = "7 days";

// Name your cookie
var cookiename = "stylecookie";

// Cookie path
var cookiepath = "/";

// Cookie domain or subdomain
var cookiedomain = ".concept2.net";

// Cookie secure
var cookiesecure = "";

	
// Script logic
if (manual_or_random=="manual"){
	var selectedtitle = getCookie(cookiename);
	if (selectedtitle == null) {
		setStylesheet(defaultstyle);
	} else {
		setStylesheet(selectedtitle);
	}
} else if (manual_or_random=="random"){
	if (randomsetting=="eachtime") {
		setStylesheet("", "random");
	} else if (randomsetting=="sessiononly"){ 
		// If "mysheet_s" session cookie is empty
		if (getCookie("mysheet_s")==null) {
			// Activate random alt stylesheet while remembering its "title" value
			document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/";
		} else {
			// Just activate random alt stylesheet stored in cookie
			setStylesheet(getCookie("mysheet_s"));
		}
	} else if (randomsetting.search(/^[1-9]+ days/i)!=-1){
		// If "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
		if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){
			// Activate random alt stylesheet while remembering its "title" value
			setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting));
			// Also remember the number of days to persist per the "x days" variable
			setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting));
		}
	} else {
		// Just activate random alt stylesheet stored in cookie
		setStylesheet(getCookie("mysheet_r"));
	} 
}


// Main stylesheet switcher function. 
// Second parameter if defined causes a random alternate stylesheet
// (including none) to be enabled
function setStylesheet(title, randomize){
	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;
			// Store reference to alt stylesheets inside array
			altsheets.push(cacheobj);
			// Enable alternate stylesheet with title that matches parameter
			if(cacheobj.getAttribute("title") == title) {
				// Enable chosen style sheet
				cacheobj.disabled = false;
			}
		}
	}
	if (typeof randomize!="undefined"){
		// If second parameter is defined, randomly enable an
		// alt style sheet (includes none)
		var randomnumber=Math.floor(Math.random()*altsheets.length)
		altsheets[randomnumber].disabled=false;
	}
	// If in "random" mode, return "title" of randomly enabled
	// alt stylesheet
	return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "";
}

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

function setCookie(name, value, days, path, domain, secure) {
	var expireDate = new Date()
	// Set "expstring" to either future or past date,
	// to set or delete cookie, respectively
	var expstring = (typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5);
	document.cookie =
		name+"="+escape(value)+
		(days	 ? "; expires="+expireDate.toGMTString() : "")+
		(path    ? "; path="   +path   : "")+
		(domain  ? "; domain=" +domain : "")+
		(secure  ? "; secure" : "");
}


// Interface function to switch style sheets plus save
// "title" attr of selected stylesheet to cookie
function chooseStyle(styletitle, days){
	if (document.getElementById){
		setStylesheet(styletitle);
		setCookie(cookiename, styletitle, days, cookiepath, cookiedomain, cookiesecure);
	}
}


// Delete cookie
function deleteCookie(name) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		( ( cookiepath ) ? "; path=" + cookiepath : "") +
		( ( cookiedomain ) ? "; domain=" + cookiedomain : "" ) +
		";expires = 01/01/2000 00:00:00";
		alert(name + " - Cookie Deleted"); // Show alert
	}
}


// Optional function that shows which style sheet is currently
// selected within group of radio buttons or select menu
function indicateSelected(element){
	// If element is a radio button or select menu
	if ( selectedtitle != null &&
		(element.type==undefined || element.type=="select-one")){
		var element=(element.type=="select-one") ? element.options : element;
		for (var i=0; i<element.length; i++) {
			// If form element value and cookie value match...
			if (element[i].value==selectedtitle){
				// If this is a select menu
				if (element[i].tagName=="OPTION") { 
					element[i].selected=true;
				} else { 
					// Else if it's a radio button
					element[i].checked=true;
				}
				break;
			}
		}
	}
}

