$("document").ready(function () {
    getWeather();
    //getWUndergroundWeather();
});

function getWeather() {
    var latitude = '42.785';
    var longitude = '-78.609';

    var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + latitude + "&lng=" + longitude + "&callback=?";
    //alert(url);
    $.getJSON(url, function (data) {
        var clouds = data.weatherObservation.clouds;
        var weather = data.weatherObservation.weatherCondition;
        var temp = data.weatherObservation.temperature;
        var humidity = data.weatherObservation.humidity;
        var conditions_img = getConditions(clouds, weather);

        var conditions = '';
        if (weather == 'n/a') {
            if (clouds == 'n/a') {
                conditions = 'fine';
            } else {
                conditions = clouds;
            }
        } else {
            if (weather == '') {
                if (clouds == 'n/a') {
                    conditions = 'no report';
                } else {
                    conditions = clouds;
                }
            } else {
                conditions = weather;
            }
        }

        setConditions(conditions, conditions_img, temp, humidity);
    });
}

function getWUndergroundWeather() {
    var url = "http://rss.wunderground.com/auto/rss_full/NY/East_Aurora.xml?units=english";
    $.getJSON(url, function (data) {
        alert(data);
        //var clouds = data.weatherObservation.clouds;
        //<yweather:wind chill="28"   direction="50"   speed="3" /> 
        //<yweather:atmosphere humidity="100"  visibility="0.12"  pressure="30.07"  rising="1" /> 
        //<yweather:astronomy sunrise="6:46 am"   sunset="7:45 pm"/>
        //<yweather:condition  text="Fog"  code="20"  temp="28"  date="Thu, 07 Apr 2011 7:18 am EDT" />
    });
}

function getYahooWeather() {
    var url = "http://weather.yahooapis.com/forecastrss?w=12763092";
    $.getJSON(url, function (data) {
        alert(data);
        var clouds = data.weatherObservation.clouds;
        //<yweather:wind chill="28"   direction="50"   speed="3" /> 
        //<yweather:atmosphere humidity="100"  visibility="0.12"  pressure="30.07"  rising="1" /> 
        //<yweather:astronomy sunrise="6:46 am"   sunset="7:45 pm"/>
        //<yweather:condition  text="Fog"  code="20"  temp="28"  date="Thu, 07 Apr 2011 7:18 am EDT" />
    });
}

function getConditions(clouds, weather) {
    if (weather == 'n/a') {
        switch (clouds) {
            case 'n/a':
                return 'sunny.gif';
            case 'clear sky':
                return 'sunny.gif';
            case 'few clouds':
                return 'partly_cloudy.gif';
            case 'scattered clouds':
                return 'partly_cloudy.gif';
            case 'broken clouds':
                return 'partly_cloudy.gif';
            default:
                return 'cloudy.gif';
        }
    } else {
        weather = weather.replace('light ', '').replace('heavy ', '').replace(' in vicinity', '');
        switch (weather) {
            case 'drizzle':
                return 'rain.gif';
            case 'rain':
                return 'rain.gif';
            case 'snow':
                return 'snow.gif';
            case 'snow grains':
                return 'sleet.gif';
            case 'ice crystals':
                return 'icy.gif';
            case 'ice pellets':
                return 'icy.gif';
            case 'hail':
                return 'sleet.gif';
            case 'small hail':
                return 'sleet.gif';
            case 'snow pellets':
                return 'sleet.gif';
            case 'unknown precipitation':
                return 'rain.gif';
            case 'mist':
                return 'mist.gif';
            case 'fog':
                return 'fog.gif';
            case 'smoke':
                return 'smoke.gif';
            case 'volcanic ash':
                return 'smoke.gif';
            case 'sand':
                return 'dust.gif';
            case 'haze':
                return 'haze.gif';
            case 'spray':
                return 'rain.gif';
            case 'widespread dust':
                return 'dust.gif';
            case 'squall':
                return 'flurries.gif';
            case 'sandstorm':
                return 'dust.gif';
            case 'duststorm':
                return 'dust.gif';
            case 'well developed dust':
                return 'dust.gif';
            case 'sand whirls':
                return 'dust.gif';
            case 'funnel cloud':
                return 'flurries.gif';
            case 'tornado':
                return 'storm.gif';
            case 'waterspout':
                return 'storm.gif';
            case 'showers':
                return 'storm.gif';
            case 'thunderstorm':
                return 'thunderstorm.gif';
            default:
                if (weather.indexOf("rain")) {
                    return 'rain.gif';
                } else if (weather.indexOf("snow")) {
                    return 'snow.gif';
                } else if (weather.indexOf("thunder")) {
                    return 'thunderstorm.gif';
                } else if (weather.indexOf("dust")) {
                    return 'dust.gif';
                } else {
                    return 'sunny.gif';
                }
        }
    }
}

function setConditions(conditions, conditions_img, temp, humidity) {
    var country = 'United States';
    var region = 'NY';
    var city = 'East Aurora';
    var loc_country_code = 'US';
    if (loc_country_code == 'US') {
        temp = parseInt(temp*1.8) + 32;
        temp_type = "F";
    } else {
        temp_type = "C";
    }

    $("#weather_widget").append("<img id='weather_img' src='http://www.google.com/images/weather/" + conditions_img + "' />");
    $("#weather_widget").append("<div id='weather_conditions'><p id='weather_temp'><b>Temp: </b>" + temp + "&deg; " + temp_type + "</p><p id='weather_hum'><b>Humidity: </b>" + humidity + "%</p><p id='weather_cond'>" + conditions.substr(0, 1).toUpperCase() + conditions.substr(1) + "</p></div>");
}
