
var DHTML = (document.getElementById || document.all || document.layers);


/*
 * Return the given object in the DOM.
 */
function object_get(name) {
	if (document.getElementById) {
		return document.getElementById(name).style;
	} else if (document.all) {
		return document.all[name].style;
	} else if (document.layers) {
		return document.layers[name];
	} else {
		return false;
	}
}

function findpos_x(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findpos_y(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


/*
 * Show the given layer on top of the given object.
 */
function layer_show(name, obj) {
	if (!DHTML)
		return;
	if (!name)
		return;

	var x = object_get(name);
	x.top = findpos_y(obj) - 40;
	x.left = findpos_x(obj);
	x.visibility = 'visible';
}


/*
 * Hide the given layer.
 */
function layer_hide(name) {
	if (!DHTML)
		return;
	if (!name)
		return;

	var x = object_get(name);
	x.visibility = 'hidden';
}


/*
 * Toggle whether the given layer is shown or hidden.
 */
function layer_toggle(name, obj) {
	if (!DHTML)
		return;
	if (!name)
		return;
	var x = object_get(name);
	if (!x)
		return;
	if ((x.visibility) && (x.visibility == 'visible')) {
		layer_hide(name);
	} else {
		layer_show(name, obj);
	}
}


/*
 * Create a cookie with the given name and value, set to expire in the given
 * number of days (no expiry set if days not given).
 */
function cookie_create(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}


/*
 * Return the value of the cookie with the given name.
 */
function cookie_read(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;
}


/*
 * Set the preferred stylesheet to be the one with the given title, and
 * store the setting in a cookie.
 */
function stylesheet_set(title) {
	var i, a, found;

	if (!DHTML)
		return;

	found = 0;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (
		  (a.getAttribute("rel").indexOf("style") != -1)
		  && (a.getAttribute("title"))
		) {
			if (a.getAttribute("title") == title)
				found = 1;
		}
	}
	if (!found)
		return;

	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;
		}
	}

	cookie_create("style", title, 365);
}


/*
 * Return the title of the currently active stylesheet.
 */
function stylesheet_get() {
	var i, a;

	if (!DHTML)
		return null;

	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (
		  (a.getAttribute("rel").indexOf("style") != -1)
		  && (a.getAttribute("title"))
		  && (!a.disabled)
		)
			return a.getAttribute("title");
	}

	return null;
}


/*
 * Set the active stylesheet to the one in the cookie.
 */
function stylesheet_onload() {
	var cookie = cookie_read("style");
	if (cookie)
		stylesheet_set(cookie);
}


/*
 * Store the active stylesheet in a cookie.
 */
function stylesheet_onunload() {
	var title = stylesheet_get();
	if (title)
		cookie_create("style", title, 365);
}


/*
 * Return a string containing HTML listing all available stylesheets.
 */
function stylesheet_list() {
	var str = "";

	if (!DHTML)
		return str;

	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (
		  (a.getAttribute("rel").indexOf("style") != -1)
		  && (a.getAttribute("title"))
		) {
			str += "<li>" +
			  "<a href=\"#\" onclick=\"stylesheet_set('" +
			  a.getAttribute("title") +
			  "'); layer_hide('stylelist'); " +
			  "window.location.href = window.location.href; " +
			  "return false;\">" +
			  a.getAttribute("title") +
			  "</a></li>\n";
		}
	}

	return str;
}


/*
 * Write HTML to the current document which produces a menu of available
 * stylesheets.
 */
function stylesheet_writemenu() {
	if (!DHTML)
		return;

	document.write(
	  '<div id="stylelistbase">' +
	  "<a href=\"#\" onclick=\"layer_toggle('stylelist',this); return false;\">" +
	  "Change page style" +
	  "</a></div>\n"
	);

	document.write(
	  '<div id="stylelist">' +
	  "<a href=\"#\" onclick=\"layer_hide('stylelist'); return false;\">" +
	  "X" +
	  "</a>\n" +
	  "<ul>\n" +
	  stylesheet_list() +
	  "</ul>\n" +
	  "</div>\n"
	);
}

window.onload = stylesheet_onload;
window.onunload = stylesheet_onunload;
stylesheet_onload();

/* EOF */

