//Copyright 2007 Roman Ardern-Corris
//Licenced to 3legs.com


/*
    ligoSelect
*/

extend(ligoSelect, ligoDSVisualiser);

function ligoSelect (paramsObj) {

    this.dispField;     //Name of the field to display
    this.valueField;    //Field to use as option value=""
    this.display;       //struct; parameters are "multiple", "size" (both passed down to the html output),
                        //        and "submitAllVisible" (useful for dynamically generated lists)

    ligoSelect.superclass.call(this, paramsObj);

}

//#####################################################################################################################

ligoSelect.prototype.render = function () {

    var select = document.createElement('select');
    select.id = this.instanceName;
    select.name = this.instanceName;

    if (this.display.multiple) {
        select.multiple = true;
    }
    if (this.display.size) {
        select.size = this.display.size;
    }

    this.select = select;

    for (var i=0; i < this.Dataset.data.length; i++) {
        this.addOption(
                        this.Dataset.field_by_row_and_name(i, this.dispField),
                        this.Dataset.field_by_row_and_name(i, this.valueField),
                        i
                      );
    }

    // Check for IE
    if ( /*@cc_on!@*/ false ) {
        // Do this the bad way - grab the select box, do a string-replace on it and set div.innerhtml to it
        var selecthtml = this.select.outerHTML;
        document.getElementById(this.div).innerHTML = selecthtml.replace(
                                                            '<SELECT',
                                                            '<SELECT onChange="'+this.instanceName+'.onChange()"'
                                                    );
    }
    // Standards-compliant method using event handlers
    else {
        var obj_name = this.instanceName;
        this.select.addEventListener('change', function() { window[obj_name].onChange(); }, false);

        document.getElementById(this.div).textContent = '';
        document.getElementById(this.div).appendChild(this.select);
    }

    if (this.initialNoRowSel) {
        //no row selected
        this.Dataset.setCurrentRow(-1);
    }
    else {
        //select the first row
        this.Dataset.setCurrentRow(0);
    }
}

//#####################################################################################################################

ligoSelect.prototype.addOption = function(name, value) {
    var option = document.createElement('option');

    if ( option.textContent ) {
        option.textContent = name;
    }
    else { // This is required for IE6 and 7, which are useless
        var text = document.createTextNode(name);
        option.appendChild(text);
    }

    if (this.valueField) {
        option.value = value;
    }

    this.select.appendChild(option);
}

//#####################################################################################################################

ligoSelect.prototype.onChange = function() {
    this.onRowClick( document.getElementById(this.instanceName).selectedIndex );
}

//#####################################################################################################################

ligoSelect.prototype.selectedValue = function () {
    //returns the value of the currenly selected option
    //FIXME: this should really link tighter to the parent dataset
    var sel = document.getElementById(this.instanceName);

    return sel.options[sel.selectedIndex].value;

}

//#####################################################################################################################
