var arrayPageSize = 0;
var arrayPageScroll = 0;


// ==================================
// dialogs
// ==================================

function showDialog(trigger) {
	var dialogStyle = getStyleObject(trigger);

	if (dialogStyle) {
		hideCurrentDialog();
		placeDialog(trigger);
		hideSelects();
		showOverlay();
		setupLoadingLayer();
		dialogStyle.visibility = "visible";
		window.currentlyVisibleDialog = trigger;
	}
}

function hideCurrentDialog() {
	if (window.currentlyVisibleDialog) {
		var dialogStyle = getStyleObject(window.currentlyVisibleDialog);
		if (dialogStyle)
				dialogStyle.visibility = "hidden";

		showSelects();
		window.currentlyVisiblePopup = false;
	}
}

function killDialog() {
	hideCurrentDialog();
	hideOverlay();
}



// ==================================
// general
// ==================================

function getStyleObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	}
	else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	}
	else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	}
	else {
		return false;
	}
}



// ==================================
// loading
// ==================================

function setupLoadingLayer() {
	// get dialog dimensions
	var dialogHeight = 200;
	var dialogWidth = 414;

	// find new dialog position
	var dialogTop = arrayPageScroll[1] + ((arrayPageSize[3]) / 2) - (dialogHeight/2);
	var dialogLeft = (arrayPageSize[0] / 2) - (dialogWidth / 2);

	// place dialog
  var dialogStyle = getStyleObject("layLoading");
			dialogStyle.left = (dialogLeft < 0) ? "0px" : dialogLeft + "px";
			dialogStyle.top = (dialogTop < 0) ? "0px" : dialogTop + "px";
}

function hideLoading() {
	var loadingStyle = getStyleObject('layLoading');
			loadingStyle.display = 'none';
}



// ==================================
// overlay
// ==================================

function showOverlay() {
	var overlayStyle = getStyleObject('layOverlay');

	// set height of Overlay to take up whole page and show
	overlayStyle.height = (arrayPageSize[1] + 'px');
	overlayStyle.display = 'block';
}

function hideOverlay() {
	var overlayStyle = getStyleObject('layOverlay');
			overlayStyle.display = 'none';
}



// ==================================
// positioning
// ==================================

function placeDialog(trigger) {
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();

	// get dialog dimensions
	var dialogHeight = returnObjById(trigger).offsetHeight;
	var dialogWidth = returnObjById(trigger).offsetWidth;

	// find new dialog position
	var dialogTop = arrayPageScroll[1] + ((arrayPageSize[3]) / 2) - (dialogHeight / 2);
	var dialogLeft = (arrayPageSize[0] / 2) - (dialogWidth / 2);

	// place dialog
  var dialogStyle = getStyleObject(trigger);
			dialogStyle.left = (dialogLeft < 0) ? "0px" : dialogLeft + "px";
			dialogStyle.top = (dialogTop < 0) ? "0px" : dialogTop + "px";
}

function reRlaceDialog() {
	if (window.currentlyVisibleDialog)
		placeDialog(window.currentlyVisibleDialog);
}

function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}



// ==================================
// fields
// ==================================

function clrDefVal(trigger) {
	if (trigger.value == trigger.defaultValue)
		trigger.value = "";
}

function setDefVal(trigger) {
	if (trigger.value == "")
		trigger.value = trigger.defaultValue;
}

function hideSelects() {
	return;
	var selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++)
          selects[i].style.visibility = "hidden";
}

function showSelects() {
	return;
	var selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++)
          selects[i].style.visibility = "visible";
}



// ==================================
// events
// ==================================

addEvent(window, "resize", reRlaceDialog);
addEvent(window, "scroll", reRlaceDialog);
