var shroud = null;
var control = null;
var docloader = null;


function launchPopupDocument(url, docW, docH, layersDeep, enableClose){
	if(enableClose==undefined) enableClose=true;
	var isIE6 = (navigator.userAgent.indexOf("MSIE 6") != -1);
	var isMacFF = (navigator.userAgent.indexOf("Firefox") != -1) && (navigator.platform.indexOf("Mac") != -1);
	
	var traceback = "";
	for(i=0; i<layersDeep; i++) traceback+="../";
	var winH = (window.innerHeight) ? window.innerHeight : 0;
	if(window.scrollMaxY) winH += window.scrollMaxY;
	
	//create shroud layer
	shroud = document.createElement("DIV");
	shroud.style.zIndex = 20;
	shroud.style.position = "absolute";
	shroud.style.top = "0px";
	shroud.style.height = Math.max(document.documentElement.clientHeight,document.body.clientHeight, winH) + "px";
	shroud.style.left = "0px";
	shroud.style.width = "100%";
	
	if(!isMacFF){
	shroud.style.opacity = .7;
	shroud.style.filter = 'alpha(opacity=70)'; 
	shroud.style.backgroundColor = "#000000";
	shroud.style.color = "green";
	}else{
		shroud.style.backgroundImage="url(" + traceback + "images/black-70.png)";
	}// for IE
	document.body.appendChild(shroud);

	//create control layer
	var controlWidth = docW;
	var controlHeight = docH;
	control = document.createElement("DIV");
	control.style.zIndex = 30;
	control.style.display = "block";
	control.style.backgroundColor = "#FFFFFF";
	if(isIE6) control.style.position = "absolute";
	else if(isMacFF) control.style.position = "absolute";
	else control.style.position = "absolute";
	
	var windowHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
	var topval = Math.max(20,(windowHeight - controlHeight)/2) + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var windowWidth = (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth;
	var leftval = Math.max(0,(windowWidth - controlWidth)/2);
	
	control.style.top =  topval + "px";
	control.style.left =  leftval + "px";
	control.style.width = controlWidth + "px";
	control.style.height = controlHeight  + "px";
	control.style.overflow = "visible";
	control.style.padding = "0px";
	document.body.appendChild(control);
	
	var closedoc = null;
	if(enableClose){
		closedoc = document.createElement("DIV");
		var closelink = document.createElement("A");
		closelink.href = "javascript:closePopupDocument();";
		var imgwidth = 98;
		var imgheight = 12;
			var closeimg = document.createElement("IMG");
			closeimg.style.width= imgwidth + "px";
			closeimg.style.height= imgheight + "px";
			closeimg.style.border="none";
			if(!isIE6) closeimg.src = traceback + "images/CloseWindowDark.png";
			else {
				closeimg.src = traceback + "images/spacer.gif";
				closeimg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + traceback + "images/CloseWindowDark.png')"
			}
			closelink.appendChild(closeimg);
		closedoc.appendChild(closelink);
		closedoc.style.zIndex = 35;
		closedoc.style.position = "absolute";
		closedoc.style.width = imgwidth + "px";
		closedoc.style.height = imgheight + "px";
		closedoc.style.top = "0px";
		closedoc.style.marginTop = "10px";
		
		control.appendChild(closedoc);
	}
	
	docloader = document.createElement("IFRAME");
	docloader.frameBorder = 0;
	var timeInMillis = new Date().getTime();
	docloader.name = "docloader" + timeInMillis;
	docloader.id = "docloader" + timeInMillis;
	docloader.className = "popupDocumentIFrame";
	docloader.style.width = docW + "px";
	docloader.width = docW;
	docloader.style.height = docH + "px";
	docloader.height = docH;
	docloader.style.overflow = "auto";
	docloader.src = traceback + url;
	control.appendChild(docloader);

	if(enableClose){
		var Varoffset = 10;
		var url_tmp = url;
		url_tmp = url_tmp.split("&type=");
		
		//if(url_tmp[1] == "sample"){		// tmp fix.
		if(docloader.scrollHeight < docloader.offsetHeight){
			Varoffset = 20;
		}
		
		closedoc.style.marginLeft = (docW-(imgwidth + Varoffset)) + "px";
	}
	
	var imagelayer = document.createElement("DIV");
	imagelayer.style.width = "75px";
	imagelayer.style.height = "71px";
	imagelayer.style.position = "absolute";
	imagelayer.style.zIndex = 40;
	imagelayer.style.top = "0px";
	imagelayer.style.left = "0px";
	imagelayer.style.marginTop = "-38px";
	imagelayer.style.marginLeft = "-36px";
	if(!isIE6) imagelayer.innerHTML = "<img src=\"" + traceback + "images/mld-minilogo.png\" width=\"75\" height=\"71\"/>";
	else imagelayer.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + traceback + "images/mld-minilogo.png')";
	control.appendChild(imagelayer);
}//launchPopupDocument

function closePopupDocument(){
	if(control!=null){
		while(control.hasChildNodes()) control.removeChild(control.firstChild);
		document.body.removeChild(control);
	}
	if(shroud!=null) document.body.removeChild(shroud);
	control = null;
	shroud = null;
	if(docloader!=null){
		docloader.src = null;
	}
	docloader = null;
}//closePopupDocument
