// Author: styleswitcher.js is written by Mr. Jean
// Location: www.mrjean.be
// License: Don't use without permission!
//
// v 1.0    - Thursday, November 13, 2008
//          - First version
//
// Description:
// A script that provides dynamic style switching and remembers it with a tasty cookie.

	if (!STYLESWITCHER) { var STYLESWITCHER = new Object() }

	STYLESWITCHER = {
		
		cookieName								:	'',
		cookieDomain							:	'',
		
		executeStyleswitcher					:	function() {
			
			if(document.getElementById('blue') != null) {
			
				STYLESWITCHER.cookieName = document.getElementById('country').innerHTML;
				STYLESWITCHER.cookieDomain = document.getElementById('domain').innerHTML;
				if(document.getElementById) window.onload = function() {
					if(STYLESWITCHER.getCookie(STYLESWITCHER.cookieName)) {
						STYLESWITCHER.setActiveStyleSheet(STYLESWITCHER.getCookie(STYLESWITCHER.cookieName));
					} else {
						STYLESWITCHER.setActiveStyleSheet('blue');
					}
					STYLESWITCHER.loadEvents();
				}
				
			}
			
		},
		
		setCookie								:	function(name, value) {
			var str = name + '=' + escape(value);
			str += '; expires= Fri, 20 Mar 2020 00:00:00; path=/; domain='+STYLESWITCHER.cookieDomain;
			document.cookie = str;
		},
		
		getCookie								:	function(name) {
			var allCookies = document.cookie.split('; ');
			for (i in allCookies) {
				var singleCookie = allCookies[i].split('=');
				if(singleCookie[0] == name) {
					return unescape(singleCookie[1]);
				}
			}
			return undefined;
		},
		
		setActiveStyleSheet						:	function(title) {
			$("#blue").removeClass("active");
			$("#orange").removeClass("active");
			$("#green").removeClass("active");
			$("#red").removeClass("active");
			var ucTitle = title;
			$("#"+ucTitle).addClass("active");
			var links = document.getElementsByTagName('link');
			for(var i = 0; i<links.length; i++) {
				if (links[i].getAttribute('rel').indexOf('style') != -1 && links[i].getAttribute("title")) {
					if(links[i].getAttribute("title") != "default") {
						links[i].disabled = true;
						if(links[i].getAttribute("title") == title) {
							links[i].disabled = false;
						}
					}
				}
			}
			STYLESWITCHER.setCookie(STYLESWITCHER.cookieName, STYLESWITCHER.getActiveStyleSheet());
		},
		
		getActiveStyleSheet						:	function() {
			var links = document.getElementsByTagName("link");
			for (var i = 0; i<links.length; i++) {
				if(links[i].getAttribute("rel").indexOf("style") != -1 && links[i].getAttribute("title") && !links[i].disabled && links[i].getAttribute("title") != "default") {
					return links[i].getAttribute("title");
				}
			}
			return null;
		},
		
		loadEvents								:	function() {
			var objBlue			=	document.getElementById("blue");
			var objOrange		=	document.getElementById("orange");
			var objGreen		=	document.getElementById("green");
			var objRed		=	document.getElementById("red");
			objBlue.onclick = function() {
				STYLESWITCHER.setActiveStyleSheet('blue');
				$("#blue").addClass("active");
				$("#orange").removeClass("active");
				$("#green").removeClass("active");
				$("#red").removeClass("active");
				return false;
			}
			objOrange.onclick = function() {
				STYLESWITCHER.setActiveStyleSheet('orange');
				$("#blue").removeClass("active");
				$("#orange").addClass("active");
				$("#green").removeClass("active");
				$("#red").removeClass("active");
				return false;
			}
			objGreen.onclick = function() {
				STYLESWITCHER.setActiveStyleSheet('green');
				$("#blue").removeClass("active");
				$("#orange").removeClass("active");
				$("#green").addClass("active");
				$("#red").removeClass("active");
				return false;
			}
			objRed.onclick = function() {
				STYLESWITCHER.setActiveStyleSheet('red');
				$("#blue").removeClass("active");
				$("#orange").removeClass("active");
				$("#green").removeClass("active");
				$("#red").addClass("active");
				return false;
			}
		}
	}
	
$(document).ready(STYLESWITCHER.executeStyleswitcher);
