// 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 bweather = {

Init: function()
	{
		w = bweather;
		var widgetdiv = document.getElementById('beijing-weather');
		w.FetchCity("ZBAA");
	},

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 = "http://cbc.ca/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('beijing-weather');
				widgetdiv.innerHTML = '<a href="http://www.cbc.ca/includes/changelocation/index.html" onclick="bweather.Change();return false;" id="gnww-cc">Change City</a><br /><strong>Weather information is currently unavailable</strong>';
			}
		}
	},

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

		if (err != 1)
		{
			w.city.now = w.city.desc.split("; ")[0];
			w.city.now = w.city.now.split("</strong>");
			w.city.now = w.city.now.join("");			
			w.city.now = w.city.now.split("&deg;c");
			w.city.now = w.city.now.join("&deg;C</strong>");
			
			var widgetdiv = document.getElementById('beijing-weather');			

			widgetdiv.innerHTML = '<h4>Current Beijing Weather:</h4><p>'+ w.city.now +'</p>';
			widgetdiv.className = "gnww-b-" + w.city.icon;			
		}
	},

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");
	}
};

bweather.Init();