
var StyleSwitcher = {
	
	styles 				: [],
	el 					: null,
	callback			: null,
	
	init : function(element, callback) {
		var $ = StyleSwitcher, loader;
		$.el = element;
		
		if (typeof callback !== 'undefined' && callback !== null) {
			$.callback = callback
		}
	
		YAHOO.util.Event.onDOMReady($.fnInit);
	},	
	
	fnInit : function() {
		var $ = StyleSwitcher;
		var html = '', styleSheet, i, j, element, div, ul, li, links, url;
		
		// make sure we have at least two styles
		if ($.styles.length < 2) { return; }
	
		// make sure we have passed something in
		if (typeof $.el === 'undefined') { return; }
		
		// make sure it is an object or a string
		if (typeof $.el === 'object') {
			element = $.el;
		} else if (typeof $.el === 'string') {
			element = document.getElementById($.el);
			// make sure we can find the element
			if (typeof element === 'undefined' || element === null) {
				return;
			}
		} else {
			return;
		}
		
		// find the styles on the page and take a copy of them for the objects
		$.getAllStyleSheets();
		
		url = $.readCookie();
		
		if (url === "") {			
			// set the default style to active
			$.setDefaultStyleActive();
		}
		
		// we should be good to go now
		div = $.createFormElement(null, 'div', [{name:'id',value:'theme'}], '', '');
		$.createFormElement(div, 'p', [], '', 'Choose a theme:');
		ul = $.createFormElement(div, 'ul', [], '', '');
		
		for (i = 0; i < $.styles.length; i++){
			li = $.createFormElement(ul, 'li', [], $.styles[i].className, '');
			$.createFormElement(li, 'a', [{name:'href',value:'javascript:void(0);'},{name:'url',value:$.styles[i].url},{name:'id',value:$.styles[i].className}], '', $.styles[i].name);
		}
		
		YAHOO.util.Event.addListener(ul, "click", $.event_StyleClicked);
		
		element.appendChild(div);
		
		if ($.callback !== null) {
			$.callback.call();	
		}
	},
	
	getAllStyleSheets : function() {
		var $ = StyleSwitcher, i;
		
		var links = document.getElementsByTagName('link');
		for (i = 0; i < links.length; i++) {
			for (j = 0; j < $.styles.length; j++) {
				if (links[i].getAttribute("href").toLowerCase() === $.styles[j].url.toLowerCase()) {
					$.styles[j].object = links[i];	
					break;
				}
			}
		}		
	},
	
	setDefaultStyleActive : function() {
		var $ = StyleSwitcher, i;
		
		for (i = 0; i < $.styles.length; i++) {
			if ($.styles[i].defaultStyle) {
				if ($.styles[i].object !== null) {
					$.styles[i].object.disabled = false;
				}
			} else {
				if ($.styles[i].object !== null) {
					$.styles[i].object.disabled = true;
				}
			}
		}
	},
	
	setActiveStyle : function(url) {
		var $ = StyleSwitcher, style;
		
		for (var i = 0; i < $.styles.length; i++) {
			if ($.styles[i].url.toLowerCase() === url.toLowerCase()) {
				if ($.styles[i].object !== null) {
					$.styles[i].object.disabled = false;
				}
			} else {
				if ($.styles[i].object !== null) {
					$.styles[i].object.disabled = true;
					$.writeCookie(url);
				}
			}
		}
	},
	
	loadStyleSheet : function(url) {
		YAHOO.util.Get.css(url);
	},

	createFormElement : function(parent, elementType, attributes, className, value) {
		var $ = StyleSwitcher;
		var element, i, newNode;
		
		if (typeof elementType === 'undefined' || elementType === null || elementType === '')
		{
			element = document.createTextNode(value);
		}
		else
		{
			element = document.createElement(elementType);
			
			for (i = 0; i < attributes.length; i++)
			{
				if (attributes[i].name !== 'style')
				{
					element.setAttribute(attributes[i].name, attributes[i].value);
				}
				else
				{
					setElementStyle(element, attributes[i].value);
				}
			}
			if (className !== '')
			{
				element.className = className;
			}
			if (value !== '')
			{
				if (elementType !== 'input')
				{
					newNode = document.createTextNode(value);
					element.appendChild(newNode);
				}
				else
				{
					element.value = value;					
				}
			}
		}
		
		if (parent !== null && parent !== '')
		{
			parent.appendChild(element);
		}				

		return element;
	},
	
	setElementStyle : function(element, style) {
		var $ = StyleSwitcher;
		if (element.style)
		{
			element.style.cssText = style;
		}
		else
		{
			element.cssText = style;
		}	
	},	
		
	addCssFile : function(name, url, className, defaultStyle) {
		var $ = StyleSwitcher;
		if (!name || name === '') { return; }
		if (!url || url === '') { return; }
		
		//$.loadStyleSheet(url);
		
		$.styles.push({name:name, url:url, className:className, object:null, defaultStyle:defaultStyle});
	},
	
	readCookie : function() {
		var $ = StyleSwitcher, url = "", i, start, end, value, styleStart, styleEnd;
		
		if (document.cookie.length === 0) { return ''; }
		
		start = document.cookie.indexOf('Style=');
		
		if (start === -1) { return ''; }
		start += 'Style='.length;
		end = document.cookie.indexOf(';', start);
		if (end === -1) { end = document.cookie.length; }
		
		url = document.cookie.substring(start, end);
		
		$.loadStyleSheet(url);
		$.setActiveStyle(url);

		return url;	
	},
	
	writeCookie : function(url) {
		var date, expires;
		
		date = new Date();
		date.setTime(date.getTime() + (365 * 24 * 60 * 1000));
		expires = '; expires=' + date.toGMTString();
		document.cookie = 'Style=' + encodeURI(url) + expires + '; path=/';
	},
	
	event_StyleClicked : function(sender, obj, args) {
		var $ = StyleSwitcher, i;
		var link = YAHOO.util.Event.getTarget(sender);
		
		if (!link) { return; }
		
		var url = link.getAttribute('url');
		if (!url || url === '') { return; }
		
		for (i = 0; i < $.styles.length; i++)
		{
			if ($.styles[i].url.toLowerCase() === url.toLowerCase()) {
				if ($.styles[i].object === null) {
					$.loadStyleSheet(url);	
				}
				
				$.getAllStyleSheets();
			}
		}

		$.setActiveStyle(url);		
	}	
};

StyleSwitcher.addCssFile('Jump','/styles/jump.css', 'theme-1', true);
StyleSwitcher.addCssFile('Woody', '/styles/woody.css', 'theme-2', false);
StyleSwitcher.addCssFile('Rainbow', '/styles/rainbow.css', 'theme-3', false);
StyleSwitcher.addCssFile('Colour', '/styles/colour.css', 'theme-4', false);
StyleSwitcher.init('theme-wrapper', ((typeof supersleight === 'undefined' || supersleight === null) ? null : supersleight.run));