var cookies = false;

$(document).ready(function(){

	if(typeof $.cookie !== 'undefined'){
		cookies = true;	
	}

//////////////////////////////////////////////////////////////////////
	if(cookies){
	
		// SHOULD THESE USE THE SAME FUNCTIONS AS BELOW!!!!!!!!!!!!!!!!!!!!!!
		// maybe not, as they do a simpler task (ie, don't have to replace other classes)
	
		// use the TEXTSIZE cookie to make the text the correct size
		if($.cookie('TEXTSIZE') == "smalltext")       {$("body").toggleClass("smalltext");}
		else if($.cookie('TEXTSIZE') == "mediumtext") {$("body").toggleClass("mediumtext");}
		else if($.cookie('TEXTSIZE') == "largetext")  {$("body").toggleClass("largetext");}
	
		// use the SITECOLOURS cookie to make the text the correct colour
		if($.cookie('SITECOLOURS') == "yellow_black")       {$("body").toggleClass("yellow_black");}
		else if($.cookie('SITECOLOURS') == "red_pink")      {$("body").toggleClass("red_pink");}
		else if($.cookie('SITECOLOURS') == "black_white")   {$("body").toggleClass("black_white");}
	}
//////////////////////////////////////////////////////////////////////

	var css_on = false;
	if($("#features").width() != $("#identity").width()) { var css_on = true; } // is CSS on?
	if(css_on){
		// replace text resize link with A A A
		$("#textsize").parent().parent().after('<div id="style_controls"><ul class="textsize2"><li><a href="#" id="smalltext" title="testun gwreiddiol">A</a></li><li><a href="#" id="mediumtext" title="testun cyfrwng">A</a></li><li><a href="#" id="largetext" title="testun fawr">A</a></li></ul><ul class="sitecolours"><li><a href="#" id="black_white" title="du/gwyn">A</a></li><li><a href="#" id="red_pink" title="coch/pinc">A</a></li><li><a href="#" id="yellow_black" title="melyn/du">A</a></li></ul></div>');
		$("#textsize").parent().remove();
		// the following all have the same function, so MUST be able to optimise this.
	
		$("#smalltext").click(function(){
			$.cookie("TEXTSIZE", $(this).attr("id"), { expires: 7, path: '/', secure: false });
			if($("body").hasClass("largetext"))  {$("body").removeClass("largetext");}
			if($("body").hasClass("mediumtext")) {$("body").removeClass("mediumtext");}
			if($("body").hasClass("smalltext"))  {}
			else                                 {$("body").addClass("smalltext");}
			return false;
		});
		$("#mediumtext").click(function(){
			$.cookie("TEXTSIZE", $(this).attr("id"), { expires: 7, path: '/', secure: false });
			if($("body").hasClass("largetext"))  {$("body").removeClass("largetext");}
			if($("body").hasClass("smalltext"))  {$("body").removeClass("smalltext");}
			if($("body").hasClass("mediumtext")) {}
			else                                 {$("body").addClass("mediumtext");}
			return false;
		});
		$("#largetext").click(function(){
			$.cookie("TEXTSIZE", $(this).attr("id"), { expires: 7, path: '/', secure: false });
			if($("body").hasClass("smalltext"))  {$("body").removeClass("smalltext");}
			if($("body").hasClass("mediumtext")) {$("body").removeClass("mediumtext");}
			if($("body").hasClass("largetext"))  {}
			else                                 {$("body").addClass("largetext");}
			return false;
		});
		
		$("#yellow_black").click(function(){
			if($.cookie("SITECOLOURS") == $(this).attr("id")) {$.cookie("SITECOLOURS", null, { path: '/', secure: false });}
			else                                              {$.cookie("SITECOLOURS", $(this).attr("id"), { expires: 7, path: '/', secure: false });}
			if($("body").hasClass("red_pink"))     {$("body").removeClass("red_pink");}
			if($("body").hasClass("black_white"))  {$("body").removeClass("black_white");}
			if($("body").hasClass("yellow_black")) {$("body").removeClass("yellow_black");}
			else                                   {$("body").addClass("yellow_black");}
			return false;
		});
		$("#red_pink").click(function(){
			if($.cookie("SITECOLOURS") == $(this).attr("id")) {$.cookie("SITECOLOURS", null, { path: '/', secure: false });}
			else                                              {$.cookie("SITECOLOURS", $(this).attr("id"), { expires: 7, path: '/', secure: false });}
			if($("body").hasClass("yellow_black")) {$("body").removeClass("yellow_black");}
			if($("body").hasClass("black_white"))  {$("body").removeClass("black_white");}
			if($("body").hasClass("red_pink"))     {$("body").removeClass("red_pink");}
			else                                   {$("body").addClass("red_pink");}
			return false;
		});
		$("#black_white").click(function(){
			if($.cookie("SITECOLOURS") == $(this).attr("id")) {$.cookie("SITECOLOURS", null, { path: '/', secure: false });}
			else                                              {$.cookie("SITECOLOURS", $(this).attr("id"), { expires: 7, path: '/', secure: false });}
			if($("body").hasClass("yellow_black")) {$("body").removeClass("yellow_black");}
			if($("body").hasClass("red_pink"))     {$("body").removeClass("red_pink");}
			if($("body").hasClass("black_white"))  {$("body").removeClass("black_white");}
			else                                   {$("body").addClass("black_white");}
			return false;
		});
	}
	
	//redirect "listen_to_site"
	$("#listen_to_site").click(function(){
		document.location = "http://spoxy4.insipio.com/generator/cy/" + document.location;
		return false;
	});
	
	// for customise.cfm
	$('#choose-colours label').click(function(){
		$('#choose-colours li.active').removeClass('active');
		$(this).parents('li').addClass('active');
	})
	
	// adding mobile link
	var mobile_li = document.createElement("li");
	var mobile_link = document.createElement("a");
	var mobile_text = document.createTextNode("fersiwn symudol");
	mobile_li.appendChild(mobile_link);
	mobile_link.appendChild(mobile_text);
	mobile_link.setAttribute("href","http://www.wrexham.gov.uk/mobile/");
	$("#listen_to_site").parent().after($(mobile_li));
});