var startPos = 130;										// Posizione iniziale. Uguale o pù grande del contenitore delle news.
var speed = 18;												// Velocità dello scroller, valore più alto per velocità più bassa
var cssClass = 'newsDiv';							// Classe scroller news se js è attivo
var newsID = 'news'; 									// id della box delle news
var xmlDocFile = "/lib/xml/offerte.xml";			// path file xml con le offerte
var scrollPos = startPos;
var endPos = 0;
var ul;
var scrollDiv;
var xmlDoc;
	
/* Inizializzazione scroller */
function initScroller() {
	
	// check DOM
	if(!document.getElementById || !document.createTextNode){ return; }
	
	// Check div
	scrollDiv = document.getElementById(newsID);
	if(!scrollDiv){return;}
	
	ul = document.getElementById(newsID).getElementsByTagName('ul')[0];
			
	// Carica il file
	loadXMLDoc();
		
	scrollDiv.onmouseover = function() { clearInterval(interval); }
	scrollDiv.onmouseout = function() { interval=setInterval('scrollNews()', speed); }
}
/* Fine inizializzazione scroller */

window.onunload = function() {
	clearInterval(interval);
}
function scrollNews()	{	
	
	endPos = (document.getElementById('list').offsetHeight) * -1;
	var n = scrollDiv.getElementsByTagName('ul')[0];
	n.style.top = scrollPos +'px';
	if(scrollPos==endPos) scrollPos=startPos;
	scrollPos--;

}
function loadXMLDoc() 	{
		
	if (document.implementation && document.implementation.createDocument) {
  	xmlDoc = document.implementation.createDocument("","",null)
		xmlDoc.async = true; // False blocca il codice fino alla fine del caricamento
		xmlDoc.onload = generaNews;
	} else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");		
		xmlDoc.onreadystatechange = function() { if (xmlDoc.readyState == 4) generaNews() };	
	} else return;
	
	xmlDoc.load(xmlDocFile);
				
}
function generaNews() {

	var x = xmlDoc.getElementsByTagName('element');
	
	for (i = 0; i < x.length; i++) {
		
		for (j = 0; j < x[i].childNodes.length; j++) {
			
			tmpNodo = x[i].childNodes[j];
			if (tmpNodo.nodeType != 1) continue;
				  
			switch(tmpNodo.nodeName) {
								
				case 'titolo':
									
					if (tmpNodo.childNodes.length > 0 && tmpNodo.firstChild.nodeValue != null) {
						var pTag = document.createElement('div');
    				pTag.className = 'newsTit';	
						pTag.innerHTML += tmpNodo.firstChild.nodeValue;	
					}

					break;

				case 'sottoTit':
									
					if (tmpNodo.childNodes.length > 0 && tmpNodo.firstChild.nodeValue != null) {
						var pTag = document.createElement('div');
    				pTag.className = 'newsSottoTit';	
						pTag.innerHTML += tmpNodo.firstChild.nodeValue;	
					}

					break;
					
				case 'testo':
					
					if (tmpNodo.childNodes.length > 0 && tmpNodo.firstChild.nodeValue != null) {
						var pTag = document.createElement('div');
    				pTag.className = 'newsTesto';	
						pTag.innerHTML += tmpNodo.firstChild.nodeValue;		
					}

					break;
				
				case 'descLink':
					
					tmpTxt = tmpNodo.firstChild.nodeValue;	
					break;
				
				case 'link':
					
					if (tmpNodo.childNodes.length > 0 && tmpNodo.firstChild.nodeValue != null) {
						var pTag = document.createElement('div');
    				pTag.className = 'newsTesto';	
						pTag.innerHTML += '<a href="' + tmpNodo.firstChild.nodeValue + '">' + tmpTxt + '</a>';		
					}
					break;
					
				case 'image':
					
					var pTag = document.createElement('div');
    			pTag.className = 'newsImg';	
					pTag.innerHTML += '<img src="../img/' + tmpNodo.firstChild.nodeValue + '" />';
					break;
			}
	
			ul.appendChild(pTag);		
			
		}
		
	}
	
	scrollDiv.className = cssClass;
	interval = setInterval('scrollNews()', speed);

}

function clip() {
	
	if (id1.style.display != "none") {
		
		posizione = parseInt(id1.style.left)
	
		if (posizione > -(id1.offsetWidth)) {
				
			id1.style.left = posizione - 2 + 'px'
			
			if (posizione < -(id1.offsetWidth - slWidth) && id2.style.display == "none") {

				id2.style.left = slWidth + 'px'
				id2.style.display = 'inline'
			}				
		
		} else {
			
			id1.style.display = 'none'
		}
	}

	if (id2.style.display != "none") {
		
		posizione = parseInt(id2.style.left)
	
		if (posizione > -(id2.offsetWidth)) {
				
			id2.style.left = posizione - 2 + 'px'				

			if (posizione < -(id2.offsetWidth - slWidth) && id1.style.display == "none") {
			
				id1.style.left = slWidth + 'px'
				id1.style.display = 'inline'
			}			
			
		} else {
			
			id2.style.display = 'none'
		}
	}
	
	if (id3.style.display != "none") {
		
		posizione = parseInt(id3.style.left)
	
		if (posizione > -(id3.offsetWidth)) {
				
			id3.style.left = posizione - 2 + 'px'
			
			if (posizione < -(id3.offsetWidth - slWidth) && id4.style.display == "none") {

				id4.style.left = slWidth + 'px'
				id4.style.display = 'inline'
			}				
		
		} else {
			
			id3.style.display = 'none'
		}
	}

	if (id4.style.display != "none") {
		
		posizione = parseInt(id4.style.left)
	
		if (posizione > -(id4.offsetWidth)) {
				
			id4.style.left = posizione - 2 + 'px'				

			if (posizione < -(id4.offsetWidth - slWidth) && id3.style.display == "none") {
			
				id3.style.left = slWidth + 'px'
				id3.style.display = 'inline'
			}			
			
		} else {
			
			id4.style.display = 'none'
		}
	}
	
	if (id5.style.display != "none") {
		
		posizione = parseInt(id5.style.left)
	
		if (posizione > -(id5.offsetWidth)) {
				
			id5.style.left = posizione - 2 + 'px'
			
			if (posizione < -(id5.offsetWidth - slWidth) && id6.style.display == "none") {

				id6.style.left = slWidth + 'px'
				id6.style.display = 'inline'
			}				
		
		} else {
			
			id5.style.display = 'none'
		}
	}

	if (id6.style.display != "none") {
		
		posizione = parseInt(id6.style.left)
	
		if (posizione > -(id6.offsetWidth)) {
				
			id6.style.left = posizione - 2 + 'px'				

			if (posizione < -(id6.offsetWidth - slWidth) && id5.style.display == "none") {
			
				id5.style.left = slWidth + 'px'
				id5.style.display = 'inline'
			}			
			
		} else {
			
			id6.style.display = 'none'
		}
	}
	
}