/*
 * ComboBox
 */
function ComboOption(text, param, img) {
	this.text = text;
	this.param = param;
	this.img = img;
}

function IsFirefox() {
if(navigator.userAgent.indexOf("Firefox")!=-1){
var versionindex=navigator.userAgent.indexOf("Firefox")+8
if (parseInt(navigator.userAgent.charAt(versionindex))>=1)
return true;
}
else {
return false
}
}
 

function ComboBox(elementId, entries, handler,width,height,side) {
	var element = document.getElementById(elementId);
    if (element==null) return;
	element.combobox = this;
	this.element = element;
	this.handler = handler;
    this.width=width;
    this.height=height;
    this.side=side;
	var body = getDocumentBody();
	
	var oThis = this;
	if (!IsFirefox()) {
		this._outsideClickEvent = function(e) {
			oThis.outsideClickEvent(e);
		};
		addEvent(document, "click", this._outsideClickEvent);
	}
	
	this._showPopupEvent = function(e) {
		oThis.showPopupEvent(e);
	};
	addEvent(element, "click", this._showPopupEvent);
	this._selectItemEvent = function(e) {
			oThis.selectItemEvent(e);
	};
	this._onMouseOverEvent = function(e) {
		var el = getEventElement(e);
		el.className = "hover";
	};
	this._onMouseOutEvent = function(e) {
		var el = getEventElement(e);
		el.className = el.className.replace("hover", "");
	};
	
	var popup = document.createElement("DIV");
	popup.className = "ComboBox";
 	
	var table = document.createElement("TABLE");
	var tbody = document.createElement("TBODY");
	for(var i=0; i<entries.length; i++) {
		// Create a new row
		var row = document.createElement("TR");
		// Add the image if it was set
		if(entries[i].img!=null) {
			var imgCell = document.createElement("TD");
			imgCell.style.textAlign = "center";
			var itemImg = document.createElement("IMG");
			itemImg.src = entries[i].img;
			imgCell.appendChild(itemImg);
			row.appendChild(imgCell);
		}
		// Add the text line if it was set
		if(entries[i].text!=null) {
			var textCell = document.createElement("TD");
			textCell.style.textAlign = "left";
			textCell.appendChild(document.createTextNode(entries[i].text));
			row.appendChild(textCell);
		}
		
		row.comboEntry = entries[i];
		
		// Add Events
		addEvent(row, "mouseover", this._onMouseOverEvent);
		addEvent(row, "mouseout", this._onMouseOutEvent);
		if(handler!=null) {
	      	addEvent(row, "click", this._selectItemEvent);
	    }
	    tbody.appendChild(row);
	}
    if (IsFirefox()) {
		var row = document.createElement("TR");
		// Add the image if it was set
		// Add the text line if it was set
		var textCell = document.createElement("TD");
		textCell.style.textAlign = "right";
		textCell.style.fontWeight = "bold";
		textCell.style.paddingBottom = "4px";
		textCell.appendChild(document.createTextNode("[X]"));
		row.appendChild(textCell);
		
		row.comboEntry = entries[i];
		
		// Add Events
		addEvent(row, "mouseover", this._onMouseOverEvent);
		addEvent(row, "mouseout", this._onMouseOutEvent);
		if(handler!=null) {
	      	addEvent(row, "click", this.closeEvent);
	    }
	    tbody.appendChild(row);
    }
	table.appendChild(tbody);
	popup.appendChild(table);
	body.appendChild(popup);

    if (!IsFirefox()) {
    h = (popup.offsetHeight-2);
    if (height!=null) {
        if (h>height) {
            popup.style.height = height+"px";
        }
    }
    }
    else {
	    h = (popup.offsetHeight-2);
            popup.style.height = h+"px";
    	popup.style.overflow="hidden";
    }
    

    if (width==null) {
        popup.style.width = (element.offsetWidth-2)+"px";
        this.width=(element.offsetWidth-3);
    }
    else {
        popup.style.width = width;
    }
	this.popup = popup;
	addDestructor(this, "destroy");

    this.show();
    this.hide();
}

ComboBox.prototype.destroy = function(e) {
	// Remove the main popup event
	removeEvent(this.element, "click", this._showPopupEvent);
	this._showPopupEvent = null;
	// Remove the outside click
	removeEvent(this.element, "click", this._outsideClickEvent);
	this._outsideClickEvent = null;
	// Remove all events from the rows
	var rows = this.popup.getElementsByTagName("TR");
	for(var i=0; i<rows.length; i++) {
		removeEvent(rows[i], "mouseover", this._onMouseOverEvent);
		removeEvent(rows[i], "mouseout", this._onMouseOutEvent);
		removeEvent(rows[i], "click", this._selectItemEvent);
	}
	this._onMouseOverEvent = null;
	this._onMouseOutEvent = null;
	this._selectItemEvent = null;
};
 
ComboBox.prototype.selectItemEvent = function(e) {
 	var el = getEventElement(e);
 	el=findParentTag(el,"TR");
 	hideElement(this.popup);
 	if(this.handler!=null) {
		this.handler(el.comboEntry);
	}
};
 
ComboBox.prototype.closeEvent = function(e) {
		window.activeTransient.hide();
	 	cancelEvent(e);
};

ComboBox.prototype.outsideClickEvent = function(e) {
	if(window.activeTransient==this) {
		window.activeTransient.hide();
	 	cancelEvent(e);
	}
};

ComboBox.prototype.hide = function() {
    //this.element.src=this.element.src.replace("_show\.","_off.");
	hideElement(this.popup);
	window.activeTransient = null;
};

ComboBox.prototype.show = function() {
    if (IsFirefox()) {
        var x = getElementX(this.element)-7;
    }
    else {
        var x = getElementX(this.element)-11;
    }
    if (this.side!=null) {
            x = getElementX(this.element)+this.element.offsetWidth-this.width-10;
    }
    if (IsFirefox()) {
	var y = getElementY(this.element)+this.element.offsetHeight+5;
    } 
    else {
	var y = getElementY(this.element)+this.element.offsetHeight-5;
    }
	moveElement(this.popup,x,y);
	showElement(this.popup);
    //this.element.src=this.element.src.replace("_on\.","_show.");
	window.activeTransient = this;
};
 
ComboBox.prototype.showPopupEvent = function(e) {
	if(isDefined(this.element.blur)) {
		this.element.blur();
	}
	if(isDefined(window.activeTransient) && window.activeTransient!=this && window.activeTransient!=null) {
		window.activeTransient.hide();
	}
	if(isElementVisible(this.popup)) {
		this.hide();
	} else {
		this.show();
	}
	cancelEvent(e);
	return true;
};

