var Search = {
    options : {
        continent : 0,
        country   : 0,
        state     : 0,
        city      : 0
    },

    init : function(options) {
        Object.extend(this.options, options || {});
        if (0 != this.options.continent) {
            this.changeContinent();
            /*if (0 != this.options.country) {
                this.changeCountry();
                if (0 != this.options.state) {
                    this.changeState();
                }
            }*/
        }
        this.initObservers();
    },

    initObservers : function() {
        try {
            $('continent').onchange = function() {
                this.reset('continent');
                this.options.continent = $('continent').value;
                this.changeContinent();
            }.bind(this),

            $('country').onchange = function() {
                this.reset('country');
                this.options.country = $('country').value;
                this.changeCountry();
            }.bind(this),

            $('state').onchange = function() {
                this.reset('state');
                this.options.state = $('state').value;
                this.changeState();
            }.bind(this)
        } catch (e) {};
        try {
            $('searchForm').onsubmit = function() {
                this.search();
                return false;
            }.bind(this);
        } catch (e) {};
    },

    reset : function(type) {

        switch(type) {
            case 'continent' :
                resetOptions = ['country', 'state', 'city'];
                break;

            case 'country' :
                resetOptions = ['state', 'city'];
                break;

            case 'state' :
                resetOptions = ['city'];
                break;
        }

        resetOptions.each(function(i) {
            this.options[i] = 0;
            this.removeLiOptions(i);
        }.bind(this));
    },

    changeContinent : function() {

        new Ajax.Request('/ajax/forms/fill-form/?type=continent',
        {
            parameters : this.options,
            onComplete : function(transport) {
               this.fillForm(transport, this.changeCountry());
            }.bind(this)
        });

        return false;
    },

    changeCountry : function() {

        new Ajax.Request('/ajax/forms/fill-form/?type=country',
        {
            parameters : this.options,
            onComplete : this.fillForm
        });
        return false;
    },

    changeState : function() {

        new Ajax.Request('/ajax/forms/fill-form/?type=state',
        {
            parameters : this.options,
            onComplete : this.fillForm
        });
        return false;
    },

    search : function() {
        var values = [];
        ['continent', 'country', 'state', 'city'].each(function(section) {
            var _index = $(section).options.selectedIndex;
            if (_index > 0) {
                var text = ($(section).options[_index].text);
                values.push(text.replace(/\s+/g, '-') + '-' + $(section).value);
            }
        });
        var _section = values.join('/');
        if (!_section) {
            _section = 'worldwide';
        }

        if ($('searchForm').action.indexOf('/search') != -1) {
            $('searchForm').action = '/escort/' + _section + '/1';
        } else if ($('searchForm').action.indexOf('/advanced-search') != -1) {
            $('searchForm').action = '/advanced-search/' + _section + '/1';
        }
        $('searchForm').submit();
        return false;
    },

    fillForm : function(transport, callback) {
        var obj = transport.responseJSON || {};

        for(var i in obj) {
            // clean drop downs
            Search.removeLiOptions(i);

            (obj[i].each(function(j) {
                option = new Option(j.title, j.id);
                if(Search.options[i] == j.id) {
                    option.selected = "selected";
                }
                $(i).options.add(option);
            }));
        }

        callback.apply(callback);
        return;
    },

    removeLiOptions : function(id) {
        $(id).value=0;
        ($(id).childElements()).each(function(option) {
            option.remove();
        });
        $(id).options.add(new Option('Any', 0));
    }
}