
function PopupDiv(parent,title,width,height,close_mode,move_mode,resize_mode){
 var item=this;
 var cursor={x:0,y:0,dx:0,dy:0};
 var mousemovefunc=[];
 function MouseMove(ev){
  ev=(ev)?ev:window.event;
  cursor.dx=ev.clientX-cursor.x;
  cursor.dy=ev.clientY-cursor.y;
  cursor.x=ev.clientX;
  cursor.y=ev.clientY;
  for(var i=0;i<mousemovefunc.length;i++){
   mousemovefunc[i](cursor);
  }
 }
 function AddMouseMoveFunc(handler){
  mousemovefunc.push(handler);
 }
 function MouseUp(){
  mousemovefunc.length=0;
  setElementOpacity(maindiv, 1);
 }
 addEvent(document.documentElement,'mousemove',MouseMove);
 addEvent(document.documentElement,'mouseup',MouseUp);
 
 function MoveDiv(cursor){
  maindiv.style.left=px(parseInt(maindiv.style.left)+cursor.dx);
  maindiv.style.top=px(parseInt(maindiv.style.top)+cursor.dy);
 }
 function ResizeDiv(cursor){
  var new_w=parseInt(maindiv.style.width)+cursor.dx;
  if(new_w>=parseInt(item.minWidth)){
   maindiv.style.width=px(new_w);
  }
  var new_h=parseInt(parseInt(bodydiv.style.height)+cursor.dy);
  if(new_h>=parseInt(item.minHeight)){
   bodydiv.style.height=px(new_h);
  }
 }
 var maindiv=document.createElement('DIV');
 maindiv.style.position='absolute';
 maindiv.style.left=px(0);
 maindiv.style.top=px(0);
 maindiv.style.display='none';
 maindiv.style.zIndex=10000;
 maindiv.style.width=(width)?width:'500px';
 /*
 maindiv.style.border='1px solid #c72930';
 maindiv.style.backgroundColor='#ebebeb';
 */
 
 var titlediv=document.createElement('DIV');
 /*
 titlediv.style.height=px(20);
 titlediv.style.padding=px(3);
 titlediv.style.backgroundColor='#a5c9ef';
 titlediv.style.backgroundImage='url(\'/images/title_bg.gif\')';
 titlediv.style.backgroundRepeat='repeat-y';
 titlediv.style.fontWeight='bolder';
 titlediv.style.color='#ffffff';
 titlediv.style.borderBottom='1px solid #c72930';
 */
 if(close_mode){
  var img=document.createElement('IMG');
  img.border=0;
  img.align='right';
  img.style.cursor='pointer';
  img.src='/images/close.gif';
  img.onmousedown=function(ev){
   item.hide();
   return cancelEvent(ev);
  };
  titlediv.appendChild(img);
 }
 if(move_mode){
  titlediv.style.cursor='move';
  titlediv.ondragstart=cancelEvent;
  titlediv.onselectstart=cancelEvent;
  titlediv.onmousedown=function(ev){
   setElementOpacity(maindiv, 0.5);
   AddMouseMoveFunc(MoveDiv);
   return cancelEvent(ev);
  };
 }
 titlediv.appendChild(document.createTextNode(title));
 maindiv.appendChild(titlediv);
 var bodydiv=document.createElement('DIV');
 /*bodydiv.style.padding=px(3);
 bodydiv.style.overflow='auto';*/
 bodydiv.style.height=(height)?height:'500px';
 maindiv.appendChild(bodydiv);
 if(resize_mode){
  var statusdiv=document.createElement('DIV');
/*  statusdiv.style.borderTop='1px solid #c72930';
  statusdiv.style.height='12px';*/
  var img=document.createElement('IMG');
  img.border=0;
  img.align='right';
  img.style.cursor='se-resize';
  img.src='/images/resize.gif';
  img.ondragstart=cancelEvent;
  img.onselectstart=cancelEvent;
  img.onmousedown=function(ev){
   setElementOpacity(maindiv, 1);
   AddMouseMoveFunc(ResizeDiv);
   return cancelEvent(ev);
  };
  statusdiv.appendChild(img);
  maindiv.appendChild(statusdiv);
 }
 parent.appendChild(maindiv);
 
 function setDivCenter(){
  var w=0,h=0;
  if(self.innerHeight) {
   w=self.innerWidth;
   h=self.innerHeight;
  }
  else if(document.documentElement&&document.documentElement.clientHeight){
   w=document.documentElement.clientWidth;
   h=document.documentElement.clientHeight;
  }
  else if(document.body){
   w=document.body.clientWidth;
   h=document.body.clientHeight;
  }
  
  var sL,sT;  
  if(self.pageXOffset) {
   sL=self.pageXOffset;
   sT=self.pageYOffset;
  }
  else if(document.documentElement && document.documentElement.scrollLeft) {
   sL=document.documentElement.scrollLeft;
   sT=document.documentElement.scrollTop;
  }
  else {
   sL=document.body.scrollLeft;  
   sT=document.body.scrollTop;  
  }

  maindiv.style.left=px(Math.round((w-maindiv.offsetWidth)/2)+sL);
  maindiv.style.top=px(Math.round((h-maindiv.offsetHeight)/2)+sT);
 }
 this.minWidth=100;
 this.minHeight=100;
 this.onshow=null;
 this.onclose=null;
 
 this.setMainDivClassName=function(class_name) {
  maindiv.className=class_name;
 };
 
 this.setTitleDivClassName=function(class_name) {
  titlediv.className=class_name;
 };
 
 this.setBodyDivClassName=function(class_name) {
  bodydiv.className=class_name;
 }
 
 this.setStatusDivClassName=function(class_name) {
  statusdiv.className=class_name;
 }
 
 this.setSize=function(w,h) {
  w=parseInt(w); h=parseInt(h);
  if(w>this.minWidth && h>this.minHeight) {   
   var styles=['padding-left','padding-right','margin-left','margin-right','border-left-width','border-right-width'];
   for(var i=0,d=null;i<styles.length;i++) {
    if(!isNaN((d=parseInt(getStyle(maindiv,styles[i]))))){w+=d;}
	if(!isNaN((d=parseInt(getStyle(bodydiv,styles[i]))))){w+=d;}
   }
   maindiv.style.width=px(w);
   bodydiv.style.height=px(h);   
  }
 }
 
 this.replaceBodyHTML=function(HTML){
  bodydiv.innerHTML=HTML;
 };
 this.getBodyHTML=function(){
  return bodydiv.innerHTML;
 };
 this.clearBody=function(){
  while(bodydiv.firstChild){
   bodydiv.removeChild(bodydiv.firstChild);
  }
 };
 this.appendBodyChild=function(child){
  bodydiv.appendChild(child);
 };
 this.show=function(){
  if(this.onshow){
   this.onshow();
  }
  maindiv.style.display='block';
  setDivCenter();
 }; 
 this.hide=function(){
  if(this.onclose){
   this.onclose();
  }
  maindiv.style.display='none';
 };
 this.setZIndex=function(val){
  maindiv.style.zIndex=val;
 }
}
