var currentSelect;
var lastSelect;
var customSelectionArray = [];

String.prototype.escape = function (){
	out = this.replace(/\\/g, "\\\\");
	out = out.replace(/'/g, "\\'");
	out = out.replace(/"/g, '\\"');
	out = out.replace(/\r/g, '\\r');
	out = out.replace(/\n/g, '\\n');
	out = out.replace(/<\//g, '<\/');
	return out;
}

function left(obj) {
      var x = 0;
      while(obj) {
            x += obj.offsetLeft;
            obj = obj.offsetParent;
      }
      return x;
}

function mytop(obj) {
      var y = 0;
      while(obj) {
            y += obj.offsetTop;
            obj = obj.offsetParent;
      }
      return y;
}

function CustomSelect(id){

    if (currentSelect == undefined)
        currentSelect = id;
    if (currentSelect != id) {
        select = createSelectObject(currentSelect);
        if(select.isSelect()) select.hideOptions();
        currentSelect = id;
    }
    lastSelect = id;
    this.id = id;
    this.element = document.getElementById(id);
    this.selectAreaElement = document.getElementById(id + "_selectArea");
    this.realoptionsElement = document.getElementById(id + "_optionsBox");
	if(!document.getElementById(this.id + "_optionsBoxInBody")){
		var InBodyOptionsElement = document.createElement('div');
		var UlElement = document.createElement('ul');
		UlElement.id = this.id + "_ulInBody";
		UlElement.innerHTML = document.getElementById(id + "_ul").innerHTML;
		UlElement.onmouseover = document.getElementById(id + "_ul").onmouseover;
		InBodyOptionsElement.appendChild(UlElement);

		InBodyOptionsElement.style.position = 'absolute';
	    InBodyOptionsElement.className = 'optionsDivInvisible';
	    InBodyOptionsElement.id = id + "_optionsBoxInBody";

        document.body.appendChild(InBodyOptionsElement);

	}

    this.UlElement = document.getElementById(this.id + "_ulInBody");
	this.items = this.UlElement.getElementsByTagName('a');
    this.optionsElement = document.getElementById(id + "_optionsBoxInBody");
    this.selectBoxElement = document.getElementById(id + "_selectBox");
	this.hiddenInput = document.getElementById(this.id + "_hiddenInput");
    this.enabled = true;
 	this.lastSelOption = -1;
 	this.lastHLOption = -1;
	this.lastOption = -1;
	this.lastKey = '';
	
	this.getOptionOffsetHeight = function(optionIndex)
	{
		var result = 0
		for (cnt = 0; cnt<optionIndex; cnt++) {
			result = result + this.items[cnt].offsetHeight;
		}
		return result;
	};
	this.isOpened = function()
	{
		return (this.optionsElement.className == "optionsDivVisible");
	};
	this.getLastHLOption = function()
	{
		this.items = this.UlElement.getElementsByTagName('a');
		if (this.lastHLOption < 0) this.lastHLOption = this.element.selectedIndex;
		if (typeof this.items[this.lastHLOption] === 'undefined') this.lastHLOption = this.element.selectedIndex;
		return this.lastHLOption;
	}
	this.highlightOption = function(i)
	{
		if (this.getLastHLOption() !== i) this.items[this.lastHLOption].className = "";
		this.items[i].className = 'selectLite';
		this.lastHLOption = i;
		var _itop = this.getOptionOffsetHeight(i);
		if (_itop > (this.UlElement.scrollTop + this.UlElement.offsetHeight - this.items[i].offsetHeight)) {
			this.UlElement.scrollTop = _itop - this.UlElement.offsetHeight + this.items[i].offsetHeight + 3;
		}
		else if (_itop < (this.UlElement.scrollTop - this.items[i].offsetHeight)) {
			this.UlElement.scrollTop = _itop;
		}
	}
	this.highlightNextOption = function()
	{
		this.getLastHLOption();
		if (this.lastHLOption == this.element.options.length - 1) return;
        this.items[this.lastHLOption].className = "";
		this.lastHLOption ++;
		this.highlightOption(this.lastHLOption);
	};
	this.highlightPrevOption = function()
	{
		this.getLastHLOption();
		if (this.lastHLOption == 0) return;
        this.items[this.lastHLOption].className = "";
		this.lastHLOption --;
		this.highlightOption(this.lastHLOption);
	};
	this.showOptions = function(){
    	hideSelectOptions();
    	customSelectionArray[this.id] = this.id;

    	if (this.enabled == true) {
    		if (this.selectAreaElement.clientWidth > 0) {
    			var leftPos = left(this.selectAreaElement);
                this.optionsElement.style.width = (this.selectAreaElement.clientWidth - 1) + 'px';
			    this.optionsElement.style.left = left(this.selectAreaElement) + 'px';
			    this.optionsElement.style.top = mytop(this.selectAreaElement) + this.selectAreaElement.clientHeight + 'px';
                this.optionsElement.className = "optionsDivVisible";
                this.highlightOption(this.element.selectedIndex);
            } else {
                idSelect = this.id;
                removeSelectObject(this.id);
                objSel = createSelectObject(idSelect);
                objSel.showOptions();
            }
        }
    };
    this.hideOptions = function(){
        if (this.optionsElement) {
            this.optionsElement.className = "optionsDivInvisible";
        }
    };
    this.selectOption = function(optionKey, optionValue){
        optionElement = document.getElementById(this.id + '_option_' + optionKey);
        if (optionElement) {
            this.items = this.UlElement.getElementsByTagName('a');
			this.items[this.element.selectedIndex].className = "";
			optionElement.selected = true;
			this.items[this.element.selectedIndex].className = "selectLite";
            if(this.element.onchange) {
                this.element.onchange();
            }
            this.selectBoxElement.innerHTML = optionValue;
        }
    };
    this.switchVisibleOptions = function(){
        if(this.optionsElement.className == "optionsDivVisible"){
			this.hiddenInput.blur();
            this.hideOptions();
        } else{
			this.hiddenInput.focus();
            this.showOptions();
        }
    };
    this.addOption = function(option){
		option.id = this.id + '_option_' + option.value;
		optionText = option.text;
        this.element.appendChild(option);
        var elementNum = this.element.options.length - 1;
        newElement = document.createElement('li');
        newElement.innerHTML = '<a href="javascript:void(0)"' + 'onclick="var select = createSelectObject(\'' + this.id + '\');select.selectOption(\'' + option.value + '\', \'' + optionText.escape() + '\'); select.hideOptions();return false;"' +
			' onmouseover="createSelectObject(\'' + this.id + '\');obj' + this.id + '.highlightOption(\'' + elementNum + '\');return false;">' + optionText + '</a>';
		this.UlElement.appendChild(newElement);
        if (this.element.options.length == 1){
            this.selectOption(option.value, optionText);
        }
    };
    this.getValue = function(){
        return this.element.options[this.element.selectedIndex].value;
    };
    this.getText = function(){
        return this.element.options[this.element.selectedIndex].text;
    }
    this.show = function(){
    	if (this.selectAreaElement) {
            this.selectAreaElement.style.display = "block";
        }
    };
    this.hide = function(){
        this.hideOptions();
        this.selectAreaElement.style.display = "none";
    };
    this.removeOptions = function(){
        this.element.options.length = 0;
        this.UlElement.innerHTML = '';
        this.selectBoxElement.innerHTML = "";
    };
    this.isSelect = function(){
        if (document.getElementById(this.id + "_selectArea")) return true; else return false;
    };

    this.enable = function(){
        this.enabled = true;
        this.selectAreaElement.className = 'selectArea';
    };

    this.disable = function(){
        this.enabled = false;
        this.selectAreaElement.className = 'selectAreaGrey';
    };
	
	this.searchOption = function(){
		this.items = this.UlElement.getElementsByTagName('a');
		var key = this.hiddenInput.value;
		key = key.toLowerCase();
		if (key != this.lastKey) this.lastSelOption = -1;
		if (this.lastOption == this.lastSelOption) this.lastSelOption = -1;
		this.lastKey = key;
		var done = false;
		if (key) {
			for (var i = 0; i < this.items.length; i++) {
				var firstLetter = this.items[i].innerHTML.substr(0, 1);
				firstLetter = firstLetter.toLowerCase();
				if (firstLetter == key && !done && this.lastSelOption < i) {
					this.items[i].className = 'selectLite';
					this.lastHLOption = i;
					var _itop = this.getOptionOffsetHeight(i);
					if (_itop > (this.UlElement.scrollTop + this.UlElement.offsetHeight - this.items[i].offsetHeight)) {
						this.UlElement.scrollTop = _itop - this.UlElement.offsetHeight + this.items[i].offsetHeight + 3;
					}
					else if (_itop < (this.UlElement.scrollTop - this.items[i].offsetHeight)) {
						this.UlElement.scrollTop = _itop;
					}
					done = true;
					this.lastSelOption = i;
					this.lastOption = i;
				}
				else {
					this.items[i].className = '';
					if (firstLetter == key) this.lastOption = i;
				}
			}
		}
	}
	
	this.eraseSearch = function(){
		if (this.lastSelOption >= 0) this.items[this.lastSelOption].className = '';
		this.lastSelOption = -1;
	}
	
	this.clickOnFound = function(){
	
		if (this.getLastHLOption() >= 0) this.items[this.lastHLOption].onclick();
	}
	
	this.scrollOptionsTo = function(percent)
	{
		if(this.optionsElement.className == "optionsDivVisible"){
			this.UlElement.scrollTop = this.optionsElement.scrollHeight*percent/100;
		}
	}
	
}

var _global = this;

function createSelectObject(id)
{
    if (document.getElementById(id + "_selectArea")) {
        if (_global['obj'+ id] === undefined) {
            _global['obj'+ id] = new CustomSelect(id);
        }else{
            if(_global['obj'+ currentSelect]){
    	        currentSelect = id;
    		}
    	}
        return _global['obj'+ id];
	}
    return false;
}

function removeSelectObject(id)
{
    _global['obj'+ id] = undefined;
}

function hideSelectOptions()
{
    for(name in customSelectionArray) {
        obj = createSelectObject(name);
        if (obj !== false) {
            obj.hideOptions();
        }
    }
}
