(function($){
   var ACDNotizie = function(elemento, options)
   {
        var contenitore = $(elemento),
            step = 0,
            blocca = false,
            corrente = 0,
            defaults = {
            'quanti':           null,
            'url':              '/js/getultimaviabilita.php',
            'token':            null,
            'tempo':            5000,
            'tempohover':       2000,
            'velocita':         400
        };

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

        var prossimo = function() {
            if ( step < (opzioni.quanti - 1 )) {
                step++;
                setTimeout((function() {
                    prendi();
                }), opzioni.tempo);
            }
            else {
                setTimeout((function() {
                    scambia();
                }), opzioni.tempo);
            }
        }

        var scambia = function() {
            if (blocca == true) {
                setTimeout((function() {
                        scambia();
                }), opzioni.tempohover);
            }
            else {
                var elementi = $(contenitore).children();

                if ( (corrente+1) < elementi.length) {
                    corrente = corrente + 1;
                    ultimo = corrente - 1;
                } else {
                    corrente = 0;
                    ultimo = elementi.length - 1;
                }

                if ( corrente == ultimo) {
                    $(elementi[corrente]).show().fadeIn(opzioni.velocita);
                    prossimo();
                }
                else {
                    $(elementi[ultimo]).fadeOut(opzioni.velocita).hide(0, function(){
                        $(elementi[corrente]).show().fadeIn(opzioni.velocita);
                        prossimo();
                    });
                }
            }
        };

        var prendi = function() {
            $.ajax({
                url: opzioni.url,
                data: {"step":step },
                type: "POST",
                dataType: "json",
                success:function(data) {
                    if (data.error != null) {
                        $(contenitore).append('<li style="display:none;">Errore di comunicazione.</li>');
                    }
                    else {
                        var item = $('<li>'+data.html+'</li>').hide();
                        $(contenitore).append(item);
                    }
                    scambia();
                }
            });
        };

        var parti = function() {
            $(contenitore).mouseover(function() { blocca = true;});
            $(contenitore).mouseout(function() { blocca = false;});
            opzioni.token = $("#"+opzioni.token).val();
            prendi();
        };
        parti();
   };

   $.fn.acdnotizie = function(options)
   {
       return this.each(function()
       {
           var questo = $(this);
           if (questo.data('acdnotizie')) return;
           var acdnotizie = new ACDNotizie(questo, options);
           questo.data('acdnotizie', acdnotizie);
       });
   };
})(jQuery);


