var popup;
var mask;

var width;

var popup_position;
var popup_height;
var popup_width;

var popup_interval;
var mask_interval;

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 (input_element){
	
	if(popup_position == 'center'){

		var eDiv = document.all? document.all.popup_div : document.getElementById('popup_div');
		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)-(popup_width/2)) + "px";
			eDiv.style.top = DSOC_Top + ((VP_Height/2)-(popup_height/2)) + "px";
		}

		
	} else {
	
		dx = findPos(input_element, 'x');
		dx = dx - width;
		document.getElementById('popup_div').style.left = dx + "px";
	
	}
	
}

function fillHeight(){
	
	var eDiv = document.all? document.all.mask_div : document.getElementById('mask_div');
	var eBody = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	
	var DSOC_Top = document.all? eBody.scrollTop : pageYOffset;
	var DSOC_Left = document.all? eBody.scrollLeft : pageXOffset; 
	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 = DSOC_Left + "px";
		eDiv.style.width = VP_Width + "px";
	}
	
}

function createPopUp (input_width, 
					  input_height, 
					  input_mask, 
					  input_border, 
					  input_position, 
					  input_element,
					  input_elementid,
					  input_closeonclick,
					  input_href){

	popup_width = input_width;
	popup_height = input_height;
	
	popup_x = findPos(input_element, 'x');
	popup_y = findPos(input_element, 'y');
	
	if(input_position == 'left'){
		popup_position = input_position;
		width = 0;
	} else {
		popup_position = input_position;
		width = input_width;
	}
	
	border_width = 1;
	if (typeof input_border != 'undefined'){
		if(!input_border) {
			border_width = 0;
		}
	}
	
	if (input_mask) {
		border_color = "E2E0DB";
	} else {
		border_color = "9dd5cf";
	}
	
	popup = document.createElement("div");
	popup.id = 				"popup_div";
	popup.className = 		"popup_div";
	popup.style.position = 	"absolute";
	popup.style.border = 	border_width + "px solid #" + border_color;
	popup.style.width = 	input_width +"px";
	popup.innerHTML = 		
		
		'<table cellspacing=0 cellpadding=0 border=0 style="border:none; width:'+ input_width +'px; height:'+ input_height +'px;">' +
			
			'<tr>' + 
				
				'<td style="width:10px; height:10px; vertical-align:top;">' + 
					'<img src="/includes/popup/shadow.top_left.png" class="png" height="10" width="10" /><br />' +
				'</td>' +
				
				'<td style="height:10px; background:url(/includes/popup/shadow.top.png) repeat-x; behavior: url(/includes/_png.htc);">' +
					'<img src="/images/dot_clear.gif" /><br />' +
				'</td>' +
				
				'<td style="width:10px; height:10px; vertical-align:top;">' + 
					'<img src="/includes/popup/shadow.top_right.png" class="png" height="10" width="10" /><br />' +
				'</td>' +
				
			'</tr>' +
			
			'<tr>' +
		
				'<td style="width:10px; background:url(/includes/popup/shadow.left.png) repeat-y; behavior: url(/includes/_png.htc);">' +
					'<img src="/images/dot_clear.gif" /><br />' +
				'</td>' +
				
				'<td align="center" style="vertical-align:top;">' +
					'<iframe src="'+input_href+'" name="'+input_elementid+'" id="'+input_elementid+'" style="border:0; width:'+(input_width-10)+'px; height:'+(input_height-10)+'px;" frameborder="0" scrolling="no"></iframe>' +
				'</td>' +
				
				'<td style="width:10px; background:url(/includes/popup/shadow.right.png) repeat-y; behavior: url(/includes/_png.htc);">' +
					'<img src="/images/dot_clear.gif" /><br />' +
				'</td>' +
				
			'</tr>' +
			
			'<tr>' +
				
				'<td style="width:10px; height:10px; vertical-align:bottom;">' +
					'<img src="/includes/popup/shadow.bottom_left.png" class="png" height="10" width="10" /><br />' +
				'</td>' +
				
				'<td style="height:10px; background:url(/includes/popup/shadow.bottom.png) repeat-x; behavior: url(/includes/_png.htc);">' +
					'<img src="/images/dot_clear.gif" /><br />' +
				'</td>' +
				
				'<td style="width:10px; height:10px; vertical-align:bottom;">' +
					'<img src="/includes/popup/shadow.bottom_right.png" class="png" height="10" width="10" /><br />' +
				'</td>' +
				
			'</tr>' +
			
		'</table>';
	
	document.body.insertBefore(popup, document.body.firstChild);
	popup_interval = setInterval(moveElement, 1, input_element);
	
	if(popup_position == 'center'){
		moveElement(input_element);
	} else {
		popup.style.top = 	eval(popup_y + 20) + "px";
		popup_x = 			popup_x - width;
		popup.style.left = 	eval(popup_x) + "px";
	}
		
	// Create Mask	
	mask = 					document.createElement("div");
	mask.id = 				"mask_div";
	mask.style.height = 	viewport('height') + 'px';
	mask.style.width = 		"100%";
	mask.style.overflow = 	"hidden";
	mask.style.top = 		"0px";
	mask.innerHTML = 		"<br />";
	
	if(input_closeonclick){
		mask.onclick = closePopUp;
	}
	
	if (input_mask) {
		mask.className= "mask_div";
	} else {
		mask.className= "mask_clear";
	}
	
	document.body.insertBefore(mask, document.body.firstChild);
	mask_interval = setInterval(fillHeight, 100);
	
}

function closePopUp() {
	
	if (typeof(popup) != "undefined"){
		popup.innerHTML = "";
		document.body.removeChild(popup);
		mask.innerHTML = "";
		document.body.removeChild(mask);
		window.onscroll = "";
	}
	clearInterval(popup_interval);
	clearInterval(mask_interval);
	
}
