// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

//Relative URL syntax:
var rssbridgeurl="http://www.comune.torino.it/cgi-bin/musicainpiemonte/bridge.cgi";


////////////No need to edit beyond here//////////////

function createAjaxObj()
{
	var httprequest = false;
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
	{ 
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject) // if IE
	{ 
		try
		{
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try
			{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else
	{
		this.init.innerHTML = 'Il vostro browser non supporta questo servizio!';
	}
	return httprequest;
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rssticker_ajax(idBox, idCanale, idItem, delay, maxChar, maxItem)
// -------------------------------------------------------------------

//function rssticker_ajax(idBox, idCanale, idItem, delay, maxChar, maxItem)
function rssticker_ajax(idBox, idItem, delay, maxChar, maxItem)
{
	this.idBox = idBox;
	//this.idCanale = idCanale;
	this.idItem = idItem;
	this.delay = delay;
	this.maxChar = (typeof maxChar != "undefined")? maxChar : "";
	this.maxItem = (typeof maxItem != "undefined")? maxItem : "";
	this.mouseoverBol = 0; //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer = 0;
	this.title = [];
	this.link = [];
	this.description = [];
	this.category = [];
	this.comments = [];
	this.pubdate = [];
	this.count = 0;
	this.init = document.getElementById(this.idItem);
	this.init.innerHTML = 'Recupero notizie in corso...';
	this.ajaxobj = createAjaxObj();
	this.getAjaxcontent();
	this.opacitysetting = 0.2; //Opacity value when reset. Internal use.
	if (window.getComputedStyle) //detect if moz-opacity is defined in external CSS for specified class
		this.mozopacityisdefined = (window.getComputedStyle(document.getElementById(this.idItem), "").getPropertyValue("-moz-opacity") == 1) ? 0 : 1;
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "bridge.php" with the supplied parameters
// -------------------------------------------------------------------

rssticker_ajax.prototype.getAjaxcontent=function()
{
	if (this.ajaxobj)
	{
		var instanceOfTicker = this;
		if(this.maxItem)
			var parameters = "maxItem="+this.maxItem;
		this.ajaxobj.onreadystatechange = function() { instanceOfTicker.initialize(); }
		//this.ajaxobj.open('GET', rssbridgeurl, true);
		this.ajaxobj.open('GET', rssbridgeurl+"?"+parameters, true);
		this.ajaxobj.send(null);
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssticker_ajax.prototype.initialize = function()
{ 
	if (this.ajaxobj.readyState == 4) //if request of file completed
	{ 
		if (this.ajaxobj.status == 200) //if request was successful
		{
			var xmldata = this.ajaxobj.responseXML;
			if(xmldata.getElementsByTagName("item").length == 0) //if no <item> elements found in returned content
			{
				document.getElementById(this.idItem).innerHTML = "<strong>Errore!</strong> Nessuna notizia da visualizzare!";
				return;
			}
			var instanceOfTicker = this;
			this.feeditems = xmldata.getElementsByTagName("item");
			//Cycle through RSS XML object and store each peice of an item inside a corresponding array
			for (var i = 0; i < this.feeditems.length; i++)
			{
				if(this.feeditems[i].getElementsByTagName("category")[0].firstChild)
					this.category[i] = this.feeditems[i].getElementsByTagName("category")[0].firstChild.nodeValue;
				if(!this.category[i])
					this.category[i] = "Cittą di Torino";

				if(this.feeditems[i].getElementsByTagName("comments")[0].firstChild)
					this.comments[i] = this.feeditems[i].getElementsByTagName("comments")[0].firstChild.nodeValue;					
				if(!this.comments[i])
					this.comments[i] = "#";

				if(this.feeditems[i].getElementsByTagName("title")[0].firstChild)
					this.title[i] = this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
				if(!this.title[i])
					this.title[i] = "";
				
				if(this.feeditems[i].getElementsByTagName("link")[0].firstChild)
					this.link[i] = this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;
				if(!this.link[i])
					this.link[i] = "#";
			}
			document.getElementById(this.idItem).onmouseover = function() { instanceOfTicker.mouseoverBol = 1; }
			document.getElementById(this.idItem).onmouseout = function() { instanceOfTicker.mouseoverBol = 0; }
			//document.getElementById(this.idCanale).onmouseover = function() { instanceOfTicker.mouseoverBol = 1; }
			//document.getElementById(this.idCanale).onmouseout = function() { instanceOfTicker.mouseoverBol = 0; }
			this.rotatemsg();
		}
	}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

rssticker_ajax.prototype.rotatemsg = function(canale)
{
	var instanceOfTicker = this;
	var descrizioneItem	= "";
	var dataItem = "";
	if (this.mouseoverBol == 1) //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function() { instanceOfTicker.rotatemsg(canale) }, 100);
	else  //else, construct item, show and rotate it!
	{
		//var spanCanale = document.getElementById(this.idCanale);
		var spanItem = document.getElementById(this.idItem);		
		/*if(this.category[this.pointer] != canale)
		{
			this.fadetransition('reset', '', spanCanale); //FADE EFFECT- RESET OPACITY
			var canale = this.category[this.pointer];
			var titoloCanale = '<a href="'+this.comments[this.pointer]+'">'+this.category[this.pointer]+'</a>';
			spanCanale.innerHTML = titoloCanale;
			this.fadetimer1 = setInterval( function() { instanceOfTicker.fadetransition('up', 'fadetimer1', spanCanale) }, 150); //FADE EFFECT - PLAY IT
		} */
		var titoloItem = this.title[this.pointer];
		spanItem.innerHTML = "";
		linkItem = this.link[this.pointer];
		this.fadetimer2 = setInterval( function() { instanceOfTicker.typewritertransition(titoloItem, linkItem, 'fadetimer2', spanItem) }, 15); //TYPE WRITER EFFECT - PLAY IT
		this.pointer = (this.pointer < this.feeditems.length-1) ? this.pointer+1 : 0;
		setTimeout(function(){ instanceOfTicker.rotatemsg(canale) }, this.delay) //update container every second
	}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

rssticker_ajax.prototype.fadetransition = function(action, timerid, obj)
{
	if (action == "reset")
		this.opacitysetting = 0.2;
	obj.style.opacity = this.opacitysetting;
	obj.style.filter = 'alpha(opacity='+this.opacitysetting*100+')';		
	if (action == "up")
		this.opacitysetting += 0.2;
	if (action == "up" && this.opacitysetting >= 1)
		clearInterval(this[timerid]);
}

// -------------------------------------------------------------------
// typewritertransition()- cross browser method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

rssticker_ajax.prototype.typewritertransition = function(titoloItem, linkItem, timerid, obj)
{
	if(this.maxChar && titoloItem.length > this.maxChar)
	{
		var myRe = '^.{'+this.maxChar+'}[^\\s\\W]*';
		var re = new RegExp(myRe, "gi");
		var results = titoloItem.match(re);		
		var titoloItem = results[0] + "...";
	}
  obj.innerHTML += titoloItem.charAt(this.count);
  if (this.count < titoloItem.length - 1)
		  this.count++;
	else
	{
		this.count = 0;
		clearInterval(this[timerid]);
		obj.innerHTML = '<a href="'+linkItem+'">'+titoloItem+'</a>';		
	}
}