/*
 ======================================================================
 RSS JavaScript Ticker object
 Author: George at JavaScriptKit.com/ DynamicDrive.com
 Created: Feb 5th, 2006. Updated: Feb 5th, 2006
 ======================================================================
*/

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)
			{
			
			}
		}
	}
	return httprequest
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rss_ticker(RSS_id, cachetime, divId, divClass, delay, optionalswitch, showDate) 
// -------------------------------------------------------------------

function rss_ticker(RSS_id, cachetime, divId, divClass, delay, optionalswitch, showDate)
{
	this.RSS_id=RSS_id //Array key indicating which RSS feed to display
	this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
	this.tickerid=divId //ID of ticker div to display information
	this.delay=delay //Delay between msg change, in miliseconds.
	
	if(typeof optionalswitch!="undefined")
	{
		this.logicswitch = optionalswitch
	}
	else
	{
		this.logicswitch = -1
	}
	//this.logicswitch=(typeof optionalswitch!="undefined")? optionalswitch : -1
	if(typeof showDate!="undefined")
	{
		this.showDate = showDate
	}
	else
	{
		this.showDate = -1
	}
	//this.showDate=(typeof showDate!="undefined")? showDate : -1
	
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer=0
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'">Initializing weather...</div>')
	this.getAjaxcontent()
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
// -------------------------------------------------------------------

rss_ticker.prototype.getAjaxcontent=function()
{
	if (this.ajaxobj)
	{
		var instanceOfTicker=this
		var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
		this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
		this.ajaxobj.open('GET', "ajax/rssfetch.php?"+parameters, true)
		this.ajaxobj.send(null)
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rss_ticker.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("day").length==0)
			{ 
				//if no <item> elements found in returned content
				document.getElementById(this.tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"+this.ajaxobj.responseText
				return
			}
			var instanceOfTicker=this
			this.feeditems=xmldata.getElementsByTagName("day")
			this.feeditemsday=xmldata.getElementsByTagName("cc")
			this.feeditemsday[0].setAttribute("dhtemp", this.feeditemsday[0].getElementsByTagName("tmp")[0].firstChild.nodeValue)
			this.feeditemsday[0].setAttribute("dltemp", this.feeditemsday[0].getElementsByTagName("flik")[0].firstChild.nodeValue)
			this.feeditemsday[0].setAttribute("dcdescription", this.feeditemsday[0].getElementsByTagName("t")[0].firstChild.nodeValue)
			this.feeditemsday[0].setAttribute("dcicon", this.feeditemsday[0].getElementsByTagName("icon")[0].firstChild.nodeValue)
			
			//Cycle through RSS XML object and store each peice of the item element as an attribute of the element
			var days = new Array(5)
			for (var i=1; i<this.feeditems.length; i++)
			{
				days[i] = xmldata.getElementsByTagName("day")[i].getAttribute("t")
				this.feeditems[i].setAttribute("htemp", this.feeditems[i].getElementsByTagName("hi")[0].firstChild.nodeValue)
				this.feeditems[i].setAttribute("ltemp", this.feeditems[i].getElementsByTagName("low")[0].firstChild.nodeValue)
				this.feeditems[i].setAttribute("cdescription", this.feeditems[i].getElementsByTagName("t")[0].firstChild.nodeValue)
				this.feeditems[i].setAttribute("cicon", this.feeditems[i].getElementsByTagName("icon")[0].firstChild.nodeValue)
			}
			this.rotatemsg(days)
		}
	}
	
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

rss_ticker.prototype.rotatemsg=function(days)
{
	
	var instanceOfTicker=this
	if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function(){instanceOfTicker.rotatemsg()}, 10)
	else
	{
		var tickerDiv=document.getElementById(this.tickerid)
		
		//title
		var tickercontent='<a href="'+this.feeditems[this.pointer].getAttribute("clink")+'">'+this.feeditems[this.pointer].getAttribute("ctitle")+'</a>'
		//description

		if (this.logicswitch=="showdescription")
			
			var now = new Date();
			var hours = now.getHours();
			if(hours  < 18)
			{
				nightDay = "Today";
			}
			else
			{
				nightDay = "Tonight";
			}
			var icon = new Array(5)
			var high_temp = new Array(5)
			var lower_temp = new Array(5)
			var description = new Array(5)
			var contents = new Array(5)
			contented ="<table border='0' cellpadding=\"0\" cellspacing=\"0\" style=\"border: 1px solid #B2D33E; height:142px; background-color: #EFEBF9; \">";
			day_cond = this.feeditemsday[0].getAttribute("dcdescription")
			day_hi_temp = this.feeditemsday[0].getAttribute("dhtemp")
			day_low_temp = this.feeditemsday[0].getAttribute("dltemp")
			day_icon = this.feeditemsday[0].getAttribute("dcicon")
	
			
			contents[0]="<td width=\"80\" valign=\"top\" style=\"border-right: 1px solid #B2D33E; text-transform: capitalize; background: url(../img/homepage/weatherHeader.gif) 100% -3% no-repeat; font-size:10px;\"><div style=\"color:#636060; margin-top:4px; margin-bottom:10px; font-weight:bold;  \">"+nightDay+"</div><img src='/img/local_info/"+day_icon+".png' height=\"64\" width=\"64\"><br />"+day_cond+"<br />High: "+day_hi_temp+"°C<br /> Low: "+day_low_temp+"°C</td>";
		
			for (var i=1; i<this.feeditems.length; i++)
			{
				//pos = this.feeditems[i].getAttribute("cdescription").indexOf('/')+1;
				description[i] = this.feeditems[i].getAttribute("cdescription")//.substring(pos)
				lower_temp[i] = this.feeditems[i].getAttribute("ltemp")
				high_temp[i] = this.feeditems[i].getAttribute("htemp")
				icon[i] = this.feeditems[i].getAttribute("cicon")
				contents[i]="<td width=\"80\" valign=\"top\" style=\"border-right: 1px solid #B2D33E; text-transform: capitalize; background: url(../img/homepage/weatherHeader.gif) 100% -3% no-repeat; font-size:10px;\" ><div style=\"color:#636060; margin-top:4px; margin-bottom:10px; font-weight:bold;  \">"+days[i]+"</div><img src='/img/local_info/"+icon[i]+".png' height=\"64\" width=\"64\"><br />"+description[i]+"<br />High: "+high_temp[i]+"°C<br /> Low: "+lower_temp[i]+"°C</td>";
			
			}
			
			contented2="</tr></table>";
			//alert(contented+contents[0]+contents[1]+contents[2]+contents[3]+contents[4])
			tickerDiv.innerHTML = contented+contents[0]+contents[1]+contents[2]+contents[3]+contents[4]+contented2;
			this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
			setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
			
			//pubDate
		if (this.showDate =="showDate")
			
			tickercontent = this.feeditems[this.pointer].getAttribute("cpubDate").toLowerCase()
			
	}
}