(function($){

    $.fn.extend({
    
        //Passaggio della variabile options alla funzione  
        slideShow: function(options){
			
			$("p:first",this).addClass("active");
			$("img:first",this).addClass("active");
        
        
            //Setta i valori di default  
            var defaults = {
                speed: 2000,
                interval: 2000,
            	delay:0
            }
            //Fonde i valori di default con le opzioni passate      
            var options = $.extend(defaults, options);
			
            var $testi = $('p', $(this));
			
            $testi.wrapAll(document.createElement("DIV"));
            return this.each(function(){
                var o = options;
                var obj = $(this);
                var testi = $('#testi');
            	if (o.delay > 0) {
					setTimeout(function(){
						setInterval(function(){
						slideSwitch(obj, o.speed);
						slideSwitchTxt(obj, o.speed);
					}, eval(o.interval + o.speed));
					}, o.delay);
				}
				else
				{
					setInterval(function(){
						slideSwitch(obj, o.speed);
						slideSwitchTxt(obj, o.speed);
					}, eval(o.interval + o.speed));
					
				}
            });
        }
    });
    
	function slideSwitch(obj, speed){
		var $active = $('IMG.active', obj);
		if ($active.length == 0) {
			$active = $('IMG:last', obj);
		}
			var $next = $active.next().length ? $active.next() : $('IMG:first', obj);
			$active.animate({opacity: 0.0}, speed/2)
			$next.css({
			    opacity: 0.0
			}).addClass('active').animate({
			    opacity: 1.0
			}, speed/2, function(){
			    $active.removeClass('active');
			});
	}
	
	function slideSwitchTxt(obj, speed){
		var $activeTxt = $('div > P.active', obj);	
		if ($activeTxt.length == 0) { 
		    $activeTxt = $('div > P:last', obj);
		}
			var $nextTxt = $activeTxt.next().length ? $activeTxt.next() : $('div > P:first', obj);
		
			$activeTxt.animate({opacity: 0.0}, speed/2);				
			$nextTxt.css({
			    opacity: 0.0
			}).addClass('active').animate({
			    opacity: 1.0
			}, speed/2, function(){
		    	$activeTxt.removeClass('active');
			});
	}
    
})(jQuery);

