var divPopupHolder;
var divMaskHolder;
var divPosition;
var divWidth;
var dHeight;
var dWidth;

var divInterval;
var maskInterval;

function findPos(obj,xy) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	if(xy == 'x') return curleft;
	else return curtop;
}

function viewport(c){
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined'){
		viewportwidth = window.innerWidth - 16;
		viewportheight = window.innerHeight;
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	}

	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	if(c == 'width'){
		return viewportwidth;	
	} else if (c == 'height'){
		return viewportheight;
	} else {
		return (viewportwidth + ',' + viewportheight);
	}
	
}


function moveElement (element){
	
	if(divPosition == 'screen_center'){

		var eDiv = document.all? document.all.divPopUp : document.getElementById('divPopUp');
		var eBody = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
		
		var DSOC_Left = document.all? eBody.scrollLeft : pageXOffset;
		var DSOC_Top = document.all? eBody.scrollTop : pageYOffset;
		var VP_Width = viewport('width');
		var VP_Height = viewport('height');
		
		if (document.all || document.getElementById){
			eDiv.style.left = parseInt(DSOC_Left) + ((VP_Width/2)-(dWidth/2)) + "px";
			eDiv.style.top = DSOC_Top + ((VP_Height/2)-(dHeight/2)) + "px";
		}

		
	} else {
	
		dx = findPos(element, 'x');
		dx = dx - divWidth;
		document.getElementById('divPopUp').style.left = dx + "px";
	
	}
	
}

function fillHeight(){
	
	var eDiv = document.all? document.all.divMask : document.getElementById('divMask');
	var eBody = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	
	var DSOC_Top = document.all? eBody.scrollTop : pageYOffset;
	var VP_Height = viewport('height');
	var VP_Width = viewport('width');
	
	if (document.all || document.getElementById){
		eDiv.style.top = DSOC_Top + "px";
		eDiv.style.height = VP_Height + "px";
		eDiv.style.left = "0px";
		eDiv.style.width = VP_Width + "px";
	}
	
}

function createPopUp (pWidth,pHeight,pMask,pBorder,pPositioning,element,elementId,href){
	
	/*
		pWidth - Width of the modal window
		pHeight - Height of the modal window
		pMask - (Boolean) Show mask or not
		pBorder - (Boolean) Show border around modal window or not
		Element - The element calling this function
		Href - The url of the modal window
	*/
	
	dWidth = pWidth;
	dHeight = pHeight;
	
	x = findPos(element, 'x');
	y = findPos(element, 'y');
	
	if(pPositioning == 'left'){
		divPosition = pPositioning;
		divWidth = 0;
	} else {
		divPosition = pPositioning;
		divWidth = pWidth;
	}
	
	pBorderWidth = 1;
	if (typeof pBorder != 'undefined'){
		if(!pBorder) {
			pBorderWidth = 0;
		}
	}
	
	if (pMask) {
		borderColor = "E2E0DB";
	} else {
		borderColor = "9dd5cf";
	}
	
	divPopupHolder = document.createElement("div");
	divPopupHolder.id = "divPopUp";
	divPopupHolder.className= "divPopUp";
	divPopupHolder.style.position = "absolute";
	divPopupHolder.style.border = pBorderWidth + "px solid #" + borderColor;
	divPopupHolder.style.width = pWidth +"px";
	divPopupHolder.innerHTML = '<iframe src="'+href+'" name="'+elementId+'" id="'+elementId+'" style="border:0; width:'+pWidth+'px; height:'+pHeight+'px;" frameborder="0" scrolling="no"></iframe>';
	document.body.insertBefore(divPopupHolder, document.body.firstChild);
	
	if(divPosition == 'screen_center'){
		moveElement(element);
	} else {
		divPopupHolder.style.top = eval(y + 20) + "px";
		x = x - divWidth;
		divPopupHolder.style.left = eval(x) + "px";
	}
	
	divInterval = setInterval(moveElement, 1, element);
	
	// Create Mask	
	divMaskHolder = document.createElement("div");
	divMaskHolder.id = "divMask";
	divMaskHolder.style.height = viewport('height') + 'px';
	divMaskHolder.style.width = "100%";
	divMaskHolder.style.overflow = "hidden";
	divMaskHolder.style.top = "0px";
	divMaskHolder.innerHTML = "<img src='/images/popup/opacity_module.png' height='100%' width='100%' class='png'><br>";
	
	if(elementId != "compose_status"){
		divMaskHolder.onclick = closePopUp;
	}
	
	if (pMask) {
		divMaskHolder.className= "divMask";
	} else {
		divMaskHolder.className= "divClearMask";
	}
	document.body.insertBefore(divMaskHolder, document.body.firstChild);
	maskInterval = setInterval(fillHeight, 100);
	
}

function closePopUp() {
	
	if (typeof(divPopupHolder) != "undefined"){
		divPopupHolder.innerHTML = "";
		document.body.removeChild(divPopupHolder);
		divMaskHolder.innerHTML = "";
		document.body.removeChild(divMaskHolder);
		window.onscroll = "";
	}
	clearInterval(divInterval);
	clearInterval(maskInterval);
	
}