/*
###############################################################################
# Chirpy! v0.1, a quote management system                                     #
# Copyright (C) 2005 Tim De Pauw <ceetee@users.sourceforge.net>               #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               #
#                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc., 51  #
# Franklin St, Fifth Floor, Boston, MA  02110-1301  USA                       #
###############################################################################

###############################################################################
# style_switcher.js                                                           #
# Makes the user's alternate stylesheet choice persistent using a cookie      #
# Largely based on http://www.alistapart.com/articles/alternate/              #
###############################################################################
*/

function setActiveStyleSheet (title) {
	var a;
	var l = document.getElementsByTagName("link");
	var found = false;
	for (var i = 0; a = l[i]; i++) {
		var t = a.getAttribute("title");
		if (a.getAttribute("rel").indexOf("stylesheet") >= 0 && t) {
			var d = (t != title);
			a.disabled = d;
			if (!d) found = true;
		}
	}
	return found;
}

function getActiveStyleSheet () {
	var a;
	var l = document.getElementsByTagName("link");
	for (var i = 0; a = l[i]; i++) {
		var t = a.getAttribute("title");
		if (a.getAttribute("rel").indexOf("stylesheet") >= 0 && t && !a.disabled)
			return t;
	}
	return null;
}

function getPreferredStyleSheet () {
	var a;
	var l = document.getElementsByTagName("link");
	for (var i = 0; a = l[i]; i++) {
		var t = a.getAttribute("title");
		if (t && a.getAttribute("rel") == "stylesheet")
			return t;
	}
	return null;
}

function readCookie (name) {
	var nameEq = name + "=";
	var ca = document.cookie.split(/; */);
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		if (c.indexOf(nameEq) == 0)
			return c.substring(nameEq.length, c.length);
	}
	return null;
}

function createCookie (name, value) {
	var date = new Date();
	date.setYear(1900 + date.getYear() + 1);
	document.cookie = name + "=" + value + "; expires=" + date.toGMTString()
		+ "; path=" + cookiePath + "; domain=" + cookieDomain;
}

window.onunload = function (e) {
	var style = getActiveStyleSheet();
	if (style) createCookie("style", style);
}

setActiveStyleSheet(readCookie("style"))
	|| setActiveStyleSheet(getPreferredStyleSheet());