var currentDraggingWindow;
var windowList = new Array();

	
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
var fShow;
var fHide;

if (isIE)    {
	fShow="visible";fHide="hidden";
}
if (isNN)    
{
	fShow="visible";fHide="hidden";
}

if (isN4) 
{
	fShow="show";fHide="hide";
}

function MM_findObj(n, d) { //v4.01
	  var p,i,x;  
	  if(!d) 
	  	d=document; 
	  if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);		
	  }
	  if(!(x=d[n])&&d.all) 
	  	x=d.all[n];
	  for (i=0;!x&&i<d.forms.length;i++)
	  	x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
	  	x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById)
	  	 x=d.getElementById(n); 
	  return x;
}

function PopupWindow(objName, title, width, height, src, isFrame) {
	this.objName = objName;
	this.title = title;
	this.width=width;
	this.src = src;
	this.height=height;

	this.titleObj="titleBar";
	this.controlFrame="controlScreen";
	this.bodyFrame="bodyFrame";
	this.dragging = true;
	this.nowX=0;
	this.nowY=0;
	this.offsetx=0;
	this.offsety=0;

	this.show = function() {
		document.onmousedown=checkToDrag;
		var strTitle="";
		strTitle+="<div id=\"titleBar\" style=\"width:"+(this.width)+"px \">";
		strTitle+="<span class=\"left\">&nbsp;";
		strTitle+=this.title;
		strTitle+="</span><a href=\"javascript:hideWindow('"+objName+"')\" class=\"right\"><img src=\"http://www.procamrunning.in/img/close.png\" align=\"right\" width=\"15\" height=\"15\"></a>";
		strTitle+="</div>";
		strTitle+="<div id=\"controlScreen\" style=\"width:"+this.width+"px ;height:14px;\">";
		strTitle+="</div>";
		strTitle+="<div id=\"bodyFrame\" style=\"top:-"+(this.height-24)+";width:"+this.width+"px;height:"+(this.height-36)+"px;\">";
		if (isFrame) {
			strTitle+="<iframe name=\"contentFrame\" width=\""+this.width+"\" height=\""+(this.height-36)+"\" frameborder=\"no\" border=\"0\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\" SCROLLING=\"auto\" src=\""+this.src+"\"></iframe>";
		} else {
			strTitle+=src;
		}
		strTitle+="</div>";			
		var obj = MM_findObj(this.objName);
		obj.innerHTML = strTitle;
		obj.style.visibility=fShow;
		obj.style.overflow="hidden";
		obj.style.width=this.width+3;
		obj.style.height=this.height+1;
		this.position();
		addWindow(this);
		/*if (isFrame) {
			window.frames["contentFrame"].document.onkeydown=escWindow;
		} else {
			document.onkeydown=escWindow;
		}*/	
	}
	
	this.hide = function() {
		var obj = MM_findObj(this.objName);
		obj.style.visibility=fHide;
		obj.innerHTML="";
	}

	this.position = function(x, y) {
	}
	
	this.position = function() {
		var yScroll = 0;
		if (document.layers) {
			yScroll = window.pageYOffset;
		} else {
			yScroll = document.body.scrollTop || document.documentElement.scrollTop;
		}
		browseWidth=screen.width;
		browseHeight=screen.height;
		var obj = MM_findObj(this.objName);
		obj.style.top= yScroll + ((browseHeight - obj.offsetHeight) / 2)  + 'px';
		obj.style.left= ((browseWidth - obj.offsetWidth) / 2) + 'px';
	}

	this.startDrag = function(e) {
		var ie = document.all;
		var obj = MM_findObj(this.objName);
		offsetx= ie ? event.clientX : e.clientX;
		offsety= ie ? event.clientY : e.clientY;
		nowX=parseInt(obj.style.left);
		nowY=parseInt(obj.style.top);
		this.dragging = true;
		document.onmousemove=keepDragging;
		document.onmouseup=dragEnd;

		var objScreen=MM_findObj(this.controlFrame);
		var objBody=MM_findObj(this.bodyFrame);
		if (ie) {
			objScreen.style.background="#ffffff";
		}
		objScreen.style.zIndex=obj.style.zIndex + 2;
		objBody.style.zIndex=obj.style.zIndex + 1;
	}

	this.endDrag = function() {
		this.dragging = false;
		document.onmousemove=null;
		document.onmouseup=null;
		var obj = MM_findObj(this.objName);
		var objScreen=MM_findObj(this.controlFrame);
		var objBody=MM_findObj(this.bodyFrame);
		objScreen.style.zIndex=obj.style.zIndex + 1;
		objBody.style.zIndex=obj.style.zIndex + 2;
	}

	this.drag = function(e) {
		var ie = document.all;
		var obj = MM_findObj(this.objName);
		obj.style.left= (ie ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx)+"px"; 
		obj.style.top= (ie ? nowY+event.clientY-offsety : nowY+e.clientY-offsety)+"px"; 
	}
}
/*
function pos(){
	document.getElementById('light').style.display='block';
	var yScroll = 0;
	if (document.layers) {
		yScroll = window.pageYOffset;
	} else {
		yScroll = document.body.scrollTop || document.documentElement.scrollTop;
	}
	browseWidth=screen.width;
	browseHeight=screen.height;
	var obj = document.getElementById('light').className;
	obj.innerHTML="<div id='light' class='white_content' style=''top:'+ yScroll + ((browseHeight - obj.offsetHeight) / 2)  + 'px';'left'+ ((browseWidth - obj.offsetWidth) / 2) + 'px';'><a href='#' onclick='document.getElementById('light').style.display='none';	document.getElementById('fade').style.display='none''><img src='<?php echo SITEURL; ?>/img/close.png' style='margin-top:-22px;' class='right'></a><br>";
	alert(obj.innerHTML);
		obj.style = 'top:'+ yScroll + ((browseHeight - obj.offsetHeight) / 2)  + 'px';
	obj.style = 'left'+ ((browseWidth - obj.offsetWidth) / 2) + 'px';
}*/

function checkToDrag(e) {
	var ie = document.all;
	var topDog = ie ? "BODY" : "HTML";
	var eventSource = ie ? event.srcElement : e.target;  
	if (windowList != null && windowList.length > 0)
	{
		for (var i=0; i<windowList.length; i++)
		{
			var titleObj = windowList[i].titleObj;
			while (eventSource.id != titleObj && eventSource.tagName != topDog) {
				eventSource= ie ? eventSource.parentElement : eventSource.parentNode;
			}
			
			if (eventSource.id == titleObj) {
				windowList[i].startDrag(e);
				currentDraggingWindow = windowList[i];
				break;
			}
		}
	}
}

function keepDragging(e) {
	if (currentDraggingWindow != null)
	{
		currentDraggingWindow.drag(e);
	}
	return false;  
}

function dragEnd(e) {
	if (currentDraggingWindow != null)
	{
		currentDraggingWindow.endDrag();
		currentDraggingWindow=null;
	}
	return false;  
}

function hideWindow(objName) {
	if (windowList != null && windowList.length > 0)
	{
		for (var i=0; i<windowList.length; i++)
		{
			if (windowList[i].objName == objName)
			{
				windowList[i].hide();
				removeWindow(i);
				break;
			}
		}
	}
}


function addWindow(obj) {
	windowList[windowList.length]=obj;
}

function removeWindow(index) {
	var len=windowList.length;	
	if(len !=0){
		for (var i=index;i<len-1;i++){
			windowList[i]=windowList[i+1];
		}
	}
	windowList.length = windowList.length-1;
}

function findWindow(objName) {
	if (windowList != null && windowList.length > 0)
	{
		for (var i=0; i<windowList.length; i++)
		{
			if (windowList[i].objName == objName)
			{
				return windowList[i];
			}
		}
	}
}

function hideAllWindows() {
	if (windowList != null && windowList.length > 0)
	{
		for (var i=0; i<windowList.length; i++)
		{
			windowList[i].hide();
			removeWindow(i);
		}
	}
}

function escWindow(e){ //Hide the popup window on pressing escape key
	e= e || window.event || window.frames["contentFrame"].event;
	if(e.keyCode==27){
		try{
			parent.hideAllWindows();
		}catch(e){			
		}
	}
}
