var API = {
    url: 'http://contacts.tn.ru/ajax.asp?function=%action%&type=%type%&country=%country%&subtype=-1&site=shinglas',
    search: 'http://contacts.tn.ru/Default.asp?action=search&site=shinglas&country=%country%&city=%city%&type=%type%',
   
    shinglasAPI: function() {
        var self = this;
        
        var country = $('#f-dealers-countries');
        var city = $('#f-dealers-cities');
        var type = $('#f-dealers-type');
        var submit = $('#f-dealers-submit');
        
        if (!(country.length && city.length && type.length && submit.length))
            return;
        
        var activateSubmit = function()
        {
            submit.addClass('disabled');
            if (country.val() != '-1')
                submit.removeClass('disabled');
        };
        /*
        type.change(function(){
            var url = self.url.replace('%country%', country.val()).replace('%type%', type.val());
            self.loadOptions(self, url.replace('%action%', 'get_countries'), country, true);
            self.loadOptions(self, url.replace('%action%', 'get_cities'), city, true);
            activateSubmit();
        }).change();
        */
        var url = self.url.replace('%country%', country.val()).replace('%type%', type.val());
        self.loadOptions(self, url.replace('%action%', 'get_countries'), country, true);
        self.loadOptions(self, url.replace('%action%', 'get_cities'), city, true);
        
        country.change(function(){
            var url = self.url.replace('%country%', country.val()).replace('%type%', type.val());
            self.loadOptions(self, url.replace('%action%', 'get_cities'), city, true);
            activateSubmit();
        });
        
        city.change(function(){activateSubmit(); });
        
        submit.click(function(){
            if ($(this).hasClass('disabled'))
                return false;
            var url = self.search.replace('%country%', country.val()).replace('%type%', type.val()).replace('%city%', city.val());
            self.request({url: url, needParce: 1}, function(data){ $('#dealers').html(data) }, true);
        });
        
        activateSubmit();
    },
    loadOptions: function(self, url, conteiner)
    {
        self.request(
            {
                url: url
            }, 
            function(data){
                if (data.isError != false) return;
                conteiner.empty().append('<option value="-1"></option>').change();
                $.each(data.list, function(){
                    conteiner.append('<option value="'+this.value+'">'+this.text+'</option>');
                });
            },
            false
        );
    },
    request: function(params, callback, isHtml)
    {
        url = '/gateway.php';
        
        $.get(url, params, function(res){
            if (typeof callback != 'function')
                return;
            
            if (res && isHtml){
                callback(res);
                return;
            }
            
            eval('var data = '+res+'');
            callback((typeof data == 'object') ? data : {} );
        });
    }
}
$(document).ready(function(){
    (function() {
        this.shinglasAPI();
    }).call(API);
});
