function Weather()
{
    var me = this;
    try
    {
        var request = false;
        if(window.XMLHttpRequest && !(window.ActiveXObject))
        {
    	    try
    	    {
    	        request = new XMLHttpRequest();
    	        request.nonIE = true;
    	    }
            catch(e) {}
        }
        else if(window.ActiveXObject)
        {
            	try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
            	catch(e) {}
        }
        if(!request)
        {
            this.onFailure();
            return;
        }
        request.open("GET", Weather.proxyLink, true);
        request.onreadystatechange = function()
        {
            if(request.readyState == 4)
            {
                if(request.status == 200)
                    me.onSuccess(request.responseXML, request.nonIE);
                else
                    me.onFailure();
            }
        };
        request.send("");
    }
    catch(e) { alert(e.toString()); }
}

Weather.serviceLink = "http://weather.yahooapis.com/forecastrss?p=94085&u=f";
Weather.imagePath = {
    pre         : "http://us.i1.yimg.com/us.yimg.com/i/us/nws/weather/gr/",
    postDay     : "d.png",
    postNight   : "n.png"
};

Weather.prototype.onSuccess = function(xml, nonIE)
{
    var condition = xml.documentElement.getElementsByTagName(nonIE ? "condition" : "yweather:condition")[0];
    
    var date = condition.getAttribute("date");
    var time = date.match(/(\d+):(\d\d)\s*([ap]m)/);
    
    var astronomy = xml.documentElement.getElementsByTagName(nonIE ? "astronomy" : "yweather:astronomy")[0];
    var sunrise = astronomy.getAttribute("sunrise").match(/(\d+):(\d\d)\s*([ap]m)/);
    var sunset = astronomy.getAttribute("sunset").match(/(\d+):(\d\d)\s*([ap]m)/);
    var isDay = Weather.isDay(time, sunrise, sunset);
    
    document.getElementById("weatherIcon").src = Weather.imagePath.pre + condition.getAttribute("code") + (isDay ? Weather.imagePath.postDay : Weather.imagePath.postNight);
    document.getElementById("weatherTemp").innerHTML = condition.getAttribute("temp") + "&#176;";
    document.getElementById("weatherDate").innerHTML = "Sunnyvale, CA 94085<br/>" + date;
};

Weather.prototype.onFailure = function()
{
    document.getElementById("weatherBox").style.display = "none";
};

Weather.isDay = function(time, sunrise, sunset)
{
    if(time[3] == "am")
    {
        var thour = time[1] % 12;
        var shour = sunrise[1] % 12;
        var tmin = time[2];
        var smin = sunrise[2];
        return (thour > shour) || (thour == shour && tmin > smin);
    }
    else
    {
        var thour = time[1] % 12;
        var shour = sunset[1] % 12;
        var tmin = time[2];
        var smin = sunset[2];
        return (thour < shour) || (thour == shour && tmin < smin);
    }
};

Weather._init = function()
{
    var host = window.location.host;
    var path = window.location.pathname.match(/(.*\/)/)[1];
    Weather.proxyLink = "http://" + host + path + "php/proxy.php?proxy_url=http://weather.yahooapis.com/forecastrss?p=94085&u=f";
};

Weather._init();

