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) {
		
		$("div.box.dropDown:visible").each(function() {
			
			if(!$(this).data("hiding"))
				$(this).stop(true, true).ctdHide(styEffectDropDownOut[0], styEffectDropDownOut[2], styEffectDropDownOut[1], function() {
					$(this).data("hiding", false);
					// IE Fixes
					$(this).css("filter", "");
					$(this).find("img").css("height", "");
					$(this).find("img").css("width", "");
				}).data("hiding", true);
				
		});
		
		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();
		
		
		// Expand dropdown on the left side of the sender
		spaceRight = $(window).width() - $("#box_" + activeBox).width() - offset.left;
		
		if(spaceRight < 0)
			left -= -$(_element).width() + $("#box_" + activeBox).width();
		
		
		$("#box_" + activeBox).css("left", left + "px");
		$("#box_" + activeBox).css("top", top + "px");
		
		$("#box_" + activeBox).ctdShow(styEffectDropDownIn[0], styEffectDropDownIn[2], styEffectDropDownIn[1], function() {
			$("#" + this.id + " input.text").each(function() {
				this.focus();
			});
		});
		
		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 setupBoxes() {
	
	$("div.box.dropDown, div.box.dropDownDis").removeClass("dropDownDis").addClass("dropDown").css("display", "none");
	
}