if(typeof jQuery != undefined) {
	j = jQuery;
	j.fn.premium = function(options) {
		var options = j.extend({
			'opacity' : 0.5,					// opacité des boutons inactif
			'duration' : 'slow',				// Durée de la transition
			'delay' : 7,						// Durée d'attente entre deux élèments. 0 pour aucun changement automatique
			'itemsSelector' : '.items .item',	// Sélecteur de la liste de boutons
			'viewsSelector' : '.views .view',	// Sélecteur de la liste des contenus
			'prevSelector' : false,				// Sélecteur du bouton pour passer au précédent
			'nextSelector' : false,				// Selecteur du bouton pour passer au suivant
			'selectedClass' : 'active',			// La classe lorsque l'élèment est actif
			'firstIndex' : 0,					// L'index de départ
			'maxLoop' : 0, 						// Le nombre de tours automatique. 0 pour infini
			'reloadContent' : true,				// Recharge le contenu après un deuxième passage
			'loadFirstContent' : true,			// Charge le contenu du premier élèment affiché
			'getContent' : function() {			// Fonction permettant de récupérer le contenu
				return false;
			},
			'onChange' : function() {			// Fonction appelé après changement de contenu
				return false;
			}
		}, options);

		var currentIndex = -1;
		var timer = null;
		var jitems = this.find(options.itemsSelector);
		var jviews = this.find(options.viewsSelector);
		var currentLoop = -1;

		// Gestion des erreurs
		// -----------------------------------------------------------
		if(!jitems.length) {
			throw 'j.fn.premium : Le sélecteur "'+this.selector+' '+options.itemsSelector+'" ne retourne aucun résultat';
		}

		if(!jviews.length) {
			throw 'j.fn.premium : Le sélecteur "'+this.selector+' '+options.viewsSelector+'" ne retourne aucun résultat';
		}

		if(jviews.length != jitems.length) {
			throw 'j.fn.premium : Le sélecteur "'+this.selector+' '+options.itemsSelector+'" et le selecteur "'+this.selector+' '+options.viewsSelector+'" ne retournent pas le même nombre de résultat';
		}

		/**
		 * Sélectionne l'élement
		 * @param int index Element à sélectionner
		 */
		var select = function(index, loadContent, animate) {
			if(typeof loadContent == 'undefined') {
				loadContent = true;
			}

			if(typeof animate == 'undefined') {
				animate = true;
			}

			var duration = (animate ? options.duration : 0);
			var previousIndex = currentIndex;

			// Contrôle des dépassements
			index = (index < 0 ? jviews.length-1 : index);
			index = (index >= jviews.length ? 0 : index);

			if(index == options.firstIndex) {
				currentLoop++;
			}

			var jitem = jitems.eq(index);
			var jview = jviews.eq(index);

			// On passe au suivant si le bouton n'est pas visible
			if(!jitem.filter(':visible').length) {
				select(index+1);
				return;
			}

			// Chargement du contenu si pas déjà chargé et si getcontent défini
			if(loadContent) {
				if(!jitems[index].alsoLoaded || options.reloadContent) {
					var content = options.getContent.call(this, jitem);

					if(content) {
						jview.html(content);
					}
				}
			}

			jitems[index].alsoLoaded = true;

			currentIndex = index;

			// SI PRÉCÉDENT
			if(previousIndex >= 0) {
				var jpreviousItem = jitems.eq(previousIndex);
				var jpreviousView = jviews.eq(previousIndex);

				turnOffItem(previousIndex, animate);
				jpreviousItem.removeClass(options.selectedClass);

				if(duration) {
					jpreviousView.stop().fadeTo(duration, 0, function() {
						jpreviousView.hide();
					});
				} else {
					jpreviousView.hide();
				}
			}

			// AFFICHAGE DU SUIVANT
			turnOnItem(index, animate);
			jitem.addClass(options.selectedClass);

			if(duration) {
				jview.stop().fadeTo(duration, 1, function() {
					options.onChange.call(this);
				}).show();
			} else {
				jview.show();
				options.onChange.call(this);
			}

			// SI DELAY, ACTIVATION DU TIMER
			if(options.delay > 0 && (!options.maxLoop || currentLoop < options.maxLoop)) {
				clearTimeout(timer);

				timer = setTimeout(function() {
					select(index+1);
				},  options.delay*1000);
			}
		};

		/**
		 * Allume l'élément de la liste
		 * @param int index
		 */
		var turnOnItem = function(index, animate) {
			if(typeof animate == 'undefined') {
				animate = true;
			}

			var duration = (animate ? options.duration : 0);

			if(options.opacity > 0 || options.opacity >= 1) {
				jitems.eq(index).stop().fadeTo(duration, 1);
			}
		};

		/**
		 * Eteint l'élément de la liste
		 * @param int index
		 */
		var turnOffItem = function(index, animate) {
			if(!options.opacity || options.opacity >= 1 || index == currentIndex) {
				return;
			}

			if(typeof animate == 'undefined') {
				animate = true;
			}

			var duration = (animate ? options.duration : 0);

			jitems.eq(index).stop().fadeTo(duration, options.opacity);
		};

		// Si items, ajout d'évènement de click et initialisation
		jitems.each(function(i) {
			j(this)
				.click(function() {
					select(i);
					return false;
				})
				.hover(function() {
					turnOnItem(i);
				}, function() {
					turnOffItem(i);
				});
		});

		// Evenement sur le bouton précédent
		if(options.prevSelector) {
			j(options.prevSelector).click(function() {
				select(currentIndex-1);
				return false;
			});
		}

		// Evenement sur le bouton suivant
		if(options.nextSelector) {
			j(options.nextSelector).click(function() {
				select(currentIndex+1);
				return false;
			});
		}

		if(options.opacity > 0) {
			jitems.fadeTo(0, options.opacity);
		}

		jviews.hide();
		
		select(options.firstIndex, options.loadFirstContent, false);
	};
}

