newsScrollOptions = {'enabled':true,'initdelay':0,'speed':2,'mousepause':true,'direction':'up','newspause':true,'pausetime':1000,'heightnews':300};

NewsScroll = function(target, options)
{
    // ----------------------------------- STATIC MEMBERS ----------------------
    NewsScroll.defaultEnabled    = true;
    NewsScroll.defaultInitDelay  = 2000;
    NewsScroll.defaultSpeed      = 2;
    NewsScroll.defaultMousePause = true;
    NewsScroll.defaultDirection  = 1;
    NewsScroll.defaultNewsPause  = false;
    NewsScroll.defaultPauseTime  = 2000;
    NewsScroll.defaultHeightNews = 200;
    NewsScroll.defaultName = "newsScroll";
    
    NewsScroll.spanTime = 35;
    
    var defopts = {
        "enabled"    : NewsScroll.defaultEnabled,
        "name"       : NewsScroll.defaultName,
        "initdelay"  : NewsScroll.defaultInitDelay,
        "speed"      : NewsScroll.defaultSpeed,  
        "mousepause" : NewsScroll.defaultMousePause,
        "direction"  : NewsScroll.defaultDirection,	
        "newspause"  : NewsScroll.defaultNewsPause,
        "pausetime"  : NewsScroll.defaultPauseTime,
        "heightnews" : NewsScroll.defaultHeightNews
    };
    
    if(!options){ options = defopts; };
    
    // ----------------------------------- CLASS MEMBERS ----------------------

    // Specifica se abilitare lo scrolling o meno
    this.enabled = ('enabled' in options) ? ((options.enabled == false) ? false : true) : NewsScroll.defaultEnabled;

    // Specifica il ritardo iniziale in millisecondi prima che inizi lo scrolling
    this.initDelay = ('initdelay' in options) ? parseInt(options.initdelay) : NewsScroll.defaultInitDelay; 
    
    // Specifica la velocità di scrolling (0-5)(più il numero è grande, maggiore è la velocità, 0=fermo)
    this.speed = (('speed' in options) ? parseInt(options.speed) : NewsScroll.defaultSpeed);
    
    // Indica se mettere in pausa lo scrolling quando il mouse è sopra le news (0=no 1=si)
    this.mousePause = ('mousepause' in options) ? 
        ((options.mousepause == '0' || options.mousepause == 'false') ? false : true) : NewsScroll.defaultMousePause; 
    
    // Specifica la direzione di scrolling (0='down' 1='up')
    this.direction = ('direction' in options) ? 
        ((options.direction == 'up' || options.direction == '1') ? 
            1 : ((options.direction == 'down' || options.direction == '-1') ? -1 : NewsScroll.defaultDirection) ) : 
                NewsScroll.defaultDirection;
    
    // Specifica se mettere in pausa lo scrolling quando si visualizza ogni news
    this.newsPause = ('newspause' in options) ? ((options.newspause == false) ? false : true) : NewsScroll.defaultNewsPause;  
    
    // Se newsPause è vero specifica il tempo di pausa in millisecondi dello scrolling per ogni news
    this.pauseTime = ('pausetime' in options) ? parseInt(options.pausetime) : NewsScroll.defaultPauseTime; 
    
    // Specifica l'altezza in pixel di ogni news
    this.heightNews = ('heightnews' in options) ? parseInt(options.heightnews) : NewsScroll.defaultHeightNews;
    
    // Specifica il nome dell'istanza dell'oggetto NewsScroll
    this.name = ('name' in options) ? options.name  : NewsScroll.defaultName;
    
    this.arrNews   = Array();   // array con i riferimenti delle news
    this.target    = target;    // ID elemento che contiene le news
    this.objTarget = null;      // Elemento contenente le news
    this.orSpeed = this.speed;  // copia di speed
    
    this.startPos = 0;          // posizione di partenza del container news
    this.countShowNews = 1;     // contatore nelle news visualizzate
    this.maxShowNews = 1;       // numero massimo di news da visualizzare
    this.center = true;         // indica se la news è in centro nel container
    this.pause = false;         // indica se lo scrolling è in pausa
    
    this.strInterval = 
        'setInterval("'+this.name+(this.direction > 0 ? ".scrollUpNews()" : ".scrollDownNews()")+'", '+NewsScroll.spanTime+')';
    
    if(this.enabled) 
    { 
        this.init();
        this.initializeScroll();    
    }
    else 
    {
        this.objTarget = NewsScroll.$("marqueecontainer");
        this.objTarget.style.overflowX = "hidden";
        this.objTarget.style.overflowY = "scroll";
    }
}
// ############################################################

// Recupera un elemento nel dom della pagina con ID specificato
NewsScroll.$ = function(id)
{
    var el; 
    if(document.getElementById) el = document.getElementById(id); 
    else el = document.all[id];
    return el;
}
// ############################################################

// Inizializza oggetto
NewsScroll.prototype.init = function()
{
    this.objTarget = NewsScroll.$("marqueecontainer");
    this.objTarget.style.overflow = "hidden";
    
    // recupero container news
    this.objTarget = NewsScroll.$(this.target);
	if(this.objTarget == null){ return; }
	if(this.mousePause){ this.setMouseMoveAction(this.objTarget); }
	
	// Recupero le news
	var news = this.objTarget.getElementsByTagName('div');
	if(news != null) 
	{
	    for(i = 0; i < news.length; i++) { 
	        if(news[i].attributes['class'] && news[i].attributes['class'].value == 's-news') {
	            if(news[i].innerHTML != ""){ this.arrNews.push(news[i]); }
	        }
	    }		
	}
	
	// rimuovo tutti i figli dal container
	while(this.objTarget.hasChildNodes()) { 
	    this.objTarget.removeChild(this.objTarget.firstChild); 
	}

	for(i = 0; i < this.arrNews.length; i++) { 
	     this.objTarget.appendChild(this.arrNews[i]); 
	}

	// duplico la prima news per avere un riavvolgimento senza tagli
	if(this.arrNews.length > 0) 
	{
	    var el = null;
	    if(this.arrNews[0].cloneNode) {
	        el = this.arrNews[0].cloneNode(true)
	    }

	    this.objTarget.appendChild(el);
	    this.arrNews.push(el);
	}		

	// Imposto alcuni parametri delle news
	for(i = 0; i < this.arrNews.length; i++) 
	{
	    this.arrNews[i].style.height = (this.heightNews) + 'px';
	    //this.arrNews[i].style.overflow = 'hidden';
	    //this.arrNews[i].style.borderTop = "1px solid black";
	}

	// imposto proprietà contenitore news
	this.objTarget.style.position = 'relative';
	this.objTarget.style.height = (this.heightNews * this.objTarget.childNodes.length) + 'px';
	//this.objTarget.style.border = "1px solid red";
	
	// imposto alcuni parametri in base alla direzione dello scrolling
	if(this.direction > 0) 
	{ 
	    this.startPos = 0;
	    this.maxShowNews = (this.arrNews.length - 1);
	}
	else 
	{ 
	    this.startPos = -(this.heightNews * (this.arrNews.length - 1));
	    this.maxShowNews = (this.arrNews.length);
	}

    // posizione iniziale container news
	this.objTarget.style.top = this.startPos + 'px';
}
// ############################################################

// Inizializza lo scrolling.
// Avvio lo scrolling solamente se ci sono almeno 2 news.
NewsScroll.prototype.initializeScroll = function()
{
    if(this.objTarget == null){ return; }
    if(this.arrNews.length > 1) { setTimeout(this.strInterval, this.initDelay); }
}
// ############################################################ 

// Scrolla le news verso l'alto
NewsScroll.prototype.scrollUpNews = function()
{
    if(this.pause) return;    

    var top = parseInt(this.objTarget.style.top);
    var limit = (this.heightNews * this.countShowNews); 

    // scrollo la posizione della news verso l'alto
    if(top > -(limit)) { 
        this.objTarget.style.top = top - (this.speed) + 'px'; //this.center = false;
    }
    // finecorsa della news
    else { this.centerNews(); }
}
// ############################################################ 

// Scrolla le news verso il basso
NewsScroll.prototype.scrollDownNews = function()
{   
    if(this.pause) return;    

    var top = parseInt(this.objTarget.style.top);
    var limit = -(this.heightNews * (this.arrNews.length - this.countShowNews)); 

    // scrollo la posizione della news verso il basso
    if(top < (limit)) { 
        this.objTarget.style.top = top + (this.speed) + 'px'; //this.center = false;
    }
    // finecorsa della news
    else { this.centerNews(); }
}
// ############################################################ 

 // Mette in pausa lo scrolling
NewsScroll.prototype.pauseScroll = function()
{
    this.pause = true;
    setTimeout(this.name+'.startScroll()', this.pauseTime);
}
// ############################################################ 

 // Avvia lo scrolling
NewsScroll.prototype.startScroll = function()
{
    this.pause = false;       
}
// ############################################################
    
// News centrata nel container
NewsScroll.prototype.centerNews = function()
{
    this.center = true;
    this.speed = 0;
    
    // metto in pausa lo scrolling se abilitato
    if(this.newsPause && !this.pause) 
    {
        if(this.direction > 0) { var centerPos = -(this.heightNews * this.countShowNews); }
        else { var centerPos = -(this.heightNews * (this.arrNews.length - this.countShowNews)); }

        this.objTarget.style.top = centerPos + 'px';
        this.pauseScroll();
    }     

    // ho visualizzato tutte le news, resetto posizione iniziale container news e contatore news
    if(this.countShowNews >= this.maxShowNews) 
    {
        //alert("Shown all news, top: "+this.objTarget.style.top+", startPosition: "+this.startPos);
        this.objTarget.style.top = this.startPos + 'px';
        this.countShowNews = 1;
    }
    // non ho ancora visualizzato tutte le news, proseguo
    else 
    {           
        this.countShowNews++;
    }

    this.speed = this.orSpeed;
    this.center = false;
}
// ############################################################ 
    
// Setta gli eventi mouseover e mouseout per mettere in pausa le news
NewsScroll.prototype.setMouseMoveAction = function(el)
{
    if(typeof el == 'string') { el = this.$(el); }
	if(el == null){ return; }
	var self = this;
	el.onmouseover = function() { self.speed = (self.mousePause ? 0 : self.speed); }
	el.onmouseout  = function() { self.speed = self.orSpeed; }
}
// ############################################################  


