// JavaScript Document
$(document).ready(function() {
	
	//Disable left arrow
	$("#left").animate({"opacity": 0.2}, 200);
	
	//Get size of the segments, how many segments there are, then determin the size of the area to avoid line breaks.
	var maskWidth = $(".segment").width();
	var segmentSum = $(".group .segment").size();
	var groupWidth = (maskWidth+30) * segmentSum;
	
	//Counter and number of limit of movements
	var effectCount = 0;
	var effectLimit = segmentSum - 3;	
	
	//Adjust the area to its new size
	$(".group").css({'width' : groupWidth});
	
		
	//Click functions
	$("#left").click(function(){
		if(effectCount>0) {
			$("#right").animate({"opacity": 1}, 200);
			$(".group").animate({"left": "+=310px"}, 500);
			effectCount-=1;
			if(effectCount==0) {
				$("#left").animate({"opacity": 0.2}, 500);
			}
		}
	});
	
	$("#right").click(function(){
		if(effectCount<effectLimit) {
			$("#left").animate({"opacity": 1}, 200);
			$(".group").animate({"left": "-=310px"}, 500); //or "slow"
			effectCount+=1;
			if(effectCount==effectLimit) {
				$("#right").animate({"opacity": 0.2}, 500);
			}
		}
	});
	
	//Auto-slider function
	rotate = function(){
		
		//Slider Animation
		if(effectCount<effectLimit && effectCount>=0) {
			$("#left").animate({"opacity": 1}, 200);
			$(".group").animate({"left": "-=310px"}, 500); //or "slow"
			effectCount+=1;
			if(effectCount==effectLimit) {
				$("#right").animate({"opacity": 0.2}, 500);
			}
		}
		else {
			effectCount=0;
		}
	
	}; 
	
	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every N seconds			
			if(effectCount==effectLimit) { //If paging reaches the end...
				//go back to first
				$("#right").animate({"opacity": 1}, 200);
				$(".group").animate({"left": "0px"}, 500);
				effectCount=-1;
				if(effectCount<=0) {
					$("#left").animate({"opacity": 0.2}, 500);
				}				
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".mask").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});
	
	$(".arrow").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});
	
	
	/*Submenu*/
	/*$(".submenu li ul").hide();*/
	$(".submenu li .expand").not(".contract").nextAll("ul").hide();
	
	$(".submenu li .expand").click(function(){
		if($(this).hasClass("selected")) {
			/*$(".submenu li ul").slideUp();*/
			/*Remove this part to another IF*/
		}
		else if($(this).hasClass("contract")) {
			$(this).nextAll("ul").slideUp(400);
			$(this).toggleClass("contract");
		}
		else {
			/*$("a").not(".selected").children(".submenu li ul").slideUp();*/
			/*$(".submenu li ul").slideUp();*/
			
			/*$("a").not(".selected").next("ul").slideUp(400);
			$("a").not(".selected").removeClass("contract");*/
			$("a").not(".active").nextAll("ul").slideUp(400);
			$("a").not(".active").removeClass("contract");
			
			$(this).nextAll("ul").slideDown(400);
			$(this).toggleClass("contract");
		}
	});
	
	/*Expandable Table, should use JQuery 1.4.4+*/
	$(".expandable tr.expand").nextUntil("tr.expand").not("tr.noexpand").hide();
	$(".expandable tr td:last-child").css({'text-align' : 'center'});
	$(".expandable tr td:first-child").css({'text-align' : 'center'});
	$(".expandable tr.expand td:last-child").addClass("expand-ie");
	$(".expandable tr.expand").click(function(){
		$(this).nextUntil("tr.expand").not("tr.noexpand").fadeToggle(300, "linear");
		$(this).children("td:last-child").toggleClass("contract");
	});

	
	/*Fancybox*/
	/* This is basic - uses default settings */
	
	$("a.zoom").fancybox();
	
	/* Apply fancybox to multiple items */
	
	$("a.zoom").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'changeSpeed'	:	200,
		'overlayShow'	:	true,
		'titlePosition'	:	'inside',
		'overlayColor'	:	'#000',
		'overlayOpacity':	'0.65',		
	});
	
	$("a.zoom[rel]").fancybox({		
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'changeSpeed'	:	200,
		'overlayShow'	:	true,
		'titlePosition'	:	'inside',
		'overlayColor'	:	'#000',
		'overlayOpacity':	'0.65',	
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-inside">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + ': ' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
});
