$(document).ready(function() {
	/* homepage slider parameters $('#slideshow').after('<div id="nav">').cycle({ */						   
	$('#slideshow').cycle({ 
	fx:     'fade', 
	speed:  2000, 
	timeout: 5000, 
	pager:  '#nav' 
	});

	/* entire block clickable for latest news */						   
    
    $("#sidebar li").click(function(){
    	window.location=$(this).find("a").attr("href");
		return false;
	});

	$("#sidebar li").mouseover(function(){
		$(this).addClass('hover');
		$(this).find("a").attr("class", "hover");
	 
    }).mouseout(function(){
	    $(this).removeClass('hover');
		$(this).find("a").attr("class", "");
    });
    
     $("#nav .children li").click(function(){
    	window.location=$(this).find("a").attr("href");
		return false;
	});
    
    $("#nav .children li").mouseover(function(){
		$(this).addClass('hover');
		$(this).find("a").attr("class", "hover");
	 
    }).mouseout(function(){
	    $(this).removeClass('hover');
		$(this).find("a").attr("class", "");
    });
    
    $("#sidenav .navlist > li:last").addClass("nomargin");
    $("#sidenav .navlist li:last").addClass("nomargin");
    $("#sidenav .children li").removeClass("nomargin");
    $(".archivediv:last").addClass("nomargin");
		
});

$(document).ready(function() {
	
	//Set Default State of each portfolio piece
	$("#prodthumbs ul").show();
	
	//first image thumbnail set to .active
	$("#prodthumbs ul li:first a").addClass("active");

	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $("#productimg").width();
	var imageSum = $("#productimg img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Current slide
	var currSlide;
	
	//Adjust the image reel to its new size
	$("#productimg").css({'width' : imageReelWidth});
	
	//Paging + 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

		$("#prodthumbs ul li a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$("#productimg").animate({
			left: -image_reelPosition
		}, 500 );
	};
	
	//Rotation + Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			
			$active = $('#prodthumbs ul li a.active').parent().next().children();
			
			//alert($active.length);
			if ( $active.length === 0) { //If paging reaches the end…
			$active = $('#prodthumbs ul li:first a'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
			
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$("#productimg img").hover(function() {
	clearInterval(play); //Stop the rotation
	}, function() {
	rotateSwitch(); //Resume rotation
	});
	
	//On Click
	$("#prodthumbs ul a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	
});
