var popup_action='none',popup_id=null;
function popup(argsObj) {
   // Mise aux valeurs par defaut pour les arguments non définis
   // acol0: couleur de la bordure de la fenetre non active
   // acol1: couleur de la bordure de la fenetre active
   // bcol0: couleur de la barre de titre non active
   // bcol1: couleur de la barre de titre active
   args={id:'popup',title:'',inner:'',move:'1',resize:'1',buttons:true,index:'0',
         grid:'1',x0:'100',y0:'100',w0:'100',h0:'100',wmin:'0',hmin:'0',
         acol0:'#77B',acol1:'#77B',bcol0:'#77B',bcol1:'#77B',overflow:'auto',display:'block',exit:''};
   for (var argName in argsObj) { args[argName] = argsObj[argName]; }
   if ((args.resize=='0' && args.move=='1') || (args.resize=='1' && args.move=='0')) {
      args.resize=='1';args.move=='1';
   }
   if (args.wmin=='0') args.wmin=args.w0;
   if (args.hmin=='0') args.hmin=args.h0;
   var style="left: "+args.x0+"px; top: "+args.y0+"px; width: "+args.w0+"px; height: "+args.h0+"px;";
   if (args.index != '0') style+=' z-index: '+args.index+';';
   if (args.display != 'block') style+=' display: '+args.display+';';
   style+=' border-color: '+args.acol1+';';
   var html="<DIV id='"+args.id+"' class='popup_body' style='"+style+"'>";
   html+="<input type='hidden' id='"+args.id+"_resize' value='"+args.resize+"'>";
   html+="<input type='hidden' id='"+args.id+"_move' value='"+args.move+"'>";
   html+="<input type='hidden' id='"+args.id+"_wmin' value='"+args.wmin+"'>";
   html+="<input type='hidden' id='"+args.id+"_hmin' value='"+args.hmin+"'>";
   html+="<input type='hidden' id='"+args.id+"_grid' value='"+args.grid+"'>";
   html+="<input type='hidden' id='"+args.id+"_acol0' value='"+args.acol0+"'>";
   html+="<input type='hidden' id='"+args.id+"_acol1' value='"+args.acol1+"'>";
   html+="<input type='hidden' id='"+args.id+"_bcol0' value='"+args.bcol0+"'>";
   html+="<input type='hidden' id='"+args.id+"_bcol1' value='"+args.bcol1+"'>";
   if (args.title != '') {
      html+="<DIV id='"+args.id+"_title' class='popup_title'";
      html+=" style='background-color: "+args.bcol1+";'>&nbsp;"+args.title+"</DIV>";
   } else {
      html+="<DIV id='"+args.id+"_title' style='display:none;'></DIV>";
   }
   html+="<DIV id='"+args.id+"_inner' class='popup_inner' style='overflow: "+args.overflow+";height: "+(args.h0-20)+"px;'>"+args.inner+"</DIV>";
   if (args.buttons == true) {
      var act;
      html+="<DIV class='popup_toolbar'><table width='100%' cellspacing='0' cellpadding='0'><tr>";
      if (args.resize!='-1' && args.move!='-1') {
         act="if ($('"+args.id+"_move').value!='-1') $('"+args.id+"_move').value=$('"+args.id+"_move').value=='1'?'0':'1';";
         act+="if ($('"+args.id+"_resize').value!='-1') $('"+args.id+"_resize').value=$('"+args.id+"_resize').value=='1'?'0':'1';";
         act+="($('"+args.id+"_move').value=='1' || $('"+args.id+"_resize').value=='1') ? $('"+args.id+"_btnV').innerHTML='V':$('"+args.id+"_btnV').innerHTML='v';";
         html+="<td class='popup_btn0'><a style='text-decoration: none' href='javascript:void(0)' onclick=\""+act+"\"><span id='"+args.id+"_btnV'>V</span></a></td>";
         act="$('"+args.id+"').style.left='"+args.x0+"px';$('"+args.id+"').style.top='"+args.y0+"px';";
         act+="$('"+args.id+"').style.width='"+args.w0+"px';$('"+args.id+"').style.height='"+args.h0+"px';";
         act+="$('"+args.id+"_inner').style.height='"+(args.h0-20)+"px';";
         html+="<td class='popup_btn0'><a style='text-decoration: none' href='javascript:void(0)' onclick=\""+act+"\">0</a></td>";
         act="$('"+args.id+"').style.width='"+args.wmin+"px';$('"+args.id+"').style.height='"+args.hmin+"px';";
         html+="<td class='popup_btn0'><a style='text-decoration: none' href='javascript:void(0)' onclick=\""+act+"\">_</a></td>";
      }
      if (args.exit != '') {
         act=args.exit;
      } else {
         act="$('"+args.id+"').style.display='none';";
      }
      html+="<td class='popup_btn0' onclick=\""+act+"\">x</td>";
      html+="</tr></table></DIV>";
   }
   html+="</DIV>";
   document.write(html);
}

function $(id) { return document.getElementById(id); }
function getReal(elem,type,value) {
   tmpelem=elem;
	while ((tmpelem != null) && (tmpelem.tagName != "BODY")) {
		if (eval("tmpelem." + type) == value) return tmpelem;
		tmpelem = tmpelem.parentElement;
	}
	return elem;
}
document.onmousemove=function() {
	xMouse = event.clientX+document.body.scrollLeft;
	yMouse = event.clientY+document.body.scrollTop;
   if (popup_action == 'move') {
      popup_id.style.left=(parseInt((xMouse-popup_inter_posX)/popup_grid)*popup_grid)+'px';
      popup_id.style.top=(parseInt((yMouse-popup_inter_posY)/popup_grid)*popup_grid)+'px';
      //popup_id.style.borderStyle="dashed";
      //popup_id.style.MozOpacity=0.5;
   } else {
   	var el = getReal(event.srcElement,"className","popup_body");
   	if (el.className == "popup_body" && $(el.id+'_resize').value == '1') {
      	tmp_dir="";
      	if (yMouse < el.offsetTop+8) tmp_dir = "n";
      	else if (yMouse > el.offsetHeight+el.offsetTop-8) tmp_dir += "s";
      	if (xMouse < el.offsetLeft+8) tmp_dir += "w";
      	else if (xMouse > el.offsetWidth+el.offsetLeft-8) tmp_dir += "e";
   		if (tmp_dir == "") el.style.cursor = "default"; else el.style.cursor = tmp_dir+"-resize";
   	}
   	if (popup_action == 'resize' && popup_dir != null && popup_dir != "") {
		   if (popup_dir.indexOf("e") != -1) {
   			popup_id.style.width = Math.max(popup_wmin,
   			   parseInt((popup_width+window.event.clientX-popup_grabx)/popup_grid)*popup_grid)+"px";
   	   }
   		if (popup_dir.indexOf("s") != -1) {
   			popup_id.style.height = Math.max(popup_hmin,
   			   parseInt((popup_height+window.event.clientY-popup_graby)/popup_grid)*popup_grid)+"px";
         }
   		if (popup_dir.indexOf("w") != -1) {
   			popup_id.style.left = Math.min(popup_left+
   			   parseInt((window.event.clientX-popup_grabx)/popup_grid)*popup_grid,popup_left+popup_width-popup_wmin)+"px";
   			popup_id.style.width = Math.max(popup_width+
   			   parseInt((-window.event.clientX+popup_grabx)/popup_grid)*popup_grid)+"px";
   		}
   		if (popup_dir.indexOf("n") != -1) {
   			popup_id.style.top = Math.min(popup_top+
   			   parseInt((window.event.clientY-popup_graby)/popup_grid)*popup_grid,popup_top+popup_height-popup_hmin)+"px";
   			popup_id.style.height = Math.max(popup_hmin,
   			   popup_height+parseInt((-window.event.clientY+popup_graby)/popup_grid)*popup_grid)+"px";
   		}
   		$(popup_id.id+'_inner').style.height=(popup_id.offsetHeight-24)+'px';
   	} 
   }
}
document.onmousedown=function() {
   if (popup_id != null) {
      popup_id.style.borderColor=$(popup_id.id+'_acol0').value;
      $(popup_id.id+'_title').style.background=$(popup_id.id+'_bcol0').value;
   }
	if (eval("event.srcElement.className") == "popup_title") {
	   popup_id=getReal(event.srcElement,"className","popup_body");
      if (popup_id != null) {
   	   if ($(popup_id.id+'_move').value == '1') {
            popup_action='move';
            popup_inter_posX=event.clientX+document.body.scrollLeft-parseInt(popup_id.style.left);
            popup_inter_posY=event.clientY+document.body.scrollTop-parseInt(popup_id.style.top);
            popup_grid=parseInt($(popup_id.id+'_grid').value);
            popup_id.style.borderColor=$(popup_id.id+'_acol1').value;
         }
         $(popup_id.id+'_title').style.background=$(popup_id.id+'_bcol1').value;
      }
	} else {
   	popup_id = getReal(event.srcElement,"className","popup_body");
	   if (popup_id.className == "popup_body") {
   	   if ($(popup_id.id+'_resize').value == '1') {
         	popup_action='resize'
         	popup_dir = tmp_dir;
         	popup_grabx = window.event.clientX;
         	popup_graby = window.event.clientY;
         	popup_width = popup_id.offsetWidth;
         	popup_height = popup_id.offsetHeight;
         	popup_left = popup_id.offsetLeft;
         	popup_top = popup_id.offsetTop;
      	   popup_wmin=parseInt($(popup_id.id+'_wmin').value);
      	   popup_hmin=parseInt($(popup_id.id+'_hmin').value);
            popup_grid=parseInt($(popup_id.id+'_grid').value);
            popup_id.style.borderColor=$(popup_id.id+'_acol1').value;
         }
         $(popup_id.id+'_title').style.background=$(popup_id.id+'_bcol1').value;
      } else {
         popup_id=null;
      }
   }
   
}
document.onmouseup=function() {
   popup_action='none';
   if (popup_id != null) {
      //popup_id.style.borderStyle="solid";
      //popup_id.style.MozOpacity=1.0;
      //popup_id=null;
   }
}

