/* 
 * Photos Slider v 1.0
 * Designed for prestashop 1.2 and jQuery 1.2.6.
 * Module: PhotosSlider
 * Desc: Slide gallery presenting photos of home page featured featured products..
 * Credits: Féoze [www.feoze.fr]
**/

var $photosSlider;
var $panels;
var maxIndex;
var currentIndex;
var impactTimer;
var zIndexTopRight;
var zIndexTopLeft;
	
jQuery.fn.initPhotosSlider = function () {
	$photosSlider = $(this);
	$photosSlider.data("currentlyMoving", false);
	$panels = $photosSlider.children(".panel");
	
	maxIndex = $panels.size() - 1;
	currentIndex = Math.floor(maxIndex / 2);

	$panels.css({'position':'absolute'})
	$panels.find("a.photo-link").siblings().hide();
	$panels.find("a.photo-link").click(function(event) {
		var index = $panels.index($(this).parent());
		if(index < currentIndex) {
			event.preventDefault();
			$photosSlider.changePanel(false);
		}
		else if (index > currentIndex) {
			event.preventDefault();
			$photosSlider.changePanel(true);
		}
		//else let the link go to the product page
	});

	//Slider init
	var zIndex = 1;
	var i = 0;

	//Reduce previous slides
	while(i < currentIndex) {
		var leftShift = Math.floor(Math.random()*15); 
		var topShift = Math.floor(Math.random()*15); 
		$panels.eq(i).css({marginLeft : -180, paddingTop: topShift, paddingLeft: leftShift, zIndex : zIndex  });
		i++;
		zIndex++;
	}
	zIndexTopLeft = zIndex - 1;
	
	//Grow current slide
	$panels.eq(currentIndex).css({marginLeft : 0, zIndex : zIndex });
	$panels.eq(currentIndex).find("a.photo-link > img").animate({ width: 300, marginTop:-90 });
	$panels.eq(currentIndex).find("a.photo-link").siblings().fadeIn("slow");
	
	zIndex = (maxIndex - i);
	i = currentIndex + 1;

	//Reduce next slides
	zIndexTopRight = zIndex;
	while(i <= maxIndex) {
		var leftShift = Math.floor(Math.random()*15); 
		var topShift = Math.floor(Math.random()*15); 
		$panels.eq(i).css({marginLeft : 320, paddingTop: topShift, paddingLeft: leftShift, zIndex : zIndex });
		i++;
		zIndex--;
	}
	
	//Init Timer
	impactTimer = window.setTimeout(function(){$photosSlider.changePanel(true);}, 7000);
}

//Change the current panel
//Direction == true : panels are moving to the right
//Direction == false :  panels are moving to the left
jQuery.fn.changePanel = function (direction) {
		  
	//If not currently moving
	if (($photosSlider.data("currentlyMoving") == false)) {
		
		//Start moving
		$photosSlider.data("currentlyMoving", true);
		
		//If we reached the end of a direction we go to the opposite direction
		if((direction && (currentIndex == maxIndex )) || (!direction && (currentIndex == 0))) {
			direction = !direction;
		}
		
		//Index of the next panel to show
		var nextIndex = direction ? (currentIndex + 1) : (currentIndex - 1);
		
		//Reduce the current pannel
		var leftShift = Math.floor(Math.random()*15); 
		var topShift = Math.floor(Math.random()*15); 
		$panels.eq(currentIndex).css({zIndex : (direction ? (++zIndexTopLeft) : (++zIndexTopRight))});
		$panels.eq(currentIndex).animate({marginLeft : (direction ? -180 : 320)});
		$panels.eq(currentIndex).find("a.photo-link > img").animate({ width: (160), marginTop: 0, paddingTop: topShift, paddingLeft: leftShift });
		$panels.eq(currentIndex).find("a.photo-link").siblings().fadeOut("slow");
		
		//Grow the next panel
		$panels.eq(nextIndex).find("a.photo-link > img").animate({ width: 300, marginTop:-90 });
		$panels.eq(nextIndex).find("a.photo-link").siblings().fadeIn("slow");
		$panels.eq(nextIndex).animate(
			{ marginLeft : 0}, 
			function() {
				//Update variable and stop moving
				currentIndex = nextIndex;
				$photosSlider.data("currentlyMoving", false);
			}
		);
		
		//Restart timer
		clearTimeout(impactTimer);
		impactTimer = window.setTimeout(function(){$photosSlider.changePanel(direction);}, 7000);
	}
}
