

$(document).ready(function(){

	var play = -1;	
    
    // Hide slider until initialized
    $("#header .slider .image-reel").hide();
    
	// Set the active paging image
	$("#header .extras .slider-navigation a:first").addClass("slider-active");
	$(".slider-active img").attr("src", "uploads/YES/images/slider-nav-circle-selected.png");	

	var imageWidth = $("#header .slider").width();
	var imageSum = $("#header .slider .image-reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	// Set new size of image reel
	$("#header .slider .image-reel").css({'width' : imageReelWidth});	
    
    
    $("#header .slider .image-reel").fadeIn('fast');

	// Rotate function variable
	rotate = function(){	

		var id = $active.attr("rel") - 1;
		var newPos = id * imageWidth;		

		// Set active pager

		$(".slider-active img").attr("src", "uploads/YES/images/slider-nav-circle.png");
		$(".slider-active").removeClass('slider-active'); 
		$active.addClass('slider-active'); 
		$(".slider-active img").attr("src", "uploads/YES/images/slider-nav-circle-selected.png");

		// Animate
		$("#header .slider .image-reel").animate({
			left: -newPos
		}, 500 );

	}; 

	// Timer function
	rotateSwitch = function(){	

		play = setInterval(function(){ 
			$active = $('.slider-active').next();
			if ( $active.length === 0) { 
				// Go back to first link if we've rolled over
				$active = $('#header .extras .slider-navigation a:first'); 
			}
			rotate();
		}, 7000); 
		
	};

	$("#header .slider .image-reel a").hover(function() {
		clearInterval(play);
	}, function() {
		rotateSwitch();
	});	

	//Navigation Click
	$("#header .extras .slider-navigation 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
	});	
    
    // Focus hacks
    onBlur = function() {
		clearInterval(play); //Stop the rotation
    };
    
    onFocus = function() {
		rotateSwitch(); // Resume rotation timer
    };

    if (/*@cc_on!@*/false) { // check for Internet Explorer
        document.onfocusin = onFocus;
        document.onfocusout = onBlur;
    } else {
        window.onfocus = onFocus;
        window.onblur = onBlur;
    }

    if (play == -1) {
        rotateSwitch(); // Run function on launch if not already active
    }

});
