$(document).ready(function() {

	//Add image slider on homepage
	if($("#splash").length>0) {
		$("#splash").easySlider({
		controlsShow: false,
		auto: true,
		continuous: true,
		pause: 5000,
		speed: 1600
		});
	}
	function revealLoginForm() {
		var needsRawHtml = false;
			if($("#loginform").length > 0) {
				//The login form is already on the page, it's just hidden
				formcontent = $("#loginform");
			}
			else {
				needsRawHtml = true;
				formcontent = '<div id="loginform"></div>';
			}
			
			if($("#sidebar").length > 0) {
				//Append the new div to the sidebar menu wrapper on the home page
				if(needsRawHtml == true) { $("#sidebar").append(formcontent); }
				else { $("#sidebar").append($(formcontent)); }
			}
			else {
				if(needsRawHtml == true) { $("#login").after(formcontent); }
				else { $("#login").after($(formcontent)); }
				
			}
			$("#login").slideDown('normal',function() { $("#loginform").slideDown(); } );
	}
	
	//Show the login form if necessary
	if(typeof(showLoginForm) != 'undefined' && showLoginForm == true) {
		revealLoginForm();
	}
	
	$("#login").toggle(
		function() {
			revealLoginForm();
			//$("#loginform").load('formtemplate.html',function(t,r) { $(this).slideDown(); }, 'html');
		},
		function() {
			$("#loginform").slideUp(function() {  });
		}
		);
	
	$("#newsletterLink").click(function() {
		
		if($("#newsletterSignupWrap").length > 0) {
			//It's been toggled already, so hide it and remove it
			$("#newsletterSignupWrap").slideUp(function() { $(this).remove() });
			return false;
		}
		else {
			$("#sidebar").append('<div id="newsletterSignupWrap"></div>');
			if(typeof(NewsletterOutput) != 'undefined' && NewsletterOutput != '') {
				$("#newsletterSignupWrap").html('<div class="signupOutput">'+NewsletterOutput+'</div>');
				$("#newsletterSignupWrap").slideDown();
				if(typeof(autoHideNewsletterFeedback) != 'undefined' && autoHideNewsletterFeedback == 1) {
					$("#newsletterSignupWrap").animate({opacity:1},4200,function(){ $(this).slideUp(function() { $(this).remove() });  });
				}
			}
			else {
				$("#newsletterSignupWrap").load('newsletter-signup.html .maincol > div:first',function(t,r) {
						$(".newsletterTerms").toggle(
							function() {
								$(this).append('<div id="newsletterTerms"></div>');
								$("#newsletterTerms").load('newsletter-terms.html .maincol > *:gt(0)',function(t,r) { $(this).slideDown(); }, 'html');
								return false;
							},
							function() {
								$("#newsletterTerms").slideUp(function() { $(this).remove() });
								return false;
							}
						);
				$(this).slideDown();
				}, 'html');
			}
			return false;
		}
	
	});
	$(".newsletterTerms").toggle(
		function() {
			$(this).append('<div id="newsletterTerms"></div>');
			$("#newsletterTerms").load('newsletter-terms.html .maincol > *:gt(0)',function(t,r) { $(this).slideDown(); }, 'html');
			return false;
		},
		function() {
			$("#newsletterTerms").slideUp(function() { $(this).remove() });
			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 <= 12) {
					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");
		}
	
	);;
	
	$.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)");
						var menu = $("#mainmenu").html();
						$("#mainmenu").after('<ul id="mainmenustatic">'+menu+'</ul>');
						$("#mainmenustatic").children("ul").each(function() { $(this).attr("style",""); });
						$("#mainmenustatic > li.level1").each(function() {
								$(this).children("a").each(function() {
										if($(this).html() == "") { $(this).html($(this).attr("title")); } 
									})
							});
						
						$(".h1img, .h2img").each(function() {
							replaceText = $(this).attr("alt");
							$(this).before("<h1 class=\"replacedImage\">"+replaceText+'</h1>');
							$(this).hide();
						});
						
						$("#mainmenu").hide();
					}
					else {
						this.setAttribute("rel","Text only");
						$(this).html("Text only version");
						$("#mainmenustatic").remove();
						$("#mainmenu").show();
						
						$(".replacedImage").hide();
						$(".h1img, .h2img").show();
						
					}
					return false;
				}
			);

});
