

$(document).ready(function(){
    
    var childData = function(selector, arg){
        return selector.find(arg).attr('data');
    }
    
    var getWeather = function(weatherLocation){

       if ((weatherLocation.toUpperCase() == "BEAUFORT") ||
        (weatherLocation.toUpperCase() == "BELURAN") ||
        (weatherLocation.toUpperCase() == "KUALA PENYU") ||
        (weatherLocation.toUpperCase() == "KUNAK") ||
        (weatherLocation.toUpperCase() == "MEMBAKUT") ||
        (weatherLocation.toUpperCase() == "MENUMBOK") ||
        (weatherLocation.toUpperCase() == "PAPAR") ||
        (weatherLocation.toUpperCase() == "PITAS") ||
        (weatherLocation.toUpperCase() == "RANAU") ||
        (weatherLocation.toUpperCase() == "TAMPARULI") ||
        (weatherLocation.toUpperCase() == "TELUPID")) {
        weatherLocation = weatherLocation + ", Sabah"
    }
    
        weatherLocation = weatherLocation.substr(0,1).toUpperCase() + weatherLocation.substring(1)
        $('#weatherLocation').val(weatherLocation);
        
        disableWeatherSearch();
        
        $("#panel-weather").fadeOut("fast");
        $("#panel-weather").queue(function () {
            $(this).dequeue();
        });
 
        $('#message-error').fadeOut("fast");
        $("#message-error").queue(function () {
            $(this).dequeue();
        });        

        $.ajax({
            type: "GET",
            data:"where=" + weatherLocation,
            url: "/weather/weather_api.aspx",
            error: function (data) {
                $('#weather-servererror').show();
                $('#weather-notfound').hide();

                $('#message-error').fadeIn("fast");
                $("#message-error").queue(function () {
                    $(this).dequeue();
                });            
                
                enableWeatherSearch();

            },
            success: function(data){ 
               
                try {
                var forecast = data.match(/<forecast_information>(.*?)<\/forecast_information>/)[1];
                var cCondition = data.match(/<current_conditions>(.*?)<\/current_conditions>/)[1];
                var weather = {
                    'city': forecast.match(/<city data="(.*?)"\/>/)[1],
                    'date': forecast.match(/<forecast_date data="(.*?)"\/>/)[1],
                    'condition': cCondition.match(/<condition data="(.*?)"\/>/)[1],
                    'temp_c': cCondition.match(/<temp_c data="(.*?)"\/>/)[1],
                    'humidity': cCondition.match(/<humidity data="(.*?)"\/>/)[1],
                    'icon': cCondition.match(/<icon data="(.*?)"\/>/)[1]
                };

                weather.humidity = weather.humidity.replace(/humidity\s*:/i, '');
            } catch (e) {
                weather = {};
            }

            if (weather.city) {
                
                $('#weather-city').text(weather.city);
                $('#weather-date').text(weather.date);
                $('#weather-condition').text(weather.condition);
                $('#weather-tempC').text(weather.temp_c + ' C');
                $('#weather-humidity').text(weather.humidity);
                $('#weather-icon').attr({
                    'src': 'http://www.google.com' + weather.icon
//                    'src': weather.icon
                });

                $("#panel-weather").fadeIn("fast");
                $("#panel-weather").queue(function() {
                    $(this).dequeue();
                });

                try {
                    // console.log('setting weather location: ', weatherLocation);
//                    createCookie('weatherLocation', weatherLocation);
                } catch (e) {
                    // call failed - maybe createCookie() not defined?
                }

                enableWeatherSearch();

            } else {
                $('#weather-servererror').hide();
                $('#weather-notfound').show();

                $('#message-error').fadeIn("fast");
                $("#message-error").queue(function() {
                    $(this).dequeue();
                });

                enableWeatherSearch();

            }
        }
     
        });
    }

    var weatherIsBusy = false;

    function enableWeatherSearch(){
        $('#weather-submit')
            .val('search')
            .removeAttr('disabled');    

        $('#weatherLocation').removeAttr('disabled');             
    
        weatherIsBusy = false;
    }

    function disableWeatherSearch(){
        // called when loading
        $('#weather-submit')
            .val('loading..')
            .attr('disabled', 'disabled');
        
        $('#weatherLocation').attr('disabled', 'disabled');            

        weatherIsBusy = true;
    }

    $('#weather-submit').click(function(){
        var weatherLocation = $('#weatherLocation').val();
        getWeather(weatherLocation);
    });

    $('.weather-example').click(function(){
        if(weatherIsBusy)return false; // prevent user from modifying values when weather is loading
        
        getWeather( $(this).text() );    
    });

    var weatherLocation = 'Kota Kinabalu';

    try{
//        weatherLocation = readCookie('weatherLocation');
    }catch(e){
        weatherLocation = 'Kota Kinabalu';
    }
    
    getWeather(weatherLocation);
    
});



