var boxes;
var boxesAdditional = new Array();
var activeBox;
var boxHeight = 100;
var lastBoxShown = 0;
var dontHideBox = false;

function hideBox() {
	var now = (new Date()).getTime();
	var diff = now - lastBoxShown;
	if(activeBox != null && diff > 50 && !dontHideBox) {
		getById("box_" + activeBox).style.display = "none";
		//hideBoxStep(activeBox);
		activeBox = null;
	} else if(dontHideBox) {
		dontHideBox = false;
	}
}

function showBox(_box, _element, _offsetX, _offsetY, _inLayout) {

	if(activeBox != _box) {
		
		var layoutX = 0;
		
		if(!_inLayout)
			layoutX = $("#layout").offset().left;
		
		hideBox();
		activeBox = _box;
		if(_offsetX == null) _offsetX = 0;
		if(_offsetY == null) _offsetY = 0;
		
		var offset = $(_element).offset();
		
		var left = offset.left - layoutX + _offsetX;
		var top = offset.top + _offsetY + $(_element).outerHeight();
		
		$("#box_" + activeBox).css("left", left + "px");
		$("#box_" + activeBox).css("top", top + "px");
		$("#box_" + activeBox).css("display", "block");
		
		lastBoxShown = (new Date()).getTime();
		
	}
	
	return false;

}

function switchBox(_box, _element, _offsetX, _offsetY, _inLayout) {

	if(activeBox != null && activeBox != _box) {
		showBox(_box, _element, _offsetX, _offsetY, _inLayout);
	}

}

function showBoxStep(_id) {
	
	var height = getById("box_" + _id).offsetHeight;
	var heightNew = height + 10;
	
	if(heightNew > boxHeight)
		heightNew = boxHeight;
		
	getById("box_" + _id).style.height = heightNew + "px";
	
	if(heightNew < boxHeight)
		setTimeout("showBoxStep('" + _id + "')", 30);
	else 
		setFirstInputFocus(id("box_" + _id).childNodes);
		
}

function setFirstInputFocus(_array) {

	for(var i = 0; i < _array.length; i++) {
		if(_array[i].tagName == "INPUT") {
			if(_array[i].type == "text") {
				_array[i].focus();
				return true;
			}
		} else {
			var result = setFirstInputFocus(_array[i].childNodes);
			if(result)
				return true;
		}
	}

}

function setupBoxes() {
	
	for(i = 0; i < boxes.length; i++) {
		if(getById("box_" + boxes[i]) == null)
			continue;
		getById("box_" + boxes[i]).className = getById("box_" + boxes[i]).className.replace("dropDownDis", "dropDown");
		getById("box_" + boxes[i]).style.display = "none";
	}
	
	for(i = 0; i < boxesAdditional.length; i++) {
		if(getById("box_" + boxesAdditional[i]) == null)
			continue;
		getById("box_" + boxesAdditional[i]).className = getById("box_" + boxesAdditional[i]).className.replace("dropDownDis", "dropDown");
		getById("box_" + boxesAdditional[i]).style.display = "none";
	}
}

