/*============================== Class structure and function dialogWindow ===============================*/
function cDialogWindow() {
/*------------------------------------- Class structure --------------------------------------*/
// layout related variables
this.oDialog = null;
this.oDialogShadow = null;
this.oMessage1 = null;
this.oMessage2 = null;
this.oProgressBarBorder = null;
this.oProgressBar = null;
this.oAltTD = null
this.oAltButton = null
this.oBlankTD = null
this.oDefaultTD = null
this.oDefaultButton = null
this.oFilter = null;

// data related variables
this.messageQueue = new Array();
this.intervalHandler = null;
this.finishX = 99;
this.finishOpacity = 0;
this.borderX = null;

this.create = create;
this.show = show;
this.hide = hide;
this.stopRun = stopRun;
this.setPosition = setPosition;
this.addMessageToQueue = addMessageToQueue;
this.runMessageQueue = runMessageQueue;
this.setButtons = setButtons;
this.showButtonsArea = showButtonsArea;
this.hideButtonsArea = hideButtonsArea;
this.resetButtons = resetButtons;
/*------------------------------------- Class functions --------------------------------------*/
function create() {
	// create the dialog window
	this.oDialog = document.createElement("div");
	this.oDialog.id = "dialogWindow";

	// create dialog window structure
	var oTable = this.oDialog.appendChild(document.createElement("table"));
	oTable.cellSpacing = "0";
	oTable.cellPadding = "0";
	oTable.style.direction = "rtl";
	oTable.style.width = "100%";
	oTable.style.height = "100%";
	

	this.oMessage1 = oTable.insertRow(-1).insertCell(-1);
	this.oMessage1.style.textAlign = "center";
	this.oMessage1.style.height = "25%";
	this.oMessage1.innerHTML = "&nbsp;";

	this.oMessage2 = oTable.insertRow(-1).insertCell(-1);
	this.oMessage2.style.textAlign = "center";
	this.oMessage2.style.height = "25%";
	this.oMessage2.innerHTML = "&nbsp;";

	var oTD = oTable.insertRow(-1).insertCell(-1);
	oTD.style.textAlign = "center";
	oTD.style.verticalAlign = "middle";
	oTD.style.height = "25%";

	this.oProgressBar = document.createElement("div");
	this.oProgressBar.id = "dialogProgressBar";
	
	this.oProgressBarBorder = document.createElement("div");
	this.oProgressBarBorder.id = "dialogProgressBorder";
	this.oProgressBarBorder.appendChild(this.oProgressBar);
	oTD.appendChild(this.oProgressBarBorder);

	// create buttons
	this.oButtonsArea = oTable.insertRow(-1).insertCell(-1);
	this.oButtonsArea.style.textAlign = "center";
	this.oButtonsArea.style.height = "25%";
	this.oButtonsArea.style.visibility = "hidden";

	this.oDefaultButton = document.createElement("input");
	this.oDefaultButton.type = "button";
	this.oDefaultButton.value = "Default";
	this.oDefaultButton.className = "Button";

	this.oAltButton = document.createElement("input");
	this.oAltButton.type = "button";
	this.oAltButton.value = "Alt";
	this.oAltButton.className = "Button";

	var oTable = document.createElement("table");
	oTable.cellSpacing = "0";
	oTable.cellPadding = "0";
	oTable.style.margin = "auto auto";
	oTable.style.direction = "rtl";
	
	var oTR = oTable.insertRow(-1);
	this.oAltTD = oTR.insertCell(-1);
	this.oAltTD.style.display = "none";
	this.oAltTD.appendChild(this.oAltButton);

	this.oBlankTD = oTR.insertCell(-1);
	this.oBlankTD.style.width = "20px";
	this.oBlankTD.style.display = "none";

	this.oDefaultTD = oTR.insertCell(-1);
	this.oDefaultTD.appendChild(this.oDefaultButton);

	this.oButtonsArea.appendChild(oTable);

	// append the dialog window
	document.body.appendChild(this.oDialog);

	// create the dialog window shadow
	this.oDialogShadow = document.createElement("div");
	this.oDialogShadow.id = "dialogWindowShadow";

	// append the dialog window shadow
	document.body.appendChild(this.oDialogShadow);

	// get a reference to the progress bar filter
	if (this.oProgressBar.filters) {
		this.oFilter = this.oProgressBar.filters.item("DXImageTransform.Microsoft.Alpha");
	}
}

function show(progressBarVisibility) {
	// set the dialog window dialog
	this.setPosition();

	// display the progress bar
	if (progressBarVisibility) {
		if (this.oFilter) {
			this.oProgressBar.style.visibility = "visible";
			this.oProgressBarBorder.style.visibility = "visible";
		}
	} else {
		this.oProgressBar.style.visibility = "hidden";
		this.oProgressBarBorder.style.visibility = "hidden";

		// cancel progress bar
		this.finishX = 0;
		this.finishOpacity = 100;
	}

	// display the dialog window
	this.oDialog.style.display = "block";
	this.oDialogShadow.style.display = "block";

	// lock page, hide selects and disable buttons
	appLock(this.oDefaultButton, this.oAltButton);
}

function hide() {
	// hide the dialog window
	this.oDialogShadow.style.display = "none";
	this.oDialog.style.display = "none";

	// reset the intervalHandler
	this.intervalHandler = null;

	// reset the progress bar
	if (this.oFilter) {
		this.finishX = 99;
		this.finishOpacity = 0;
		this.oFilter.finishX = this.finishX;
		this.oFilter.finishOpacity = this.finishOpacity;
		this.borderX = null;
	}

	// reset the messages and message queue
	this.messageQueue = new Array();
	this.oMessage1.innerHTML = "&nbsp;";
	this.oMessage2.innerHTML = "&nbsp;";

	// hide the progress bar
	this.oProgressBar.style.visibility = "hidden";
	this.oProgressBarBorder.style.visibility = "hidden";

	// reset and hide buttons
	this.setButtons("Default", null, "Alt", null);
	this.hideButtonsArea()

	// unlock page, show selects and enable buttons
	appUnlock();
}

function stopRun() {
}

function setPosition() {
	var left = Math.round((document.body.clientWidth - 300) / 2);
	var top = Math.round((document.body.clientHeight - 250) / 2);
	this.oDialog.style.left = left;
	this.oDialog.style.top = top;
	this.oDialogShadow.style.left = left + 7;
	this.oDialogShadow.style.top = top + 7;
}

function addMessageToQueue(message1, message2, newBorderX) {
	this.messageQueue.push(new cDialogQueue(message1, message2, newBorderX));
	if (this.intervalHandler == null) this.intervalHandler = setInterval("dialogWindow.runMessageQueue()", 10);
}

function runMessageQueue() {
	if ((this.finishX > this.borderX) && (this.borderX != null)) {
		// advance the progressbar
		if (this.oFilter) this.oFilter.finishX = this.finishX - 1;
		this.finishX = this.finishX - 1
	} else {
		this.borderX = null;
		switch (this.messageQueue.length) {
			default:	// > 0
				// display the messages and set the borderX
				if (this.messageQueue[0].message1) this.oMessage1.innerHTML = this.messageQueue[0].message1;
				if (this.messageQueue[0].message2) this.oMessage2.innerHTML = this.messageQueue[0].message2;
				this.borderX = this.messageQueue[0].borderX;
	
				this.messageQueue = this.messageQueue.delItem(0);
			break;

			case 0:
				if (this.finishX == 0) {
					if (this.finishOpacity < 100) {
						// add to opacity;
						this.finishOpacity = this.finishOpacity + 2;
						if (this.oFilter) this.oFilter.finishOpacity = this.finishOpacity;
					} else {
						// stop the run
						if (this.intervalHandler) clearInterval(this.intervalHandler);

						// show buttons
						this.showButtonsArea();
					}
				}
			break;
		}
	}
}

function setButtons(defaultValue, defaultCommand, altValue, altCommand) {
	this.oDefaultButton.value = defaultValue;
	this.oDefaultButton.onclick = function() {dialogWindow.hide(); eval(defaultCommand)}
	this.oAltButton.value = altValue;
	this.oAltButton.onclick = function() {dialogWindow.hide(); eval(altCommand)}

	if (altValue) {
		this.oBlankTD.style.display = "block";
		this.oAltTD.style.display = "block";
	} else {
		this.oBlankTD.style.display = "none";
		this.oAltTD.style.display = "none";
	}
}

function showButtonsArea() {
	this.oButtonsArea.style.visibility = "visible";
	//this.oDefaultButton.focus();
}

function hideButtonsArea() {
	this.oButtonsArea.style.visibility = "hidden";
}

function resetButtons() {
	var NC = "";
	this.oButtonsArea.innerHTML = NC;
}
/*=================================== Instance Declaration ===================================*/
}
var dialogWindow = new cDialogWindow();

if (top.preloader) top.preloader.advanceProgressBar();