/**
 * @author jordi.touza
 */
if (typeof(_funky.combo) == "undefined") _funky.combo = {};
else alert("var name 'combo' is already set!");

_funky.combo = function (id, fOnChoice){
	
	if (!document.getElementById)		return 0;			// no DOM 
	
	this.id			= id;
	this.options	= new Array;
	this.items		= new Array;
	
	this.obj 		= _funky.DOM.getElement(id);
	this.container	= this.obj.parentNode;
			
	this.div 		= _funky.DOM.createElement("div", 	{id:"div", className:"comboPanel" });
	this.input 		= _funky.DOM.createElement("input", {id:this.id + "Box", className:"combo", readOnly:"readOnly" });	
	
	this.input.className 	= "combo";				//FIX
	this.input.readOnly 	= "readOnly";			//FIX
	
	this.oUL 		= _funky.DOM.createElement("ul", {id:""});	
	
	for (i=0; i<this.obj.options.length; i++){
				
		this.options[i] = this.obj.options[i].value;
		this.items[i] 	= this.obj.options[i].innerHTML;
		
		var oLI 		= _funky.DOM.createElement("li", {id:this.options[i]});
		oLI.innerHTML 	= this.items[i];

		this.oUL.appendChild(oLI)
	}
	
	this.input.value = this.obj.options[this.obj.selectedIndex].text;

	var p = this;
	
	this.input.onmousedown 	= function(ev){ return p.onMouseDown(ev); };
	this.input.onkeypress 	= function(ev){ return p.onKeyPress(ev); };		
	//this.oUL.onmousedown 	= function(ev){ return p.onChoice(ev); };	
	this.oUL.onmousedown 	= function(ev){ return p.onChoice(ev, fOnChoice); };	
	
	document.body.onclick	= function(ev){ return p.hidde(ev); };	

	this.obj.style.display 	= "none";
	this.div.style.display 	= "none";	
	
	this.container.appendChild(this.input);
	
	this.div.style.position	= "absolute";
	this.div.style.left		= (_funky.DOM.getPosition(this.input).x ) + "px";
	this.div.style.top		= (_funky.DOM.getPosition(this.input).y + 28) + "px";		
	
	this.container.appendChild(this.div);	
	this.div.appendChild(this.oUL);
			
};

_funky.combo.prototype.hidde = function(e) {

	var e = (window.event) ? window.event.srcElement : e.target;
	
	if( e.id != this.id + "Box"){
		
		this.div.style.display 	= "none";
	}
	
};

_funky.combo.prototype.onChoice = function(e, fOnChoice) {
//_funky.combo.prototype.onChoice = function(e, fOnChoice) {

	var e = (window.event) ? window.event.srcElement : e.target;
	this.obj.selectedIndex 	= this.items.indexOf(e.innerHTML)
	var sFunction = this.obj.getAttributeNode("onChange").value;
	
	sFunction = sFunction.strReplace("this.", "this.obj.");

	//eval(sFunction);
	if(fOnChoice) eval(fOnChoice + "('" + this.obj.options[this.obj.selectedIndex].innerHTML + "','" + this.obj.options[this.obj.selectedIndex].value + "')");
		
	this.input.value 		= e.innerHTML;
	
	this.div.style.display 	= "none";
	
}

/**
 * onKeyPress
 * @param {event} e
 */
_funky.combo.prototype.onKeyPress = function(e) {
	
	var key = (window.event) ? window.event.keyCode : e.keyCode;
	
	this.div.style.display = this.div.style.display == "none" ? "" : "none";
	
	return;
};

/**
 * onMouseDown
 * @param {event} e
 */
_funky.combo.prototype.onMouseDown = function(e) {
	
	var key = (window.event) ? window.event.keyCode : e.keyCode;
				
	this.div.style.display = this.div.style.display == "none" ? "" : "none";
	
	return;
};



