﻿/* common dialog scripts */

// Namespace
if (Dialog == null || typeof(Dialog) != "object") { var Dialog = new Object(); }
if (LightBox == null || typeof(LightBox) != "object") { var LightBox = new Object(); }

LightBox.Show = function(element, position) {
	///<summary>Shows a lightbox containing an element</summary>
	///<param name="element">Element reference or ID of the element to show</param>
	///<param name="position">Object describing the width and position of the box. It must contan these numeric properties: top, left, width.</param>
	element = $(element);
	var lbbg = $("lightboxBG");
	lbbg.style.height = Dialog.GetBodyHeight() + "px";
	lbbg.style.zIndex = 900;
	Effect.Appear(lbbg, {from:0, to:0.8, duration:0.5});
	var frame = $("lightboxFrame");
	frame.absolutize();
	frame.style.zIndex = 1000;
	if (position && position.top)
		frame.style.top = position.top + "px";
	if (position && position.left)
		frame.style.left = position.left + "px";
	if (position && position.width)
		frame.style.width = position.width + "px";
	frame.insert(element);
	frame.show();
	Effect.SlideDown(element);
}
LightBox.Hide = function(content, returnTo) {
	///<summary>Hides a lightbox</summary>
	///<param name="content">Element or id of content to hide and return to its original parent</param>
	///<param name="returnTo">Element or id of the desired parent of the element</param>
	var frame = $("lightboxFrame");
	var bg = $("lightboxBG");
	if (content == null)
		content = frame.firstDescendant();
	Effect.BlindUp(content, { duration:0.4, 
		afterFinish: function() { 
			frame.hide(); 
			if (returnTo) { $(returnTo).insert($(content)); } 
		} 
	});
	Effect.Fade(bg, { from:0.8, duration:0.5, delay:0.2 });
}

Dialog.Close = function(element, returnTo) {
	///<summary>Closes a dialog</summary>
	///<param name="element">An id or element reference to an open dialog (or a child element of an open dialog).</param>
	///<param name="returnTo">The element to return the closed dialog to.</param>
	element = $(element);
	returnTo = $(returnTo);
	if (!element.hasClassName("dialog")) {
		element = element.up(".dialog");
	}
	if (!element) {
		if (DEBUG_ON) alert("fant ingen dialog å lukke");
		return;
	}
	
	LightBox.Hide(element, returnTo);
	window.setTimeout("$('" + element.identify() + "').hide()", 1000);
}
Dialog.Clear = function(element) {
    ///<summary>Clears the form elements in a dialog</summary>
    ///<param name="element">Element or id reference to the container for the form elements</param>
    element = $(element);
    element.select("input[type=text], input[type=password]").invoke("clear");
    element.select("input[type=checkbox], input[type=radio]").each(function(elm) {
        elm.checked = false;
    });
    element.select("select").each(function(elm) {
        elm.value = "";
    });
    element.select(".error").each(function(elm) {
        if ((elm.type == "text") || (elm.type == "password")) {
            elm.value = "";
            elm.className = "";
        } else {
            elm.update("");
        }
    });
}
Dialog.Open = function(element) {
	///<summary>Opens a dialog</summary>
	///<param name="element">Element to open as dialog</param>
	element = $(element);
	var offsetLeft = $("site").cumulativeOffset().left;
	//var offsetTop = $("sitecontent").cumulativeOffset().top;  // BUG i IE7
	var offsetTop = 123;
	var siteWidth = $("sitecontent").getWidth();
	Dialog.Clear(element);
	var options = { top:offsetTop, left: offsetLeft, width: siteWidth };
	LightBox.Show(element, options);
	if (element.OnClientLoad) {
		eval(element.OnClientLoad);
	}
}

Dialog.GetBodyHeight = function() {
	///<summary>Gets the height of the document body</summary>
	var footer = $("footer");
	var footerHeight = footer.getHeight();
	var footerOffset = footer.cumulativeOffset().top;
	return footerHeight + footerOffset;
}
Dialog.ShowWaitScreen = function() {
	///<summary>Shows a loading screen</summary>
	var lbbg = $("lightboxBG");
	lbbg.style.height = Dialog.GetBodyHeight() + "px";
	lbbg.style.zIndex = 900;
	lbbg.setOpacity(0.8);
	lbbg.show();
}
Dialog.HideWaitScreen = function() {
	///<summary>Hides the loading screen</summary>
	Effect.Fade("lightboxBG", { from:0.8, duration:0.4});
}

Dialog.InvokeUserHandler = function(action, params, callbackSuccess, callbackFailure) {
	///<summary>Invokes a user handler action</summary>
	///<param name="action" type="String">The action to invoke</param>
	///<param name="params">The parameters to supply to the handler</param>
	///<param name="callbackSuccess">Callback method to execute on success</param>
	///<param name="callbackFailure">Callback method to execute on failure</param>
	if (params == null) params = {};
	params.action = action;
	new Ajax.Request("/userhandler.ashx", { 
		method: "post", 
		parameters: params, 
		onSuccess: callbackSuccess, 
		onFailure: callbackFailure });
}
