<!--
w=window;
d=document;
l=d.layers;
function gE(e){return d.getElementById(e)};
function cE(i){if(l){d.layers[i]=new Layer(0);eval("document."+i+"=d.layers[i]");}else{if(typeof d.createElement!='undefined'){X="<div id='"+i+"' style=\"position:absolute\">&nbsp;</div>";Y=d.createElement("DIV");if(Y){Y.innerHTML=X;d.body.appendChild(Y);}else if(typeof d.body.insertAdjacentHTML!='undefined')d.body.insertAdjacentHTML("BeforeEnd",X);}}}

function squares_center_el(el_id, h, v, sticky)
{
	var ww, wh, wl, wt, ew, eh;
	var el = d.getElementById(el_id);
	if (!el) return false;
	
	if (typeof w.innerWidth!='undefined') 
	{ ww = w.innerWidth;
		wh = w.innerHeight; } 
	else 
	{ if (d.documentElement && 
		    typeof d.documentElement.clientWidth != 'undefined' && 
		    d.documentElement.clientWidth != 0) 
		{ ww  = d.documentElement.clientWidth;
			wh = d.documentElement.clientHeight; } 
		else 
		{ if (d.body && 
			    typeof d.body.clientWidth != 'undefined')  {
				ww = d.body.clientWidth;
				wh = d.body.clientHeight; }
			else return false;
		}
	}
	if (typeof w.pageYOffset != 'undefined') 
	{ wt  = w.pageYOffset;
		wl = w.pageXOffset; }
	else if (d.compatMode && document.compatMode != 'BackCompat')
	{ wt = d.documentElement.scrollTop;
		wl = d.documentElement.scrollLeft; }
	else if (typeof d.body.scrollTop != 'undefined')
	{ wt = d.body.scrollTop;
		wl = d.body.scrollLeft; }
	else return false;

	ew = el.offsetWidth;
	eh = el.offsetHeight;

	if (isNaN(ew) || !ew || ew == undefined  ||
	    isNaN(eh) || !eh || eh == undefined  ||
	    isNaN(ww) || !ww || ww == undefined  ||
	    isNaN(wh) || !wh || wh == undefined  ||
	    isNaN(wl) || wl == undefined  ||
	    isNaN(wt) || wt == undefined)  
		return false;

	if (h) el.style.left = Math.round( wl + ww*0.5 - ew*0.5 ) +'px';
	if (v) el.style.top  = Math.round( wt + wh*0.5 - eh*0.5 ) +'px';

	if (sticky)
	{
	  if (typeof w.onresize == "function")
			eval("w.onresize = function(e) { squares_center_el('"+ el_id +"', "+ h +", "+ v +", false); var _y="+ w.onresize +"; _y(e); }");
		else
			eval("w.onresize = function(e) { squares_center_el('"+ el_id +"', "+ h +", "+ v +", false); }");

	  if (typeof window.onscroll == "function")
			eval("w.onscroll = function(e) { squares_center_el('"+ el_id +"', "+ h +", "+ v +", false); var _y="+ w.onscroll +"; _y(e); }");
		else
			eval("w.onscroll = function(e) { squares_center_el('"+ el_id +"', "+ h +", "+ v +", false); }");
	}

	return true;
}


/** 
	The constructor of the loader animation object
	Params:
	default_loader_text = the default loader text, e.g. "loading..."
	max_progress = the maximum amount of progress for the loader, e.g. a loading percentage (default = 100)
	extra_params = values that are special for the loader animation (.ini style string)
*/
var LASquares = function(default_loader_text, extra_params)
{
	this.sticky_mode = false;

	this.squares = new Object;
	this.squares.elements = new Array(4);
	this.squares.active = 0;

	// create main element (container)
	cE("squares_loader");
	this.squares.main = gE("squares_loader");
	if (!this.squares.main)
		throw new Error("error creating squares_loader element");
		
	html = '<div id="loader_text">'+ default_loader_text +'</div>';

	// create 4 loader squares
	for (var i = 0; i < 4; i++)
	{
		html += '<div id="la_square_'+ i +'" class="square"></div>';
	}

	this.squares.main.style.visibility = "hidden";
	this.squares.main.style.zIndex = 999; // on top :)
	this.squares.main.innerHTML = html;

	for (var i = 0; i < 4; i++)
	{
		this.squares.elements[i] = gE("la_square_"+ i);
		if (!this.squares.elements[i])
			throw new Error("Error creating square element: "+ "la_square_"+ i);
	}
	// get the loader text element
	this.el_text = gE("loader_text");
	if (!this.el_text)
		throw new Error("Error creating text element");
	
	// center elements
	this.setrect(NaN, NaN, NaN, NaN);
}

/** 
	This function is called when the loader needs to be shown to the user
	Params:
	url = the url that is currently being loaded 
	loader_text = a custom loading text (empty string = use default)
	loading_element = the element that is being updated by ajax (default = null)
*/
LASquares.prototype.show = function(url, loader_text, loading_element)
{
	if (typeof loader_text == "string" && loader_text != "")
		this.el_text.innerHTML = loader_text;
	with (this.squares)
	{
		active = 0;
		elements[active].className = "active_square";
	 
		main.style.visibility = "visible";
	}
}

/** 
	This function is called by the loady (user of the loader) to update the animation
	Params:
	progres = a value that indicates the amount of progress made
*/
LASquares.prototype.update = function(progress)
{
	with (this.squares)
	{
		elements[active].className = "square";
		active++;
		if (active >= elements.length) active = 0;
		elements[active].className = "active_square";
	}
}

/** 
	This function is called when the loader is no longer needed
*/
LASquares.prototype.hide = function()
{
	this.squares.main.style.visibility = "hidden";
}

/** 
	Use this function for positioning the loader element
	x = the x offset of the loader animation element (NaN = use default)
	y = the x offset of the loader animation element (NaN = use default)
	w = the width of the loader animation element (NaN = use default)
	h = the height of the loader animation element (NaN = use default)
*/
LASquares.prototype.setrect = function(x, y, w, h)
{
	if (isNaN(x) && isNaN(y) && isNaN(w) && isNaN(h))
	{
		squares_center_el(this.squares.main.id, true, true, !this.sticky_mode);
		this.sticky_mode = true;
	}
	else with(this.squares.main.style)
	{
		// there is no support for the following:
		//if (this.sticky_mode) un_center_el(this.squares.main.id);
	
		if (isNaN(x)) left = x +"px";
		if (isNaN(y)) top = y +"px";
		if (!isNaN(w)) width = w +"px";
		if (!isNaN(h)) height = h +"px";
	}
}
// -->
