//
// funzioni di gestione delle popup
//


// attiva il popup caricando la pagina specificata
function popup_initialize(page,w,h,left,top)
{

  var t=popup_build();

  // se non sono specificati left e top setto la centratura automatica 
  if (typeof(left)=='undefined' && typeof(top)=='undefined') {
     t.style.top=(document.body.clientHeight-h)/2+document.body.scrollTop;
     t.style.left=(document.body.clientWidth-w)/2+document.body.scrollLeft;
  } else {
     t.style.left=left;
     t.style.top=top;
  }
  t.style.width=w;
  t.style.height=h;

  // non deve restare fermo quando il body scrolla
  t.style.removeExpression('top');
  t.style.removeExpression('left');

  document.frames['popup'].location.href=page;

  t.style.visibility='visible';
}


// attiva il popup caricando la pagina specificata nel container popup.asp
function popup_initialize2(page,title,querystring,w,h,left,top)
{

  var t=popup_build();

  // mantiene fermo il popup se il body scrolla
  t.style.setExpression('top','eval(this.currentTop)+eval(document.body.scrollTop)');
  t.style.setExpression('left','eval(this.currentLeft)+eval(document.body.scrollLeft)');

  // posizione e dimensioni attuali
  if (typeof(left)!='undefined' && left!=0) {
     t.currentLeft=left;
     t.style.left=left;
  }
  if (typeof(top)!='undefined' && top!=0) {
     t.currentTop=top;
     t.style.top=top;
  }
  if (typeof(w)!='undefined' && w!=0) {
     t.currentWidth=w;
     t.style.pixelWidth=w;
  }
  if (typeof(h)!='undefined' && h!=0) {
     t.currentHeight=h;
     t.style.pixelHeight=h;
  }

  document.frames['popup'].location.href='popup.asp?page='+page+'&title='+title+'&'+querystring;
  t.style.visibility='visible';

}


// crea i tag che costituiscono il popup
function popup_build()
{

  var t=document.getElementById('popup_frame');

  if (t == null) {
     t=document.createElement('div');
     t.id='popup_frame';
     t.style.pixelWidth='640';
     t.style.pixelHeight='400';
     t.style.position='absolute';
     t.style.zIndex='1';
     t.style.visibility='hidden';
     t.currentLeft='150';
     t.currentTop='100';
     document.body.appendChild(t);

     var tmp=document.createElement('iframe');
     tmp.id='popup';
     tmp.name='popup';
     tmp.scrolling='no';
     tmp.style.border='2px outset';
     tmp.width='100%';
     tmp.height='100%';
     t.appendChild(tmp);

     // se non valorizzo name in questa maniera non posso usarlo come target delle form (!)
     document.frames['popup'].name='popup';
  }

  t.history_list='';
  t.history_len=0;
  t.history_back=false;
  return(t);
}


// disattiva il popup
function popup_abort()
{

  var t=document.getElementById('popup_frame');

  if (t == null) return;

  document.frames['popup'].location.href='about:blank';
  t.style.visibility='hidden';
  t.history_list='';
  t.history_len=0;
  t.history_back=false;

}


// attiva il tracking del drag-drop della finestra popup
function popup_startdrag(x,y)
{
  var t=document.getElementById('popup_frame');
  t.setCapture();
  t.drag_offsetX=x;
  t.drag_offsetY=y;
  t.drag_status=true;

  document.onmousemove=popup_drag;
  document.onmouseup=popup_stopdrag;
}

// riposiziona il popup durante il drag
function popup_drag()
{
  var t=document.getElementById('popup_frame');
  if (t.drag_status==true) {
     if (event.clientX>t.drag_offsetX) 
        t.style.pixelLeft=event.clientX+document.body.scrollLeft-t.drag_offsetX;
     else
        t.style.pixelLeft=document.body.scrollLeft;

     if (event.clientY>t.drag_offsetY+28) 
        t.style.pixelTop=event.clientY+document.body.scrollTop-t.drag_offsetY;
     else
        t.style.pixelTop=document.body.scrollTop+28;

     t.currentLeft=t.style.pixelLeft-document.body.scrollLeft;
     t.currentTop=t.style.pixelTop-document.body.scrollTop;

     return false;
  }
}

// disattiva il tracking del drag-drop della finestra popup
function popup_stopdrag()
{
  var t=document.getElementById('popup_frame');

  t.drag_status=false;
  t.releaseCapture();

  document.onmousemove=null;
  document.onmouseup=null;
}


// attiva il tracking del resize della finestra popup
function popup_startresize(x,y)
{
  var t=document.getElementById('popup_frame');
  t.setCapture();

  // memorizzo le dimensioni attuali
  t.currentWidth=t.style.pixelWidth;
  t.currentHeight=t.style.pixelHeight;

  t.resize_offsetX=x+t.style.pixelLeft;
  t.resize_offsetY=y+t.style.pixelTop;
  t.resize_status=true;

  document.onmousemove=popup_resize;
  document.onmouseup=popup_stopresize;
}

// ridimensiona il popup durante il resize
function popup_resize()
{
  var t=document.getElementById('popup_frame');
  if (t.resize_status==true) {

     var w=t.currentWidth+event.clientX-(t.resize_offsetX-document.body.scrollLeft);
     if (w>300 && w<document.body.scrollLeft+document.body.clientWidth-t.style.pixelLeft) 
        t.style.pixelWidth=w;

     var h=t.currentHeight+event.clientY-(t.resize_offsetY-document.body.scrollTop);
     if (h>100 && h<document.body.scrollTop+document.body.clientHeight-t.style.pixelTop) 
        t.style.pixelHeight=h;

     return false;
  }
}

// disattiva il tracking del resize della finestra popup
function popup_stopresize()
{
  var t=document.getElementById('popup_frame');

  t.resize_status=false;
  t.releaseCapture();

  document.onmousemove=null;
  document.onmouseup=null;
}


