function Popup(name)
{
	this.name=name;
	this.graphic=null;
	this.title="";
	this.content="";
	this.width="100px";
	this.height="100px";
	this.left="0px";
	this.top="0px";
	this.linkObject=null;
	this.beforeClosingEvent=null;
}

Popup.prototype.setLinkObject=function(oSrc,dir)
{
	if(oSrc==undefined) return;
	if(typeof oSrc=='string')
	{	
		oSrc=document.getElementById(oSrc);
	}
	if(typeof oSrc!='object') return ;
	if(dir==undefined) dir='under';
	if(oSrc.style.position!="relative" && oSrc.style.position!="absolute") oSrc.style.position="relative";
	this.linkObject=oSrc;
	if(this.graphic!=null && this.linkObject!=null)
	{
		if(dir=='under')
			this.graphic.style.top=(this.linkObject.offsetTop+this.linkObject.offsetHeight)+'px';
		else this.graphic.style.top=(this.linkObject.offsetTop-this.graphic.offsetHeight)+'px';
		
		if((this.linkObject.offsetLeft+this.graphic.offsetWidth)>document.body.offsetWidth)
			this.graphic.style.left=(this.linkObject.offsetLeft-this.graphic.offsetWidth+this.linkObject.offsetWidth)+'px';
		else
			this.graphic.style.left=this.linkObject.offsetLeft+'px';
			
	}
}

Popup.prototype.setTitle=function(title)
{
	this.title=title;
	if(this.graphic!=null) this.build();
}

Popup.prototype.setContent=function(html)
{
	this.content=html;
	if(this.graphic!=null) this.build();
}

Popup.prototype.setWidth=function(w)
{
	if (w.toString().indexOf("px")==-1) w+="px";
	this.width=w;
	if(this.graphic!=null) {this.graphic.style.width=w;this.show();}
}

Popup.prototype.setHeight=function(h)
{
	if (h.toString().indexOf("px")==-1) h+="px";
	this.height=h;
	if(this.graphic!=null) {this.graphic.style.height=h;this.show();}
}

Popup.prototype.resizeTo=function(w,h)
{
	if (w.toString().indexOf("px")==-1) w+="px";
	if (h.toString().indexOf("px")==-1) h+="px";
	this.width=w;
	this.height=h;
	if(this.graphic!=null) 
	{
		this.graphic.style.width=w;
		this.graphic.style.height=h;
		this.show();
	}
}

Popup.prototype.moveTo=function(x,y)
{
	/*/if(this.linkObject!=null)
	{
		alert("error: can t move the Popup, a link object is defined");
		return;
	}*/
	
	if(typeof x =='string' && this.graphic!=null)
	{
		switch(x)
		{
			case 'center':
				x=(screen.availWidth/2)-(this.graphic.offsetWidth/2);
			break;
			case 'left':
				x=0;
			break;
			case 'right':
				x=(screen.availWidth-this.graphic.offsetWidth);
			break;
			default:
				x=parseInt(x);
			break;
		}
		x+=document.documentElement.scrollLeft;
	}
	else x=parseInt(x);
	if(typeof y =='string' && this.graphic!=null)
	{
		switch(y)
		{
			case 'center':
				y=((screen.availHeight/2)-(this.graphic.offsetHeight)/2)-20;
			break;
			case 'top':
				y=0;
			break;
			case 'bottom':
				y=(screen.availHeight-this.graphic.offsetHeight);
			break;
			default:
				y=parseInt(y);
			break;
		}
		y+=document.documentElement.scrollTop;
	}
	else y=parseInt(y);
	
	if (y.toString().indexOf("px")==-1) y+="px";
	if (x.toString().indexOf("px")==-1) x+="px";
	this.left=x;
	this.top=y;
	if(this.graphic!=null) 
	{
		//alert(x+" "+y);
		this.graphic.style.top=y;
		this.graphic.style.left=x;
		this.show();
	}
}

Popup.prototype.build=function()
{
	if(document.getElementById(this.name)==null)
	{
		var oChild=document.createElement("div");
		oChild.id=this.name;
		oChild.style.position='absolute';
		oChild.style.top=this.top;
		oChild.style.left=this.left;
		oChild.style.width=this.width;
		oChild.style.height=this.height;
		oChild.style.display='none';
		oChild.style.zIndex=100;
		oChild.style.overflow='hidden';
		oChild.className='popup';
		oChild.Popup=this;
		document.body.appendChild(oChild);
	}
	var oDiv=document.getElementById(this.name);
	if(oDiv==null) {alert("error: impossible to display Popup");return false;}
	var html;
	var btnClose="<INPUT TYPE=\"BUTTON\" value=\"X\" onClick=\"PopupHide('"+this.name+"');if(document.all) event.cancelBubble=true;if(event.stopPropagation)event.stopPropagation();return false;\">";
	html="<div id='"+this.name+"_entete' class='popup_entete' >";
	html+="<div id='"+this.name+"_titre' class='popup_titre'>"+this.title+"</div>";
	html+="<div id='"+this.name+"_action' class='popup_action'>"+btnClose+"</div></div>";
	html+="<div id='"+this.name+"_content' class='popup_content'>";
	if(this.content.toLowerCase().indexOf("http://")==0 || this.content.toLowerCase().indexOf(".php?")!=-1)
		html+="<IFRAME id='"+this.name+"_ifrm' src=\""+this.content+"\" frameborder='0'></IFRAME>";
	else
		html+=this.content;
	html+="</div>";

	//alert(html);
	oDiv.innerHTML=html;
	oDiv.style.display='block';
	this.graphic=oDiv;
	if(this.linkObject!=null) this.setLinkObject(this.linkObject);
	
	var oTitre=document.getElementById(this.name+'_titre');
	var oAction=document.getElementById(this.name+'_action');
	oTitre.style.width=(parseInt(this.width)-oAction.offsetWidth)+'px';
	
	var oFrm=document.getElementById(this.name+'_ifrm');
	if(oFrm!=null) 
	{
		var oEntete=document.getElementById(this.name+'_entete');
		oFrm.style.height=(parseInt(this.height)-oEntete.offsetHeight)+'px';
	}
	
	return true;
}

Popup.prototype.init=function()
{
	if(document.getElementById(this.name)==null)
	{
		var oChild=document.createElement("div");
		oChild.id=this.name;
		oChild.style.position='absolute';
		oChild.style.top=this.top;
		oChild.style.left=this.left;
		oChild.style.width=this.width;
		oChild.style.height=this.height;
		oChild.style.display='none';
		oChild.style.zIndex=100;
		oChild.style.overflow='hidden';
		oChild.className='popup';
		oChild.Popup=this;
		document.body.appendChild(oChild);
	}
	var oDiv=document.getElementById(this.name);
	if(oDiv==null) {alert("error: impossible to display Popup");return false;}
	var html;
	var btnClose="<INPUT TYPE=\"BUTTON\" value=\"X\" onClick=\"PopupHide('"+this.name+"');if(document.all) event.cancelBubble=true;if(event.stopPropagation)event.stopPropagation();return false;\">";
	html="<div id='"+this.name+"_entete' class='popup_entete' >";
	html+="<div id='"+this.name+"_titre' class='popup_titre'>"+this.title+"</div>";
	html+="<div id='"+this.name+"_action' class='popup_action'>"+btnClose+"</div></div>";
	html+="<div id='"+this.name+"_content' class='popup_content'>";
	if(this.content.toLowerCase().indexOf("http://")==0 || this.content.toLowerCase().indexOf(".php?")!=-1)
		html+="<IFRAME id='"+this.name+"_ifrm' src=\""+this.content+"\" frameborder='0'></IFRAME>";
	else
		html+=this.content;
	html+="</div>";
	
	oDiv.innerHTML=html;
	this.graphic=oDiv;
	if(this.linkObject!=null) this.setLinkObject(this.linkObject);
}

Popup.prototype.refresh=function()
{
	if(this.graphic==null) return;
	var oTitre=document.getElementById(this.name+'_titre');
	var oAction=document.getElementById(this.name+'_action');
	oTitre.style.width=(parseInt(this.width)-oAction.offsetWidth)+'px';
	
	var oFrm=document.getElementById(this.name+'_ifrm');
	if(oFrm!=null) 
	{
		var oEntete=document.getElementById(this.name+'_entete');
		oFrm.style.height=(parseInt(this.height)-oEntete.offsetHeight)+'px';
	}
}

Popup.prototype.show=function()
{
	if(this.graphic!=null) {this.graphic.style.display='block';this.refresh();}
}

Popup.prototype.hide=function()
{
	if(typeof this.beforeClosingEvent == 'function') 
	{
		if(!this.beforeClosingEvent()) return false;
	}
	if(this.graphic!=null) this.graphic.style.display='none';
	return true;
}

function PopupHide(name)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) oDiv.Popup.hide();
}

function PopupDrag(name)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) 
	{
		oDiv.Popup.dragdrop=true;
	}
}

function PopupMove(name,evt)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) 
	{
		if(oDiv.Popup.dragdrop)
		{
			et=new EventTools(evt);//alert(et.getMouseX());
			oDiv.style.left=(et.getMouseX()-15)+"px";
			oDiv.style.top=(et.getMouseY()-15)+"px";
		}
	}
}

function PopupDrop(name)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) 
	{
		oDiv.Popup.dragdrop=false;
	}
}

function PopupMouseOver(name)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) 
	{
		oDiv.style.cursor="move";
	}
}

function PopupMouseOut(name)
{
	var oDiv=document.getElementById(name);
	if(oDiv!=null) 
	{
		//oDiv.style.cursor="auto";
		oDiv.Popup.dragdrop=false;
	}
}
