/*
	ActiveTextSize v1.0.2
	Paul Porthouse
	29th October 2008
	
	Manages the text size functionality on the website.
	
	Requires:
		YUI Library:
			dom.js
			event.js
			cookie.js
			
	History:
		02-02-2010 1.0.3
			- reversed the order that the items are added to the list as they
			- are being added to the start of the list.
	    02-02-2010 1.0.2
	        - copied the implementation for STHomes new website
		08-09-2009 1.0.2
			- rewrote component to reduce it's footprint by using 
				the utility functions.
		07-11-2008 1.0.1
			- changed the loaded base to be able to use https://
				and from different urls
		29-10-2008 1.0.0
			- Initial release
*/

SouthTynesideCouncil.WebTeam.TextResize = function() {
	var wt = SouthTynesideCouncil.WebTeam,
		version = '1-0-3',
		cssRoot = '/assets/textresize/' + version + '/css/',
		stylesheet = null,
		loader = null;
		
		
	
	function initialise() {
		var parent = document.getElementById('site-tools'),
			ul, label, liSmall, linkSmall, liMedium, linkMedium, liLarge, linkLarge;
			
		if (!wt.isValid(parent)) { return; }
		
		label = wt.createNode(null, 'span', [], '', 'Change text size :');
		
		ul = wt.createNode(null, 'ul', [{name:'id',value:'site-tools-text-size'}], '', '');
		
		liSmall = wt.createNode(ul, 'li', [{name:'size',value:'0'}], 'small', '');
		linkSmall = wt.createNode(liSmall, 'a', [{name:'href',value:'javascript:void(0);'}], '', 'A');
		linkSmall.appendChild(wt.createNode(null, 'span', [], '', '- small text size'));
		
		liMedium = wt.createNode(ul, 'li', [{name:'size',value:'1'}], 'medium', '');
		linkMedium = wt.createNode(liMedium, 'a', [{name:'href',value:'javascript:void(0);'}], '', 'A');
		linkMedium.appendChild(wt.createNode(null, 'span', [], '', '- medium text size'));
		
		liLarge = wt.createNode(ul, 'li', [{name:'size',value:'2'}], 'large', '');
		linkLarge = wt.createNode(liLarge, 'a', [{name:'href',value:'javascript:void(0);'}], '', 'A');
		linkLarge.appendChild(wt.createNode(null, 'span', [], '', '- large text size'));
		
		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", "textsize");
		if (!value) { value = 0; }
		
		setTextSize(parseInt(value, 10));
	}
	
	function saveCookie(size) {
		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", "textsize", size, config);	
	}
	
	function setTextSize(size) {
		switch (size) {
			case 1:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css(cssRoot + 'textsize-larger.css', {onSuccess: event_StylesheetLoaded});
				saveCookie(1);
				break;
			case 2:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css(cssRoot + 'textsize-largest.css', {onSuccess: event_StylesheetLoaded});	
				saveCookie(2);		
				break;
			default:
				if (stylesheet) {
					stylesheet.purge();
				}
				YAHOO.util.Get.css(cssRoot + 'textsize-normal.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;
		}
		
		setTextSize(parseInt(size, 10));
	}
	
	function event_StylesheetLoaded(sender, type, args) {
		if (stylesheet) {
			stylesheet.purge();
		}
		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 myTextResize = new SouthTynesideCouncil.WebTeam.TextResize();