var ie = document.all?true:false

function hideSelects(){
	var selects = document.getElementsByTagName("select");
	for(var i = 0; i < selects.length; i++){				
		if(isCrossing(selects[i], showedList)){
			selects[i].style.visibility = "hidden"
		}
	}
}
function showSelects(){
	var selects = document.getElementsByTagName("select");
	for(var i = 0; i < selects.length; i++){		
		selects[i].style.visibility = "";
	}	
}
function isCrossing(obj1, obj2){
	var coor1 = getAbsolutePosition(obj1)
	var coor2 = getAbsolutePosition(obj2)
	if(coor1.x > coor2.x + obj2.clientWidth){
		return false
	}
	if(coor1.x + obj1.clientWidth < coor2.x){
		return false
	}
	if(coor1.y > coor2.y + obj2.clientHeight){
		return false
	}
	if(coor1.y + obj1.clientHeight < coor2.y){
		return false
	}
	return true;
}
function getAbsolutePosition(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
    return r;
}
function inElem(obj, pos){
	if(!obj)
		return false;
	var coor = getAbsolutePosition(obj)
	
	if(pos.x-1 <= coor.x || pos.x>= coor.x + obj.clientWidth){
		return false
	}
	if(pos.y-1 <= coor.y || pos.y-1 >= coor.y + obj.clientHeight){
		return false
	}
	return true;
}

function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}


var showedList = null;
var currentTitle = null
function showList(index){
	
	showedListNew = document.getElementById("list" + index);
	if(showedList && showedList!=showedListNew){
		showedList.style.display = "none"
		if(ie){
			showSelects();
		}
	}
	showedList = showedListNew
	currentTitle = document.getElementById("title" + index);
	showedList.style.display = "block";
	if(ie){
		hideSelects();
	}
	document.body.onclick = function(e){
		if(!e){
			e = event
		}
		var pos = getPosition(e)
		var res = inElem(showedList, pos)
		var res1 = inElem(currentTitle, pos)
		if(res == false && res1==false){
			showedList.style.display = "none";
			if(ie){
				showSelects();
			}
		}
	}
}
