jQuery(document).ready(function($) {

	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	
	$('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#menu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
			
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	
	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 10;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */


        $(".featured-img-wrap-fullwidth").hover(function(){
            $(this).find(".hover-technical").slideDown(600);
        },function(){
            $(this).find(".hover-technical").slideUp(600);
        });


        	//chain
	$("#chain-box").customChain({
		previous: ".arrow3-left",
		next: ".arrow3-right",
		target: ".chain li"
	});
	$("#chain-box-2").customChain({
		previous: ".arrow3-left",
		next: ".arrow3-right",
		target: ".chain li"
	});
	$(".chain-box").mouseover(function(e){
		$target = $(e.target);
		if($target.is(".image img")){
			$target.animate({"opacity":0.6});
		}
	});
	$(".chain-box").mouseout(function(e){
		$target = $(e.target);
		if($target.is(".image img")){
			$target.animate({"opacity":1});
		}
	});

   
});

/***************************************************
		CAROUSEL
***************************************************/

function mycarousel_initCallback(carousel) {
    jQuery('.carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('.carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

jQuery(document).ready(function() {
    jQuery(".carousel").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});

/***************************************************
		IMAGE ZOOM
***************************************************/
$(document).ready(function() {
	$("a.zoom img").mouseover(function(){
		$(this).stop(true,true);
		$(this).fadeTo(300, 0.6);
	});

	$("a.zoom img").mouseout(function(){
		$(this).fadeTo(400, 1.0);
	});

});

/***************************************************
		PRETTYPHOTO
***************************************************/

$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'light_square',autoplay_slideshow: false});
});


//customChain
(function($){
	$.fn.customChain = function(opt){

		var defaults = {
			target: "li",
			previous: ".previous",
			next: ".next",
			speed: 400,
			totalDisplay: 6
		};

		var o = $.extend(defaults, opt);

		return this.each(function(){

			var $this = $(this);
			var $next = $(o.next, $this);
			var $previous = $(o.previous, $this);
			var $target = $(o.target, $this);
			var total  = $target.length;
			var animation = true;
			var current = 0;
			var prev = 0;

			stimulateIn(current);

			$next.click(function(){
				if(animation){
					return false;
				}
				current += o.totalDisplay;
				if(current>=total){
					current = 0;
				}
				showFrom();
				return false;
			});
			$previous.click(function(){
				if(animation){
					return false;
				}
				current -= o.totalDisplay;
				if(current<0){
					var mod = total%o.totalDisplay;
					if(mod>0){
						current = total-(total%o.totalDisplay);
					}else{
						current = total-o.totalDisplay;
					}
				}
				showFrom();
				return false;
			});


			function showFrom(){
				animation = true;
				for(i = prev,j = 0,k = 0; j<o.totalDisplay; i++, j++){
					$target.eq(i).delay(j*120).animate({"top":220}, o.speed, function(){
						k++;
						//alert(total);
						if(k>=o.totalDisplay || i > total){
							stimulateIn();
						};
					});
				};
			};

			function stimulateIn(){
				$target.hide();
				for(i = current,j =0; j<o.totalDisplay; i++, j++){
						$target.eq(i).delay(j*120).animate({"top":0, "opacity":"show"}, o.speed);
				}
				prev = current;
				animation = false;
			}

		});

	}

})(jQuery);
