/*******************************************************
// themes.js
// does all the heavy lifting for theme assignment
// 
// public functions:
// 	checkquerystring() - processes the querystring for a theme, sets a cookie if found
// 	loadtheme(prefix) - applies base theme, reads cookie, applies extra theme
// 
// revision:
// 	~jan, 2004:	written by psyci
// 	2004.02.12:	cleaned up by fen.. some parts rewritten
//      2004.02.07      adapted for doc, some revisions
*******************************************************/


	/*******************************************************
	// 
	// GLOBALS - some of these are referenced by the main page writing scripts
	// 
	*******************************************************/

// the base theme, which is always applied first
var basethemeURL = "themes/light";

// the default theme, which may be applied if no theme is specified
var defaultthemeURL = "themes/light";

// this will be set to the URL of the specified theme, if such there be
var fullthemeURL = basethemeURL;

// a value for this is passed in from index.html. It always seems to be ""
var prefixURL = "";

// an array of all the certified themes to be listed on the top page
var themeURLarray = new Array();
themeURLarray.push( "themes/light" );

	/*******************************************************
	// 
	// Public functions - both called from index.html
	// 
	*******************************************************/

function checkquerystring() {
// if a theme is set in the querystring, set themeURL to it and set a cookie
// also, prompt the user if it doesn't seem to be a certified theme

	// look for a theme in the query..
	var queryTheme = "";
	var dls = document.location.search;
	if (dls.substr(0,1) == "?") { dls = dls.substr(1); }
	var pairs = dls.split("&");
	for (var a in pairs) {
		var tmp = pairs[a].split("=");
		if (tmp[0] == "theme") {
			queryTheme = tmp[1];
		}
	}
	// and if found, use it..
	if (queryTheme != "") {
		if (queryTheme.indexOf("//") > -1) {
			var s = "WARNING:  This theme might not be hosted by our site.  \n";
			s += "It may contain code that is harmful to your computer, or \n";
			s += "other nasty things that we take no responsibility for.  We \n";
			s += "recommend that you do not use this as a theme, but provide \n";
			s += "the service for theme authors who want to test their theme out.\n\n";
			s += "Do you still wish to use this theme?"
			var useURL = confirm(s);
			var queryTheme = (useURL) ? queryTheme : defaultthemeURL;
		}
		set_cookie( "sitetheme", queryTheme );
	}
}

function loadtheme(prefix) {
// write the css/js file references to the various themes, and set some globals.
// takes in "prefix", the use of which isn't yet clear to me - fen
	// base theme info:
	write_CSS_JS( prefix + basethemeURL );
	// get theme from cookie if possible 
	var mytheme = get_cookie( "sitetheme" );
	if (mytheme != "") {
		// not sure of the point of this..
		prefixURL = prefix;
		if (mytheme.indexOf("//") > -1) {
			prefixURL = "";
		}
		// fullthemeURL is reffed by the themes. 
		fullthemeURL = prefixURL + mytheme;
		// write the non-base theme references
		write_CSS_JS( fullthemeURL );
	}
}



	/*******************************************************
	// 
	// Private functions
	// 
	*******************************************************/


function write_CSS_JS( path ) {
// write links to the CSS and JS files inside path (which should not end in "/")
   	document.write("<LINK rel=\"stylesheet\" type=\"text/css\" href=\"" + path + "/style.css\">");
	document.write("<SCR" + "IPT LANGUAGE=\"Javascript\" SRC=\"" + path + "/script.js\"></SCR" + "IPT>");
}

function set_cookie(name, value) {
// set_cookie - makes a cookie of name=value, expiring in 1 year
	expdate = new Date();
	expdate.setTime(expdate.getTime() + 365 * 24 * 60 * 60 * 1000); //Expire 1 year from now
	document.cookie = name + '=' + escape(value) + ';expires=' + expdate.toGMTString();
}

function get_cookie(name) {
// get_cookie - returns the named cookie's value, or "" if none exists
	var dc = document.cookie;
	var carr = dc.split( "; " );
	for (var a in carr) {
		var nameval = carr[a].split( "=" );
		if (name == nameval[0]) {
			return unescape( nameval[1] );
		}
	}
	return "";
}








