/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

// Take content in a div with id="defer-xyz" and move it to a div with id="xyz"
function relocateDeferredContent() {
	var divs=document.getElementsByTagName("div");
	var replacements=new Array();
	for(var i2=0;i2<divs.length;i2++){
		var deferredContent = divs[i2];
		if (deferredContent.id.indexOf("defer-") == 0) {
			var placeHolder = document.getElementById(deferredContent.id.slice(6));
			replacements.push([deferredContent, placeHolder]);
		}
	}
	for(i=0;i2<replacements.length;i2++){
		replacements[i2][0].parentNode.removeChild(replacements[i2][0]);
		replacements[i2][1].parentNode.replaceChild(replacements[i2][0], replacements[i2][1]);
		replacements[i2][0].style.display = "block";
	}
	return true;
}


$(document).ready(function() {
	setTimeout('relocateDeferredContent()',50);
	
	$("#themeTable a").click(function(){
		switchStylestyle(this.getAttribute("rel"));
		var themeText = $(this).text();
		$("#themeChangeText span").text(themeText);
		$("#themeChangeOverlay").show();
		$("#themeChangeText").show();
		setTimeout ( "themeTime()", 3500 );
		$("#themeTable a").removeClass('activeTheme');
		$(this).addClass('activeTheme');
		return false;
	});
	

	$("#locationsTable a").click(function(){
		switchStylestyle(this.getAttribute("rel"));
		var themeText = $(this).text();
		$("#themeChangeText span").text(themeText);
		$("#themeChangeOverlay").show();
		$("#themeChangeText").show();
		setTimeout ( "themeTime()", 3500 );
		$("#locationsTable a").removeClass('activeTheme');
		return false;
	});	

	
    $(".portItemWrap").hover(
      function () {
		$(this).find("a span").animate({opacity: 1}, 300);
      }, 
      function () {
        $(this).find("a span").animate({opacity: 0}, 300);
      }
    );
	
    $(".mainShowWrap a").hover(
      function () {
        $(this).children("div").fadeIn(200);
      }, 
      function () {
         $(this).children("div").fadeOut(200);
      }
    );
	

	var c = readCookie('style');
	if (c) switchStylestyle(c);

		$("#changeThemeWrap").css("marginTop", "-180px")

		 animateUp = function () {
			 $("#changeThemeWrap").animate({ 
					marginTop: "-180px"
				}, 1200 );
			 $("#toggleTheme span").text("CHANGE LOCATION");
		 };
		 animateDown = function () {
			 $("#changeThemeWrap").animate({ 
					marginTop: "0px"
				}, 1200 );
			 $("#toggleTheme span").text("LEAVE IT AS IS");
		 };
		$("#toggleTheme").toggle(animateDown, animateUp);

});


function themeTime() {
	$("#themeChangeOverlay, #themeChangeText").fadeOut();
	$('#toggleTheme').click();
}

function switchStylestyle(styleName)
{
	$('link[@rel*=style][@title]').each(function(i) 
	{
		this.disabled = true;
		if (this.getAttribute('title') == styleName) this.disabled = false;
	});
	createCookie('style', styleName, 365);
	
}

// 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

// hoverBTN functions
var hoverColour = "#663300";

$(function(){
	$("a.hoverBtn").show("fast", function() {
		$(this).wrap("<div class=\"hoverBtn\">");
		$(this).attr("class", "");
	});
	
	//display the hover div
	$("div.hoverBtn").show("fast", function() {
		//append the background div
		$(this).append("<div></div>");
		
		//get link's size
		var wid = $(this).children("a").width();
		var hei = $(this).children("a").height();
		
		//set div's size
		$(this).width(wid);
		$(this).height(hei);
		$(this).children("div").width(wid);
		$(this).children("div").height(hei);
		
		//on link hover
		$(this).children("a").hover(function(){
			//store initial link colour
			if ($(this).attr("rel") == "") {
				$(this).attr("rel", $(this).css("color"));
			}
			//fade in the background
			$(this).parent().children("div")
				.stop()
				.css({"display": "none", "opacity": "1"})
				.fadeIn("fast");
			//fade the colour
			$(this)	.stop()
				.css({"color": $(this).attr("rel")})
				.animate({"color": hoverColour}, 350);
		},function(){
			//fade out the background
			$(this).parent().children("div")
				.stop()
				.fadeOut("slow");
			//fade the colour
			$(this)	.stop()
				.animate({"color": $(this).attr("rel")}, 250);
		});
	});
});

