	/*
		newsScrollerDom 1.2
		URL: http://www.web-ma.com/code/scroller

		Copyright (C) 2006 Web-MA Soluzioni Informatiche

		This program is free software; you can redistribute it and/or
		modify it under the terms of the GNU General Public License
		as published by the Free Software Foundation; either version 2
		of the License, or (at your option) any later version.

		This program is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		The GNU license to which this software is referering is available at:
		http://www.gnu.org/licenses/gpl.txt

	  -------------------------------------------------------------------------
	
		Version history

		* 1.0.0	05/12/2006: - initial release;
		
		* 1.1.0	10/12/2006: - add. Added li element to the HTML doc to let file pass the W3C validation; 
									  calling the removeChild method to delete this element before to 
									  start add the new ones solve the problem of the empty element;
		
		* 1.2.0	04/15/2007: - add. A variable now can control if the news should be fixed or scroll;
									- add. Is possible to add images into the news, also in a popup style;

		* 1.2.1	04/17/2007: - fix. Is now possible to leave an empty element of type image inside the xml file;
											To add an image, add this section into the xml file under each element section
											you want to display an image.
											
											<images>
												<imgName>ROOT PATH OF YOUR IMAGES</imgName>
											</images>
											
											You can control the image placeholder throught the variable ImagePlaceHolder.
											You can control the JS function to call when an image placeholder will be clicked
											through the variable JSImageOnClickFunction;
		
		* 1.2.2  31/07/2007: - fix. Inside the stopNews function was wrongly used the a fixed name of the ID
											 instead of the variable;
											 
									  fix. The onclick function didn't open the link supplied in the XML file.
	*/

	/* Variables declaration: you can change the values as you want */
	var startPos = 220;	// initial position. Should be equal to #news.JS's height or greater.
	var speed = 30; // Speed of scroller higher number = slower scroller
	var cssClass = 'JS'; // class to add when JS is available
	var stopMessage = 'Stop scrolling'; // Message to stop scroller
	var stopperID = 'newsStopper'; // ID of the generated paragraph
	var newsID = 'news'; // ID of the news box
	var enableStopper = false;
	var xmlDocFile = "doc/newsScrollerDom.xml";
	var useXml = true;
	var fixedNews = false;
	var JSImageOnClickFunction = "popImage('%PH%', 'Novità');"; // Use %PH% as a placeholder text to which internally the code
																  // will substitute the image name;
	var ImagePlaceHolder = "images/foto.gif";

	// Don't change
	var scrollPos = startPos;
	var endPos = 0; // end position
	var ul;
	var elements;
	var scrollDiv;

	/* Initialise scroller when window loads */
	window.onload=function()
	{
		// check for DOM
		if(!document.getElementById || !document.createTextNode){ return; }
		
		scrollDiv = document.getElementById(newsID);
		if(!scrollDiv){return;}
		
		if (!fixedNews)
			initScroller();
			
		ul = document.getElementById(newsID).getElementsByTagName('ul')[0];

		if (useXml) loadXMLDoc();
		else fillByArray();		
	}

	window.onunload = function()
	{
		/* stop scroller when window is closed */
		if (!fixedNews)
			clearInterval(interval);
	}

	/* Initialise scroller */
	function initScroller()
	{
		scrollDiv.className = cssClass;
		interval = setInterval('scrollNews()', speed);

		scrollDiv.onmouseover=function() {	clearInterval(interval); }
		scrollDiv.onmouseout=function() { interval=setInterval('scrollNews()', speed); }

		if (enableStopper)
		{
			var anchorTag = document.createElement('a');
			var paragraphTag = document.createElement('p');

			paragraphTag.setAttribute('id', stopperID);
			anchorTag.href = '#';
			anchorTag.appendChild(document.createTextNode(stopMessage));
			anchorTag.onclick=stopNews;
			paragraphTag.appendChild(anchorTag);
			scrollDiv.parentNode.insertBefore(paragraphTag, scrollDiv.nextSibling);
		}
	}

	function stopNews()
	{
		clearInterval(interval);
		scrollDiv.className='';
		scrollDiv.parentNode.removeChild(scrollDiv.nextSibling);
		return false;
	}

	function scrollNews()
	{
		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.onload = fillByXml;
		}
		else if (window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function() { if (xmlDoc.readyState == 4) fillByXml() };
	 	}
		else return;

		xmlDoc.load(xmlDocFile);
	}

	function fillByXml()
	{
		var x = xmlDoc.getElementsByTagName('element');
		var tmp = 0;

		ul.removeChild(ul.firstChild);
		var li = document.createElement('li');

		for (i = 0; i < x.length; i++)
		{
			elements = new Array();
			
			for (j = 0; j < x[i].childNodes.length; j++)
			{
				if (x[i].childNodes[j].nodeType != 1) continue;
			
				switch(x[i].childNodes[j].nodeName)
				{
					case 'images': 
						var thisNode = x[i].childNodes[j];
						var imgAry = new Array(0);
						var m = 0;

						for (z = 0; z < thisNode.childNodes.length; z++)
						{
							if (thisNode.childNodes[z].nodeType != 1) continue;
							if (thisNode.childNodes[z].childNodes.length < 1) continue;
							imgAry[m] = thisNode.childNodes[z].firstChild.nodeValue;
							m++;
						}
						
						elements[tmp] = imgAry;
						break;
					
					default : 
						if (x[i].childNodes[j].childNodes.length > 0 && x[i].childNodes[j].firstChild.nodeValue != null)
						{
							re = /\\n/gi;
							elements[tmp] = x[i].childNodes[j].firstChild.nodeValue.replace(re, "<br>");
						}
						else
							elements[tmp] = "";
						
						break;
				}
				tmp++;
			}
			
			tmp = 0;
			createNode(elements[0], elements[1], elements[2], elements[3], ul);
		}

		endPos = (document.getElementById('list').offsetHeight) * -1;
	}
	
	function fillByArray()
	{
		if (elements.length == 0) return;

		for (z = 0; z < elements.length; z++)
		{
			createNode(elements[z], elements[z + 1], elements[z + 2], elements[z + 3], ul);
			z += 3;
		}

		endPos = (document.getElementById('list').offsetHeight) * -1;
	}	

	function createNode(anchorText, anchorLink, descr, images, parentNode)
	{
		var li = document.createElement('li');

		li.appendChild(createAnchor(anchorText, anchorLink, descr));

		if (descr != "")
		{
			li.appendChild(document.createElement('br'));
			li.appendChild(document.createTextNode(descr));
		}

		var isImgArray = isArray(images);

		if (isImgArray)
		{
			li.appendChild(document.createElement('br'));

			for (t = 0; t < images.length; t++)
			{
				li.appendChild(createImage(images[t]));
			}
		}
		else if (images != "" && !isImgArray && images != null)
		{
			li.appendChild(document.createElement('br'));

			var img = images.split(';');
			
			for (t = 0; t < img.length; t++)
				li.appendChild(createImage(img[t]));
		}
		
		parentNode.appendChild(li);
	}
	
	function createImage(imageName)
	{
		var re = /%PH%/gi;
		var img = document.createElement('img');

		img.src = ImagePlaceHolder;
		img.id = imageName;
		img.setAttribute('onmouseover', 'window.status=\'\'; return true;');
		img.setAttribute('style', 'cursor: help; border: 0');
		img.onclick = function() { eval(JSImageOnClickFunction.replace(re, this.id)); }
		return img;
	}
	
	function createAnchor(anchorText, anchorLink, descr)
	{
		if (anchorLink == "") 
			return document.createTextNode(anchorText);

		else
		{
			var anchorTag = document.createElement('a');
			anchorTag.href = "#";
			anchorTag.appendChild(document.createTextNode(anchorText));
			anchorTag.setAttribute('onmouseover', 'window.status=\'' + anchorText + '\'; return true;');
			anchorTag.setAttribute('onmouseout', 'window.status=\'\'; return true;');
			anchorTag.onclick = function() { eval('this.href=\'' + anchorLink + '\';') };
	
			return anchorTag;
		}
	}
	
	function isArray()
	{
		if (typeof arguments[0] == 'object')
		{  
			var test = arguments[0].constructor.toString().match(/array/i);
			return (test != null);  
		}
		return false;
	}	
