$(document).ready(function(){

/* Start of Slider */
	/* Activate first Pic and item */
	$("#slider .promoPic a").hide();
	$("#slider .promoPic a:first").show().addClass("active");
	$("#slider .itemList li:first").addClass("active");

	/* Get size of the item, how many item there are, then determin the size of the item reel. */
	var itemHeight = $("#slider .itemList li").height();
	var itemSum = $("#slider .itemList li").size();
	var last3ItemSum = itemHeight * (itemSum -3);
	var itemReelHeight = itemHeight * itemSum;

	/* Adjust the item reel to its new size */
	$("#slider .visibleArea").css({'height' : itemReelHeight});

	/* Slider Function */
	rotate = function(){
		var triggerID = $active.find("a").attr("rel") - 1; /* Get number of times to slide */
		var image_reelPosition = triggerID * itemHeight; /* Determines the distance the image reel needs to slide */

		$("#slider .itemList li").removeClass('active'); /* Remove all active class */
		$active.addClass('active'); /* Add active class (the $active is declared in the rotateSwitch function) */

		/* Slider Animation */
		if ( itemSum - triggerID > 2){
			$("#slider .visibleArea").animate({
				top: -image_reelPosition
			}, 500 );
		}
		if ( itemSum - triggerID < 3){	
		}

		/* Rotate Big Pic */
		var PicNum = $("#slider .itemList li.active a").attr("rel");
		$("#slider .promoPic a").fadeOut('300').removeClass('active');
		$('#slider .promoPic a[rel="' + PicNum + '"]').fadeIn('500').addClass('active');
	};

	/* Rotation and Timing Event */
	rotateSwitch = function(){
		play = setInterval(function(){ /* Set timer - this will repeat itself every 7 seconds			 */
			$active = $('#slider .itemList li.active').next(); /* Move to the next paging */

			if ( $active.length === 0) { /* If paging reaches the end... */
			
				$active = $('#slider .itemList li:first'); /* go back to first */
				
			}
			rotate(); /* Trigger the paging and slider function */

		}, 5000); /* Timer speed in milliseconds (1000 = 1 seconds) */
	};

	rotateSwitch(); /* Run function on launch */

	/* On Hover */
	$("#slider").hover(function() {
		clearInterval(play); /* Stop the rotation */
	}, function() {
		rotateSwitch(); /* Resume rotation timer */
	});	

	/* On Click Item */
	$("#slider .item").click(function() {
		$active = $(this); /* Activate the clicked paging */
		/* Reset Timer */
		clearInterval(play); /* Stop the rotation */
		rotate(); /* Trigger rotation immediately */
		return false; /* Prevent browser jump to link anchor */
	});

	/* On Click Next Button */
	$("#slider .next").click(function() {
		$active = $('#slider .itemList li.active').next(); /* Move to the next paging */
		/* Reset Timer */
		clearInterval(play); /* Stop the rotation */
		if ( $active.length === 0) { /* If paging reaches the end... */
			
				$active = $('#slider .itemList li:first'); /* go back to first */
				
			}
		rotate(); /* Trigger rotation immediately */
		return false; /* Prevent browser jump to link anchor */
	});

	/* On Click Pre Button */
	$("#slider .pre").click(function() {
		$active = $('#slider .itemList li.active').prev(); /* Move to the next paging */
		/* Reset Timer */
		clearInterval(play); /* Stop the rotation */
		if ( $active.length === 0) { /* If paging reaches the first item... */
			
				$active = $('#slider .itemList li:last'); /* go back to last */
				
			}
		rotate(); /* Trigger rotation immediately */
		return false; /* Prevent browser jump to link anchor */
	});
/* End of Slider */


/* Start of Menu hover effect - fix for IE6 doesn't support li:hover */
	$("#menu > ul > li").hoverIntent({
		sensitivity: 1,
		interval: 100,
		timeout: 0,
		over: showMenu,
		out: hideMenu
	});
	function showMenu() {
		$(this).addClass("hover");
		$(this).children("a").addClass("subTrigger");
	}
 
	function hideMenu() {
		$(this).removeClass("hover");
		$(this).children("a").removeClass("subTrigger");
	}

	$("#menu > ul > li").click(function(){	
		$(this).toggleClass("hover");
		$(this).children("a").toggleClass("subTrigger");
	});
/* End of Menu hover effect */
});
