function popOutWindow(){
  this.isOpen=0;
  this.menu=null;
  this.container=null;
  var instance= this;
  ajaxCallFunction("ajax.iml?mdl=popOutWindow/resource.aj?"+Date(),function(sc,st,rt,rxml){
     var res=new HTMLParse(rt);
     instance.container=res.container[0];
     instance.pointer=res.pointer[0];
     instance.container.main[0].expandable[0].closelink[0].node.onclick=function(){instance.closePopOut();return false;};
  });
}

popOutWindow.prototype.open=function(evt,div,w){
  if(!w) w=250;
  this.process(evt,div,null,w);

}

popOutWindow.prototype.openMenu=function(evt,menu,w){
  if(!w) w=250;
  w+=150;
  this.process(evt,null,menu,w);
}

popOutWindow.prototype.process=function(evt,div,menu,w){   
  evt = new Evt(evt);
  if(!this.container){ 
    return;
  }
  if(this.isOpen) this.closePopOut();

  this.startPos=new Coordinate(evt.x,evt.y);

  if(menu){ this.setMenu(menu);}
  else{ this.removeMenu(); this.processDiv(div);}

  document.body.appendChild(this.container.node);
  document.body.appendChild(this.pointer.node);

  this.processPosition(this.startPos);
  this.setWidth(w);  
  this.resize();  
  this.isOpen=1;
}


popOutWindow.prototype.clearMenu=function(){
  if(this.menu)
    for(var i=0; i<this.menu.length; i++)  this.menu[i].div.className="unselected";  
}

popOutWindow.prototype.processDiv=function(div){
  this.removeContent();
  this.content=div;
  this.contentStyle=new Object();
  this.contentStyle.parent=this.content.parentNode;
  this.contentStyle.visibility=this.content.style["visibility"];
  this.contentStyle.position=this.content.style["position"];
  this.contentStyle.display=this.content.style["display"];
//  this.contentStyle.fl=this.content.style["float"];  
  this.content.style["visibility"]="";
  this.content.style["position"]="";
  this.content.style["display"]="";
//  this.content.style["float"]="left";
  this.container.main[0].contentDiv[0].node.appendChild(this.content);  
}

popOutWindow.prototype.removeContent=function(){
  if(this.content){
    if(this.contentStyle.parent){
      this.content.style["visibility"]=this.contentStyle.visibility;
      this.content.style["position"]=this.contentStyle.position;
      this.content.style["display"]=this.contentStyle.display;
//      this.content.style["float"]=this.contentStyle.fl;
      this.contentStyle.parent.appendChild(this.content);    
    }else{
      this.container.main[0].contentDiv[0].node.removeChild(this.content);  
    }  
    this.content=null;
  }
}


popOutWindow.prototype.setMenu=function(menu){
    if(this.menu) for(var i=0; i<this.menu.length; i++){  this.container.main[0].menuDiv[0].node.removeChild(this.menu[i].div); }
    this.menu=menu;
    this.container.main[0].menuDiv[0].node.style["display"]="";
    for(var i=0; i<this.menu.length; i++){
      this.menu[i].parent=this;
      this.container.main[0].menuDiv[0].node.appendChild(this.menu[i].div);
    }
    this.container.main[0].menucorner[0].node.className="menu";
    this.container.main[0].menucorner[1].node.className="menu";
    this.pointer.pointerMain[0].node.className="menu";    
    this.menu[0].clickMe();

}

popOutWindow.prototype.removeMenu=function(){
    if(this.menu)  for(var i=0; i<this.menu.length; i++){  this.container.main[0].menuDiv[0].node.removeChild(this.menu[i].div); }
    this.menu=null;
    this.container.main[0].menuDiv[0].node.style["display"]="none";
    this.container.main[0].menucorner[0].node.className="";
    this.container.main[0].menucorner[1].node.className="";
    this.pointer.pointerMain[0].node.className="";    
}



popOutWindow.prototype.resize=function(){
  var height=this.container.main[0].node.offsetHeight;
  var width=this.container.main[0].node.offsetWidth;
  if(height<100){
     height=100;
     this.container.main[0].node.style["height"]=height+"px";
  }
  this.container.shadow[0].node.style["height"]=height+"px";
}

popOutWindow.prototype.processPosition=function(pos){
  this.container.node.style["left"]=pos.x+23;
  if(pos.y<100)  this.container.node.style["top"]=0;  
  else this.container.node.style["top"]=pos.y-100;  
  this.pointer.node.style["left"]=pos.x;
  if(pos.y<50) this.pointer.node.style["top"]=20;
  else  this.pointer.node.style["top"]=pos.y-15;
}

popOutWindow.prototype.setWidth=function(w){
  this.container.main[0].node.style["width"]=w;
  this.container.main[0].expandable[0].node.style["width"]=w-28;
  this.container.main[0].expandable[1].node.style["width"]=w-28-136;
  this.container.shadow[0].node.style["width"]=w;
//  this.shadow.childNodes[1].style["width"]=w-28;
//  this.shadow.childNodes[5].style["width"]=w-28;
}

popOutWindow.prototype.closePopOut=function(){
  if(this.isOpen){
    this.removeContent();
    this.removeMenu();

    document.body.removeChild(this.container.node);
    document.body.removeChild(this.pointer.node);
  } 
  this.isOpen=0;
}


/*************************************************************/
/*********************  popOutMenuItem  ****************/
/*************************************************************/

function popOutMenuItem(title,content,func){
  this.parent=null;
  this.content=content;
  this.title=title;
  this.func=func;
  var icon, text;
  var instance=this;  
  this.div=ce("div"); this.div.className="unselected"; 
    icon=ce("div"); icon.className="icon"; 
    text=ce("div"); text.className="text";
    this.div.appendChild(icon); this.div.appendChild(text);
    text.innerHTML=title;  this.div.onclick=function(){instance.clickMe();};  
}

popOutMenuItem.prototype.clickMe=function(){
  if(this.parent)  this.parent.clearMenu();
  this.div.className="selected";
  this.parent.processDiv(this.content);
  this.parent.resize();
  if(this.func) this.func();    
  this.parent.resize();
}


var myPopOut=new popOutWindow();
