$(function(){
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size()+1;
	var pagingSum  = $(".paging a").size();
	var maxHideNumber = pagingSum-5;
	var imageReelWidth = imageWidth * imageSum;
	var trigerCount = 0;
	var idleTime = 0; 
	var triggerID, image_reelPosition;
	$promoIdleTime= $('#promoIdleTime').val()*1000;
	$sessionIdleTime= $('#SessionIdleTime').val()*1000;
 
 	
	if(pagingSum>1)	{
			//Set Default State of each portfolio piece
		$(".paging").show();
		$(".paging a:first").addClass("active");
		$(".previousClick").show();
		$(".playPause").show();
		$(".nextClick").show();	
	} else{
		$(".pagingWindow").css("right", 18); 
	}
   //Increment the idle time counter every second. 
   var idleInterval = setInterval("timerIncrement()", $sessionIdleTime); 
  timerIncrement = function(){	
     idleTime++; 
     if (idleTime > 1) 
     { 
       doPreload(); 
     } 
   } 
   //Zero the idle timer on mouse movement. 
   $(this).mousemove(function(e){ 
      idleTime = 0; 
   }); 
    doPreload = function(){
	  idleTime = 0;
	  showRoatation();
   } 
if(pagingSum < 5){
	$(".pagingWindow").css({'width' : (23*pagingSum)});
}
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
      var $mainNavigation = $("#mainNavigation"),
        $current = null;
		$navitemFirst = $("li.navitemFirst a");
		$navitemFirst.addClass("selected");
      
      $mainNavigation.find("li.navitem").each(function(){
         var $this = $(this), 
             $section = $this.find(".section"),
             $a = $this.find("h3 a");
             
         $a.hover(
            function(){ 
			   $(".main_view").hide();
			   clearInterval(play);
			    $section.show();
               if ($current != null) {
                  if ($current[0] != $section[0]) {
                     $current.hide();
                     $current.parent().find("h3 a").removeClass("selected");
                     $section.show();
                     clearInterval(play);
                  }
               } else {
					clearInterval(play);
					$section.show();
			   }
               $current = $section;
				clearInterval(play);
				$navitemFirst.removeClass("selected");
               $(this).addClass("selected");
            },
            function(){ 
				clearInterval(play);   
			}
         );
	});
	$navitemFirst.hover(
            function(){ 
				showRoatation();
            },
            function(){  }
         );
	showRoatation = function(){	
				clearInterval(play);
				$mainNavigation.find("li.navitem").find("h3 a").removeClass("selected");
				$navitemFirst.addClass("selected");
				$mainNavigation.find("li.navitem").find(".section").hide();
				$(".main_view").show();
				$(".playPause img").attr("src", "/static/media_structure/common/img/pause.gif");
					$active = $('.paging a:first'); //go back to first
					$(".paging a").show()
					trigerCount=0;
					triggerID = $active.attr("rel") - 1; //Get number of times to slide
					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").css("left", -(image_reelPosition+imageWidth)); 
				rotateSwitch(); // Resume rotation
	}
	directRotate = function(){	
		triggerID = $active.attr("rel") - 1; //Get number of times to slide
		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").hide();
		$(".image_reel").fadeIn(100);
		$(".image_reel").css("left", -(image_reelPosition+imageWidth)); 
	};
	initRotate = function(){	
		triggerID = $active.attr("rel") - 1; //Get number of times to slide
		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").css("left", 0); 
	};
	$(".previousClick a").hover(
            function(){ 
              clearInterval(play);
            },
            function(){ 
				if ( $(".playPause img").attr("src") === "/static/media_structure/common/img/pause.gif") {
				rotateSwitch(); // Resume rotation
				}
			}
	);
	$(".previousClick a").click(function() {
			$active = $('.paging a.active').prev();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:last'); //go back to first
				triggerID = $active.attr("rel");
				image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
				trigerCount = triggerID-4; 
				for(k=0; k<trigerCount; k++) {
					$(".paging a").eq(k).hide();
				}
				$(".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").css("left", -(image_reelPosition+imageWidth)); 
			}
			if ( $active.length > 0) {
				directRotate();
			}
		 if(trigerCount>0){
			$(".paging a").eq(trigerCount-1).show();
			trigerCount--;
		 }
		 else if (trigerCount=== 0)
		 {
			$(".paging a").show();
		 }
		$(".playPause img").attr("src", "/static/media_structure/common/img/pause.gif"); 
		return false; //Prevent browser jump to link anchor
      });
      
	$(".nextClick a").hover(
            function(){ 
              clearInterval(play);
            },
            function(){ 
				if ( $(".playPause img").attr("src") === "/static/media_structure/common/img/pause.gif") {
					rotateSwitch(); // Resume rotation
				}
			}
	);
	$(".nextClick a").click(function() {
			$active = $('.paging a.active').next();
				if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
				$(".paging a").show()
					trigerCount=-1;
					initRotate();
			}
			if ( $active.length > 0) {
				directRotate();
			}
		 if(trigerCount<maxHideNumber){
			clearInterval(play);
			$(".paging a").eq(trigerCount).hide();
			trigerCount++
		}
		$(".playPause img").attr("src", "/static/media_structure/common/img/pause.gif"); 
		return false; //Prevent browser jump to link anchor
	});
	//Paging + Slider Function
	rotate = function(){	
		if(pagingSum>1)	{
			triggerID = $active.attr("rel") - 1; //Get number of times to slide
			if(trigerCount<maxHideNumber){
				$(".paging a").eq(trigerCount).hide();
				trigerCount++
			}
			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+imageWidth)
			}, 500 );
		}
	}; 
	//Rotation + Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			idleTime = 0; 
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
				$(".paging a").show()
					trigerCount=-1;
					initRotate();
			}
			rotate(); //Trigger the paging and slider function
		}, $promoIdleTime); //Timer speed in milliseconds
	};
	rotateSwitch(); //Run function on launch
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		directRotate(); //Trigger rotation immediately
		$(".playPause img").attr("src", "/static/media_structure/common/img/pause.gif"); 
			rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
               });
	$(".playPause img").click(function() {	
		if ( $(this).attr("src") === "/static/media_structure/common/img/pause.gif") {
			$(this).attr("src", "/static/media_structure/common/img/play.gif"); 
			clearInterval(play);
            }
		else{
			 $(this).attr("src", "/static/media_structure/common/img/pause.gif"); 
			rotateSwitch(); // Resume rotation
		}
		return false; //Prevent browser jump to link anchor
	}); 
      }    
)
