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

	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" style="margin:0; border:0; width:' + width + 'px;height:' + height + 'px;" src="' + url + '" frameborder=0></iframe>';
		}
		dialogBoxOpen(theId, backgroundId, top, left, width, height, position_over, fixed);
	}
	else {
		alert('could not find div ' + theId);
	}
	
	return div;
}

// close a dialog  box
function dialogBoxCloseWindow(theId,closebox) {
	dialogBoxClose1(theId);
	if(!closebox) {
		promoPopInGoToCloseDestination();
	}
	promoPopInSetCloseDestination('');
}

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

var openDialogBoxs = new Array();

var destinationOnClose = '';

function promoPopInSetCloseDestination(url) {
	destinationOnClose = url;
}


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 viewport_dimensions    = document.viewport.getDimensions();
	var viewport_scroll_offset = document.viewport.getScrollOffsets()
	
	if(backgroundId) {
		var backgroundNode = document.getElementById(backgroundId);

		if(viewport_dimensions.height > document.body.clientHeight) {
			backgroundNode.style.height = (viewport_dimensions.height) + "px";
		}
		else {
			backgroundNode.style.height = (document.body.clientHeight + 16) + "px";
		}
		backgroundNode.style.width 		= (document.body.clientWidth + 16) + "px";
		backgroundNode.style.top 		= '0px';
		backgroundNode.style.left 		= "0px";
		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 {    
    	dialogNode.style.top = (viewport_scroll_offset.top + 30) + "px";
	}

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

	if(fixed) {
		dialogNode.style.top = "100px";
		dialogNode.style.position = 'fixed';
	}
	else {
		dialogNode.style.position = 'absolute';
	}

	dialogNode.style.display = "";
	//$(dialogId).fade({ duration: 1, from: 0, to: 1 });


	dialogBoxRegisterDialogBox(dialogId, backgroundId, position_over);
}

function dialogBoxClose1(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);
		}
	}
}

function promoPopInGoToCloseDestination() {
	if(destinationOnClose) {
		if(window.parent) {
			window.parent.location.href = destinationOnClose;
		}
		else {
			window.location.href = destinationOnClose;
		}
	}
}


