/********************************************
 *                                          *
 *            PARANGOLÉ COMBOBOX            *
 *                                          *
 ********************************************/


/********** COMBO BOX **********/

//CONSTRUCTOR
JSComboBox = function(comp_name, func) {

//VARS
	//PRIVATE VARS
	
	var _list_elements = Array();
	var _comp_name = comp_name;
	var _list_location_x = 0;
	var _list_location_y = 0;
	
	var _component_mouse_location = null;		//which component where mouse over
	
	var _time_close_list = 500;			//time to close list - 1000 equals a 1 second
	var _timeout = null;
	
	var _func_callback = func;			//Function Callled when a item is selected
	

	//Componente ID's 
	var _containers_name = eval(	
					{
						"main_container" 	: "combobox_" + _comp_name,
						"string_container" 	: "combobox_string_" + _comp_name,
						"dropdown_container" 	: "combobox_dropdown_" + _comp_name,
						"list_container" 	: "combobox_list_" + _comp_name,
						"acochambre_container" 	: "temp_div_locator_tabajara",
						"list_item"		: "combobox_" + _comp_name + "_item_"
					}
				);
				

	//PUBLIC VARS
	
	/**
	 *  No public vars
	 **/
	

//PUBLIC:
	
	//Add new Itens 
	this.addItemStatic = function (v, s) {
		var new_element	= new JSComboBoxItem(v, s);
		_list_elements.push(new_element);
	}
	
	//Add new Itens and update component list
	this.addItem = function (v, s) {
		this.addItemStatic(v,s);
		this.updateList();
	}
	
	
	//Update List
	this.updateList = function() {
		
		list_container = document.getElementById(_containers_name.list_container);
		list_size = _list_elements.length;
		
		//Destroy all elements of list
		list_elements = list_container.getElementsByTagName("DIV");
		list_elements_length = list_elements.length;
		
		for(i=list_elements_length-1; i>=0; i--) {	
			list_container.removeChild(list_elements[i]);
		}
		
		
		//Build all elements of list from array
		for(i=0; i<list_size; i++) {
			itemvalue = _list_elements[i].value;
			itemtext = _list_elements[i].getText();
			
			anchor = document.createElement("DIV");
			anchor.innerHTML = itemtext;
			anchor.style.cursor = "default";
			anchor.id = _containers_name.list_item + i;
			
			JSEvent.setEventListener(anchor, 'click', selectListItem);
			
			list_container.appendChild(anchor);
		}
		
	}
	
	
	//Clear List
	this.clearList = function() {
		list_container = document.getElementById(_containers_name.list_container);
		
		while(_list_elements.length >= 1) {
			_list_elements.pop();
		}
		
		setElementsValueFromListIndex(null);
		
		this.updateList();
	}
	
	
	//Sets a selected element
	this.setSelectedIndex = function(idx) {
		listsize = _list_elements.length - 1;		
		string_container = document.getElementById(_containers_name.string_container);
		input_hidden = document.getElementById(_comp_name);
		
		if (listsize >= idx) {
			string_container.innerHTML = _list_elements[idx].getText();
			input_hidden.value = _list_elements[idx].getValue();
		}
	}
	


//PRIVATE:

	//Find component position
	var findPosition = function(obj) {
		var leftPosition = 0;
		var topPosition = 0;
		
		topPosition = findPositionTop(obj);
		leftPosition = findPositionLeft(obj);
		
		coord = new JSCoord(leftPosition, topPosition);
		return coord;	
	}
	
	
	//Find component left offset
	var findPositionLeft = function(obj) {
		var position = 0;
		
		if(obj.offsetParent) {
			while(obj.offsetParent) {
			    position += obj.offsetLeft;
			    obj = obj.offsetParent;
			}
		}
		
		return position;
	}
	
	
	//Find component top offset
	var findPositionTop = function(obj) {
		var position = 0;
		
		if(obj.offsetParent) {
			while(obj.offsetParent) {
			    position += obj.offsetTop;
			    obj = obj.offsetParent;
			}
		}
		
		return position;
	}
		
	
	
	//Open Itens List
	var openListItens = function() {
		listItens = document.getElementById(_containers_name.list_container);

		//If list still opened, close list		
		if(listItens.style.display.toLowerCase() != "none") {
			closeListItens();
			return;
		}
		
		
		targetcontainer = document.getElementById(_containers_name.main_container);
		target_container_height = targetcontainer.offsetHeight;
		
		position = findPosition(targetcontainer);
		
		listItens.style.display = "block";
	}	
	
	
	
	//Open Itens List	
	var closeListItens = function() {
		listItens = document.getElementById(_containers_name.list_container);
		listItens.style.display = "none";
	}
	
	
	
	//Verify element is valid
	var isComponentElement = function(idstring) {
		if ( idstring == _containers_name.main_container || idstring == _containers_name.list_container || idstring == _containers_name.dropdown_container || idstring == _containers_name.string_container ) 
			return 1;
		
		return 0; // Is not a valid element from component
	}
	
	
	
	//close List if no component's elements mouse over
	var checkComponentMouseToClose = function() {	
		if(!_component_mouse_location) {
			closeListItens();
		}
	}
	
	

	//Set component to verify
	var sendToVerify = function() {
		_component_mouse_location = this;
		clearInterval(_timeout);						//Stop clock count to close list
	}
	


	//Unset component to verify
	var releaseToVerify = function() {
		_component_mouse_location = null;
		_timeout = setTimeout(checkComponentMouseToClose, _time_close_list);	//Start clock count to close list
	}
	


	//Select an item from list
	var selectListItem = function(e) {
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;		
		
		itemname = targ.id;
		position = itemname.lastIndexOf("_");
		itemidx = itemname.substr(position+1);
		setElementsValueFromListIndex(itemidx);
		closeListItens();
		
		//Callback function
		if(_func_callback) eval(_func_callback);
		
	}
	
	
	
	//Set value em text to elements
	var setElementsValueFromListIndex = function(indexnumber) {
		inputhidden = document.getElementById(_comp_name);
		string_container = document.getElementById(_containers_name.string_container);
		
		if(indexnumber == null) {
			inputhidden.value = "";
			string_container.innerHTML = "";
			return;
		}
		
		string_container.innerHTML = _list_elements[indexnumber].getText();
		inputhidden.value = _list_elements[indexnumber].getValue();		
	}



	//Showup Component
	var show = function(){
	
		containers_name = eval(_containers_name);
		
		//Gets parent contaniner's name by ACOCHAMBRÊ method
		document.write("<div id='" + _containers_name.acochambre_container + "'></div>");
		temp_div = document.getElementById(_containers_name.acochambre_container);
		parentElement = temp_div.parentNode;
		parentElement.removeChild(temp_div);
		
		//Insert ComboBox Elements
		var main_container 		= document.createElement("DIV");
		var list_container 		= document.createElement("DIV");
		var string_container 	= document.createElement("DIV");
		var dropdown_container 	= document.createElement("DIV");
		var inputhidden			= document.createElement("INPUT");
		
		main_container.id 		= _containers_name.main_container;
		list_container.id 		= _containers_name.list_container;
		dropdown_container.id 	= _containers_name.dropdown_container;
		string_container.id 	= _containers_name.string_container;
		inputhidden.id			= _comp_name;
		
		
		
		inputhidden.name 		= _comp_name;
		inputhidden.type		= "hidden";
		
		
		main_container.className 	= "combobox_main";
		list_container.className 	= "combobox_list";
		dropdown_container.className 	= "combobox_dropdown";
		string_container.className 	= "combobox_string";
		
		string_container.style.overflow = "hidden";
		string_container.style.whiteSpace = "nowrap";
		
		list_container.style.position = "absolute";
		list_container.style.display = "none"
		list_container.style.zindex = 99999;
		
		

		
		
		
		parentElement.appendChild(main_container);
		//document.body.appendChild(list_container);
		//document.body.insertBefore(list_container);
		//document.body.insertBefore(list_container, document.body.firstChild);		
		main_container.appendChild(string_container);
		main_container.appendChild(dropdown_container);
		main_container.appendChild(list_container);
		parentElement.appendChild(inputhidden);
		
		
		//Listeners
		
		JSEvent.setEventListener(dropdown_container, 'click', openListItens);
		
		JSEvent.setEventListener(main_container, 'mouseout', releaseToVerify);
		JSEvent.setEventListener(list_container, 'mouseout', releaseToVerify);
		JSEvent.setEventListener(dropdown_container, 'mouseout', releaseToVerify);
		JSEvent.setEventListener(string_container, 'mouseout', releaseToVerify);
				
		JSEvent.setEventListener(main_container, 'mouseover', sendToVerify);
		JSEvent.setEventListener(list_container, 'mouseover', sendToVerify);
		JSEvent.setEventListener(dropdown_container, 'mouseover', sendToVerify);
		JSEvent.setEventListener(string_container, 'mouseover', sendToVerify);
		
	}
	
	show();
	this.updateList();
	
	// BUG IE6!!!! Posiciona absolutamente
	if(navigator.appName=='Microsoft Internet Explorer'){
		if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1){
			//alert(parentElement);
		}
	}

}


/*********** COMBO BOX ITEM ***********/
JSComboBoxItem = function(v, s) {
	var _value = v;
	var _text = s;
	
	this.getValue = function() {
		return _value;
	}
	
	this.getText = function() {
		return _text;
	}
}


/*********** LOCATION **********/
JSCoord = function (x, y) {
	var _x = x;
	var _y = y;
	
	this.getX = function() {
		return _x;
	}
	
	this.getY = function() {
		return _y;
	}
}



/********* JSEvent **********/
JSEvent = function() {}

//Acoxambrê method set an event listener on an object
JSEvent.setEventListener = function (obj, event_type, callback_function){

	if (obj.addEventListener) { //W3C
		obj.addEventListener(event_type, callback_function, false)
	}

	if (obj.attachEvent) { // Internet Explorer
		obj.attachEvent("on" + event_type, callback_function)
	}
}