/*
NOTE: This file requires functionality from the aaijs.UI namespace
      in /ContentServer/Script/default.js.

USAGE:
	var waitBox =
		new aaijs.Controls.WaitBox(
			"/ContentServer/Skins/Common/images/gears.gif",
			true,
			"Processing...");

	waitBox.show();

OR

	(new aaijs.Controls.WaitBox(
		"/ContentServer/Skins/Common/images/gears_an_original.gif",
		true,
		"Processing...")).show();
*/
if (typeof (aaijs) == "undefined") aaijs = function() { }
if (typeof (aaijs.Controls) == "undefined") aaijs.Controls = function() { }

// precache the "loading" image - must be done for firefox and chrome
var bbImageUrl = "/ContentServer/Skins/Common/images/gears.gif";
var bbPreloadImage = new Image();
bbPreloadImage.src = bbImageUrl;

aaijs.Controls.WaitBox = function(imageUrl, isModal, text)
{
	if (typeof (imageUrl) == "string")
		this._imageUrl = imageUrl;
	if (typeof (isModal) == "boolean")
		this._isModal = isModal;
	if (typeof (text) == "string")
		this._text = text;
}

aaijs.Controls.WaitBox.prototype =
{
	_imageUrl: "",
	_isModal: false,
	_text: "",
	_box: null,
	_overlay: null,
	_isInitialized: false,
	createBox: function()
	{
		var img = document.createElement("img");
		img.alt = this._text;
		img.title = this._text;
		img.width = 141;
		img.height = 141;
		img.style.margin = "1px";
		
		var span = document.createElement("span");
		span.style.margin = "2px";
		span.innerHTML = this._text;
		
		this._box = document.createElement("div");
		
		this._box.style.backgroundColor = "#fff";
		this._box.style.color = "#000";
		this._box.style.textAlign = "center";
		this._box.style.padding = "1px";
		this._box.className = "waitbox"; // in case default styles are overridden
		this._box.style.display = "none";
		this._box.style.position = "absolute";
		this._box.style.zIndex = 500;

		this._box.appendChild(img);
		this._box.appendChild(document.createElement("br"));
		this._box.appendChild(span);
		document.body.appendChild(this._box);
		
		img.src = this._imageUrl;
	},
	createOverlay: function()
	{
		if (!this._isModal)
			return;

		this._overlay = document.createElement("div");
		this._overlay.className = "waitbox-overlay";
		this._overlay.style.display = "none";
		this._overlay.style.position = "absolute";
		this._overlay.style.zIndex = 400;
		this._overlay.style.opacity = 0.7;
		this._overlay.style.filter = "alpha(opacity=70)";

		document.body.appendChild(this._overlay);
	},
	initialize: function()
	{
		if (this._isInitialized)
			return;

		this.createBox();
		this.createOverlay();

		this._isInitialized = true;
	},
	show: function()
	{
		this.initialize();
		
		var windowSize = aaijs.UI.getInnerWindowSize();
		var scrollPosition = aaijs.UI.getScrollPosition();

		if (this._isModal)
		{
			var documentSize = aaijs.UI.getDocumentSize();
			this._overlay.style.display = "block";
			this._overlay.style.left = "0px";
			this._overlay.style.top = "0px";
			this._overlay.style.width = documentSize.width + "px";
			this._overlay.style.height = documentSize.height + "px";
		}

		this._box.style.display = "block";

		var boxWidth = this._box.offsetWidth || 0;
		var boxHeight = this._box.offsetHeight || 0;

		this._box.style.left = Math.round(scrollPosition.x + (windowSize.width / 2) - (boxWidth / 2)) + "px";
		this._box.style.top = Math.round(scrollPosition.y + (windowSize.height / 2) - (boxHeight / 2)) + "px";
	},
	hide: function()
	{
		if (this._overlay)
			this._overlay.style.display = "none";
		if (this._box)
			this._box.style.display = "none";
	}
}

// how to override the BusyBox
BusyBox = function()
{
	var message = "Processing...";

	// see if the message was passed into the constructor
	if (arguments && arguments.length >= 9 && typeof (arguments[8]) == "string")
		message = arguments[8];
	
	this._waitBox = new aaijs.Controls.WaitBox(bbImageUrl, false, message);
	this._waitBox.initialize();
}

BusyBox.prototype =
{
	_waitBox: null,
	Show: function()
	{
		this._waitBox.show();
	},
	Hide: function()
	{
		this._waitBox.hide();
	}
}

