
WtxSelect.prototype.onvaluechange = new Function(); 


WtxSelect.prototype.optionContent = function(){
    return this._content.innerHTML;
}

WtxSelect.prototype.value = function(){
    return this.currentValue;
}

WtxSelect.prototype.clearOptions = function(){
    while (this.optionsTable.rows.length){
        this.optionsTable.deleteRow(0);        
    }
}


WtxSelect.prototype.content = function(){
    return this._content.innerHTML;
}

WtxSelect.prototype.setOptionWidth = function(width){
    this.optionsTable.width = width;
}
WtxSelect.prototype.choose = function(item, value){

    this._content.innerHTML = item.innerHTML;
    this.currentValue = value;
    
    this.onvaluechange();
    
}
WtxSelect.prototype.show = function(){
    offset = Position.cumulativeOffset(this._select);
    this.optionsDiv.style.top = offset[1] + Element.getHeight(this.selectAlias) + 'px';
    this.optionsDiv.style.left = offset[0] + 'px';
    this.optionsDiv.style.display = 'block'; 
    this.trigger = true;  
}
WtxSelect.prototype.hide = function(){
    if (!this.trigger){
        this.optionsDiv.style.display = 'none';
    }
    this.trigger = false;
  
}

WtxSelect.prototype.addOption = function(option, value){
    var row = this.optionsTable.insertRow(this.optionsTable.rows.length);
    var td = document.createElement('td');
    td.className = 'SelectOption';
    td.innerHTML = option;
    td.onclick = new Function (this.selectAlias + '.choose(this, ' + value + ');');
    row.appendChild(td);
    if (this.isFirstOption){
        this.isFirstOption = false;
        td.onclick();
    }
}

WtxSelect.prototype.initOptions = function(){
    this.optionsDiv = document.createElement('div');
    this.optionsDiv.style.display = 'none';
    this.optionsDiv.style.zIndex = 1;
    this.optionsDiv.style.position = 'absolute';	
    
    this.optionsTable = document.createElement('table');
    this.optionsTable.cellSpacing = 0;
    this.optionsTable.cellPadding = 0;
    this.optionsTable.className = 'Options';
    this.optionsDiv.appendChild(this.optionsTable);
    
    document.body.appendChild(this.optionsDiv);      
}

function WtxSelect(select, content){
    this._content = $(content);
    this.contentAlias = content;
    this._select = $(select);
    this.selectAlias = select;
    this.trigger = false;
    this.currentValue = '';
    this.isFirstOption = true;
    this.initOptions();
    document.body.onclick = new Function(this.selectAlias + '.hide()');
}
