// CBC.ca Global Navigation Weather AJAX Display
// 2007 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 = {

	envdomain:window.location.host,
	cookiedomain:'',

Init: function()
	{
		w = gnwidget;
		cookie = w.ReadCookie("weatherCenter", ";");
		if(cookie && cookie.length<8){
			w.CreateCookie('weatherCenter',"s0000430",'1000');
			cookie = "s0000430";
		}
		citycode = (cookie) ? cookie : "s0000430"; /* 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;
	},
	
CreateCookie : function(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			domain = "; domain=." + this.cookiedomain;
			document.cookie = name+"="+value+expires+domain+"; path=/";
		},
		
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 = '<a href="http://'+this.envdomain+'/includes/changelocation/index.html" onclick="gnwidget.Change();return false;" id="gnww-cc">Change City</a><br /><strong>Weather information is currently unavailable</strong>';
				/* 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;
			/* console.log("Error parsing XML\n" + e) */
		}

		if (err != 1)
		{
			w.city.now = w.city.desc.split("; ")[0];
			w.city.tom = w.city.desc.split("; ")[1].replace(/Tomorrow/,"<em>Tomorrow:</em><br />");
			var widgetdiv = document.getElementById('gn-weather-widget');
			if (w.city.name.length > 25) w.city.name = w.city.name.substr(0,24) + "&hellip;";
			widgetdiv.innerHTML = '<div id="gnww-city-link"><a href="http://'+this.envdomain+'/weather/' + w.city.code + '.html" class="gn-weather-link" target="_top" title="Details"><strong>' + w.city.name + '</strong></a> | <a href="http://'+this.envdomain+'/includes/changelocation/index.html" onclick="gnwidget.Change();return false;" id="gnww-cc">Change City</a></div><div id="gnww-display"><div id="gnww-now">'+ w.city.now +'</div><div id="gnww-tom">'+ w.city.tom +'</div></div>';
			widgetdiv.className = "gnww-" + w.city.icon;
			document.getElementById('gnww-display').onclick = function(envdomain) {return function(){top.location.href='http://'+envdomain+'/weather/' + w.city.code + '.html';}}(this.envdomain);
		}
		else
		{
			var widgetdiv = document.getElementById('gn-weather-widget');
			widgetdiv.innerHTML = '<a href="http://'+this.envdomain+'/includes/changelocation/index.html" target="_top" onclick="gnwidget.Change();return false;" id="gnww-cc">Change City</a><br /><strong>Weather for selected city currently unavailable</strong>';
		}
	},

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 ("/includes/changelocation/index.html","selectcity","menubar=0,resizable=0,scrollbars=0,width=340,height=500");
	}
};

gnwidget.Init();
