(function($){
    $.fn.countryBox = function (opts){
        opts = $.extend({}, $.fn.countryBox.defaults, opts);

        return this.each(function(){
            new CountryBox(this, opts);
        });
    };
    var CountryBox = function(context, opts) {
        $('#'+opts.countrySelect).change(function(){
            $('#'+opts.regionHolder).addClass('hides');
            $('#'+opts.filialsHolder).addClass('hides');
            $('#'+opts.citySelectHolder).addClass('hides');
            $('#'+opts.cityInputHolder).addClass('hides');
            $('#'+opts.adSourceHolder).addClass('hides');
            $('#'+opts.adListHolder).addClass('hides');
            if ($('#'+opts.countrySelect).val()!=''){
                process($('#'+opts.countryLoader),{
                    xaction: 'ajaxCountryBox',
                    field: 'country',
                    country: $('#'+opts.countrySelect).val()
                });
            }
        });
        $('#'+opts.regionSelect).change(function(){
            $('#'+opts.filialsHolder).addClass('hides');
            $('#'+opts.citySelectHolder).addClass('hides');
            $('#'+opts.cityInputHolder).addClass('hides');
            $('#'+opts.adSourceHolder).addClass('hides');
            $('#'+opts.adListHolder).addClass('hides');
            if ($('#'+opts.regionSelect).val()!=''){
                process($('#'+opts.regionLoader),{
                    xaction: 'ajaxCountryBox',
                    field: 'region',
                    country: $('#'+opts.countrySelect).val(),
                    region: $('#'+opts.regionSelect).val()
                });
            }
        });
        $('#'+opts.citySelect).change(function(){
            if ($('#'+opts.citySelect).val()=='!!!other!!!'){
                $('#'+opts.cityInput).val('');
                $('#'+opts.cityInputHolder).removeClass('hides');
                $('#'+opts.adSourceHolder).addClass('hides');
                $('#'+opts.adListHolder).addClass('hides');
            } else {
                $('#'+opts.cityInputHolder).addClass('hides');
                $('#'+opts.cityInput).val($('#'+opts.citySelect).val());
                process($('#'+opts.cityLoader),{
                    xaction: 'ajaxCountryBox',
                    field: 'city',
                    country: $('#'+opts.countrySelect).val(),
                    region: $('#'+opts.regionSelect).val(),
                    city: $('#'+opts.citySelect).val()
                });
            }
        });
        $('#'+opts.cityInput).focus(function(){
            $('#'+opts.adSourceHolder).addClass('hides');
            $('#'+opts.adListHolder).addClass('hides');
        });
        $('#'+opts.cityInput).blur(function(){
            $('#'+opts.adListHolder).addClass('hides');
            if ($('#'+opts.cityInput).val()!=''){
                process($('#'+opts.cityLoader),{
                    xaction: 'ajaxCountryBox',
                    field: 'city',
                    country: $('#'+opts.countrySelect).val(),
                    region: $('#'+opts.regionSelect).val(),
                    city: $('#'+opts.cityInput).val()
                });
            }
        });
        $('#'+opts.adSourceSelect).change(function(){
            $('#'+opts.adListHolder).addClass('hides');
            if ($('#'+opts.adSourceSelect).val()!=''){
                process($('#'+opts.adSourceLoader),{
                    xaction: 'ajaxCountryBox',
                    field: 'adSource',
                    country: $('#'+opts.countrySelect).val(),
                    region: $('#'+opts.regionSelect).val(),
                    city: $('#'+opts.cityInput).val(),
                    adSource: $('#'+opts.adSourceSelect).val()
                });
            }
        });
        function process(loader, args){
            loader.addClass('loading');
            $('#'+opts.countrySelect).attr('disabled',true);
            $('#'+opts.regionSelect).attr('disabled',true);
            $('#'+opts.citySelect).attr('disabled',true);
            $('#'+opts.cityInput).attr('disabled',true);
            $('#'+opts.adSourceSelect).attr('disabled',true);
            $.ajax({
                type: 'POST',
                dataType: 'text',
                data: args,
                success: function(html){
                    eval('data='+html+';');
                    if (data.phoneCode){
                        $('#'+opts.phoneCode).val('+'+data.phoneCode);
                    }
                    if (data.regions){
                        updateSelect(opts.regionSelect, data.regions, false);
                        $('#'+opts.regionHolder).removeClass('hides');
                    } else {
                        if (args.field=='country'){
                            $('#'+opts.regionHolder).addClass('hides');
                        }
                    }
                    if (data.filials){
                        $('#'+opts.filialsBox).html('');
                        for(id in data.filials){
                            $('#'+opts.filialsBox).append('<li>'+data.filials[id].value+'</li>');
                        }
                        $('#'+opts.filialsHolder).removeClass('hides');
                    } else {
                        if (args.field=='region' || args.field=='country'){
                            $('#'+opts.filialsBox).html('');
                            $('#'+opts.filialsHolder).addClass('hides');
                        } else {
//                            $('#'+opts.filialsHolder).addClass('hides');
                        }
                    }
                    if (data.cities){
                        updateSelect(opts.citySelect, data.cities, true);
                        $('#'+opts.cityInput).val($('#'+opts.citySelect).val());
                    }
                    if (data.showCities && (data.showCities==1)){
                        $('#'+opts.citySelectHolder).removeClass('hides');
                        
                    } else{
                        if ((args.field=='region') || (args.field=='country')) {
                            $('#'+opts.citySelectHolder).addClass('hides');
                            $('#'+opts.cityInputHolder).addClass('hides');
                        } else {
  //                          $('#'+opts.citySelectHolder).addClass('hides');
                        }
                    }
                    if (data.adSources){
                        if (args.field!='adSource'){
                            updateSelect(opts.adSourceSelect, data.adSources, false);
                            $('#'+opts.adSourceHolder).removeClass('hides');
                        }
                    } else {
//                        $('#'+opts.adSourceHolder).addClass('hides');
                    }
                    if (data.adList){
                        updateSelect(opts.adListSelect, data.adList, false);
                        $('#'+opts.adListHolder).removeClass('hides');
                    } else {
//                        $('#'+opts.adListHolder).addClass('hides');
                    }
                    loader.removeClass('loading');
                    $('#'+opts.countrySelect).removeAttr('disabled');
                    $('#'+opts.regionSelect).removeAttr('disabled');
                    $('#'+opts.citySelect).removeAttr('disabled');
                    $('#'+opts.cityInput).removeAttr('disabled');
                    $('#'+opts.adSourceSelect).removeAttr('disabled');
                }
            });
        }
        function insertOption(select, value, text){
            newOption = document.createElement("OPTION");
            newOption.text = text;
            newOption.value = value;
            select.options.add(newOption);
        }
        function updateSelect(id, options, isCity){
            var select = document.getElementById(id);
            select.innerHTML = '';
            if (!isCity) insertOption(select, '', ' ');
            for (optId in options){
                insertOption(select, options[optId].value, options[optId].text);
            }
            if (isCity) insertOption(select, '!!!other!!!', ' --- Другой --- ');
        }
    }
    $.fn.countryBox.defaults = {
        phoneCode: 'phoneCode',
        countrySelect: 'countrySelect',
        countryLoader: 'countryLoader',
        regionSelect: 'regionSelect',
        regionHolder: 'regionHolder',
        regionLoader: 'regionLoader',
        filialsHolder: 'filialsHolder',
        filialsBox: 'filialsBox',
        citySelect: 'citySelect',
        cityInput: 'cityInput',
        citySelectHolder: 'citySelectHolder',
        cityInputHolder: 'cityInputHolder',
        cityLoader: 'cityLoader',
        adSourceSelect: 'adSourceSelect',
        adSourceHolder: 'adSourceHolder',
        adSourceLoader: 'adSourceLoader',
        adListSelect: 'adListSelect',
        adListHolder: 'adListHolder'
    }
})(jQuery);