/*
	StyleSwitcher v1.0.2
	Paul Porthouse
	3rd February 2010
	
	Manages the requested styles on the site and allows the user to select one
	
	Requires:
		YUI Library:
			dom.js
			event.js
			cookie.js
			
	History:
		02-02-2010 1.0.0
			- Initial release
*/

SouthTynesideCouncil.WebTeam.StyleSwitcher = function() {
	var wt = SouthTynesideCouncil.WebTeam,
		version = '1-0-3',
		cssRoot = '/assets/styleswitcher/' + version + '/css/',
		stylesheet = null,
		loader = null;
	
	function initialise() {
		var parent = document.getElementById('site-tools'),
			ul, liBW, linkBW, liYB, linkYB, liPB, linkPB, liNormal, linkNormal, label;
			
		if (!wt.isValid(parent)) { return; }
		
		label = wt.createNode(null, 'span', [], '', 'Change colours :');
		
		ul = wt.createNode(null, 'ul', [{name:'id',value:'site-tools-colours'}], '', '');
		
		liNormal = wt.createNode(ul, 'li', [{name:'size',value:'0'}], 'default', '');
		linkNormal = wt.createNode(liNormal, 'a', [{name:'href', value:'javascript:void(0);'}], '', '');
		wt.createNode(linkNormal, 'span', [], '', 'Colour : default');		
		
		liBW = wt.createNode(ul, 'li', [{name:'size',value:'1'}], 'black-white', '');
		linkBW = wt.createNode(liBW, 'a', [{name:'href', value:'javascript:void(0);'}], '', '');
		wt.createNode(linkBW, 'span', [], '', 'Colour : black and white');
		
		liYB = wt.createNode(ul, 'li', [{name:'size',value:'2'}], 'yellow-black', '');
		linkYB = wt.createNode(liYB, 'a', [{name:'href', value:'javascript:void(0);'}], '', '');
		wt.createNode(linkYB, 'span', [], '', 'Colour : yellow and black');		
		
		liPB = wt.createNode(ul, 'li', [{name:'size',value:'3'}], 'pink-black', '');
		linkPB = wt.createNode(liPB, 'a', [{name:'href', value:'javascript:void(0);'}], '', '');
		wt.createNode(linkPB, 'span', [], '', 'Colour : pink and black');		
		
		if (parent.childNodes.length > 1) {
		    parent.insertBefore(ul, parent.childNodes[1]);
		    parent.insertBefore(label, parent.childNodes[1]);
		} else {
			parent.appendChild(label);
		    parent.appendChild(ul);
		}			
		
		YAHOO.util.Event.on(ul, 'click', event_LinkClicked);
	}
	
	function readCookie() {
		var value = YAHOO.util.Cookie.getSub("Configuration", "style");
		if (!value) { value = 0; }
		
		setStyle(parseInt(value, 10));
	}
	
	function saveCookie(style) {
		var date = new Date();
		var newYear = date.getFullYear() + 1;
		date.setFullYear(newYear);
		
		var config = {
			path: "/",
			expires: date
		};
		
		if (document.domain.indexOf("southtynesidehomes.org.uk") > -1) { config.domain = "southtynesidehomes.org.uk"; }

		YAHOO.util.Cookie.setSub("Configuration", "style", style, config);	
	}
	
	function setStyle(style) {
		switch (style) {
			case 1:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css('/css/black-white.css', {onSuccess: event_StylesheetLoaded});
				saveCookie(1);
				break;
			case 2:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css('/css/black-yellow.css', {onSuccess: event_StylesheetLoaded});	
				saveCookie(2);		
				break;
			case 3:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css('/css/black-pink.css', {onSuccess: event_StylesheetLoaded});
				saveCookie(3);
				break;
			default:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css('/css/empty.css', {onSuccess: event_StylesheetLoaded});
				saveCookie(0);
				break;
		}	
	}	
	
	function event_LinkClicked(sender, type, args) {
		var link = YAHOO.util.Event.getTarget(sender),
			size;
		link = wt.getAncestorNodeOfType(link, 'li', false);
		
		size = link.getAttribute('size');
		if (!wt.isValid(size)) {
			return;
		}
		
		setStyle(parseInt(size, 10));
	}
	

	
	function event_StylesheetLoaded(sender, type, args) {
		if (stylesheet) {
			stylesheet.purge();
			stylesheet = null;
		}
		stylesheet = sender;
	}
	
	function event_CookiesReady(sender, type, args) {
		readCookie();
	}
	
	// load the required javascript libraries
	loader = new YAHOO.util.YUILoader({ 
		base: "", 
		require: ["cookie"], 
		loadOptional: false, 
		combine: true, 
		filter: "MIN", 
		allowRollup: true,
		onSuccess: event_CookiesReady
	});
		
	loader.insert();	
	
	YAHOO.util.Event.onContentReady('site-tools', initialise);
};

var myStyleSwitcher = new SouthTynesideCouncil.WebTeam.StyleSwitcher();