	/*
		newsScrollerDom 1.3
		URL: http://code.web-ma.com/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	12/05/2006: - initial release;
		
		* 1.1.0	12/10/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  07/31/2007: - fix. Inside the stopNews function was wrongly used a fixed name of the ID
						instead of the variable;
											 
				     - fix. The onclick function didn't open the link supplied in the XML file.
				       
		* 1.3.0 03/25/2008   - add. Added the possibility to add anchor tag inside the xml document.
						url path should be formed in this way (mandatory):
						[uri=yoururl descr=anchordescription]
	*/

	/* 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 = 20;				// Speed of scroller. At high value corresponds a slow 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 = "newsScrollerDom.xml";
	var useXml = true;
	var fixedNews = false;
	var JSImageOnClickFunction = "alert('%PH%');"; 	// Use %PH% as a placeholder text to which internally the code
							// will substitute the image name.
							// Ex. popImage('%PH%', 'News');
	var ImagePlaceHolder = "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);

		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)
							elements[tmp] = x[i].childNodes[j].firstChild.nodeValue;
						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));

		if (descr != "")
		{
			var pTag = document.createElement('div');
			var brTag = document.createElement('br');
				
			var link = /\[uri=([\d\w\.:/-]*),descr=([a-zA-Z0-9\s]*)\]/gim;
			var brk = /\\n/;
			var addBrk = false;
			var noLink = true;
			var start, lnkStart;
			
			var m = descr.match(link);

			var i = 0;
			
			while (descr != "")
			{
				
				start = descr.indexOf(descr.match(brk));
				
				if (m != null)
					lnkStart = descr.indexOf(m[i]);
				else
					lnkStart = start;
				
				if (start > -1 && start <= lnkStart)
				{
					addBrk = true;
					noLink = true;
				}
				else
				{
					if (m != null && i < m.length)
					{
						start = lnkStart;
						noLink = false;
					}
					else
					{
						start = descr.length;
						noLink = true;
					}
				}
				
				pTag.innerHTML += descr.substring(0, start);
				
				if (addBrk)
				{
					pTag.appendChild(brTag);
					addBrk = false;
					descr = descr.substring(start + 2);
					
					continue;
				}
				else
					descr = descr.substring(start); 
				
				if (!noLink)
				{
					descr = descr.substring(m[i].length);
					
					var tmp = m[i].replace(']', '').replace('[', '').split(',');
					var a = createAnchor(tmp[1].replace('descr=',''), tmp[0].replace('uri=',''));

					pTag.appendChild(a);
				}
				
				i++;
				noLink = false;
			}
			
			li.appendChild(pTag);
		}

		if (images != "" && images != null)
		{
			li.appendChild(document.createElement('br'));
			
			var isImgArray = isArray(images);

			if (isImgArray)
			{
				for (t = 0; t < images.length; t++)
					li.appendChild(createImage(images[t]));
			}
			else
			{
				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)
	{
		if (anchorLink == "") 
			return document.createTextNode(anchorText);

		else
		{
			var anchorTag = document.createElement('a');
			anchorTag.href = anchorLink;
			anchorTag.appendChild(document.createTextNode(anchorText));
			anchorTag.setAttribute('onmouseover', 'window.status=\'' + anchorText + '\'; return true;');
			anchorTag.setAttribute('onmouseout', 'window.status=\'\'; return true;');
			//anchorTag.setAttribute('onclick', 'this.href=\'' + anchorLink + '\';');
			//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;
	}	