// CBC.ca Global Navigation Weather AJAX Display
// 2006 David Raso - Interface Programmer
// Feel free to use any code

function addEventToObject(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}
function ajaxRequest(url,func,obj) {
	if (window.XMLHttpRequest) {var req = new XMLHttpRequest();}
	else if (window.ActiveXObject) {try {req = new ActiveXObject("Msxml2.XMLHTTP");}catch(e) {req = new ActiveXObject("Microsoft.XMLHTTP");}}
	if (func) {req.onreadystatechange = function() {func(req,obj);}}
	req.open('GET',url,true);
	req.setRequestHeader('X-Requested-With','XMLHttpRequest');
	req.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');
	if (req.overrideMimeType) {req.overrideMimeType("text/xml");}
	req.send(null);
	return false;
}

var gnwidget = {

Init: function()
	{
		var w = gnwidget;
		w.SignedIn();
	},

SignedIn: function()
	{
		var w = gnwidget;
		var cookie = w.ReadCookie("MyCBCSignIn", ";")
		if (cookie)
	  		{
			citycode = w.ReadCookie("locale.citycode", "&")
		  	if (citycode) w.FetchCity(citycode);
			}
		else
			{
			document.getElementById('gn-weather-widget').innerHTML = 'Sign in for local news and weather&nbsp;';
			}
	},

ReadCookie: function(cname, split)
	{
		var ca = document.cookie.split(split);
		var nameEQ = cname + "=";
		var cookie;
		for(var i=0;i < ca.length;i++)
			{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) cookie = c.substring(nameEQ.length,c.length);
  			}
  		if (cookie) return cookie;
	},

FetchCity: function(citycode)
	{
		var w = gnwidget;
		cityxmlurl = "/webcache/weatherdata/" + citycode + ".xml";
		ajaxRequest(cityxmlurl,w.XmlFetchResponse);
	},

XmlFetchResponse: function(req)
	{
	var w = gnwidget;
	if (req.readyState == 4)
		{
		if (req.status == 200)
			{
			w.Create(req);
			}
		else
			{
			/* alert("There was a problem retrieving the XML data:\n\r" + req.statusText); */
			}
		}
	},

Create: function(req)
	{
		var w = gnwidget;
		try
		{
		/* Try to parse the XML if there is an error bail */
		w.ParseXML(req);
		}
		catch(e)
		{
			var err = 1;
			/* alert("Error parsing XML\n" + e) */
		}

		if (err != 1)
		{
			/* if the text is too long it will need to be truncated */
			var maxlength = 108;
			if ((w.city.name.length + w.city.desc.length) >= maxlength) w.city.desc = w.city.desc.split("; ")[0]
			var widgetdiv = document.getElementById('gn-weather-widget');
			document.getElementById('mycbcsignin').style.display = 'none';
			widgetdiv.innerHTML =  '<span id="gn-weather-widget-display"><a href="/weather/conditions.jsp?station=' + w.city.code + '" class="gn-weather-link" title="Details">' + w.city.name + '</a> <img src="/includes/globalnav/weatherwidget/icons/' + w.city.icon + '.gif" height="15" align="top" /> <a href="/weather/conditions.jsp?station=' + w.city.code + '" class="gn-weather-link" title="Details">' + w.city.desc + '</a></span> <a href="/mycbc/do/setLocalWeather" title="Modify your location preferences"><img src="/includes/globalnav/weatherwidget/config-off.gif" onMouseOver="gnwidget.ChangeCity(this)" onMouseOut="gnwidget.Init()" align="top"/></a> <a href="/mycbc/do/signout" title="Sign Out"><img src="/includes/globalnav/weatherwidget/signout-off.gif" onMouseOver="gnwidget.SignOut(this)" onMouseOut="gnwidget.Init()" align="top"/></a>';
		}
		else
		{
			var widgetdiv = document.getElementById('gn-weather-widget');
			widgetdiv.innerHTML = '<a href="http://www.cbc.ca/weather/conditions.jsp?station=' + citycode + '" class="gn-weather-link">Weather for selected city unavailable</a>&nbsp;';
		}
	},

ChangeCity: function(icon)
	{
		var w = gnwidget;
		var widgetdiv = document.getElementById('gn-weather-widget-display');
		icon.src = '/includes/globalnav/weatherwidget/config-on.gif'
		widgetdiv.innerHTML = 'Change City';
	},

SignOut: function(icon)
	{
		var w = gnwidget;
		var widgetdiv = document.getElementById('gn-weather-widget-display');
		icon.src = '/includes/globalnav/weatherwidget/signout-on.gif'
		widgetdiv.innerHTML = 'Sign Out';
	},

ParseXML: function(req)
	{
		var w = gnwidget;

		if (req.responseXML.documentElement)
			{
			citynode = req.responseXML.documentElement.getElementsByTagName('city')[0];
			}
		else
			{
			dom = new ActiveXObject("MSXML.DOMDocument");
			dom.loadXML(req.responseText);
			citynode = dom.documentElement.getElementsByTagName('city')[0];
			}
			w.city = new Object();
			w.city.name = citynode.getElementsByTagName('n')[0].firstChild.nodeValue;
			w.city.icon = citynode.getElementsByTagName('i')[0].firstChild.nodeValue;
			w.city.desc = citynode.getElementsByTagName('d')[0].firstChild.nodeValue;
			w.city.code = citynode.getElementsByTagName('c')[0].firstChild.nodeValue;
	}
}

addEventToObject(window,'onload',gnwidget.Init);