// when the DOM is ready:
$(document).ready(function () {
	
/*
	Font replacement
*/

	Cufon.replace('#nav li', {hover: true});
	Cufon.replace('#featureQuote');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('span.section2headline');
	Cufon.replace('#map p');
	
/*
	Brands post right margin fix
*/

	$('div.brands-post:nth-child(3n+2)').css("margin-right", "0");
	
/*
	Portfolio button fade
*/

	$("#portfolioButton").hover(function(){
		$("#portfolioButton img").fadeIn(500);
		},function(){
			$("#portfolioButton img").fadeOut(250);
	});

/*
	Page scroller
*/
	$(function(){
	    $(' a[href=#home], a[href=#whoweare], a[href=#portfolio],  a[href=#video],  a[href=#news],  a[href=#contact]').click(function() {
	    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
	        && location.hostname == this.hostname) {
	            var $target = $(this.hash);
	            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	            if ($target.length) {
	                var targetOffset = $target.offset().top;
	                $('html,body').animate({scrollTop: targetOffset}, 500);
	                return false;
	            }
	        }
	    });
	});

/*
	Note toggle (3dB Asterisk)
*/

	$("span.toggle").hover(function(){
		$("div.note").fadeIn(500);
		},function(){
			$("div.note").fadeOut(250);
	});

/*
	Main Portfolio Slider
*/

	//Show the paging and activate its first link
	$(".portfolio_paging span").show();
	$(".portfolio_paging span a:first").addClass("activeSlide");

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".portfolio_reel div").size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$(".portfolio_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

	    $(".portfolio_paging a").removeClass('activeSlide'); //Remove all active class
	    $active.addClass('activeSlide'); //Add active class (the $active is declared in the rotateSwitch function)

	    //Slider Animation
	    $(".portfolio_reel").animate({
	        left: -image_reelPosition
	    }, 500 );

	}; 

	//On Click - Number toggle
	$(".portfolio_paging span 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
	});
	
	//On Click - Next button
	$("a.nextButton").click(function() {
	    $active = $('.portfolio_paging span a.activeSlide').next(); //Activate the clicked paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.portfolio_paging span a:last');
	        }
	    //Reset Timer
	    clearInterval(play); //Stop the rotation
	    rotate(); //Trigger rotation immediately
	    rotateSwitch(); // Resume rotation timer
	    return false; //Prevent browser jump to link anchor
	});
	
	//On Click - Previous button
	$("a.prevButton").click(function() {
	    $active = $('.portfolio_paging span a.activeSlide').prev(); //Activate the clicked paging
			if ( $active.length === 0) { //If paging reaches the end...
            	$active = $('.portfolio_paging span a:first');
        	}
	    //Reset Timer
	    clearInterval(play); //Stop the rotation
	    rotate(); //Trigger rotation immediately
	    rotateSwitch(); // Resume rotation timer
	    return false; //Prevent browser jump to link anchor
	});

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

	rotateSwitch(); //Run function on launch

/*
	Portfolio Individual Post Slider
*/

	$('.portfolio-post').hover(function(){
					$("img", this).stop().animate({left:'-130px'},{queue:false,duration:200});
				}, function() {
					$("img", this).stop().animate({left:'0px'},{queue:false,duration:400});
				});

/*
	Portfolio Fancybox
*/

	$("a.inline").fancybox({
			'transitionIn'	:	'fade',
			'transitionOut'	:	'fade',
			'speedIn'		:	600, 
			'speedOut'		:	250, 
		});
	
	$("p#closeButton").click(function() {
		$.fancybox.close()
		});


/*
	Remove Active link attribute
*/

	$('a.active').removeAttr('href');

/*
	Directions tabs
*/

	$(function () {
	    var tabContainers = $('div.directionsTabs > div');

	    $('div.directionsTabs ul.tabNavigation a').click(function () {
	        tabContainers.hide().filter(this.hash).show();

	        $('div.directionsTabs ul.tabNavigation a').removeClass('selected');
	        $(this).addClass('selected');

	        return false;
	    }).filter(':first').click();
	});


});


