// SCRIPT BY ANDRIS ZAČS 2006 andris.z@metaleks.lv
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0; 
var dx = 0;
var dy = 0;
var activeEl = false;
var MOVE = false;
function px_to_int( v ){
  return Number( v.substr( 0, v.length - 2 ) );
}
function getMouseXY(e){   
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){ tempX = 0 }
  if (tempY < 0){ tempY = 0 }  
  if( activeEl && MOVE ){ 
    activeEl.style.top = ( tempY - dy ) + 'px';
    activeEl.style.left = ( tempX - dx ) + 'px';
    activeEl.PosTop = tempY - dy;
    activeEl.PosLeft = tempX - dx;
    if( activeEl.onMove ) activeEl.onMove();     
  }
  return true;
}
//--------------------------------- WINDOW -------------------------------------
function order_windows( e ){  
  // --- win order
  var els = document.getElementsByTagName( 'div' );
  var jj = 0;
  var els2 = new Array();
  for( ii = 0; ii < els.length; ii ++ ){
    if( els[ ii ].className == 'window' ){
       els2.push( els[ ii ] );
    }
  }      
  for( ii = 0; ii < els2.length - 1 ; ii++ ){
    for( ij = ii + 1 ; ij < els2.length; ij++ ){
      if( Number( els2[ ii ].style.zIndex ) > Number( els2[ ij ].style.zIndex ) ){        
         var ik = els2[ ii ];
         els2[ ii ] = els2[ ij ];
         els2[ ij ] = ik; 
      }
    } 
  }   
  // === win order
  for( ii = 0; ii < els2.length; ii ++ ){          
    jj++;      
    els2[ ii ].style.zIndex = jj;    
  }  
  e.style.zIndex = jj + 1; 
}
function window_body_onclick(){
  order_windows( this.parentNode );
}
function win_move( e, ty ){
  //MOVE = ! MOVE;  
  if( ty == 1 ){
    MOVE = true;
  } else {
    MOVE = false;
  } 
  if( e.style.display == 'none' ) MOVE = false;
  if( MOVE && e.OPO ){
    e.style.opacity = '0.5';
    e.style.filter = 'alpha(opacity=50)';
  } else {
    e.style.opacity = '1';
    e.style.filter = 'alpha(opacity=100)';
  } 
  dx = tempX - e.offsetLeft;  
  if( px_to_int( e.style.top ) != 0 ){
    dy = tempY - px_to_int( e.style.top );
  } else {
    dy = tempY - e.offsetTop;
  }
  order_windows( e );   
  activeEl = e;    		
}
function win_top_start(){
  win_move( this.parentNode, 1 );  
}
function win_top_end(){
  win_move( this.parentNode, 2 );
}
function win_show(){
  this.style.display = '';
  if( this.onShow ) this.onShow();
  order_windows( this );
}
function win_hide(){
  this.style.display = 'none';
  if( this.onHide ) this.onHide();
}
function win_close(){
  if( this.onClose ) this.onClose();
  rmEl( this );
}
function win_close2( e ){
  if( e.parentNode.parentNode.parentNode.onClose ) if( ! e.parentNode.parentNode.parentNode.onClose() ) return;
  rmEl( e.parentNode.parentNode.parentNode );
  e.parentNode.parentNode.parentNode.style.opacity = '1';
  e.parentNode.parentNode.parentNode.style.filter = 'alpha(opacity=100)';
}
function win_close2_( e ){
  if( e.onClose ) if( ! e.onClose() ) return;
  rmEl( e );
  e.style.opacity = '1';
  e.style.filter = 'alpha(opacity=100)';
}
function win_stop_events( e ){
  if( e ) e.stopPropagation();
  if( window.event ) window.event.cancelBubble = true;
}
function win_move2( x, y ){
  this.style.top = y + 'px';
  this.style.left = x + 'px';
  this.PosTop = x;
  this.PosLeft = y;
  if( this.onMove ) this.onMove();  
}
function win_resize( x, y ){
  this.style.width = x + 'px';
  this.style.height = y + 'px';  
  this.Width = x;
  this.Height = y;
  this.Body.style.height = y - 32 + 'px';
  if( this.onResize ) this.onResize();  
}
function win_mi(){
  if( ! this.parentNode.parentNode.parentNode.OPO ){
    win_close2_( this.parentNode.parentNode.parentNode );
    return;
  }  
  this.Opocity = 100;  
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 40, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 80, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 120, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 160, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 200, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 240, this );
  doOnTimeout( function( ob ){ win_mi2( ob ); }, 280, this );
  doOnTimeout( function( ob ){ win_close2( ob ); }, 320, this );
}
function win_mi_(){
  if( ! this.OPO ){
    win_close2_( this );
    return;
  }  
  this.Opocity = 100;  
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 40, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 80, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 120, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 160, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 200, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 240, this );
  doOnTimeout( function( ob ){ win_mi2_( ob ); }, 280, this );
  doOnTimeout( function( ob ){ win_close2_( ob ); }, 320, this );
}
function win_mi2( e ){
  e.Opocity = e.Opocity - 15;  
  e.parentNode.parentNode.parentNode.style.opacity = e.Opocity / 100;
  e.parentNode.parentNode.parentNode.style.filter = 'alpha(opacity=' + e.Opocity + ')';
}
function win_mi2_( e ){
  e.Opocity = e.Opocity - 15;  
  e.style.opacity = e.Opocity / 100;
  e.style.filter = 'alpha(opacity=' + e.Opocity + ')';
}
function win_min(){
  if( this.parentNode.parentNode.parentNode.onResize ) this.parentNode.parentNode.parentNode.onResize();
  this.parentNode.parentNode.parentNode.WindowState = 1;
  this.parentNode.parentNode.parentNode.style.display = 'none';
  this.parentNode.parentNode.parentNode.style.opacity = '1';
  this.parentNode.parentNode.parentNode.style.filter = 'alpha(opacity=100)';
}
function win_append_( e ){  
  if( ! e ) e = document.body;  
  e.appendChild( this );
  if( this.onAppend ) this.onAppend();
  order_windows( this );   
}
function mkW( par ){
  var caption = '';
  var width = '50';
  var height = '16';
  var left = '0';
  var top = '0';
  opo = false;    
  if( par ){
    if( par.caption ) caption = par.caption;
    if( par.width ) width = par.width;
    if( par.height ) height = par.height;
    if( par.top ) top = par.top;
    if( par.left ) left = par.left;
    if( par.opocity ) opo = par.opocity;       
  }
  var win = mkEl2( 'div' );
  //CSS
  win.OPO = opo;
  win.className = 'window';
  win.style.position = 'absolute';
  win.style.backgroundColor = '#bbb';
  win.style.border = '1px solid #555';
  win.style.fontSize = '10px';
  win.style.fontFamily = 'Verdana';
  win.style.padding = '0px';
  win.style.margin = '0px';
  var iframe = mkEl2( 'iframe' );    
  if( par ) { if( par.windowState ) windowState = par.windowState; } 
  if( par ) if( typeof( par.visible ) == 'boolean' ){ if( par.visible ) win.style.display = ''; else win.style.display = 'none'; }   
  //CSS end
  win.style.width = width + 'px';
  win.style.overflow = 'hidden';
  win.Width = width;
  win.style.height = height + 'px';
  win.Height = height;
  iframe.style.position = 'absolute';
  iframe.style.top = '0px';
  iframe.frameBorder = 0;
  iframe.style.width = '100%';
  iframe.style.height = '100%';
  //hack for opera  
  if( navigator.userAgent.indexOf("Windows")!=-1
    && typeof document.body != "undefined"
    && typeof document.body.insertAdjacentHTML != "undefined"
    && ! window.opera
    && navigator.appVersion.indexOf("MSIE 5.0")==-1 ) win.appendChild( iframe );
  //==============
  if( win.onResize ) win.onResize;
  win.style.top = top + 'px';
  win.style.left = left + 'px';
  win.PosTop = win.offsetTop;
  win.PosLeft = win.offsetLeft;
  win.WindowState = 0;
    var win_top = mkEl2( 'div', {}, [], { 'onmousedown':win_top_start, 'onmouseup':win_top_end } );
    win_top.style.position = 'absolute';
    win_top.style.top = '0px';
    win_top.style.left = '0px';
    win_top.style.width = '100%';
    win_top.style.height = '16px';
    win_top.style.backgroundColor = '#008';
    win_top.style.padding = '0px';
    win_top.style.margin = '0px';    
    win.appendChild( win_top );
    win.Top = win_top
      var win_capt = mkEl2( 'div', {}, [ mkTx( caption ) ] );
      win_capt.style.styleFloat = 'left';
      win_capt.style.cssFloat = 'left';
      win_capt.style.fontWeight = 'bold';
      win_capt.style.color = '#fff';
      win_capt.className = 'window_caption';
      win_top.appendChild( win_capt );
      win.Cation = win_capt;
      var win_buttons = mkEl2( 'div' );
      win_buttons.style.styleFloat = 'right';
      win_buttons.style.cssFloat = 'right';
      win_buttons.style.padding = '0px';
      win_buttons.style.margin = '0px';
      win_top.appendChild( win_buttons );
      win_top.TopButtons = win_buttons;        
        var win_button_min = mkEl2( 'img', { 'src':'/sys/min.png' } );
        win_button_min.style.cursor = 'hand';
        win_button_min.style.cursor = 'pointer';
        win_button_min.style.marginRight = '2px';
        win_button_min.style.paddingTop = '1px';
        win_button_min.onclick = win_min;
        win_button_min.onmousedown = win_stop_events;        
        //win_buttons.appendChild( win_button_min );
        var win_button_close = mkEl2( 'img', { 'src':'/sys/close.png' } );
        win_button_close.style.cursor = 'hand';
        win_button_close.style.cursor = 'pointer';
        win_button_close.style.paddingTop = '1px';
        win_button_close.onclick = win_mi;
        win_button_close.onmousedown = win_stop_events;
        win_buttons.appendChild( win_button_close );
    var win_body = mkEl2( 'div' );
    win_body.ondblclick = window_body_onclick;
    win_body.style.position = 'absolute';
    win_body.style.top = '16px';
    win_body.style.left = '0px';
    win_body.style.padding = '0px';
    win_body.style.margin = '0px';
    win_body.style.width = '100%';
    win_body.style.backgroundColor = win.style.backgroundColor;
    win_body.style.height = height - 32 + 'px';
    win_body.className = 'window_body';
    if( par ) { if( par.overflow ) win_body.style.overflow = par.overflow }
    win.appendChild( win_body );
    win.Body = win_body;
    var win_bottom = mkEl2( 'div' );
    win_bottom.style.height = '16px';
    win_bottom.style.width = '100%';
    win_bottom.style.position = 'absolute';
    win_bottom.style.top = height - 16 + 'px';
    win_bottom.style.backgroundColor = '#ccc';   
    win.appendChild( win_bottom );
    win.Bottom = win_bottom;   
  win.Show = win_show;
  win.Hide = win_hide;
  win.Close = win_mi_;
  win.Move = win_move2;
  win.Resize = win_resize;
  win.Append = win_append_;  
  return win;
}
//================================ WINDOW ======================================
/*
PAR
  caption
  top
  left
  height
  width
  overflow
  opocity    

OBJ
  Top
  Top.TopButton
  Body
  PosTop
  PosLeft
  Bottom
  Width
  Height
  WindowState
    
FUNCTIONS
  Show();
  Hide();
  Close();
  Move( x, y );
  Resize( x, y );
  Append( where ); where def. = document
  
EVENTS
  onMove;
  onShow;
  onHide;
  onClose;
  onResize;
  onAppend  
  
2006.05.31  
*/
