var tmpx=0;
var tmpy=0;
var IE=document.all?true:false;
var ModalOn=false;

function openWindow(e,contObj,clearObj,cssname) {
	concssname=cssname;
	wfcontObj=contObj;
	if(IE)
	{
	   tmpx=event.clientX+parseInt(document.body.scrollLeft);
	   tmpy=event.clientY+parseInt(document.body.scrollTop);
	}
	else
	{
	   tmpx=e.pageX;
	   tmpy=e.pageY;
	   tmpy=tmpy-getScrollY();
	}  
	if (tmpx<0){tmpx=0;}
	if (tmpy<0){tmpy=0;}  

	if (tmpy<20&&!ModalOn)
	{
	  ModalOn=true;
	  createDiv(contObj,clearObj);
	}
	return true;
}

function getScrollY() {
	var scrOfY=0;
		if(typeof(window.pageYOffset)=='number') {
			scrOfY=window.pageYOffset;
		} else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)) {
			scrOfY=document.body.scrollTop;
		} else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)) {
			scrOfY=document.documentElement.scrollTop;
		}
	return scrOfY;
}

function getScrollX() {
	var scrOfX=0;
		if(typeof(window.pageXOffset)=='number') {
			scrOfX=window.pageXOffset;
		} else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)) {
			scrOfX=document.body.scrollLeft;
		} else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)) {
			scrOfX=document.documentElement.scrollLeft;
		}
	return scrOfX;
}

function owinWidth(){
   var winWidth;
   if (window.innerWidth){
       winWidth=window.innerWidth;
   }
      else if ((document.body)&&(document.body.clientWidth)){
       winWidth=parseInt(document.body.clientWidth);
   }
      if (document.documentElement&&document.documentElement.clientWidth){
       winWidth=parseInt(document.documentElement.clientWidth);
   }
   return winWidth;
}
function owinHeight(){
   var winHeight;
   if (window.innerHeight){
       winHeight=parseInt(window.innerHeight);
   }else if ((document.body)&&(document.body.clientHeight)){
       winHeight=parseInt(document.body.clientHeight);
   }
   if (document.documentElement&&document.documentElement.clientHeight){
       winHeight=parseInt(document.documentElement.clientHeight);
   }
   return winHeight; 
}

function createDiv(contObj,clearObj){
	pinner=document.getElementById(contObj);
	var clearid=document.getElementById(clearObj);
	var obody=document.body;
	document.documentElement.style.overflowY="auto";
	document.documentElement.style.overflowX="hidden";
	obody.style.overflowY="auto";
	obody.style.overflowX="hidden";
	var newDiv=document.createElement("div");
	newDiv.id="bgdiv";
	var winWidth=parseInt(owinWidth());
	if(document.compatMode=="CSS1Compat"||IE)
	{
	  var winHeight=parseInt(owinHeight());
	}
	else
	{
	  var winHeight=parseInt(window.screen.availHeight);
	}
	wfwinWidth=winWidth;
	wfwinHeight=winHeight;
	if(obody.scrollWidth>winWidth) winWidth=parseInt(obody.scrollWidth);
	if(obody.scrollHeight>winHeight) winHeight=parseInt(obody.scrollHeight);
	newDiv.style.width=winWidth+"px";
	newDiv.style.height=(winHeight+100)+"px";
	newDiv.style.background="gray";
	newDiv.style.position="absolute";
	if(typeof(newDiv.style.filter)=="string"){
	   newDiv.style.filter="alpha(opacity=70)";
	}else{
	   newDiv.style.opacity="0.7";
	}
	newDiv.style.top="0px";
	newDiv.style.left="0px";
	obody.appendChild(newDiv);
	newDiv.style.zIndex="1001";
	pinner.style.position="absolute";
	pinner.style.zIndex="1002";
	pinner.style.display='block';
	pinner.className=concssname;
	if(parseInt(pinner.clientHeight)>wfwinHeight)
	  pinner.style.top="0px";
    else
	/* (wfwinHeight-parseInt(pinner.clientHeight))/2+"px";*/
	pinner.style.top=((getScrollY()+(wfwinHeight-parseInt(pinner.clientHeight))/2)-60)+"px";

	if(parseInt(pinner.clientWidth)>wfwinWidth)
	  pinner.style.left="0px";
    else
	  pinner.style.left=(wfwinWidth-parseInt(pinner.clientWidth))/2+"px";
	pinner.onmouseout=function(){drag=0;}
	pinner.onmouseover=function(){drag=1;}
	pinner.style.cursor="default";
	clearid.onclick=function(){clearDiv();}
	window.onscroll=scall; 
}

function clearDiv(){
	var bgd=document.getElementById("bgdiv");
	if(bgd!=null)
	{
	 pinner.style.display="none";
	 document.body.removeChild(bgd);
	 document.documentElement.style.overflowY="auto";
	 createCookie('popwindow', 'close', 0);
	}
}


drag=0
move=0
/* window.document.onmousemove=mouseMove; */
window.document.onmousedown=mouseDown;
window.document.onmouseup=mouseUp;
window.document.ondragstart=mouseStop;

function mouseDown(ev){
    if(drag){
	  ev=ev||window.event;
      var mousePos=mouseCoords(ev); 
      clickleft=parseInt(mousePos.x)-parseInt(document.getElementById(wfcontObj).style.left);
      clicktop=parseInt(mousePos.y)-parseInt(document.getElementById(wfcontObj).style.top);
      document.getElementById(wfcontObj).style.zIndex+=1;
      move=1;
    }
}

function mouseStop(ev){
    ev=ev||window.event;
    ev.returnValue=false;
}

function mouseMove(ev){
    if(move){
	   ev=ev||window.event;
	   ev.returnValue=false;
	   pinner.style.cursor="move";
	   var mousePos=mouseCoords(ev); 
       document.getElementById(wfcontObj).style.left=(parseInt(mousePos.x)-clickleft)+"px";
       document.getElementById(wfcontObj).style.top=(parseInt(mousePos.y)-clicktop)+"px";
    }
}

function mouseCoords(ev){
    if(ev.pageX||ev.pageY){
      return {x:ev.pageX,y:ev.pageY};}
	else
	 {
      return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
	 } 
}

function mouseUp(event){
    move=0;
	pinner.style.cursor="default";
}

function scall(){
 if(pinner!=null)
 {
   pinner.style.top=(getScrollY()+(wfwinHeight-parseInt(pinner.clientHeight))/2)+"px";
   pinner.style.left=(getScrollX()+(wfwinWidth-parseInt(pinner.clientWidth))/2)+"px";
 }
} 