var windowActive = false;
var shapeWindowRefreshOnClose = false;

function customAjaxRequest(url, params, callback)
{
	$.post(url, params, callback)
	.error(function(){
					alert("Ein fehler ist aufgetreten.");
					closeWindow();
					});	
}

function ajaxRequest(action, callback, additionalParams)
{
	var params = "ajax=" + action;
	for (var i in additionalParams) {
		params += "&" + i + "=" + encodeURIComponent(additionalParams[i]);
	}

	customAjaxRequest(BASEPATH + "shape.php", params, callback);
}

function centerWindow()
{
	mLeft = -1 * Math.round($("#shapeWindow").outerWidth() / 2);
	mTop = -1 * Math.round($("#shapeWindow").outerHeight() / 2);
	
	$("#shapeWindow").css("marginLeft", mLeft);
	$("#shapeWindow").css("marginTop", mTop);	
}

function shakeWindow(count, offset)
{
	function doShake(e, step, maxSteps, signSwitch, base, offset)
	{
		step++;
		
		if (step < maxSteps)
		{
			signSwitch = -signSwitch;
			stepOffset = base + signSwitch * (((maxSteps-step) / maxSteps) * offset);
			$(e).animate({marginLeft: stepOffset}, 10, function() { doShake(e, step, maxSteps, signSwitch, base, offset) });			
		}
		else
		{
			//reset state
			$(e).css("marginLeft", base);
		}
	}
	
	base = $("#shapeWindow").css("marginLeft");
	base = parseInt(base.substr(0, base.length-2));
	
	signSwitch = 1;
	stepOffset = base + offset;
	
	$("#shapeWindow").animate({marginLeft: stepOffset}, 10, function() { doShake(this, 0, count, signSwitch, base, offset) });
}

function setupWindow(width, height, data)
{
	contentWidth = "auto";
	contentHeight = "auto";
	if (width != "auto")
		contentWidth = "100%";
	if (height != "auto")
		contentHeight = "100%";
	
	$("#shapeWindowContent").removeClass("shapeWindowLoader");
	$("#shapeWindowContent").css("display", "none").css("width", contentWidth).css("height", contentHeight);
	$("#shapeWindow").css("display", "none");
	$("#shapeWindowContent").html(data);
	$("#shapeWindowContent").fadeIn(200);
	$("#shapeWindow").css("width", width).css("height", height);
	$("#shapeWindow").fadeIn(100);
	
	centerWindow();
}

function createWindow(windowName, width, height)
{
	if (!windowActive)
	{
		if (width == null)
			width = "auto";
		if (height == null)
			height = "auto";
			
		$("body").prepend('<div id="shapeWindowDarkener"></div>');
		$("#shapeWindowDarkener").click(closeWindow);
		$("#shapeWindowDarkener").fadeIn(200);
		$("body").prepend('<div id="shapeWindow"><div id="shapeWindowContent" class="shapeWindowLoader"></div></div>');	
		$("#shapeWindow").fadeIn(200);
		centerWindow();
		ajaxRequest(windowName, function(data) { setupWindow(width, height, data); });
		windowActive = true;
	}
	else
	{
		//shake active window
		shakeWindow(10, 20);
	}
}	

function removeWindow()
{
	$("#shapeWindow").remove();
	$("#shapeWindowDarkener").remove();
	windowActive = false;
	
	if (shapeWindowRefreshOnClose)
		document.location.reload();
}

function closeWindow()
{
	$("#shapeWindow").fadeOut(200);
	$("#shapeWindowDarkener").fadeOut(200);
	window.setTimeout(removeWindow, 200);
}
