/**
 * wSlide 0.1 - http://www.webinventif.fr/wslide-plugin/
 * 
 * Rendez vos sites glissant !
 *
 * Copyright (c) 2008 Julien Chauvin (webinventif.fr)
 * Licensed under the Creative Commons License:
 * http://creativecommons.org/licenses/by/3.0/
 *
 * Date: 2008-01-27
 */
(function($){
    $.fn.wslide = function(h){
        h = jQuery.extend({
            width: 150,
			height: 150,
			duration: 1500
        }, h);
        function gogogo(g){
            g.each(function(i){
				$(this).attr('pos', 1).css("min-height", h.height);
				var uniqid = $(this).attr('id');
				var manip = '';
				var nb_elem = 0;
				$(this).children().each( function(i) {
					$(this).attr('id', uniqid+'-'+(i+1));
					if(i > 0) $(this).css( { display:'none', visibility:'visible' } ); 
					manip += ' <a href="#'+uniqid+'-'+Number(i+1)+'">'+Number(i+1)+'</a>';
					nb_elem++;
				} );				
				$(this).attr("nb_elem", nb_elem);
				$(this).after('<div class="wslide-menu" id="'+uniqid+'-menu">'+manip+'</div>');	
				$("a[href=#maCollection-1]").addClass("wactive");
				// Initialisation des fleches
				var jq_prev = $('#'+uniqid+'_prev');
				var jq_next = $('#'+uniqid+'_next');
				if( nb_elem > 1)
					jq_next.show();
				// Clique sur un numero de page
				$('a[href*="'+uniqid+'"]').click(function () { 
					$('a[href*="'+uniqid+'"]').removeClass("wactive");
					$(this).addClass("wactive");
					var lien = $(this).attr('href');
					var pos = $("#"+uniqid).attr('pos');
					$("#"+uniqid).animate({ opacity: 0 }, h.duration/2, 'swing', function() {
						$("#"+uniqid+"-"+pos).hide();
						$(lien).show();
						$(this).animate({ opacity: 1 }, h.duration/2, 'swing');
					} );
					var newpos = lien.split("-");
					$("#"+uniqid).attr('pos', newpos[1]);
					if(newpos[1] == 1) jq_prev.hide(); else jq_prev.show();
					if(newpos[1] == $("#"+uniqid).attr('nb_elem')) jq_next.hide(); else jq_next.show();
					return false;
				});
				$('a[href$="'+uniqid+'_1"]').addClass("wactive");							
				
				// Clique sur la fleche precedent
				jq_prev.click(function () {
					var posToGo = Number($("#"+uniqid).attr("pos"))-1;
					if(posToGo == 1) jq_prev.hide();
					if($("#"+uniqid+"-"+(posToGo+1)).children().length != 0) jq_next.show();						
					$("#"+uniqid).animate({ opacity: 0 }, h.duration/2, 'swing', function() {
						$("#"+uniqid+"-"+(posToGo+1)).hide();
						$("#"+uniqid+"-"+posToGo).show();
						$(this).animate({ opacity: 1 }, h.duration/2, 'swing');
					} );				
					$("#"+uniqid).attr("pos", posToGo);
					return false;
				});
				// Clique sur la fleche suivant
				jq_next.click(function () {
					var posToGo = Number($("#"+uniqid).attr("pos"))+1;
					if( posToGo == $("#"+uniqid).attr("nb_elem") ) jq_next.hide();
					jq_prev.show();					
					$("#"+uniqid).animate({ opacity: 0 }, h.duration/2, 'swing', function() {
						$("#"+uniqid+"-"+(posToGo-1)).hide();
						$("#"+uniqid+"-"+posToGo).show();
						$(this).animate({ opacity: 1 }, h.duration/2, 'swing');
					} );
					$("#"+uniqid).attr("pos", posToGo);
					return false;
				});				
            })
        }
        gogogo(this);
		return this;
    }
})(jQuery);

