/**
* Stylesheet toggle variation on styleswitch stylesheet switcher.
* Built on jQuery.
* Under an CC Attribution, Share Alike License.
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

(function($)
	{
		// Local vars for toggle
		var availableStylesheets = [];
		var activeStylesheetIndex = 0;
		
		// To loop through available stylesheets
		$.stylesheetToggle = function()
		{
			activeStylesheetIndex ++;
			activeStylesheetIndex %= availableStylesheets.length;
			$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
		};
		
		// To switch to a specific named stylesheet
		$.stylesheetSwitch = function(styleName)
		{
			$('link[@rel*=style][title]').each(
				function(i) 
				{
					this.disabled = true;
					if (this.getAttribute('title') == styleName) {
						this.disabled = false;
						activeStylesheetIndex = i;
					}
				}
			);
			createCookie('style', styleName, 365);
		};
		
		// To initialise the stylesheet with it's 
		$.stylesheetInit = function()
		{
			$('link[rel*=style][title]').each(
				function(i) 
				{
					availableStylesheets.push(this.getAttribute('title'));
				}
			);
			var c = readCookie('style');
			if (c) {
				$.stylesheetSwitch(c);
				if(c == 'Text only') {
						$(".styleswitch").attr("rel","Main styles");
						$(".styleswitch").html("View standard version (with images)");
						$("body").addClass("bodytextonly");
						$(".sectionnav").before("<div id=\"oldsectionnav\"></div>").appendTo("#mainmenuwrap").before("<h2 class=\"visibletextonly\">Other pages in this section:</h2>");
				}
				else {
					$("body").removeClass("bodytextonly");
					$(".sectionnav").insertAfter("#oldsectionnav");
					$("#oldsectionnav, .visibletextonly").remove();
				}
			}
		};
	}
)(jQuery);

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions


$(document).ready(function() {
	
	// Make parent elements rolloverable
	
		$(".rollover").hover(
			function() {
				$(this).addClass("rolled"); 
			},
			function() {
				$(this).removeClass("rolled"); 
			}
		).click(function() {
			window.location=$(this).find("a").attr("href"); return false;
		
		});
		
	/* Stylesheet switcher (for text only) */
	$.stylesheetInit();
			
			// When one of the styleswitch links is clicked then switch the stylesheet to
			// the one matching the value of that links rel attribute.
			$('.styleswitch').bind(
				'click',
				function(e)
				{
					$.stylesheetSwitch(this.getAttribute('rel'));
					if(this.getAttribute('rel') == 'Text only') {
						this.setAttribute("rel","Main styles");
						$(this).html("View standard version (with images)");
						$("body").addClass("bodytextonly");
						$(".sectionnav").before("<div id=\"oldsectionnav\"></div>").appendTo("#mainmenuwrap").before("<h2 class=\"visibletextonly\">Other pages in this section:</h2>");
					}
					else {
						this.setAttribute("rel","Text only");
						$(this).html("Text only version");
						$("body").removeClass("bodytextonly");
						$(".sectionnav").insertAfter("#oldsectionnav");
						$("#oldsectionnav, .visibletextonly").remove();
					}
					
					if($(".projecttabs").length > 0) doTabs();
					
					return false;
				}
			);
	$("#textSizeChange > span").show().css({"cursor":"pointer", "display":"block", "float":"left"});
	$("#textSizeChange > span").click(function() {
		//Get whether smaller or bigger
		var changeDir = $(this).hasClass("bigger") ? 'bigger' : 'smaller';
		
		//Check what the current text size is
				currentSize = $("body").css("font-size");
				originalSize = currentSize.substring(0, currentSize.indexOf("p") );
				//Size is in px
				currentSize = Math.floor(originalSize);
		//alert("Curent size is "+(currentSize*0.0626));
		switch(changeDir) {
			case 'smaller':
				if(currentSize <= 13) {
					currentSize = originalSize;
				}
				else {
					currentSize = currentSize - 1.5;
				}
				
			break;
			case 'bigger':
				if(currentSize == 18) {
					currentSize = originalSize;
				}
				else {
					currentSize = currentSize + 1.5;
				}
			break;
			default:
			break;
		}
		$("body").css("font-size",currentSize+"px");
		return false;
	}).hover(
		function() {
			$(this).addClass("textSizeHover");
		},
		function() {
			$(this).removeClass("textSizeHover");
		}
	
	);
});
/*
 
 jQuery Tools 1.2.4 Overlay - Overlay base. Extend it.

 NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.

 http://flowplayer.org/tools/overlay/

 Since: March 2008
 Date:    Sun Aug 15 08:16:31 2010 +0000 
*/
(function(a){function t(d,b){var c=this,i=d.add(c),o=a(window),k,f,m,g=a.tools.expose&&(b.mask||b.expose),n=Math.random().toString().slice(10);if(g){if(typeof g=="string")g={color:g};g.closeOnClick=g.closeOnEsc=false}var p=b.target||d.attr("rel");f=p?a(p):d;if(!f.length)throw"Could not find Overlay: "+p;d&&d.index(f)==-1&&d.click(function(e){c.load(e);return e.preventDefault()});a.extend(c,{load:function(e){if(c.isOpened())return c;var h=q[b.effect];if(!h)throw'Overlay: cannot find effect : "'+b.effect+
'"';b.oneInstance&&a.each(s,function(){this.close(e)});e=e||a.Event();e.type="onBeforeLoad";i.trigger(e);if(e.isDefaultPrevented())return c;m=true;g&&a(f).expose(g);var j=b.top,r=b.left,u=f.outerWidth({margin:true}),v=f.outerHeight({margin:true});if(typeof j=="string")j=j=="center"?Math.max((o.height()-v)/2,0):parseInt(j,10)/100*o.height();if(r=="center")r=Math.max((o.width()-u)/2,0);h[0].call(c,{top:j,left:r},function(){if(m){e.type="onLoad";i.trigger(e)}});g&&b.closeOnClick&&a.mask.getMask().one("click",
c.close);b.closeOnClick&&a(document).bind("click."+n,function(l){a(l.target).parents(f).length||c.close(l)});b.closeOnEsc&&a(document).bind("keydown."+n,function(l){l.keyCode==27&&c.close(l)});return c},close:function(e){if(!c.isOpened())return c;e=e||a.Event();e.type="onBeforeClose";i.trigger(e);if(!e.isDefaultPrevented()){m=false;q[b.effect][1].call(c,function(){e.type="onClose";i.trigger(e)});a(document).unbind("click."+n).unbind("keydown."+n);g&&a.mask.close();return c}},getOverlay:function(){return f},
getTrigger:function(){return d},getClosers:function(){return k},isOpened:function(){return m},getConf:function(){return b}});a.each("onBeforeLoad,onStart,onLoad,onBeforeClose,onClose".split(","),function(e,h){a.isFunction(b[h])&&a(c).bind(h,b[h]);c[h]=function(j){a(c).bind(h,j);return c}});k=f.find(b.close||".close");if(!k.length&&!b.close){k=a('<a class="close"></a>');f.prepend(k)}k.click(function(e){c.close(e)});b.load&&c.load()}a.tools=a.tools||{version:"1.2.4"};a.tools.overlay={addEffect:function(d,
b,c){q[d]=[b,c]},conf:{close:null,closeOnClick:true,closeOnEsc:true,closeSpeed:"fast",effect:"default",fixed:!a.browser.msie||a.browser.version>6,left:"center",load:false,mask:null,oneInstance:true,speed:"normal",target:null,top:"10%"}};var s=[],q={};a.tools.overlay.addEffect("default",function(d,b){var c=this.getConf(),i=a(window);if(!c.fixed){d.top+=i.scrollTop();d.left+=i.scrollLeft()}d.position=c.fixed?"fixed":"absolute";this.getOverlay().css(d).fadeIn(c.speed,b)},function(d){this.getOverlay().fadeOut(this.getConf().closeSpeed,
d)});a.fn.overlay=function(d){var b=this.data("overlay");if(b)return b;if(a.isFunction(d))d={onBeforeLoad:d};d=a.extend(true,{},a.tools.overlay.conf,d);this.each(function(){b=new t(a(this),d);s.push(b);a(this).data("overlay",b)});return d.api?b:this}})(jQuery);

$(document).ready(function() {

	//For newsletter terms and conditions popup overlay
	if($("a#newsletterTandC").length>0) {
		$("a#newsletterTandC[rel]").overlay({
			mask: 'darkred',
			onBeforeLoad: function() {
				var wrap = this.getOverlay().find(".contentWrap");
				if (wrap.is(":empty")) {
					wrap.load(this.getTrigger().attr("href"));
				}
			}

		});
	}
});
