var iframeCounter = 0;

// open a dialog box
function dialogBoxShowWindow(theId, top, left, width, height, position_over, url, modal) {

	var backgroundId = '';
	
	if(modal) {
		backgroundId = 'dialogBackgroundDark';
		var bg = document.createElement('div'); 
		bg.setAttribute('id',backgroundId);
		bg.style.position 			= 'absolute';
		bg.style.display 			= 'none';
		bg.style.backgroundColor 	= '#000000';
		bg.style.opacity 			= .80;
		bg.style.filter 			= 'alpha(opacity=80)';
		document.body.appendChild(bg);
	}
	
	var dialog = document.createElement('div'); 
	dialog.setAttribute('id',theId);
	dialog.style.display = 'none';
	dialog.style.zIndex  = 100;
	document.body.appendChild(dialog);
	
	var div = document.getElementById(theId);
	if(div) {
		if(url) {
			if(url.indexOf('?') != -1) {
				url += '&dialogId=' + theId;
			}
			else {
				url += '?dialogId=' + theId;
			}
	
			div.innerHTML = '<iframe id=iframe_' + theId + ' scrolling="no" allowtransparency="true" frameborder="0" marginwidth="0" marginheight="0" style="width:' + width + ';height:' + height + ';" src="' + url + '"></iframe>';
		}
		dialogBoxOpen(theId, backgroundId, top, left, width, height, position_over, false);
	}
	else {
		alert('could not find div ' + theId);
	}
	
	return div;
}

// close a dialog  box
function dialogBoxCloseWindow(theId) {
	dialogBoxClose(theId);
}

// -------------------------------------------
// private utilities for managing dialog boxes
// -------------------------------------------

var openDialogBoxs = new Array();

function dialogBoxRegisterDialogBox(id,backgroundId,overId) {
	openDialogBoxs[openDialogBoxs.length] = id + ',' + backgroundId + ',' + overId;
}

function dialogBoxDeregisterDialogBox(theId) {
	
	var idx = findDialogBox(theId);
	
	if(idx >= 0) {
		openDialogBoxs.splice(idx,1);
	}
}

function findDialogBox(theId) {
	var found = false;
	var idx;
	
	for(var i = 0; i < openDialogBoxs.length; i++) {
		var divs = openDialogBoxs[i].split(',');
		if(divs[0] == theId) {
			found = true;
			idx = i;
		}
	}
	
	if(found) {
		return idx;
	}
	else {
		return -1;
	}
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent) {
		while(1) {
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent) { break; }
		  obj = obj.offsetParent;
		}
	}
	else if(obj.x) { curleft += obj.x; }
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent) { break; }
		  obj = obj.offsetParent;
		}
	}
	else if(obj.y) { curtop += obj.y; }
	return curtop;
}

function getScrollTop() {
	var offset = 0;
	
	if(window.document.documentElement && window.document.documentElement.scrollTop) {
		offset=window.document.documentElement.scrollTop;
	}
	else if(window.document.body && (window.document.body.scrollTop != undefined)) {
		offset=window.document.body.scrollTop;
	}
	
	return offset;
}

function getWindowHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
    
  return myHeight;
}


function dialogBoxOpen(dialogId, backgroundId, position_top, position_left, width, height, position_over, fixed) {

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	var windowHeight = myHeight;
	var windowWidth = myWidth;
	var docHeight = document.body.offsetHeight;
	var docWidth = document.body.offsetWidth;

	var windowLeft = 0;
	var fullHeight = docHeight;
	var fullWidth  = docWidth;
	if (windowWidth > docWidth) {
		fullWidth = windowWidth;
		windowLeft = Math.round( (docWidth - windowWidth)/2 );
	}
	if (windowHeight > docHeight) {
		fullHeight = windowHeight;
	}
	
	if(backgroundId) {
		var backgroundNode = document.getElementById(backgroundId);

		var h = 0;
		if (document.body.scrollHeight > document.body.offsetHeight) {
		 	h = document.body.scrollHeight; // all but Explorer Mac
		}
		else {
 			h = document.body.offsetHeight
		}

		backgroundNode.style.height 	= h+"px";
		backgroundNode.style.width 		= fullWidth+"px";
		backgroundNode.style.top 		= '0';
		backgroundNode.style.left 		= windowLeft+"px";
		backgroundNode.style.zIndex	    = 99;

		backgroundNode.style.display = "";
	}
	
	// next, center and make the dialog div visible
	var dialogNode = document.getElementById(dialogId);
	
	dialogNode.style.display = "block";

	if(width) {
		dialogNode.style.width	= width + "px";
	}
	
	if(height) {
		dialogNode.style.height	= height + "px";
	}

	var dialogHeight = dialogNode.offsetHeight;
	var dialogWidth  = dialogNode.offsetWidth;

	var top  		= position_top;
	var left 		= position_left;
	
	if(position_over) {
		var position_over_obj = document.getElementById(position_over);
		left = findPosX(position_over_obj);
		top  = findPosY(position_over_obj);
	}

	if(top) {
		dialogNode.style.top = top + "px";
	}
	else {
		var ScrollTop = document.body.scrollTop;
		
		if (ScrollTop == 0) {
		 	if (window.pageYOffset) {
				ScrollTop = window.pageYOffset;
			}
			else {
				ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
		}    
    
    	dialogNode.style.top = ScrollTop + 30;
	}

	if(left) {
		dialogNode.style.left 	= left + "px";
	}
	else {
		dialogNode.style.left = ""+( (windowWidth-dialogWidth)/2 + windowLeft)+"px";
	}
	
	dialogNode.style.zIndex = 100;

	if(fixed) {
		dialogNode.style.position = 'fixed';
	}
	else {
		dialogNode.style.position = 'absolute';
	}

	dialogNode.style.display = "";

	dialogBoxRegisterDialogBox(dialogId, backgroundId, position_over);
}

function dialogBoxClose(theId) {

	var idx = findDialogBox(theId);

	var divs = openDialogBoxs[idx].split(',');

	var id 				= divs[0];
	var backgroundId 	= divs[1];
	var overId 			= divs[2];

	dialogBoxDeregisterDialogBox(theId);

	var dialogNode = document.getElementById(id);
	if(dialogNode) {
		dialogNode.style.display = "none";
		dialogNode.parentNode.removeChild(dialogNode);
	}
	else {
		alert('could not delete ' + id);
	}

	if(backgroundId) {
		var backgroundNode = document.getElementById(backgroundId);
		if(backgroundNode) {
			backgroundNode.style.display = "none";
			backgroundNode.parentNode.removeChild(backgroundNode);
		}
		else {
			alert('could not delete ' + backgroundId);
		}
	}
}


