jQuery(function($){
    
    var weather = {};
    
    //variables
    weather.select  = $('.select_weather');
    weather.modal   = $('.dialog_model');
    weather.content = $('.havadurumu');
    weather.city    = $('.sehir');
    
    //set cookie
    weather.cookies = function(val, options) {
        if(val) {
            return $.cookie('select_weather', val, options);
        } else {
            return $.cookie('select_weather');
        }
    }
    
    //function loader
    weather.city_load = function(val) {
        weather.content.load(BASE_URL + 'havadurumu/ajax_current/' + val);
    }
    
    //function init weather
    weather.init = function() {
        
        //dialog model select city
        weather.select.live('click', function() {
            weather.modal.dialog({
                modal: true, 
                draggable: false
            });    
        });
        
        if ( !weather.cookies() ) {
            weather.cookies( $('option:selected', weather.city).val(), {expires:300, path: '/'});
            weather.city_load( $('option:selected', weather.city ).val() );
        } else {
            $(weather.city).val( weather.cookies() );
            weather.city_load( weather.cookies() );
        }
        
        $('.sehir').live('change, click', function() {
           $('.havadurumu').html('Yükleniyor...');                                                               
           $('option:selected', weather.city).each(function() {
                var selected = $(this).val();
        		$('.havadurumu').load(BASE_URL + 'havadurumu/ajax_current/'+selected, function(html){
        			weather.cookies(selected, {expires:300, path: '/'});
                    $(this).val(html);
                    weather.modal.dialog('close');
        		});  
           });
        });                    
    }
    
    //load init
    weather.init();

});
