/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});




$jq(document).ready(function() {
	
	$jq('#customise #text').toggle(function() {
		$jq('#customise #bg-slider').stop(true, true).animate({
			height : "460px"
		}, 1500, 'easeOutQuint'); 
	}, function() {
		$jq('#customise #bg-slider').stop(true, true).animate({
			height : "0px"
		}, 800, 'easeOutQuint'); 
	});
	
	
	$jq("#customise .left img").click(function() {
		slider.right();
	});
	
	$jq("#customise .right img").click(function() {
		slider.left();	
	});
	
	$jq("#customise .background-inner").click(function() {
		slider.setBackground($jq(this));
		
		
	});
	
	setTimeout("contact.next();", 5000);
	
	
});


var contact = {
	phoneVisible : true,
	
	next: function() {
		
		if (this.phoneVisible) {
			$jq("#wwb-email").fadeIn(500, function() {
				$jq("#wwb-phone").hide();
			});
			this.phoneVisible = false;
		} 
		else
		{
			$jq("#wwb-phone").fadeIn(500, function() {
				$jq("#wwb-email").hide();
			});
			this.phoneVisible = true;
		}
		
		setTimeout("contact.next();", 5000);
	}
}


var slider = {
	
	dir : "",
	
	locked : false,
	
	left : function() {
		if (!this.locked)
		{
			this.locked = true;
			
			// cancel any ongoing animation
			if (this.dir !== "left")
			{
				$jq("#customise .background").stop(true, true);
				this.dir = "left";
			}
			
			$jq("#customise .backgrounds .background:first-child").animate({ 
				marginLeft : "-" + $jq("#customise .backgrounds .background:first-child").width() + "px"
			}, 500, 'linear',  function() {
				$jq('#customise .backgrounds').append($jq('#customise .backgrounds .background:first-child'));
				$jq('#customise .backgrounds .background:last-child').css({marginLeft: "10px"});
				
				slider.locked = false;
			});		
		}
	},
	right : function() {
		if (!this.locked)
		{
			this.locked = true;
		
			// cancel any ongoing animation
			if (this.dir !== "right")
			{
				$jq("#customise .background").stop(true, true);
				this.dir = "right";
			}
			
			$jq("#customise .backgrounds").prepend($jq("#customise .backgrounds .background:last-child"));
			$jq('#customise .backgrounds .background:first-child').css({marginLeft: "-" + $jq('#customise .backgrounds .background:first-child').width() + "px"});
			$jq("#customise .backgrounds .background:first-child").animate({
				marginLeft: "10px"
			}, 500, 'linear', function() {
				slider.locked = false;
			});
		}
	},

	setBackground : 	function(b) {
		
		var pos = "top center";
		var bg = b.css("background-image");
		
		if (bg.match("stripe"))
		{
			pos = "top left";
		}
		
		background = b.css("background-color") + " " + b.css("background-image").replace("-thumb", "-full") + " " + pos + " fixed repeat-y";
		
		$jq("#body_middlewood").css("background", background);
		
		cookie.set("background", background, 31);
		
		slider.setContainers();
		
	},
	
	setContainers : function() {
		
		var color ="";
		var background = cookie.get("background");
		if (background == "") {background == "bg-sky";}
			
		if (background.match("bg-gold") || background.match("yellowstripe")) {
			color = "orange";
		}
		else if (background.match("bg-leaf") || background.match("greenstripe")) {
			color = "green";
		}
		else if (background.match("bg-sky") || background.match("bluestripe")) {
			color = "royalblue";
		}
		
		
		var oldColor = cookie.get("color");		
		if (oldColor == "" || (color === oldColor)) {oldColor = "royalblue";}
		
		$jq("img").each(function() {
			if ($jq(this).attr("src").match(oldColor)) {
				$jq(this).attr("src", $jq(this).attr("src").replace(oldColor, color));	
			}
		});
		
		var ct = oldColor + "title";
		
		$jq("." + ct).each(function() {
			$jq(this).removeClass(ct);
			$jq(this).addClass(color + "title");
		});
		
		cookie.set("color", color, 31);
	}
	
};



var cookie = {
	set : function (c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
		";path=/";
	},
	get : function (c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1; 
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			} 
	  	}
		return "";
	}
};
