/* --------------------------------------------------------------------------------
 * Stylesheets Switcher
 * Version 0.2.1
 * Author: Martin Str&ouml;m, [martin at burnfield dot com]
 * Based on a script by Paul Sowden (http://alistapart.com/stories/alternate)
 * Requires Prototype JavaScript library (http://prototype.conio.net/)
 * -------------------------------------------------------------------------------- */

var StyleSheetSwitcher = {
	initialize: function() {
		this.setActive(Cookies.read("style") || this.getPreferred());
		Event.observe(window, 'unload', this.unloadHandler.bindAsEventListener(this));
	},

	unloadHandler: function() {
		Cookies.create("style", this.getActive(), 365);
	},

	setActive: function(title) {
		$$("link").each(function(link) {
			if (link && link.getAttribute("rel").indexOf("style") != -1 && link.getAttribute("title")) {
				link.disabled = true;
				if (link.getAttribute("title") == title) link.disabled = false;
			}
		});
	},

	getActive: function() {
		var element = $$("link").detect(function(link) {
			return (
				link.getAttribute("rel").indexOf("style") != -1 &&
				link.getAttribute("title") && !link.disabled);
		});
		return element? element.getAttribute("title") : "";
	},

	getPreferred: function() {
		var element = $$("link").detect(function(link) {
			return (
				link.getAttribute("rel").indexOf("style") != -1 && 
				link.getAttribute("rel").indexOf("alt") == -1 &&
				link.getAttribute("title"));
		});
		return element? element.getAttribute("title") : "";
	},
	
	increase: function() {
		var actStyle = StyleSheetSwitcher.getActive();
		if (actStyle == 'A+') {
			this.setActive('A++');
			Element.hide('plus');
			Element.show('plus_grey');
		} else if (actStyle == '') {
			this.setActive('A+');
			Element.hide('minus_grey');
			Element.show('minus');
		}
	},
	
	decrease: function() {
		var actStyle = StyleSheetSwitcher.getActive();
		if (actStyle == 'A+') {
			this.setActive('');
			Element.hide('minus');
			Element.show('minus_grey');
		} else if (actStyle == 'A++') {
			this.setActive('A+');
			Element.hide('plus_grey');
			Element.show('plus');
		}
	}
};


/* --------------------------------------------------------------------------------
 * Cookies
 * -------------------------------------------------------------------------------- */

var Cookies = {
	create: function(name, value, days) {
		var string = name + "=" + value;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			string += "; expires=" + date.toGMTString();
		}
		document.cookie = string + "; path=/";
	},

	read: function(name) {
		var nameEQ = name + "=";
		return (document.cookie.split(/;\s*/).detect(function(cookie) {
			return (cookie.indexOf(nameEQ) == 0);
		}) || "").substr(nameEQ.length);
	},

	exists: function(name) {
		return this.read(name) !== "";
	}
}

/* This does not work anymore
window.onload = function(e) {
  	var cookie = Cookies.read("style");
  	var actStyle = cookie ? cookie : StyleSheetSwitcher.getPreferred();

  	StyleSheetSwitcher.setActive(actStyle);
  	try {
	  	if (actStyle == 'A+') {
			Element.show('minus');
			Element.hide('minus_grey');	
		} else if (actStyle == 'A++') {
			Element.show('plus_grey');
			Element.hide('plus');
			Element.show('minus');
			Element.hide('minus_grey');
		} else {
			Element.hide('minus');
			Element.show('minus_grey');
		}
  	} catch (e) {}
} */

document.observe('dom:loaded',window.onload = function(e){
  	var cookie = Cookies.read("style");
  	var actStyle = cookie ? cookie : StyleSheetSwitcher.getPreferred();

  	StyleSheetSwitcher.setActive(actStyle);
  	try {
	  	if (actStyle == 'A+') {
			Element.show('minus');
			Element.hide('minus_grey');	
		} else if (actStyle == 'A++') {
			Element.show('plus_grey');
			Element.hide('plus');
			Element.show('minus');
			Element.hide('minus_grey');
		} else {
			Element.hide('minus');
			Element.show('minus_grey');
		}
  	} catch (e) {}
});



window.onunload = function(e) {

  var title = StyleSheetSwitcher.getActive();

  Cookies.create("style", title, 365);

}
