/**
 * @author     JM Bruneau - Sven
 * @copyright  2008.6.3
 * @content    Alertwindow and content
 * --------------------
 * @by       Johan
 * @at       2009.01.12
 * @what     Atalanta implementation.
 * --------------------
 * @by       Thomas
 * @at       2009.02.03
 * @what	    extra array in arguments.
 * --------------------
 * @by       tvin
 * @at       2009.06.16
 * @what     absolute paths, makes alerts work everywhere =)
 * --------------------
 */

/** Document Vars
 * timer = variable containing the interval of the fade 
 * t = start opacity
 * alertspace = document.getElementById form alertspace
 * publicAlertId = public known alert ID: same alert for all languages
 * ready4Go = wait till the alertspace div is filled with the content form the ajax call
 * alertspaceWidth = width of the alertbox
 * screenCenter = center of the screen
 */
var tmr;
var t;
var alertspace; 
var publicAlertId;
var ready4Go;
var alertspaceWidth
var screenCenter;
var oldamount;

/** startAlert
 * If all ajax calls are hendled start filling the  alert space
 * If the alert space is filled initiate variables and start showing 
 */
function startAlert(publicAlertId, args)
{
	if (ready4Go != 1)
	{
		var params = 'alertId='+publicAlertId;
		if (args != undefined)
		{
			for (key in args) 
			{
				params +=  "&args["+key+"]="+encodeURIComponent(args[key]);
			}
		}
		// if you change this to GET, you will need to escape more things.
		loadFile('post', '/includes/alertbox.php', params, 'text', 'alertspace'); 
			
			ready4Go = 1; 
		functie="startAlert("+publicAlertId+")";
		setTimeout(functie, 55);
	}
	else
	{
		ready4Go = 0;
		alertspace = document.getElementById('alertspace');
		alertspaceWidth = 340;
		screenCenter = (document.body.innerWidth)? document.body.innerWidth / 2:document.body.offsetWidth / 2;
		alertspace.style.left = (screenCenter - (alertspaceWidth/2))+"px";
		show(true);
		t = 0;
		setTimer();
	}
}

/** closeAlert
 * Close the alert (start fadeout)
 */
function stopAlert(publicAlertId)
{
	t = -100;
	alertspace = document.getElementById('alertspace');
	setTimer();
	return false;
}

/** setTimer
 * timer used to fade in/out
 */
function setTimer() 
{
	timer = setInterval("fade()",20);
}

/** startAlert
 * if the opacity = 0 or 100 stop fading
 */
function fade() 
{
	oldamount = Math.abs(t);
	var amount = Math.abs(t+=10);
	if (oldamount <= amount && oldamount <= 20)
	{
		window.scrollTo(0,0);
	}
	if(amount == 0 || amount == 100)
	{
		clearInterval(timer);
	}
	amount = (amount == 100)?99.999:amount;
	alertspace.style.filter = "alpha(opacity:"+amount+")";
	alertspace.style.KHTMLOpacity = amount/100;
	alertspace.style.MozOpacity = amount/100;
	alertspace.style.opacity = amount/100;
	if(amount == 0)
	show(false);
}


function show(b) 
{
	(b)? alertspace.className = 'show':alertspace.className = 'hide';
}


/** moveable
 * On mouse Down on Title bar activate event handler
 * On mouse Up on Title bar Des-activate event handler 
 */
function moveable(action)
{
	if (action == 'start')
	{
		var alertbox = getObj("alertbox");
		if(alertbox.addEventListener)
		{
			alertbox.addEventListener('mousemove', moveAlertBox, false);
		}
		else if (alertbox.attachEvent)
		{
			alertbox.attachEvent('onmousemove', moveAlertBox);
		}
	}
	else
	{
		if(alertbox.removeEventListener)
		{
			alertbox.removeEventListener('mousemove', moveAlertBox, false);
		}
		else if (alertbox.detachEvent)
		{
			alertbox.detachEvent('onmousemove', moveAlertBox);
		}
	}
}

/** moveAlertBox
 * Position the alertbox to the mouselocation
 * 
 */
function moveAlertBox(event)
{
	if (!event)
	{
		var event = window.event;
	}
	if (event.pageX || event.pageY)
	{
		posx = event.pageX;
		posy = event.pageY;
	}
	else if (event.x || event.y)
	{
		posx = event.x;
		posy = event.y;
	}
	if (posy < 100)
	{
		posy = 100;
	}
	else if (posx < 100)
	{
		posx = 100;
	}
	getStyle("alertbox").top = posy;
	getStyle("alertbox").left = posx; 
}


function trace(publicAlertId, action, lang)
{
	loadFile('post', 'includes/trace.php', 'trace=alert&alertNr='+publicAlertId+'&action='+action+'&lang='+lang);
}