﻿// JScript File

if(typeof Tops == 'undefined')
    Tops = {};
if(typeof Tops.Controls == 'undefined')
Tops.Controls = {};

Tops.Controls.WindowControl = function Tops$Controls$WindowControl()
{
    this.debug = 0;
    this.state = 0;
    this.IE = document.all?true:false;

    this.layer = null;
    
    this.mouseX = 0;
    this.mouseY = 0;
}

Tops.Controls.WindowControl.getInstance = function Tops$Controls$WindowControl$getInstance()
{
    if (arguments.length !== 0) throw Error.parameterCount();
    return Tops.Controls.WindowControl._instance || null;
}

Tops.Controls.WindowControl._initialize = function Tops$Controls$WindowControl$_initialize( windowid )
{
    if (Tops.Controls.WindowControl.getInstance()) {
        throw Error('Instance Already Exists.  Cannot have multiple instances.');
    }
    Tops.Controls.WindowControl._instance = new Tops.Controls.WindowControl();
    var obj = Tops.Controls.WindowControl.getInstance();
    obj._initializeInternal( windowid );
}


// -------------- Object Functions ----------------------

function Tops$Controls$WindowControl$prototype$_initializeInternal(windowid)
{
    this.layer = document.getElementById(windowid);

    var MyObj = this;
    var IE = document.all?true:false;
    var agt=navigator.userAgent.toLowerCase();
    var firefox = false;
    if (agt.indexOf("firefox") != -1)
        firefox = true;
    
    if (!IE && !firefox )
        document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = function(e) {
        return MyObj._captureMouseMove.call(MyObj, e);
    }
}

function Tops$Controls$WindowControl$prototype$show(location)
{
    if(location) {
        if(location == 'window')
            this.positionInWindow();
    
    } else
        this.positionOnMouse();

    this.layer.style.display = 'inline';
}

function Tops$Controls$WindowControl$prototype$hide()
{
    this.layer.style.display = 'none';
}

function Tops$Controls$WindowControl$prototype$_captureMouseMove( e )
{
    if (this.IE) { // grab the x-y pos.s if browser is IE
        this.mouseX = event.clientX;
        this.mouseY = event.clientY;
    }
    else if(e.clientY) {
        this.mouseX = e.clientX;
        this.mouseY = e.clientY;
    }
    else {  // grab the x-y pos.s if browser is NS
        this.mouseX = e.pageX;
        this.mouseY = e.pageY;
    }  
    if (this.mouseX < 0){this.mouseX = 0;}
    if (this.mouseY < 0){this.mouseY = 0;}
}

function Tops$Controls$WindowControl$prototype$positionOnMouse()
{
    if(this.layer){
        var w = getWindowInfo();
        
        this.layer.style.display = 'inline';
        var width = 0;
        var height = 0;
        if(this.layer.offsetWidth) {
            width = this.layer.offsetWidth;
            height = this.layer.offsetHeight;
        } else if(this.layer.style.pixelWidth) {
            width = this.layer.style.pixelWidth;
            height = this.layer.style.pixelHeight;
        }
        if(this.debug) {
            alert("Width: " + width + "\t\tHeight: " + height + "\n" +
            "Window Width: " + w.width + "\t\tHeight: " + w.height + "\n" +
            "Window ScrollX: " + w.scrollX + "\t\tScrollY: " + w.scrollY + "\n" +
            "Mouse X: " + this.mouseX + "\t\tY: " + this.mouseY
            );
        }
        
        this.layer.style.display = 'none';
        
        var dt = parseInt(this.mouseY);
        var db = w.height - parseInt(this.mouseY)
        var dl = parseInt(this.mouseX);
        var dr = w.width - parseInt(this.mouseX);
        
//        if( db >= height && dr >= width){
//            this.layer.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
//            this.layer.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
//        } else if( dt >= height && dr >= width){
//            this.layer.style.top = (parseInt(this.mouseY) + w.scrollY - height) + "px";
//            this.layer.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
//        } else if( db >= height && dl >= width){
            this.layer.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
            this.layer.style.left = (parseInt(this.mouseX) + w.scrollX - width) + "px";
//        } else if( dt >= height && dl >= width){
//            this.layer.style.top = (parseInt(this.mouseY) + w.scrollY - height) + "px";
//            this.layer.style.left = (parseInt(this.mouseX) + w.scrollX - width) + "px";
//        } else {
//            this.layer.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
//            this.layer.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
//        }
    }

}

function Tops$Controls$WindowControl$prototype$positionInWindow()
{
    if(this.layer){
        var w = getWindowInfo();
        
        this.layer.style.display = 'inline';
        
        var width = 0;
        var height = 0;
        if(this.layer.offsetWidth) {
            width = this.layer.offsetWidth;
            height = this.layer.offsetHeight;
        } else if(this.layer.style.pixelWidth) {
            width = this.layer.style.pixelWidth;
            height = this.layer.style.pixelHeight;
        }
        
        this.layer.style.top = ((w.height / 2) - (parseInt(height)/2) + w.scrollY) + "px";
        this.layer.style.left = ((w.width / 2) - (parseInt(width)/2) + w.scrollX) + "px";
        
    }
}

// ------------ Register Prototypes ----------------------

Tops.Controls.WindowControl.prototype = {
    _initializeInternal: Tops$Controls$WindowControl$prototype$_initializeInternal,
    show: Tops$Controls$WindowControl$prototype$show,
    hide: Tops$Controls$WindowControl$prototype$hide,
    _captureMouseMove: Tops$Controls$WindowControl$prototype$_captureMouseMove,
    positionOnMouse: Tops$Controls$WindowControl$prototype$positionOnMouse,
    positionInWindow: Tops$Controls$WindowControl$prototype$positionInWindow
};

// -------------- Generic functions -----------------------

function getWindowInfo()
{
  var o = new Object();
  
  // Window Width and Height  
  if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    o.width = document.documentElement.clientWidth;
    o.height = document.documentElement.clientHeight;
  } else if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    o.width = window.innerWidth;
    o.height = window.innerHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    o.width = document.body.clientWidth;
    o.height = document.body.clientHeight;
  }

  // Scroll
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    o.scrollY = window.pageYOffset;
    o.scrollX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    o.scrollY = document.body.scrollTop;
    o.scrollX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    o.scrollY = document.documentElement.scrollTop;
    o.scrollX = document.documentElement.scrollLeft;
  } else {
    o.scrollY = 0;
    o.scrollX = 0;
  }


  return o;
}