/**
 * Select management.
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @version 1.0
 * @package Resource it Modules
 * @copyright Resource it ApS
 *
 **/


/*===================================
  CLASS Select
  ===================================*/

    /**
     * Select Constructor.
     * @param string label      - the initial Select label
     * @param string checked    - [optional] true if checked else false - default it's unchecked
     **/
    function Select () {
        this.node = document.createElement("select");

        //set style
        this.node.style.fontFamily = resExt.theme.widgets.globals.fontFamily;
        this.node.style.fontSize = resExt.theme.widgets.globals.fontSize;

        //contains the current selected Option object
        this.selected = null;

        resExt.addEventHandler(this.node,"change",function() {
            this.select(this.node.selectedIndex);
            if ( this.onchange !== undefined ) this.onchange();
        }.bind(this));

    }//Select

    /**
     * add an option to the select box. if index value
     **/
    Select.prototype.addOption = function(text, value, index) {
        this.node.options[this.node.options.length] = new Option(text,value);
        if ( index !== undefined ) {

        }//if
    }//addOption

    Select.prototype.removeOption = function(index) {

    }//select

    /**
     * Set the chosen index as selected option - if no argument is supplied index 0 is selected
     **/
    Select.prototype.select = function(index) {
        this.node.options[index].selected = true;
        this.selected = this.node.options[index];
    }//setSelected

    /**
     * Change order by moving the selected index to new index.
     **/
    Select.prototype.chOrder = function(oldIndex, newIndex) {

    }//prototype

    /**
     * Get index for selected value. Note that if multiple options share same value, the first one is returned
     **/
    Select.prototype.getIndex = function(value) {
        for ( var c = 0; c < this.node.options.length; c++ ) {
            if ( this.node.options[c].value == value ) return c;
        }//for
        return null;
    }//getIndex

    Select.prototype.setId = function(id) {
        this.node.id = id;
    }//setId
    
    Select.prototype.enable = function() {
        this.node.disabled = false;
    }//enable

    Select.prototype.disable = function() {
        this.node.disabled = "disabled";
    }//disable

    

/*===================================
  Events
  ===================================*/

    /**
     * This events occours when ever the user selects a new option value.
     **/
    Select.prototype.change = function(action) {
        this.onchange = action;
    }//checked


