// JavaScript Document
$(document).ready(function(){
//MAIN NAV SCROLL FUNCTION
   $('ul#mainNav li a').bind('click', function(e) {
        e.preventDefault(); //prevent the "normal" behaviour which would be a "hard" jump

        var target = $(this).attr("href"); //Get the target

        // perform animated scrolling by getting top-position of target-element and set it as scroll target
        $('html, body').stop().animate({ scrollTop: $(target).offset().top }, 2000, function() {
             location.hash = target;  //attach the hash (#jumptarget) to the pageurl
        });

        return false;
	});

//NAV ROLLOVER AND ONSTATE
	$(function(){
		$('#mainNav li a').click(function(){
			$('#mainNav li a').removeClass("onstate");
			var position=$(this).position();
			var thiswidth =$(this).width()+0;
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').stop().animate({left:position.left+"px",width:thiswidth+"px"},600);
			$(this).addClass("onstate");
		});

	//HOME LOGO
		$('span.logo').click(function(){
			$('#mainNav li a').removeClass('onstate');
			$('#mainNav li.bg').stop().fadeOut();
		});
	});


//MAIN MENU ONSTATES WHEN COMING BACK FROM PROJECT PAGES
	$(function(){ 
		var url = window.location.href;
	
		if (url.search("#portfolio") > 0) {
			$('#mainNav li a.portfolio').addClass('onstate');
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').css("right", "351px");
			$('#mainNav li.bg').css("width", "74px");
		} 
	
		else if (url.search("#about") > 0) {
			$('#mainNav li a.about').addClass('onstate');
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').css("right", "267px");
			$('#mainNav li.bg').css("width", "55px");
		}
	
		else if (url.search("#contact") > 0) {
			$('#mainNav li a.contact').addClass('onstate');
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').css("right", "168px");
			$('#mainNav li.bg').css("width", "70px");
		}
	
		else if (url.search("#links") > 0) {
			$('#mainNav li a.links').addClass('onstate');
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').css("right", "92px");
			$('#mainNav li.bg').css("width", "46px");
		}
	
		else if (url.search("#brands") > 0) {
			$('#mainNav li a.brands').addClass('onstate');
			$('#mainNav li.bg').stop().show();
			$('#mainNav li.bg').css("right", "0px");
			$('#mainNav li.bg').css("width", "62px");
		}
	});


//PORTFOLIO IMAGE ROLLOVERS
	$(function(){
	//ALL MAIN IMAGES
		$("#portfolioContent li a").hover(function(){
			$(this).children("span").fadeOut();
		}, function(){
			$(this).children("span").fadeIn();
		});
	});
	
	
	
//MAIN SLIDER FUNCTION	
	//Show the paging and activate its first link
	$(".paging").show();
	$(".paging a:first").addClass("active");
	
	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".window").width()+12;
	var imageSum = $(".image_reel .slide").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		$(".image_reel").animate({
			left: -image_reelPosition
		}, 1000, 'easeInOutExpo' );

	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 15000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	
	
	//On Click
	$(".paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
});
