(function($) {

    $.fn.hermanSlider = function(options){

        var obj = $(this);
	var s = $("li", obj).length;
	var ts = s-1;
	var t = 0;
        var zi = 1000;
        var clickable = true;
	  
        var defaults = {
            prevId: 		'prevBtn',
            prevText: 		'Precedent',
            nextId: 		'nextBtn',
            nextText: 		'Suivant',
            controlsShow:	true,
            controlsBefore:	'',
            controlsAfter:	'',
            controlsFade:	true,
            speed: 		1500,
            pause:		4500
        };

        var options = $.extend(defaults, options);


        if(options.controlsShow){
            var html = options.controlsBefore;
            html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
            html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
            html += options.controlsAfter;
            $(obj).after(html);
        }

        $("a","#"+options.nextId).click(function(){
            animate("next",true);
        });
        $("a","#"+options.prevId).click(function(){
            animate("prev",true);
        });


        function animate(dir,clicked){

            if (clickable) {
                
		clickable = false;
                var ot = t;
                zi++;
                switch(dir){
                    case "next":
                        t = (ot>ts) ? (1) : t+1;
                        break;
                    case "prev":
                        t = (t<=1) ? (s) : t-1;
                        break;
                    default:
                        t = dir;
                        break;
                }

                $(("ul li:nth-child("+ot+")"),obj)
                    .fadeOut(options.speed);
                $(("ul li:nth-child("+t+")"),obj)
                    .css('z-index', zi)
                    .fadeIn(options.speed);

                clickable = true;

                if(clicked) clearTimeout(timeout);
                timeout = setTimeout(function(){
                   animate("next",false);
		},options.pause);
                
            }

        }


        // init
        $("ul li", obj).hide();
        $("ul li:first", obj).show();
        var timeout;
        timeout = setTimeout(function(){
            animate("next");
        },options.pause);

    };

})(jQuery);




