/**
 * Le gestionnaire pour les sélécteurs miniatures
 */
com.lahautesociete.www.views.ThumbsSelector = function (pSelector, pOptions)
{
	// ------- PRIVATE PROPERTIES --------
	// Redéclarer cet élément
	var that = this;
	
	// Cibler le sélécteur
	var element = $(pSelector);
	
	// Cibler les boutons
	var buttons = element.find(".thumb");

	// L'index séléctionné
	var currentIndex = 0;
	
	// Les options
	var options = $.extend({
		intro: true,
		index: 0
	}, pOptions);

	// ------- PUBLIC PROPERTIES --------
	// Le handler externe de changement
	this.onChange;
	

	// ---------- CONSTRUCTOR -----------
	function init ()
	{
		// Faire l'animation d'intro
		if (options.intro)
			that.intro();
		
		// Séléctionner l'index par défaut
		if (options.index != 0)
			that.select(options.index);

		// Ecouter les clics
		buttons.click(buttonClickHandler);
	}

	// -------- PRIVATE METHODS ---------
	// Clic sur un bouton
	function buttonClickHandler (event)
	{
		// Calculer l'index
		var computedIndex = buttons.index(this) + 1;
		
		// Si c'est différent
		if (computedIndex != currentIndex)
		{
			// Récupérer l'index et séléctionner
			that.select(computedIndex);
	
			// Dispatcher
			if (that.onChange)
				that.onChange(computedIndex);
		}
	}

	// -------- PUBLIC METHODS ---------
	// Animation d'intro
	this.intro = function (pThen)
	{
		// Afficher les boutons 1 à 1
		buttons.each(function (i, el) {
			$(this).stop().css({
				top: 100
			}).delay((buttons.length - i) * 100 + 200).animate({
				top: 0
			}, {
				duration: 400,
				easing: "easeOutBack",
				complete: (i == buttons.length - 1) ? pThen : null
			});
		});

		// Afficher le container
		element.css("display", "block");
	};
	
	// Séléctionner un index (0 pour déselectionner)
	this.select = function (pIndex)
	{
		// Si l'index change
		if (currentIndex != pIndex)
		{
			// Enregistrer l'index
			currentIndex = pIndex;
			
			// Supprimer toutes les classes selected
			buttons.removeClass("selected");

			// Cibler grâce à l'index et ajouter la classe
			$(buttons.get(pIndex - 1)).addClass("selected");
		}
	};
	
	// Initialiser
	init();
};
