/**
 * this function dims the visible browser window to 50% and locks. 
 * it also buts a layer with the given name and dimensions in the middle where some ajax-request can be loaded in 
 * layerName has to be given as jquery ID or class name which means there has to be a hash-sign or a dot in front of the name
 * 
 * @param string layerName - ID of some targetArea
 * @param int layerWidth - Width of targetArea
 * @param int layerHeight - Height of targetArea
 * @param bool fadeIn
 * @return no return
 */
function layerOverlayDim(layerName, layerWidth, layerHeight, layerHeightAuto, fadeIn)
{
    if(fadeIn === undefined || fadeIn === '')
    {
		fadeIn = true;
    }	
    
    if(layerHeightAuto == true)
    {
	var layerHeightStr = "auto"
    }
    else
    {
	var layerHeightStr = layerHeight + "px"
    }
    
	$("body").css("overflow", "hidden");
	
	var y = centerLayerY(layerHeight);
	
	$(layerName).css
	(
		{
		    "top" : y + "px",
		    "height" : layerHeightStr,
		    "width" : layerWidth + "px",
		    "margin-left" : - ( layerWidth / 2 ) + "px",
		    "position" : "absolute",
		    "left" : "50%",
		    "z-index" : "21000"
		}
	);
	
	if(fadeIn === true)
	{
	    $(layerName).fadeIn();
	}
	else
	{
		$(layerName).show();
	}

	getOverlay();
}


/**
 * this funktion makes the whole magic disappear
 * @return no return
 */
function closeLayerOverlayDim()
{
    $("body").css("overflow", "auto");
    $("#overlay").empty().hide();
}


var dim_c = 0;
var dim_t;
function dimOverlay()
{
    $("#overlay").show();
	  
	dim_c = dim_c + 1;
	dim_t = setTimeout("dimOverlay()", 100);

	$("#overlay").css("opacity", dim_c / 10);
	$("#overlay").css("filter", "alpha(opacity: " + (dim_c * 10) + ")");
	
	if(dim_c == 6)
	{
		clearTimeout(dim_t);
		dim_c = 0;
	}
}


function centerLayerY(layerheight)
{
	var currentTop = $(window).scrollTop();
	var winheight = $(window).height();
	
	var yMiddleA = winheight / 2;
	var yMiddleB = layerheight / 2;

	if(layerheight > winheight)
	{
		return currentTop + 5;
	}	
	else
	{
	    return currentTop + yMiddleA - yMiddleB;
	}					
}


function getOverlay()
{
    var currentTop = $(window).scrollTop();
    var winheight = $(window).height();
    var winwidth = $(window).width();
    
    $("#overlay").css
    (
	    {
		"width" : winwidth + 100,
		"height" : winheight + 100,
		"background-color" : "#f8f8f8",
		"z-index" : "20000",
		"position" : "absolute",
		"top" : currentTop,
		"left" : "0",
		"opacity" : "0",
		"filter" : "alpha(opacity: 0)"
	    }
    );

    dimOverlay();
}

//document.write('<div class="overlay" id="overlay" style="display: none;"></div>');
