/*

*/

function InstrumentSelector(mediator) {
    this.mediator = mediator;
    this.getAllButton = null;
    this.getStocksButton = null;
    this.getForexButton = null;
    this.getFuturesButton = null;
    this.getOptionsButton = null;

    this.getAllButton = function() { return this.allButton; },
    this.getStocksButton = function() { return this.stocksButton; },
    this.getForexButton = function() { return this.forexButton; },
    this.getFuturesButton = function() { return this.futuresButton; },
    this.getOptionsButton = function() { return this.optionsButton; },

    this.setAllButton = function(button) { this.allButton = button; },
    this.setStocksButton = function(button) { this.stocksButton = button; },
    this.setForexButton = function(button) { this.forexButton = button; },
    this.setFuturesButton = function(button) { this.futuresButton = button; },
    this.setOptionsButton = function(button) { this.optionsButton = button; },


    this.clickAllInstrumentsButton = function() {
        if (this.allButton.get("checked")) {
            this.stocksButton.set("checked", true);
            this.forexButton.set("checked", true);
            this.futuresButton.set("checked", true);
            this.optionsButton.set("checked", true);
            mediator.widgetChanged(globals.INSTRUMET_SELECTOR, this);
        }
    },

    this.clickButton = function(yahooButton) {
        var checkedButton = yahooButton.get("checked");
        if (this.allButton.get("checked")) {
            this.uncheckAllButtons();
            yahooButton.set("checked", true);
        }
        if (!checkedButton) {
            this.allButton.set("checked", false);
        }
        mediator.widgetChanged(globals.INSTRUMET_SELECTOR, this);
    },

    this.uncheckAllButtons = function() {
        this.allButton.set("checked", false);
        this.stocksButton.set("checked", false);
        this.forexButton.set("checked", false);
        this.futuresButton.set("checked", false);
        this.optionsButton.set("checked", false);
    },

   this.setState = function(id, state) {
       // See perl Globals.pm:
       // use constant ALL_INSTRUMENTS_ID  => 'All';
       // use constant STOCKS_INSTRUMENT_ID  => 'Stocks';
       // use constant FOREX_INSTRUMENT_ID   => 'Forex';
       // use constant FUTURES_INSTRUMENT_ID => 'Futures';
       // use constant OPTIONS_INSTRUMENT_ID => 'Options';
       id = id.toLowerCase();
       switch (id) {
           case "all":
               this.allButton.set("checked", state);
               break;
           case "stocks":
               this.stocksButton.set("checked", state);
               break;
           case "forex":
               this.forexButton.set("checked", state);
               break;
           case "futures":
               this.futuresButton.set("checked", state);
               break;
           case "options":
               this.optionsButton.set("checked", state);
               break;
           default:
               break;
       }
   },

    /**
    * 
    * Sets all instruments buttons state.
    *
    * @method setStates
    * @param instrumentsArray [{ name: "All",state: false},{ name: "Stocks",state: false},{ name: "Forex",state: false},{ name: "Futures",state: false},{ name: "Options",state: true}]});
    */
   this.setStates = function(instrumentsArray) {
       for (var i = 0; i < instrumentsArray.length; i++) {
           this.setState(instrumentsArray[i].name, instrumentsArray[i].state)
       }
   },

    this.getButtonState = function(button) {
        return button.get("name").toLowerCase() + "=" + button.get("checked");
    },

    this.getStatesUrl = function() {
        var result =
        this.getButtonState(this.allButton) + "&" +
           this.getButtonState(this.stocksButton) + "&" +
              this.getButtonState(this.forexButton) + "&" +
                 this.getButtonState(this.futuresButton) + "&" +
                    this.getButtonState(this.optionsButton);
        return result;

    }

}
