﻿// Developed by Mike Kiska 6/23/2006

// Requires: Controls.js, ControlFader.js, Event.js, MouseEvents.js

function DivUp(divCtl)
{
  var openerControl;
  var offsetX = 0;
  var offsetY = 0;
  var openSideX;
  var openSideY;
  var fadeInTime = 300;
  var displayTime = 0;

  var FPS = 20;
  var controlFader = new ControlFader();
  this.ControlFader = controlFader;

  this.Div = divCtl;
  divCtl.style.zIndex = '99999';

  var visible = false;
  var closing = new Event();
  this.Closing = closing;
  var closed = new Event();
  this.Closed = closed;
  var opened = new Event();
  this.Opened = opened;
  
  var duFrame = document.getElementById('divUpFrame');
  if ( duFrame == null )
  {
    duFrame = document.createElement('iframe');
    duFrame.setAttribute('id', 'divUpFrame');
    duFrame.setAttribute('src', "javascript:''");
    duFrame.setAttribute('scrolling', 'no');
    duFrame.setAttribute('frameBorder', '0');
    duFrame.setAttribute('border', '0');
    duFrame.style.position = 'absolute';
    duFrame.style.visibility = 'hidden';
    duFrame.style.overflow = 'hidden';
    duFrame.style.backgroundColor = '#FFFFFF';
    duFrame.style.zIndex = 99990;

    duFrame.style.top = '0px';
    duFrame.style.left = '0px';
    
    divCtl.parentNode.appendChild(duFrame);
  }

  var SizeIFrame = function()
  {
    if ( document.documentElement.offsetWidth < document.documentElement.scrollWidth )
      duFrame.style.width = document.documentElement.scrollWidth + 'px';
    else
      duFrame.style.width = document.documentElement.offsetWidth + 'px';
    
    if ( document.documentElement.offsetHeight < document.documentElement.scrollHeight )
      duFrame.style.height = document.documentElement.scrollHeight + 'px';
    else
      duFrame.style.height = document.documentElement.offsetHeight + 'px';
  };
  var CollapseIFrame = function()
  {
    duFrame.style.width = '1px';
    duFrame.style.height = '1px';
  };
  
  this.SetFadeInTime = function(time)
  {
    this.fadeInTime = time;
  };
  
  this.SetDisplayTime = function(time)
  {
    displayTime = time;
  };
  this.GetDisplayTime = function()
  {
    return displayTime;
  };
  
  this.SetOffsetX = function(val)
  {
    offsetX = val;
  };
  this.SetOffsetY = function(val)
  {
    offsetY = val;
  };
  
  this.SetVisible = function(val)
  {
    visible = val;
  };
  this.GetVisible = function()
  {
    return visible;
  };
  
  this.SetFPS = function(val)
  {
    FPS = val;
  };
  this.GetFPS = function()
  {
    return FPS;
  };
  var GetFPS = this.GetFPS;
    
  this.SetWidth = function(width)
  {
    divCtl.style.width = width;
  };
  this.SetHeight = function(height)
  {
    divCtl.style.height = height;
  };
  
  this.SetInnerHTML = function(html)
  {
    divCtl.innerHTML = html;
  };
  this.GetInnerHTML = function()
  {
    return divCtl.innerHTML;
  };
  
  this.SetCloseOnClickOut = function( tf )
  {
    if ( tf )
      AddOnMouseUpOnLoad();
    else
      RemoveOnMouseUp( document, MouseUp );
  };
    
  this.Show = function()
  {
    SizeIFrame();

    AttachToControl(openerControl, openSideX, openSideY, offsetX, offsetY);

    divCtl.style.visibility = 'visible';
    controlFader.Fade( divCtl.id, 0, 100, fadeInTime, GetFPS(), null );
  };
  
  this.ShowModal = function()
  {
    this.SetCloseOnClickOut( false );
    ControlFader.SetOpac( duFrame.id, 85 );    
    duFrame.style.visibility = 'visible';
    this.Show();
  };
  
  this.Hide = function()
  {
    if ( divCtl.style.visibility == 'visible' )
    {
      closing.Fire( divCtl.id, null );
      controlFader.Fade( divCtl.id, 100, 0, fadeInTime, GetFPS(), null );
    }
  };
  var Hide = this.Hide;

  var fadeComplete = function( sender, arg )
  {
    if ( arg <= 0 )
    {
      duFrame.style.visibility = 'hidden';
      divCtl.style.visibility = 'hidden';
      CollapseIFrame();
      visible = false;
      closed.Fire( divCtl.id, null );
    }
    else
    {
      visible = true;      

      opened.Fire( divCtl.id, null );
      
      if ( displayTime > 0 )
        setTimeout( Hide, displayTime );
    }
  };
  controlFader.FadeComplete.AddHandler( fadeComplete );
  
  this.AttachToControl = function(ctl, sideX, sideY, offsetXVal, offsetYVal)
  {
    offsetX = offsetXVal;
    offsetY = offsetYVal;
 
    if ( ctl == null || sideX == "centerScreen" )
    {
      PositionControlCenterScreen( divCtl );
    }
    else
    {
      openerControl = ctl;
      openSideX = sideX;
      openSideY = sideY;

      AttachControlToControlX(divCtl, ctl, sideX, offsetXVal);
      AttachControlToControlY(divCtl, ctl, sideY, offsetYVal);
    }
  };
  var AttachToControl = this.AttachToControl;
  
  var MouseUp = function(m)
  {
    try
    {
      if ( divCtl.style.visibility == 'hidden' )
        return;
    
      var pos = GetMousePos(m);
      if ( !ControlContainsPoint(divCtl, pos[0], pos[1]) )
        Hide();
    }
    catch(e){}
  };
  var AddOnMouseUpOnLoad = function()
  {
    AddOnMouseUp( document, MouseUp );
  };
  setTimeout(AddOnMouseUpOnLoad,0);
    
  this.Dispose = function()
  {
    RemoveOnMouseUp( document, MouseUp );
  };
}