﻿// JavaScript Document
// Qual'ora tale plugin venga riutilizzato su qualche sito, chiedo l'nserimento del mio sito per indicarne la fonte.
// Grazie - Nicola Ferrari - nicolaferrari.name
(function($) {
    $.fn.ScrollNews = function(option) {

        var defaults = {
            size: $(this)[0].style.height + 'px',
            timeout: 5000,
            speed: 'fast',
            direction: 'up',
            onmouseover: 'continue'
        };
        var VariabiliObject = $.extend(defaults, option);
        if (VariabiliObject.speed <= 30)
            VariabiliObject.speed = 30;
        switch (VariabiliObject.speed) {
            case 'fast':
                VariabiliObject.speed = 30;
                break;
            case 'medium':
                VariabiliObject.speed = 50;
                break;
            case 'slow':
                VariabiliObject.speed = 100;
                break;
            default:
                VariabiliObject.speed = 30;
                break;
        }
        var numerofigli = parseInt($(this).children('div').length - parseInt(1));
       // alert(numerofigli);
        if (numerofigli > 0) {
            for (i = 0; i <= numerofigli; i++) {
                $(this).children('div')[i].style.height = VariabiliObject.size;
                $(this).children('div')[i].style.overflow = 'hidden';
                $(this).children('div')[i].style.marginTop = '0px';
            }
            switch (VariabiliObject.direction) {
                case 'up':
                    $(this)[0].style.height = VariabiliObject.size;
                    $(this)[0].style.overflow = 'hidden';
                    break;
                case 'down':
                    $($(this).children('div')[numerofigli]).hide();
                    $(this).prepend($(this).children('div')[numerofigli]);
                    $(this).children('div')[0].style.marginTop = '-' + VariabiliObject.size;
                    $($(this).children('div')[0]).show();
                    $(this)[0].style.height = VariabiliObject.size;
                    $(this)[0].style.overflow = 'hidden';
                    break;
            }
            $(this)[0].VarControllo = 0;
            $(this)[0].array = VariabiliObject;

            if (VariabiliObject.onmouseover == 'stop') {
                $(this)[0].onmouseover = function() {
                    $.fn.ScrollNews.StopScroll({ id: $(this)[0].id });

                }
                $(this)[0].onmouseout = function() {
                    $.fn.ScrollNews.ContinueScroll({ id: $(this)[0].id });
                }
            }
            switch (VariabiliObject.direction) {
                case 'up':
                    $(this)[0].contatore = 0;
                    setTimeout('$("#' + this[0].id + '").ScrollNews.ScrollUp({id : \'' + this[0].id + '\',contatore : ' + $(this)[0].contatore + ',numerofigli :' + numerofigli + '})', VariabiliObject.timeout);
                    break;
                case 'down':
                    $(this)[0].contatore = parseFloat('-' + VariabiliObject.size);
                    setTimeout('$("#' + this[0].id + '").ScrollNews.ScrollDown({id : \'' + this[0].id + '\',contatore : ' + $(this)[0].contatore + ',numerofigli :' + numerofigli + '})', VariabiliObject.timeout);
                    break;
            }
        }
    }
    $.fn.ScrollNews.ScrollUp = function(oggetto) {
        var ScrollOblject = $("#" + oggetto.id);
        var ScrollObljectChild = $(ScrollOblject).children('div')[0];
        var numerofigli = oggetto.numerofigli;
        var VariabiliObject = ScrollOblject[0].array;
        $(ScrollOblject)[0].i = setInterval(function() {
            if ($(ScrollOblject)[0].VarControllo == 0) {
                $(ScrollOblject)[0].contatore = $(ScrollOblject)[0].contatore - 1;
                ScrollObljectChild.style.marginTop = (parseInt($(ScrollOblject)[0].contatore) - parseInt(1)) + 'px';
                if (parseFloat($(ScrollOblject)[0].contatore) == '-' + parseInt(VariabiliObject.size)) {
                    clearInterval($(ScrollOblject)[0].i);
                    $(ScrollObljectChild).hide();
                    $(ScrollObljectChild).remove();
                    $(ScrollOblject).append(ScrollObljectChild);
                    $(ScrollOblject).children('div')[numerofigli].style.marginTop = '0px';
                    $($(ScrollOblject).children('div')[numerofigli]).show();
                    $(ScrollOblject)[0].contatore = 0;
                    setTimeout('$("#' + oggetto.id + '").ScrollNews.ScrollUp({id : \'' + oggetto.id + '\',contatore : ' + $(ScrollOblject)[0].contatore + ',numerofigli :' + numerofigli + '})', VariabiliObject.timeout);
                }
            }
        }, VariabiliObject.speed);
    }
    $.fn.ScrollNews.ScrollDown = function(oggetto) {
        var ScrollOblject = $("#" + oggetto.id);
        var ScrollObljectChild = $(ScrollOblject).children('div')[0];
        var last = $(ScrollOblject).children('div')[oggetto.numerofigli];
        var numerofigli = oggetto.numerofigli;
        var VariabiliObject = ScrollOblject[0].array;
        $(ScrollOblject)[0].i = setInterval(function() {
            if ($(ScrollOblject)[0].VarControllo == 1) {
                //nothing
            }
            else {
                $(ScrollOblject)[0].contatore = $(ScrollOblject)[0].contatore + 1;
                ScrollObljectChild.style.marginTop = (parseInt($(ScrollOblject)[0].contatore) + parseInt(1)) + 'px';
                if (parseFloat($(ScrollOblject)[0].contatore) == parseInt(0)) {
                    clearInterval($(ScrollOblject)[0].i);
                    $($(ScrollOblject).children('div')[numerofigli]).hide();
                    $($(ScrollOblject).children('div')[numerofigli]).remove();
                    $(ScrollOblject).prepend(last);
                    $(ScrollOblject).children('div')[0].style.marginTop = '-' + VariabiliObject.size;
                    $($(ScrollOblject).children('div')[0]).show();
                    $(ScrollOblject)[0].contatore = parseFloat('-' + VariabiliObject.size);
                    setTimeout('$("#' + oggetto.id + '").ScrollNews.ScrollDown({id : \'' + oggetto.id + '\',contatore : ' + $(ScrollOblject)[0].contatore + ',numerofigli :' + numerofigli + '})', VariabiliObject.timeout);
                }
            }
        }, VariabiliObject.speed);
    }
    $.fn.ScrollNews.StopScroll = function(oggetto) {
        $("#" + oggetto.id)[0].VarControllo = 1;
    }
    $.fn.ScrollNews.ContinueScroll = function(oggetto) {
        $("#" + oggetto.id)[0].VarControllo = 0;
    }
})(jQuery); 	