/* Coded by James Thomson. Copyright 2009. All Rights Reserved */

$(document).ready(function() {
	$('a.lightbox').lightBox(); // Select all links with lightbox class for use with jQuery lightbox plugin 0.5
	
	$(".what-toggle").click(function(event) {
		$("#what").slideToggle("slow");
		event.preventDefault(); // Prevent link from performing default action
	});
	
	$(".contact-toggle").click(function(event) {
		$("#contact").slideToggle("slow");
		event.preventDefault(); // Prevent link from performing default action
	});
	
	
	$(".switch").click(function(event) {
		var arrowPos = 225;
		
		$(".current").removeClass("current"); //Remove the .current class
		$(this).parents("li").addClass("current"); //Add the .current class to this
		
		var curPos = $(".sub-menu").position(); //Get sub-menu's current position
		var thisPos = $(this).position(); //Get this current position
		var thisWidth = $(this).parents("li").outerWidth()/2; //Get this item's width then devide it by 2
		
		var finalPos = (arrowPos-thisPos.left)-thisWidth+9; //Calculate the final position. Add 9 for arrow width.
		if(curPos.left == finalPos) {return false;} //Kill the operation if these are equal as there is no need to run the animation 
		$(".sub-menu").animate({"left" : finalPos}, 1000); //Animate the slide to the calculated position		
		
		var workID = "#work-"+this.id; // Add ID from this and add work- to it.
		var workPos = $(workID).position(); //Get the current position of workID.
		var finalWorkPos = workPos.top; //Get top position
		$(".work").animate({"top" : -finalWorkPos}, 1000); //Animate to desired position 
		
		event.preventDefault(); // Prevent link from performing default action
	});
	
	$(".prev").click(function(event){
		var curPos = $(this).siblings(".inner").children("ul").position(); //Get current position
		var moveTo = curPos.left+580; //650-70. 70 for the difference from side to img
		var count = $(this).siblings(".inner").children("ul").children("li").length; //Get number of li's
		if(moveTo > 0) {moveTo = (count-1)*-650}; //Added to create a seamless carousel loop 
		$(this).siblings(".inner").children("ul").animate({"left" : moveTo}, 500); //Animate to desired position
		
		event.preventDefault(); // Prevent link from performing default action
	});
	
	$(".next").click(function(event){
		var curPos = $(this).siblings(".inner").children("ul").position(); //Get current position
		var moveTo = curPos.left-720; //650+70. 70 for the difference from side to img
		var count = $(this).siblings(".inner").children("ul").children("li").length; //Get number of li's
		if(moveTo <= count*-650) {moveTo = 0}; //Added to create a seamless carousel loop 
		$(this).siblings(".inner").children("ul").animate({"left" : moveTo}, 500); //Animate to desired position
		
		event.preventDefault(); // Prevent link from performing default action
	});
});