//-----------------------------------------------------------------------
// Copyright (C) Motorwebs Corporation. All rights reserved.
//-----------------------------------------------------------------------
// Vehicle Search Class

Type.registerNamespace('Motorwebs.UI');

Motorwebs.UI.VehicleSearch = function(element) {
    Motorwebs.UI.VehicleSearch.initializeBase(this, [element]);

    // elements
    this._title;
    this._conditionWrap = null;
    this._conditionNew = null;
    this._conditionUsed = null;
    this._priceRanges = null;
    this._years = null;
    this._makes = null;
    this._models = null;
    this._submit = null;

    // properties
    this._state = null;
    this._settings = null;
}

Motorwebs.UI.VehicleSearch.prototype = {
    initialize: function() {
        Motorwebs.UI.VehicleSearch.callBaseMethod(this, 'initialize');
        this._hookup();
    },

    dispose: function() {
        $clearHandlers(this._conditionNew);
        $clearHandlers(this._conditionUsed);
        $clearHandlers(this._priceRanges);
        $clearHandlers(this._years);
        $clearHandlers(this._makes);
        $clearHandlers(this._submit);

        Motorwebs.UI.VehicleSearch.callBaseMethod(this, 'dispose');
    },

    // event handlers ////////////////////////////////////////////////////////////////
    _onConditionClick: function(e) {
        this._state.Event = "condition";
        this._state.Condition = e.target.value;
        Widgets.VehicleSearchEvents(this._state, onSuccess, onFailure, 'vehicleSearch');
    },

    _onPriceRangesChange: function(e) {
        this._state.Event = "priceRange";
        this._state.Condition = this._getCondition();
        this._state.PriceRangeSelected = e.target.value;
        Widgets.VehicleSearchEvents(this._state, onSuccess, onFailure, 'vehicleSearch');
    },

    _onYearsChange: function(e) {
        this._state.Event = "year";
        this._state.Condition = this._getCondition();
        this._state.PriceRangeSelected = this._priceRanges.value;
        this._state.YearSelected = e.target.value;
        Widgets.VehicleSearchEvents(this._state, onSuccess, onFailure, 'vehicleSearch');
    },

    _onMakesChange: function(e) {
        this._state.Event = "make";
        this._state.Condition = this._getCondition();
        this._state.PriceRangeSelected = this._priceRanges.value;
        this._state.YearSelected = this._years.value;
        this._state.MakeSelected = e.target.value;
        Widgets.VehicleSearchEvents(this._state, onSuccess, onFailure, 'vehicleSearch');
    },

    _onSubmitClick: function(e) {
        this._state.Event = "submit";
        this._state.Condition = this._getCondition();
        this._state.PriceRangeSelected = this._priceRanges.value;
        this._state.YearSelected = this._years.value;
        this._state.MakeSelected = this._makes.value;
        this._state.ModelSelected = this._models.value;
        Widgets.VehicleSearchEvents(this._state, onSuccess, onFailure, 'vehicleSearch');
    },

    // private methods //////////////////////////////////////////////////////////////
    _getCondition: function() {
        var condition = document.forms[0].vehicleSearchCondition;
        for (var i = 0; i < condition.length; i++) {
            if (condition[i].checked) {
                return condition[i].value;
            }
        }
        return ""
    },

    _fillSelectOneColumn: function(select, list, label, selectedValue) {
        select.options.length = 0;
        select.options[0] = new Option(label, "", true, true);
        
        for (var i = 0; i < list.length; i++) {
            if (selectedValue == list[i])
                select.options[select.options.length] = new Option(list[i], list[i], true, true);
            else
                select.options[select.options.length] = new Option(list[i], list[i], false, false);
        }
        select.disabled = false;
    },

    _fillSelectTwoColumn: function(select, dict, label, selectedValue) {
        select.options.length = 0;
        select.options[0] = new Option(label, "", true, true);
        
        for (var key in dict) {
            if (selectedValue == dict[key])
                select.options[select.options.length] = new Option(key, dict[key], true, true);
            else
                select.options[select.options.length] = new Option(key, dict[key], false, false);
        }
        select.disabled = false;
    },

    _clearSelect: function(select, label) {
        if (select.options != null) {
            select.options.length = 0;
            select.options[0] = new Option(label, "", true, true);
        }
        select.disabled = true;
    },

    _hookup: function() {
        $addHandlers(this._conditionNew, { click: this._onConditionClick }, this);
        $addHandlers(this._conditionUsed, { click: this._onConditionClick }, this);
        $addHandlers(this._priceRanges, { change: this._onPriceRangesChange }, this);
        $addHandlers(this._years, { change: this._onYearsChange }, this);
        $addHandlers(this._makes, { change: this._onMakesChange }, this);
        $addHandlers(this._submit, { click: this._onSubmitClick }, this);

        // set title
        if (this._settings.Title != "Vehicle Search") {
            this._title.innerHTML = this._settings.Title;
        }

        // show condition / default condition
        if (!this._settings.ShowConditionRadioButtons) {
            this._conditionWrap.style.display = "none";
        }
        else if (this._settings.DefaultCondition != "") {
            if (this._settings.DefaultCondition == "U")
                this._conditionUsed.checked = "true";
            else
                this._conditionNew.checked = "false";
        }

        // show price ranges
        if (this._settings.ShowPriceRanges)
            this._fillSelectTwoColumn(this._priceRanges, this._state.PriceRanges, "Price Ranges");
        else
            this._priceRanges.style.display = "none";

        // show years
        if (this._settings.ShowYears)
            this._fillSelectOneColumn(this._years, this._state.Years, "Years");
        else
            this._years.style.display = "none";

        //set makes
        this._fillSelectOneColumn(this._makes, this._state.Makes, "Makes");
    },

    // public methods ////////////////////////////////////////////////////////////////
    onServiceSuccess: function(state) {
        switch (state.Event) {
            case 'condition':
                if (this._settings.ShowPriceRanges) this._fillSelectTwoColumn(this._priceRanges, state.PriceRanges, "Price Ranges");
                if (this._settings.ShowYears) this._fillSelectOneColumn(this._years, state.Years, "Years");
                this._fillSelectOneColumn(this._makes, state.Makes, "Makes");
                this._clearSelect(this._models, "Models");
                break;
            case 'priceRange':
                if (this._settings.ShowYears) this._fillSelectOneColumn(this._years, state.Years, "Years");
                this._fillSelectOneColumn(this._makes, state.Makes, "Makes");
                this._clearSelect(this._models, "Models");
                break;
            case "year":
                this._fillSelectOneColumn(this._makes, state.Makes, "Makes");
                this._clearSelect(this._models, "Models");
                break;
            case "make":
                this._fillSelectOneColumn(this._models, state.Models, "Models");
                break;
            case "submit":
                window.location.href = state.Url;
                break;
        }
    },

    // public properties //////////////////////////////////////////////////////////////
    get_title: function() { return this._title; },
    set_title: function(value) { this._title = value; },

    get_conditionWrap: function() { return this._conditionWrap; },
    set_conditionWrap: function(value) { this._conditionWrap = value; },

    get_conditionNew: function() { return this._conditionNew; },
    set_conditionNew: function(value) { this._conditionNew = value; },

    get_conditionUsed: function() { return this._conditionUsed; },
    set_conditionUsed: function(value) { this._conditionUsed = value; },

    get_priceRanges: function() { return this._priceRanges; },
    set_priceRanges: function(value) { this._priceRanges = value; },

    get_years: function() { return this._years; },
    set_years: function(value) { this._years = value; },

    get_makes: function() { return this._makes; },
    set_makes: function(value) { this._makes = value; },

    get_models: function() { return this._models; },
    set_models: function(value) { this._models = value; },

    get_submit: function() { return this._submit; },
    set_submit: function(value) { this._submit = value; },

    get_state: function() { return this._state; },
    set_state: function(value) { this._state = value; },

    get_settings: function() { return this._settings; },
    set_settings: function(value) { this._settings = value; }
}

Motorwebs.UI.VehicleSearch.registerClass('Motorwebs.UI.VehicleSearch', Sys.UI.Control);