/**
 * Le gestionnaire de slideshow tout simple
 */
createPackage("com.lahautesociete.www.views");
com.lahautesociete.www.views.ClassicSlideshow = function (pSelector, pOptions)
{
	// ------- PRIVATE PROPERTIES --------
	// Redéclarer cet élément
	var that = this;
	
	// Cibler le sélécteur
	var element 			= $(pSelector);
	
	// Le container
	var slidesContainer;

	// Cibler les slides
	var slides 				= element.find(".slide");
	
	// Les dimensions
	var sizes;

	// L'index en cours
	var currentIndex		= 0;

	// L'interval
	var interval 			= -1;

	// Les options
	var options = $.extend({
		// Démarrage automatique du slideshow à l'initialisation
		autoStart: true,

		// Première slide à afficher (0 pour aucune)
		index: 1,
		
		// Si le slideshow est vertical
		vertical: false,
		
		// Le selecteur du container
		containerSelector: ".slidesContainer",

		// Durée d'une slide (ms)
		duration: 4000,

		// Durée de l'animation (ms)
		transitionDuration: 600,
		
		// Le easing
		easing: 'easeInOutCubic'
	}, pOptions);

	// ------- PUBLIC PROPERTIES --------
	// Le handler externe de changement
	this.onChange;
	
	
	// ---------- CONSTRUCTOR -----------
	function init ()
	{
		// L'index de départ
		currentIndex = options.index;
		
		// Cibler le selecteur
		slidesContainer = element.find(options.containerSelector);
		
		// Récupérer les dimensions du slideshow
		sizes = [element.width(), element.height];
		
		// Placer le slideshow en relatif pour le container en absolu
		element.css({
			position: "relative"
		});
		
		// Appliquer a toutes les slides
		slides.css({
			width: sizes[0],
			height: sizes[1]
		});
		
		// Initialiser le container
		if (options.vertical)
		{
			// Taille du container
			slidesContainer.css({
				position: "absolute",
				width: sizes[0],
				height: 50000
			});
		}
		else
		{
			// Taille du container
			slidesContainer.css({
				position: "absolute",
				width: 50000,
				height: sizes[1]
			});
			
			// Forcer le float sur les slides
			slides.css({
				float: "left"
			});
		}
		
		// Démarrer automatiquement
		// Et si on a des slides
		if (options.autoStart && slides.length > 1)
		{
			// Démarrer
			that.start();
		}
		
		// Si on a un index
		if (currentIndex != 0)
		{
			// Séléctionner l'index
			that.select(currentIndex);
		}
	}

	// -------- PRIVATE METHODS ---------
	// Dispatcher le changement
	function dispatchChange ()
	{
		if (that.onChange != null)
			that.onChange(currentIndex);
	}

	// -------- PUBLIC METHODS ---------
	// Démarrer le slideshow
	this.start = function ()
	{
		//log("ClassicSlideshow.start");

		// Arrêter
		that.stop();

		// Démarrer
		interval = setInterval(that.next, options.duration);
	};
	
	// Arrêter le slideshow
	this.stop = function ()
	{
		//log("ClassicSlideshow.stop");

		// Vérifier si c'est démarré
		if (interval != -1)
		{
			// On arrête
			clearInterval(interval);
			interval = -1;
		}
	};
	
	// Redémarrer le slideshow
	this.restart = function ()
	{
		//log("ClassicSlideshow.restart");

		// Vérifier si c'est démarré
		if (interval != -1)
		{
			that.start();
		}
	};

	// Passer à la slide précédente
	this.prev = function ()
	{
		//log("ClassicSlideshow.prev");
		
		that.select((currentIndex <= 1 ? slides.length : currentIndex) - 1, dispatchChange);
	};

	// Passer à la slide suivante
	this.next = function ()
	{
		//log("ClassicSlideshow.next");
		
		that.select(currentIndex >= slides.length ? 1 : currentIndex + 1, dispatchChange);
	};
	
	// Séléctionner une slide en particulier
	this.select = function (pIndex, pThen)
	{
		//log("ClassicSlideshow.select", pIndex);
		
		// Séléctionner la slide suivante
		// Limiter au maximum et au minimum
		currentIndex = Math.max(0, Math.min(pIndex, slides.length));
		
		// Animer le container
		if (options.vertical)
		{
			// Slideshow vertical
			slidesContainer.stop().animate({
				top: - (currentIndex - 1) * sizes[1]
			}, {
				duration: options.transitionDuration,
				easing: options.easing
			});
		}
		else
		{
			// Slidewhow horizontal
			slidesContainer.stop().animate({
				left: - (currentIndex - 1)* sizes[0]
			}, {
				duration: options.transitionDuration,
				easing: options.easing
			});
		}
		
		// La suite
		if (pThen != null)
			pThen();
	};
	
	// Initialiser
	init();
};
