var Webget = {
    defaultCity: 'santiago',
    defaultValores: ['19','20','13', '15'],
    dd: null,
    optionsCookie: {
        path: '/',
        expires: 3650 // 10 years
    },

    // -------------------------------------------------------------------------
    init: function() {

        // Inicializa valores y clima
        Webget.setClima();
        Webget.setAllValores();

        // Para el clima
        $("#cityvalues").mcDropdown('#citymenu', {
            valueAttr: 'title',
            minRows:2,
            select: function(val) {
                Webget.changeCiudad(val);
            }
        });
        $('.combo-ciudad').click(function() {
            var dd = $("#cityvalues").mcDropdown();
            dd.openMenu();
        });

        $("#wg-valores-1 input").mcDropdown('#wg-valores-1 ul', {valueAttr: 'title',minRows:2,select: function(val) {Webget.changeValores('1', val);}});
        $("#wg-valores-2 input").mcDropdown('#wg-valores-2 ul', {valueAttr: 'title',minRows:2,select: function(val) {Webget.changeValores('2', val);}});
        $("#wg-valores-3 input").mcDropdown('#wg-valores-3 ul', {valueAttr: 'title',minRows:2,select: function(val) {Webget.changeValores('3', val);}});
        $("#wg-valores-4 input").mcDropdown('#wg-valores-4 ul', {valueAttr: 'title',minRows:2,select: function(val) {Webget.changeValores('4', val);}});
        $('.datos .indices').click(function() {
            //alert($(this).next().find('input:eq(1)').attr('name'));
            //alert($(this).next().html());
            var dd = $(this).next().find('input:eq(1)').mcDropdown();
            dd.openMenu();
        });

        $('#webget-content').fadeIn();


    },

    // -------------------------------------------------------------------------
    setClima: function() {

        var webget_ciudad = $.cookie('webget_ciudad');
        if(typeof webget_ciudad === 'undefined' || webget_ciudad === '' || webget_ciudad === null) {
            webget_ciudad = Webget.defaultCity;
        }
        var url = '/prontus_df/site/extra/webget/clima/'+webget_ciudad+'.html';
        $('#clima').load(url, function(responseText, textStatus, XMLHttpRequest) {
            if(textStatus != 'success' && webget_ciudad != Webget.defaultCity) {
                Webget.changeCiudad(Webget.defaultCity);
            }
        });
    },

    // -------------------------------------------------------------------------
    setValores: function(nro) {
        if(nro > 4 || nro < 1) {
            return;
        }
        var webget_valor = $.cookie('webget_valor_' + nro);
        if(typeof webget_valor === 'undefined' || webget_valor === '' || webget_valor === null) {
            webget_valor = Webget.defaultValores[nro-1];
        }
        var url = '/prontus_df/site/extra/fxcm/valores/'+webget_valor+'.html';
        $('#wg-valores-' + nro).prev().load(url, function(responseText, textStatus, XMLHttpRequest) {
            if(textStatus != 'success' ) {
                if(Webget.defaultValores[nro-1] !== webget_valor) {
                    $.cookie('webget_valor_' + nro, Webget.defaultValores[nro-1], Webget.optionsCookie);
                    Webget.setClima();
                }
            }
        });
    },

    // -------------------------------------------------------------------------
    setAllValores: function() {

        Webget.setValores(1);
        Webget.setValores(2);
        Webget.setValores(3);
        Webget.setValores(4);

    },

    // -------------------------------------------------------------------------
    changeCiudad: function(ciudad) {
        if(ciudad === '') {
            ciudad = Webget.defaultCity;
        }
        $.cookie('webget_ciudad', ciudad, Webget.optionsCookie);
        Webget.setClima();
    },

    // -------------------------------------------------------------------------
    changeValores: function(nro, val) {
        $.cookie('webget_valor_' + nro, val, Webget.optionsCookie);
        Webget.setValores(nro);
    }


};
