// Set the original font size (default size in pixels)
var originalFontSize = 12;

// You can change the multipliers if you want
var multiplier2 = 1.1;
var multiplier3 = 1.2;

//fix special for ugly IE6-7
domname = "http://" + document.domain + "/";

function setStyle(style) {
	
	$("#addcss").remove();
	
	//  default style
	if (style == 4) {	
		$('head').append('<link id="addcss" rel="stylesheet" href="'+domname+'assets/templates/autismireland/css/style.css" type="text/css" />');
		$.cookie('style', '', {expires: -1, path: '/'}); // delete cookie
	}
	
	// style 1 
	if (style == 1) {
		$('head').append('<link id="addcss" rel="stylesheet" href="'+domname+'assets/templates/autismireland/css/black.css" type="text/css" />');
		$.cookie('style', '', {expires: -1, path: '/'}); // delete cookie
		$.cookie('style', 1, {path: '/'}); // set cookie
	}
	
	// style 2 
	if (style == 2) {
		$('head').append('<link id="addcss" rel="stylesheet" href="'+domname+'assets/templates/autismireland/css/grey.css" type="text/css" />');
		$.cookie('style', '', {expires: -1, path: '/'}); // delete cookie
		$.cookie('style', 2, {path: '/'}); // set cookie
	}
	
	// style 3
	if (style == 3) {
		$('head').append('<link id="addcss" rel="stylesheet" href="'+domname+'assets/templates/autismireland/css/orange.css" type="text/css" />');
		$.cookie('style', '', {expires: -1, path: '/'}); // delete cookie
		$.cookie('style', 3, {path: '/'}); // set cookie
	}
}

/* Perhaps you won't need to change the code below */

$(document).ready(function(){

	savedSize = $.cookie('fontSize'); // get saved font size from cookies if any
	
	if (savedSize !== null) {
		var fontSize = parseFloat(savedSize, 10);
		$("body").css("font-size", fontSize);
	}
	
	/*
	savedStyle = $.cookie('style'); // get saved style from cookies if any
	
	if (savedStyle !== null) {
		setStyle(savedStyle);
	}
	*/
	
	changeFontSize();
	
	changeColours();
	
	createTabs();
	
	$('#service').sSelect({ddMaxHeight: '200px'});
	$('#county').sSelect({ddMaxHeight: '200px'});
	
	$('a.lightbox').lightBox();
	$('a.lightbox2').lightBox();
	
	$('a.print').click(function(){
		window.print();
		return false;
	});
	
	$('a.tweet').click(function(){
		var thisURL = encodeURIComponent(location.href);
		var thisTITLE = encodeURIComponent(document.title);
		var url = domname + "assets/snippets/addthis/addthis.php?ws=t&url="+thisURL+"&title="+thisTITLE;
		window.open(url,"addthis","");
		return false;
	});
	
	$('a.facebook').click(function(){
		var thisURL = encodeURIComponent(location.href);
		var thisTITLE = encodeURIComponent(document.title);
		var url = domname + "assets/snippets/addthis/addthis.php?ws=f&url="+thisURL+"&title="+thisTITLE;
		window.open(url,"addthis","");
		return false;
	});
	
	var inpval = $('#getupdatedform input').val();
	
	$('#getupdatedform input').focus(function(){
		if ($(this).val() == inpval) {
			$(this).val("");
		}
	});
	$('#getupdatedform input').blur(function(){
		if ($(this).val() == "") {
			$(this).val(inpval);
		}
	});
	
	var inpval2 = $('#search input').val();
	
	$('#search input').focus(function(){
		if ($(this).val() == inpval2) {
			$(this).val("");
		}
	});
	$('#search input').blur(function(){
		if ($(this).val() == "") {
			$(this).val(inpval2);
		}
	});
	
	$("a[rel='external']").attr({
		target: "_blank"
	});

	setInterval("slideSwitch()", 5000);
	
});

function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


function changeFontSize() {	

	// Set the default size 
	$("#stxt").click(function(){
		var newFontSize = parseFloat(originalFontSize, 10);
		$("body").css("font-size", newFontSize);
		$.cookie('fontSize', '', {path: '/', expires: -1 }); // delete cookie
		return false;
	});
	
	// Set the middle size
	$("#mtxt").click(function(){
		var newFontSize = parseFloat(originalFontSize, 10)*multiplier2;
		$("body").css("font-size", newFontSize);
		$.cookie('fontSize', newFontSize, {path: '/'}); // set cookie
		return false;
	});
	
	// Set the big size
	$("#btxt").click(function(){
		var newFontSize = parseFloat(originalFontSize, 10)*multiplier3;
		$("body").css("font-size", newFontSize);
		$.cookie('fontSize', newFontSize, {path: '/'}); // set cookie
		return false;
	});
	
}

function changeColours() {

	$("#chcol1").click(function(){
		setStyle(1);
		return false;
	});
	
	$("#chcol2").click(function(){
		setStyle(2);
		return false;
	});
	
	$("#chcol3").click(function(){
		setStyle(3);
		return false;
	});
	
	$("#chcol4").click(function(){
		setStyle(4);
		return false;
	});
	
}

function createTabs() {
	
	$("#newsblock h2").click(function () {
		if ($(this).hasClass("active") == false) {
			lastid = $("#newsblock h2.active").attr("id");
			$("#newsblock h2.active").removeClass("active");
			pclass = "#newsblock p." + lastid;
			$(pclass).removeClass("active");
			$(this).addClass("active");
			newid = $(this).attr("id");
			pclass = "#newsblock p." + newid;
			$(pclass).addClass("active");
		}
    });
	
}
