// File:		$Id: any_action.js 20832 2006-04-28 11:06:52Z bas $
// Copyright:	(c) 2003-2005 Mediamatic
// Description:	misc. support functions for anymeta sites

//

//TODO: make an widget of it

// No requirements if the compiled version is used
//dojo.require("dojo.html");
//dojo.require("dojo.style");

// Function:	setActionBox
// Parameters:	act			action box
//				bg			area over the complete window in the background of the action box (optional)
//				bgAddClass	add a class to the background (optional)
//				bgOpacity	set opacity to the background (0.0 - 1.0) (optional)
// Returns:		
// Description:	places the action box in the iddle of the screen and optionally streches the background 
//				over the complete widt and height of the page
//
function setActionBox(act,bg_id,bgAddClass,bgOpacity)
{
	if (bg_id != null && typeof bg_id == "string")
	{
		bg = dojo.byId(bg_id);
		if (!bg)
		{
			var bg = document.createElement("div");
			bg.setAttribute("id", bg_id);
			bg.innerHTML = '&nbsp;';

			var body = document.getElementsByTagName("body")[0];
			if (body) {
				body.appendChild(bg);
			}
		}
		
		if (dojo.byId("main"))
		{
			xmain = dojo.byId('main');
			dojo.style.setContentHeight(bg,dojo.style.getContentHeight(xmain));
		}
		else
		{
			dojo.style.setContentHeight(bg,dojo.html.getViewportHeight());
		}
		if (bgAddClass != null && typeof bgAddClass == "string")
		{
			dojo.html.addClass(bg,bgAddClass);
		}
		if (bgOpacity != null)
		{
			dojo.style.setOpacity(bg,bgOpacity);
		}

		dojo.html.show(bg);
	}

	act = dojo.byId(act);

	// the action box should be append at the body otherwise IE won't show this div
	var body = document.getElementsByTagName("body")[0];
	if (body) {
		body.appendChild(act);
	}

	dojo.html.show(act);

	viewportWidth	= dojo.html.getViewportWidth();
	viewportHeight	= dojo.html.getViewportHeight();
	actOuterWidth	= dojo.style.getOuterWidth(act);
	actOuterHeight	= dojo.style.getOuterHeight(act);

	actLeft = Math.round(viewportWidth / 2)	 - Math.round(actOuterWidth / 2);
	actTop	= Math.round(viewportHeight / 2) - Math.round(actOuterHeight / 2);

	dojo.html.placeOnScreenPoint(act,actLeft,actTop);
}

// Function:	onokActionBox
// Parameters:	stat		status of the xmlhttprequest
//				result		result of request
//				opt			extra parameters
//					- writediv	write the writetxt in this div (default 'action_ok')
//					- writetxt	which text to write the writediv
//					- writextr	xtr text to write at the end of the writetxt (ie. value of selcted thing)
//					- onfail	when fails show/hide these divs in the action_box (default null)
//					- onok		when ok show/hide these divs in the action_box (default null)
//					- timeout	set timeout to close the actionbox (default -1: no timeout)
//					- actionbox	which actionbox are we talknig about (used when clearing the actionbox on a timeout)
//					- actionbg	what is the background of the actionbox (used when clearing the actionbox on a timeout - can be null)
// Returns:		
// Description:	all kind of actions after an action_box is submitted
//
function onokActionBox(stat, result, opt)
{
	// write to action_ok
	var writediv	= 'action_ok';
	var writetxt	= '';
	var writextr	= '';
	var onfail		= null;
	var onok		= null;
	var timeout		= -1;
	var actionbox	= '';
	var actionbg	= 'action_bg';

	if (typeof opt == 'object') 
	{
		for (var p in opt) 
		{
			//alert(p + " = " + opt[p]);
			if (p == 'writediv')	writediv	= opt[p];
			if (p == 'writetxt')	writetxt	= opt[p];
			if (p == 'writextr')	writextr	= opt[p];
			if (p == 'onfail')		onfail		= opt[p];
			if (p == 'onok')		onok		= opt[p];
			if (p == 'timeout')		timeout		= opt[p];
			if (p == 'actionbox')	actionbox	= opt[p];
			if (p == 'actionbg')	actionbg	= opt[p];
		}
	}
		
	if (stat = 'ok') 
	{
		// show hide divs when ok
		if (onok) 
		{
			a = onok.split(',');
			for (var i=0; i<a.length; i++) 
			{
				s = a[i];
				c = s.charAt(0);
				v = 'block';
				if (c == '-') 
				{
					v = 'none';
					s = s.substr(1,s.length-1);
				}
				else if (c == '+') 
				{
					s = s.substr(1,s.length-1);
				}
				if (dojo.byId(s))
				{
					dojo.byId(s).style.display = v;
				}
			}
		}

		// write the default action div ( = action_ok )
		if (writetxt.length > 0 && writediv)
		{
			if (writextr.length > 0)
			{
				writetxt += writextr;
			}
			dojo.byId(writediv).innerHTML = writetxt;
		}

		// close the box after some seconds
		if (timeout >= 0 && actionbox)
		{
			setTimeout("clearActionBox('"+actionbox+"','"+actionbg+"')",timeout)
		}
		else if (timeout >= 0 && actionbox == null)
		{
			alert('Timeout used as a parameter: actuonbox is obligatorily');
		}
	}
	else 
	{
		// show hide divs when failed
		if (onfail) 
		{
			a = onfail.split(',');
			for (var i=0; i<a.length; i++) 
			{
				s = a[i];
				c = s.charAt(0);
				v = 'block';
				if (c == '-') 
				{
					v = 'none';
					s = s.substr(1,s.length-1);
				}
				else if (c == '+')
				{
					s = s.substr(1,s.length-1);
				}

				if (dojo.byId(s))
				{
					dojo.byId(s).style.display = v;
				}
			}
		}
	}
}

// Function:	clearActionBox
// Parameters:	act			action box
//				bg			area over the complete window in the background of the action box (optional)
// Returns:		
// Description:	hides the action box and optionally the background of it
//
function clearActionBox(act,bg)
{
	bg	= dojo.byId(bg);
	act	= dojo.byId(act)

	dojo.html.hide(bg);
	dojo.html.hide(act);
}

// 

