// CBC.ca Global Navigation Weather AJAX Display

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()
	{
		w = gnwidget;
		cookie = w.ReadCookie("weatherCenter", ";");
		citycode = (cookie) ? cookie : "YYZ"; /* Default City - GEOIP or user will set weatherCenter cookie based on IP - this is just for a backup */
		w.FetchCity(citycode);
	},

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)
	{
		try {wwxhrurl = wwxhrurl;}catch(e){wwxhrurl = "/webcache/weatherdata/####.xml";}
		cityxmlurl = wwxhrurl.replace(/####/g, citycode);
		ajaxRequest(cityxmlurl,w.XmlFetchResponse);
	},

XmlFetchResponse: function(req)
	{
	if (req.readyState == 4)
		{
		if (req.status == 200)
			{
			w.Create(req);
			}
		else
			{
				var widgetdiv = document.getElementById('gn-weather-widget');
				widgetdiv.innerHTML = '<span id="gn-weather-widget-display">Weather information is currently unavailable</span> <a href="http://www.cbc.ca/includes/changelocation/index.html" onclick="gnwidget.Change();return false;" id="gnww-cc"><img src="http://www.cbc.ca/includes/globalnav/weatherwidget/config-off.gif" onMouseOver="gnwidget.ChangeCity(this)" onMouseOut="gnwidget.Init()" align="top"/></a>';
				/* console.log("There was a problem retrieving the XML data:\n\r" + req.statusText); */
			}
		}
	},

Create: function(req)
	{
		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)
		{
 			var widgetdiv = document.getElementById('gn-weather-widget');
			widgetdiv.innerHTML =  '<span id="gn-weather-widget-display"><a href="http://www.cbc.ca/weather/' + w.city.code + '.html" class="gn-weather-link" title="Details">' + w.city.name + '</a> <img src="http://www.cbc.ca/includes/globalnav/weatherwidget/icons/' + w.city.icon + '.gif" height="15" align="top" /> <a href="http://www.cbc.ca/weather/' + w.city.code + '.html" class="gn-weather-link" title="Details">' + w.city.desc + '</a></span> <a href="http://www.cbc.ca/includes/changelocation/index.html" onclick="gnwidget.Change();return false;" id="gnww-cc"><img src="http://www.cbc.ca/includes/globalnav/weatherwidget/config-off.gif" onMouseOver="gnwidget.ChangeCity(this)" onMouseOut="gnwidget.Init()" align="top"/></a>';
		}
		else
		{
			var widgetdiv = document.getElementById('gn-weather-widget');
			widgetdiv.innerHTML = '<span id="gn-weather-widget-display">Weather for selected city unavailable	</span> <a href="http://www.cbc.ca/includes/changelocation/index.html" onclick="gnwidget.Change();return false;" id="gnww-cc"><img src="/includes/globalnav/weatherwidget/config-off.gif" onMouseOver="gnwidget.ChangeCity(this)" onMouseOut="gnwidget.Init()" align="top"/></a>';
		}
	},

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

ParseXML: function(req)
	{
		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;
	},

Change: function()
	{
		window.open ("http://www.cbc.ca/includes/changelocation/index.html","selectcity","menubar=0,resizable=0,scrollbars=0,width=340,height=500");
	}
}

gnwidget.Init();

