/**
**	Ogggetto javascript x estrarre news da file XML
**	ciclo lettura su doc xml e salvo elementi da passare a newsticker.js
**/


var MsNewsTicker = {
	news_container: "news_container",
	tickercontainer: "newsticker",
	fileXMLdacaricare : "xml/rotary_news.xml",
	xml_master_tag : "newselenco",
	xml_news_tag : "news",
	mess_obj: null,
	text_trim_len:50, //num caratteri ammissibili su una sola riga, poi trim
	
	openXmlFile: function() {
		var date = new Date();
		
		//load xml file
		try
		{
			if (window.ActiveXObject)
			{
				var errorHappendHere = "Check Browser and security settings";
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async=false;
				xmlDoc.load(this.fileXMLdacaricare);
			}
			else if(window.XMLHttpRequest)
			{
				var errorHappendHere = "Error handling XMLHttpRequest request";
				var d = new XMLHttpRequest();
				d.open("GET", this.fileXMLdacaricare, false);
				d.send(null);
				xmlDoc=d.responseXML;
			} else {
				var errorHappendHere = "Error.";
				xmlDoc = document.implementation.createDocument("","",null);
				xmlDoc.async=false;
				xmlDoc.load(this.fileXMLdacaricare);
			}
		}	
		catch(e)
		{
			alert(errorHappendHere);
		}
	
	
		//Cache "messages" element of xml file
		var tickerobj = xmlDoc.getElementsByTagName(this.xml_news_tag);
		
		/* Se non ci sono news NON MOSTRARE */
		if(tickerobj.length==0)
		{
			return true;
		}

		var xmlobj = Array();
		var msg ='';
		var cnt = 0;
		
		for(i=0;i<tickerobj.length;i++)
		{
			cur_el = tickerobj[i];
			/*  CONTROLLO VALIDITA' DATA DELLA NEWS, se è scaduta tralascio */
			/* es. 2010-05-10 */
			cur_validita = cur_el.getElementsByTagName('validita')[0].firstChild.nodeValue;
			anno 	= parseInt(cur_validita.substring(0,4),10);
			mese 	= parseInt(cur_validita.substring(5,7),10);
			giorno  = parseInt(cur_validita.substring(8,10),10); 
			obj_validita = new Date( anno, mese-1, giorno ); //mese -1 xè ogg Date parte da 0=gennaio
			
			if( obj_validita < date ) {
				//alert('Data NON valida - ' +i+ ' - ' +  cnt);
			} else
			{
				//alert('Data valida - ' +i+ ' - ' +  cnt);
				xmlobj[cnt] = { id:null, titolo:null, testo:null, data:null, validita:null, lnk:null, autore:null };
				xmlobj[cnt].id 			= cur_el.getElementsByTagName('id')[0].firstChild.nodeValue;
				xmlobj[cnt].titolo 		= cur_el.getElementsByTagName('titolo')[0].firstChild.nodeValue;
				xmlobj[cnt].testo			= cur_el.getElementsByTagName('testo')[0].firstChild.nodeValue;
				if(xmlobj[cnt].testo.length > this.text_trim_len)
					xmlobj[cnt].testo = xmlobj[cnt].testo.substring(0,this.text_trim_len) + ' [...]'
				xmlobj[cnt].lnk			= cur_el.getElementsByTagName('link')[0].firstChild.nodeValue;		
				xmlobj[cnt].data 			= cur_el.getElementsByTagName('data')[0].firstChild.nodeValue;		
				xmlobj[cnt].validita 		= cur_validita;		
				xmlobj[cnt].autore		= cur_el.getElementsByTagName('autore')[0].firstChild.nodeValue;
				//alert('openXmlFile:'+i + '\n'+ xmlobj[cnt].id);
				cnt++;
			}
		}

		this.mess_obj = xmlobj;
		//alert(this.mess_obj.length);
		this.printXmlNews();
		return;
	},
	
	printXmlNews : function() {
		var output='<ul>';
		if(this.mess_obj.length==0)
		{
			var output='<li></li>';
			var output='</ul>';
		} else {			
			
			for(i=0;i<this.mess_obj.length;i++) {
				//alert(this.mess_obj.length);
				cur_news = this.mess_obj[i];
				//alert('printXmlNews:' +i+ '\n' +cur_news.id);
				output += '<li id="'+ cur_news.id +'" onclick="location.href=\''+ cur_news.lnk +'\'">';
				output += '<span class="news_titolo"><b>'+ cur_news.titolo +'</b></span><br />';
				output += '<span class="news_testo">'+ cur_news.testo +'</span>';
				output += '</li>';
			}
			output += '</ul>';
			document.getElementById(this.news_container).style.display= 'block';
			document.getElementById(this.tickercontainer).innerHTML = output;
		}
		return;
	}

	
}
