
//////////////////////////////////////////////////////////////////////////////
// G L O B A L S /////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

// leave undefined
var NULL;

// window array
var WinInfo = new Array();
var WinMax = 1;

var Active = NULL;

// resize flags
var RESIZE_NORTH = 1;
var RESIZE_SOUTH = 2;
var RESIZE_EAST = 4;
var RESIZE_WEST = 8;

//////////////////////////////////////////////////////////////////////////////
// F U N C T I O N S /////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function jswin_init()
{
var Loop, List;


// find all windows
List = document.getElementsByTagName("DIV");
for(Loop=0; Loop<List.length; Loop++)
  {
  if(List[Loop].className == "jswindow")
    {
    WinInfo[List[Loop].id] = new jswindow(List[Loop]);
    WinMax++;
    WinInfo[List[Loop].id].ActShade();
    WinInfo[List[Loop].id].ActRestore();
    }
  }
}

//////////////////////////////////////////////////////////////////////////////
//// create jswindow class

function jswindow(Win)
{
this.Frame = Win;
Win.WinData = this;
this.DataArea = jswin_lookup(Win, "jswin_frame");
this.Content = jswin_lookup(Win, "jswin_content");

// javascript event handlers
this.Frame.onmousemove = jswin_track;
this.Frame.onmouseout = jswin_track;
this.Frame.onmousedown = jswin_track;
this.Frame.onmouseout = jswin_cursfix;

// window dimensions
this.Dim = new Object();
this.Dim.WinX = parseInt(this.Frame.style.left);
this.Dim.WinY = parseInt(this.Frame.style.top);
this.Dim.SizX = parseInt(this.Frame.style.width);
this.Dim.SizY = (parseInt(this.Content.style.height) + 52);
this.Dim.MinSizX = 300;
this.Dim.MinSizY = 50;

// window variables
this.State = 0;
this.Resize = 0;
this.Shaded = false;
this.ActData = NULL;

// set handlers
this.Action = NULL;
this.ActMove = jswin_move;
this.ActResize = jswin_resize;
this.ActShade = jswin_shade;
this.ActRestore = jswin_restore;
this.ActClose = jswin_close;

return;
}

//////////////////////////////////////////////////////////////////////////////
//// track cursor movement in window

function jswin_track(Event)
{
var Win;
var PosX, PosY;
var SizX, SizY;
var Cursor;

// find window object
if(this.WinData)
  Win = Active = this.WinData;
else
  Win = Active;

// determine window sizes
Win.Dim.WinX = parseInt(Win.Frame.style.left);
Win.Dim.WinY = parseInt(Win.Frame.style.top);
SizX = Win.Dim.SizX = parseInt(Win.Frame.style.width);
SizY = Win.Dim.SizY = (parseInt(Win.Content.style.height) + 52);

// find cursor position
if(is_ie5up)
  {
  PosX = (window.event.x - Win.Dim.WinX);
  PosY = (window.event.y - Win.Dim.WinY);
  Type = window.event.type;
  window.event.cancelBubble = true;
  window.event.returnValue = false;
  }
else if(is_nav6up)
  {
  PosX = (Event.pageX - Win.Dim.WinX);
  PosY = (Event.pageY - Win.Dim.WinY);
  Type = Event.type;
  Event.preventDefault();
  }


// call handler if needed
if(Win.Action)
  {
  if(Win.State || (Type == "mousedown"))
    {
    if(!Win.State)
      {
      // capture all events
      if(is_ie5up)
        {
        document.onmousemove = jswin_track;
        document.onmouseup = jswin_track;
        }
      if(is_nav6up)
        {
        document.addEventListener("mousemove", jswin_track, true);
        document.addEventListener("mouseup", jswin_track, true);
        Event.preventDefault();
        }
      }

    if(Type == "mousedown")
      Win.State = 1;
    else if(Type == "mousemove")
      Win.State = 2;
    else if(Type == "mouseup")
      Win.State = 3;

    if(Win.State && Win.Action(PosX, PosY))
      {
      Win.State = 0;
      Win.Action = NULL;

      // stop capturing events
      if(is_ie5up)
        {
        document.onmousemove = null;
        document.onmouseup = null;
        }
      if(is_nav6up)
        {
        document.removeEventListener("mousemove", jswin_track, true);
        document.removeEventListener("mouseup", jswin_track, true);
        }
      }
    return(false);
    }
  }


// determine cursor and function
Cursor = "default";
Win.Action = null;
Win.Resize = 0;
if((PosX < 0) || (PosY < 0) || (PosX > SizX) || (PosY > SizY))
  Cursor = "default";
else if((PosY < 26) && (PosX >= 20))
  {
  Cursor = "move";
  Win.Action = Win.ActMove;

  if((PosX > (SizX - 76)) && (PosY > 7) && (PosY < 20))
    {
    // check buttons
    PosX -= (SizX - 76);

    if(PosX < 18)
      {
      Cursor = "default";
      Win.Action = Win.ActShade;
      }
    else if((PosX > 23) && (PosX < 41))
      {
      Cursor = "default";
      Win.Action = Win.ActRestore;
      }
    else if((PosX > 45) && (PosX < 63))
      {
      Cursor = "default";
      Win.Action = Win.ActClose;
      }
    }
  }
else if(PosX < 7)
  {
  Cursor = "w-resize";
  Win.Action = Win.ActResize;
  Win.Resize = RESIZE_WEST;
  }
else if(PosX > (SizX - 7))
  {
  Cursor = "e-resize";
  Win.Action = Win.ActResize;
  Win.Resize = RESIZE_EAST;
  }

if(PosY > (SizY - 7))
  {
  if(Cursor == "default")
    Cursor = "s-resize";
  else
    Cursor = "s" + Cursor;
  Win.Action = Win.ActResize;
  Win.Resize |= RESIZE_SOUTH;
  }

// change cursor
if(is_ie5up)
  document.body.style.cursor = Cursor;
if (is_nav6up)
  Win.Frame.style.cursor = Cursor;

return(false);
}

//////////////////////////////////////////////////////////////////////////////
//// move window

function jswin_move(PosX, PosY)
{
PosX += this.Dim.WinX;
PosY += this.Dim.WinY;

if(this.State == 1)
  {
  // raise window
  this.Frame.style.zIndex = WinMax++;

  // store position
  this.ActData = new Object();
  this.ActData.WinX = this.Dim.WinX;
  this.ActData.WinY = this.Dim.WinY;
  this.ActData.StartX = PosX;
  this.ActData.StartY = PosY;
  }
else if((this.State == 2) || (this.State == 3))
  {
  // move window
  PosX -= this.ActData.StartX;
  PosY -= this.ActData.StartY;
  this.Frame.style.left = (this.ActData.WinX + PosX) + "px";
  this.Frame.style.top = (this.ActData.WinY + PosY) + "px";

  if(this.State == 2)
    return(false);

  // finish
  this.ActData = null;
  return(true);
  }

return(false);
}

//////////////////////////////////////////////////////////////////////////////
//// resize window

function jswin_resize(PosX, PosY)
{
var NewSiz, NewPos;


PosX += this.Dim.WinX;
PosY += this.Dim.WinY;

if(this.State == 1)
  {
  // raise window
  this.Frame.style.zIndex = WinMax++;

  // store position
  this.ActData = new Object();
  this.ActData.WinX = this.Dim.WinX;
  this.ActData.WinY = this.Dim.WinY;
  this.ActData.SizX = this.Dim.SizX;
  this.ActData.SizY = this.Dim.SizY;
  this.ActData.StartX = PosX;
  this.ActData.StartY = PosY;
  }
else if((this.State == 2) || (this.State == 3))
  {
  // resize window
  if(this.Resize & RESIZE_SOUTH)
    {
    PosY -= this.ActData.StartY;
    NewSiz = ((this.ActData.SizY - 52) + PosY);
    if(NewSiz < this.Dim.MinSizY)
      NewSiz = this.Dim.MinSizY;

    this.Content.style.height = NewSiz + "px";
    }
  if(this.Resize & RESIZE_EAST)
    {
    PosX -= this.ActData.StartX;
    NewSiz = (this.ActData.SizX + PosX);
    if(NewSiz < this.Dim.MinSizX)
      NewSiz = this.Dim.MinSizX;

    this.Frame.style.width = NewSiz + "px";
    this.Content.style.width = (NewSiz - 28) + "px";
    }
  else if(this.Resize & RESIZE_WEST)
    {
    PosX -= this.ActData.StartX;
    NewSiz = (this.ActData.SizX - PosX);
    if(NewSiz < this.Dim.MinSizX)
      NewSiz = this.Dim.MinSizX;

    NewPos = (this.ActData.WinX + (this.ActData.SizX - NewSiz));

    this.Frame.style.left = NewPos + "px";
    this.Frame.style.width = NewSiz + "px";
    this.Content.style.width = (NewSiz - 28) + "px";
    }

  if(this.State == 2)
    return(false);

  // finish
  this.ActData = null;
  return(true);
  }

return(false);
}

//////////////////////////////////////////////////////////////////////////////
//// shade window

function jswin_shade(PosX, PosY)
{
this.DataArea.style.visibility = "hidden";
this.Frame.style.overflow = "hidden";
this.Frame.style.clip = "rect(auto, auto, 27px, auto)";
return(true);
}

//////////////////////////////////////////////////////////////////////////////
//// restore shaded window

function jswin_restore(PosX, PosY)
{
this.DataArea.style.visibility = "visible";
this.Frame.style.clip = "rect(auto, auto, auto, auto)";
return(true);
}

//////////////////////////////////////////////////////////////////////////////
//// close window

function jswin_close(PosX, PosY)
{
this.DataArea.style.visibility = "hidden";
this.Frame.style.visibility = "hidden";
return(true);
}

//////////////////////////////////////////////////////////////////////////////
//// fix cursor

function jswin_cursfix(Event)
{
document.body.style.cursor = "default";
}

//////////////////////////////////////////////////////////////////////////////
//// lookup class by name

function jswin_lookup(Obj, Name)
{
var Loop, Child;


if(Obj.className == Name)
  return(Obj);

// scan for matching class name
for(Loop=0; Loop<Obj.childNodes.length; Loop++)
  {
  if(Child = jswin_lookup(Obj.childNodes[Loop], Name))
    return(Child);
  }

return(null);
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

