// JavaScript Document
 
var comboBoxListens = false;
var comboBoxOpened = false;
var allComboBoxes = new Array();

function makeComboBox(id){
	allComboBoxes.push(id);
	items = $(id).getElementsByTagName('A');
	for (var i=0; i<items.length; i++){
		items[i].onmouseover = hoverComboBox;
		items[i].onmouseout = houtComboBox;
		items[i].onmousedown = function() {selectComboBox(this, id)};
		items[i].onmouseup = function() {selectedComboBox(this, id)};
	}
	$(id + 'btn').onclick = function() {openComboBox(id)};
	$(id + 'txt').onclick = function() {openComboBox(id)};
	//alert($(id + 'txt').offsetHeight);
	//alert($(id + 'txt').clientHeight);
	//alert(document.defaultView.getComputedStyle($(id + 'txt'), null).getPropertyValue('height', null))
	//alert(items.length * $(id + 'txt').style.height);
	len = items.length * ($(id + 'txt').offsetHeight-1);
	if (len>200){
		len = 200;
	}
	$(id).style.height = len + 'px';
	if (!comboBoxListens){
		if (document.addEventListener) {
			//document.addEventListener("keyup", ComboBox_handleKey, false );
			document.addEventListener("mousedown", checkMouseComboBox, false );
		}
		else if (document.attachEvent) {
			//document.attachEvent("onkeyup", function () { ComboBox_handleKey(window.event); } );
			document.attachEvent("onmousedown", function () { checkMouseComboBox(window.event); } );
		}
		comboBoxListens = true;
	}
}


function checkMouseComboBox(e){
	if (!comboBoxOpened){
		return true;
	}
    var obj,len,el,i;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1){
    	el = el.parentNode;
    }
    var elcl = el.className;
    if(elcl.indexOf("combo-")!=0){
        closeAllComboBox();
    }
}


function closeAllComboBox(){
	comboBoxOpened = false;
	for (var i = 0; i < allComboBoxes.length; i++){
		closeComboBox(allComboBoxes[i]);
	}
}

function openComboBox(id){
	closeAllComboBox();
	comboBoxOpened = true;
	$(id).style.display = 'block';
	/*if(navigator.appName.indexOf('Explorer') == -1){
		//$(id + 'btn').focus();
		//$(id + 'cont').focus();
	}else{
		$(id).focus();
	}
	*/
	items = $(id).getElementsByTagName('A');
	for (var i=0; i<items.length; i++){
		items[i].className = '';
	}
	
	
}

function closeComboBox(id){
	$(id).style.display = 'none';
}

function selectedComboBox(item, boxId){
	alert(1)
}

function selectComboBox(item, boxId){
	$(boxId + 'txt').innerHTML = item.innerHTML;
	$(boxId + 'val').value = item.href;
	$(boxId + 'val').onchange();
	return false;
}

function hoverComboBox(event){
	this.className = 'sel';
}

function houtComboBox(event){
	this.className = '';
}
