//////////////////////////////////////////////////////////////////////////////
//	jaxselection.js
//
//	Description:
//		Selection control base class.
//////////////////////////////////////////////////////////////////////////////
Jax.Widgets.Selection = function() {
    var m_onchange = function(){};
    var m_this = this;

    // Declare class and set instance.
    Jax.oop.declare(Jax.Widgets.Selection, Jax.Widgets.Input);
    this.setInstance();	
    
    //////////////////////////////////////////////////////////////////////////
    // initialize
    //////////////////////////////////////////////////////////////////////////
    this.initialize = function(args) {
        if (args.length > 0) {
            m_this = this;
            this.name = args[0];
            this.parent = args[1];
            this.items = this.getItems(this.name, this.parent);
            
            // Set the element.
            this.element = this.items.itemAt(0);
            
            // Set onChange handler.
            this.element.onchange = new Delegate(this, onChange);
        }
    };

    //////////////////////////////////////////////////////////////////////////
    //  add(element)
    //////////////////////////////////////////////////////////////////////////
    this.add = function(text, value) {
        this.element.options.add(new Option(text, value));
    };

    //////////////////////////////////////////////////////////////////////////
    //  clear()
    //////////////////////////////////////////////////////////////////////////
    this.clear = function() {
        this.removeAll();
    };

    //////////////////////////////////////////////////////////////////////////
    //	enable(state) :  enables selection.
    //////////////////////////////////////////////////////////////////////////
    this.enable = function(state) {
        this.enabled = state;
        this.items.forEach(function(item) {
            item.disabled = !state;
        });
    };

    //////////////////////////////////////////////////////////////////////////
    //	getSelectedAt
    //////////////////////////////////////////////////////////////////////////
    this.getSelectedAt = function() {
        if (!this.element.multiple) {
            return this.element.selectedIndex;
        }
        else {
            var selectedAt = new Array()
            this.element.options.forEach(function(item, index) {
                if (item.selected) {
                    selectedAt.push(ii);
                }
            });
            return (selectedAt.length > 0) ? selectedAt : -1;
        }
    };

    //////////////////////////////////////////////////////////////////////////
    //	getLength
    //////////////////////////////////////////////////////////////////////////
    this.getLength = function() {
        return this.element.options.length;
    };

    //////////////////////////////////////////////////////////////////////////
    //  removeAll
    //////////////////////////////////////////////////////////////////////////
    this.removeAll = function() {
        this.element.options.length = 0;
    };

    //////////////////////////////////////////////////////////////////////////
    //	removeAt(index)
    //////////////////////////////////////////////////////////////////////////
    this.removeAt = function(index) {
        if ( document.all ) {
            this.element.options.remove(index);
        }
        else if (document.getElementById) {
            this.element.options[index] = null;
        }
    };

    //////////////////////////////////////////////////////////////////////////
    //  getSelectedIndex
    //////////////////////////////////////////////////////////////////////////
    this.getSelectedIndex = function() {
        return this.element.selectedIndex;
    };
    
    //////////////////////////////////////////////////////////////////////////
    //  setSelectedIndex
    //////////////////////////////////////////////////////////////////////////
    this.setSelectedIndex = function(index) {
        this.element.selectedIndex = index;
    };
    
    //////////////////////////////////////////////////////////////////////////
    //  changeHandler
    //////////////////////////////////////////////////////////////////////////
    this.changeHandler = function(handler) {
        checkHandler(handler);
        m_onchange = handler;
    };

    //////////////////////////////////////////////////////////////////////////
    //	getText
    // 
    //	Usage:
    //      Text()              :       gets selected value.
    //      Text(index)     :	gets value from index position.
    //////////////////////////////////////////////////////////////////////////
    this.getText = function() {
        switch (arguments.length) {
            case 0:
                return this.element.options[this.element.selectedIndex].text;

            case 1:
                return this.element.options[arguments[0]].text;

            default:
                throw new Exception(this, "invalid arguments");
        }
    };
    
    //////////////////////////////////////////////////////////////////////////
    //	setText
    //////////////////////////////////////////////////////////////////////////
    this.setText = function(index, value) {
        return this.element.options[index].text = value;
    };

    //////////////////////////////////////////////////////////////////////////
    //	getValue
    // 
    //	Usage:
    //		Value()	    :	gets selected value
    //		Value(index)    :	gets value from index position.
    //////////////////////////////////////////////////////////////////////////
    this.getValue = function() {
        switch (arguments.length) {
            case 0:
                return this.element.options[this.element.selectedIndex].value;

            case 1:
                return this.element.options[arguments[0]].value;

            default:
                throw new Exception(this, "invalid arguments");
        }
    };
    
    //////////////////////////////////////////////////////////////////////////
    //  setValue(index, value)
    //////////////////////////////////////////////////////////////////////////
    this.setValue = function(index, value) {
        return this.element.options[index].value = value;
    };

    //////////////////////////////////////////////////////////////////////////
    // Initialize instance data.
    //////////////////////////////////////////////////////////////////////////
    this.initialize(arguments);
    
    //////////////////////////////////////////////////////////////////////////
    // onChange()
    //////////////////////////////////////////////////////////////////////////
    function onChange() {
        m_onchange(m_this);
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Helpers.
    //////////////////////////////////////////////////////////////////////////
    function checkHandler(handler) {
        if (!Jax.js.isFunction(handler))
            throw new Error("handler is not a function type");
    }
};