function SelectSingleNode(xmlDoc, elementPath)
{
	if(window.ActiveXObject)
	{
	    return xmlDoc.selectSingleNode(elementPath);
	}
	else
	{
	   var xpe = new XPathEvaluator();
	   var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
	   var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	   return results.singleNodeValue; 
	}
}


$(document).ready(function init()
{
    if (document.getElementById('weather') != null)
    {

      var xmlhttp=null;
    	   
        // hardcode url of XML  renderer for now - we will have to decide
        // where to place it on the target server.
        //var url = "http://192.168.0.139/betasite/weatherOffice/weather.aspx?key=bc-35_e&d=" + (new Date());
	
	var virtualPath = "";
	
	var url = virtualPath + "/weather.asp?key=bc-35_e&d=1&d=" + (new Date());

        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            if (typeof(xmlhttp) != 'object' )
            {	 	
		// If IE7, Mozilla, Safari, etc: Use native object
		xmlhttp = new XMLHttpRequest();
            }
        }
        catch (e) 
        {
		// fallback: use native object 
		xmlhttp = new XMLHttpRequest();
        }
        if (xmlhttp!=null)
        {
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);

		var xmlDoc = "";

		if (document.all != null)
		{
			xmlDoc = xmlhttp.responseXML;
		}
		else
		{
			xmlDoc = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
		}

		var image;
		var temp;
		var date;
		var tempUnit;
		var valid;
		var imageDesc;
		
		valid = 1;

		if (document.all != null)
		{
			if (SelectSingleNode(xmlDoc,"//type") != null) {
				image = SelectSingleNode(xmlDoc,"//type").text;
				temp = SelectSingleNode(xmlDoc, "//temperature").text;
				tempUnit = SelectSingleNode(xmlDoc, "//unit").text;
				date = SelectSingleNode(xmlDoc, "//date").text;
				imageDesc = SelectSingleNode(xmlDoc, "//condition").text;
			} else {
				valid = 0;
			}
		}
		else
		{
			if (SelectSingleNode(xmlDoc,"//type") != null) {
				image = SelectSingleNode(xmlDoc,"//type").childNodes[0].nodeValue;
				temp = SelectSingleNode(xmlDoc, "//temperature").childNodes[0].nodeValue;
				tempUnit = SelectSingleNode(xmlDoc, "//unit").childNodes[0].nodeValue;
				date = SelectSingleNode(xmlDoc, "//date").childNodes[0].nodeValue;
				imageDesc = SelectSingleNode(xmlDoc, "//condition").childNodes[0].nodeValue;
			} else {
				valid = 0;
			}
		}

		var imagePath =   virtualPath + "/images/weather/" + image + ".png"; 

		if (valid != 0)
		{
			var spanWeather = document.getElementById('weather');
			
			var d = new Date();
			var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
			
			spanWeather.innerHTML = "<h2>Weather</h2>";

			var weatherInfo = document.createElement('p');
			
			//var weatherUrl = document.createElement('a');
			//weatherUrl.setAttribute('href', 'http://www.weatheroffice.gc.ca/city/pages/bc-35_metric_e.html');
			//weatherUrl.setAttribute('target', '_blank');

			var weatherDate = document.createElement('span');
			weatherDate.setAttribute("class","date");
			weatherDate.innerHTML = m_names[d.getMonth()] + " " + d.getDate();

			var imgWeather = document.createElement('img');
			imgWeather.src = imagePath;
			imgWeather.id = 'weather-image';
			imgWeather.setAttribute("height", "36");
			imgWeather.setAttribute("width", "36");
			imgWeather.setAttribute("alt", imageDesc);

			var weatherTemp = document.createElement('span');
			weatherTemp.setAttribute("class","temperature");
			if (temp == "n/a")
			{
				weatherTemp.innerHTML = "  ";
			}
			else
			{
				weatherTemp.innerHTML = parseInt(temp) + "&deg;"  + tempUnit;
			}


			//weatherUrl.appendChild(imgWeather);
			//weatherUrl.appendChild(weatherTemp);
			//weatherUrl.appendChild(weatherDate);


			//weatherInfo.appendChild(weatherUrl);
			

			spanWeather.appendChild(imgWeather);
			spanWeather.appendChild(weatherTemp);
			spanWeather.appendChild(weatherDate);
			$("#weather span:first").addClass("temperature");
			$("#weather span:last-child").addClass("date");
		}			
        }	
    }		
});
