﻿// JScript File

if(typeof Tops == 'undefined')
    Tops = {};
if(typeof Tops.AjaxLogin == 'undefined')
    Tops.AjaxLogin = {};
Tops.AjaxLogin.LoginWindow = {};

Tops.AjaxLogin.LoginWindow = function Tops$AjaxLogin$LoginWindow()
{
    this.debug = 0;
    this.state = 0;
    this.IE = document.all?true:false;
    
    this.params = {};
    this.views = {};

    this.loginWindow = null;
    
    this.mouseX = 0;
    this.mouseY = 0;
}


Tops.AjaxLogin.LoginWindow.getInstance = function Tops$AjaxLogin$LoginWindow$getInstance()
{
    if (arguments.length !== 0) throw Error.parameterCount();
    return Tops.AjaxLogin.LoginWindow._instance || null;
}

Tops.AjaxLogin.LoginWindow._initialize = function Tops$AjaxLogin$LoginWindow$_initialize( windowid, params )
{
    if (Tops.AjaxLogin.LoginWindow.getInstance()) {
        throw Error('Instance Already Exists.  Cannot have multiple instances.');
    }
    Tops.AjaxLogin.LoginWindow._instance = new Tops.AjaxLogin.LoginWindow();
    var obj = Tops.AjaxLogin.LoginWindow.getInstance();
    obj._initializeInternal( windowid, params );
}


// -------------- Object Functions ----------------------

function Tops$AjaxLogin$LoginWindow$_initializeInternal(windowid, params)
{
    this.loginWindow = document.getElementById(windowid);
    
    for(k in params)
    {
        this.params[k] = params[k];
    }
    
    if(this.params.views == undefined)
        throw "Missing views parameter.";
    if(this.params.views.login == undefined)
        throw "Missing login View.";
        
    for(vname in this.params.views)
    {
        this.views[vname] = document.getElementById(this.params.views[vname]);
    }

    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$AjaxLogin$LoginWindow$prototype$show(view, location)
{
    if( view == undefined || view === '')
        view = 'login';
    this.showView(view);
    
    if(location) {
        if(location == 'window')
            this.positionInWindow();
    
    } else {
        this.positionOnMouse();
    }

    this.loginWindow.style.display = 'inline';
}

function Tops$AjaxLogin$LoginWindow$prototype$showView(viewname)
{
    for(vname in this.views)
    {
        var v = this.views[vname];
        if(vname === viewname)
        {
            v.style.display = '';
        }
        else
        {
            v.style.display = 'none';
        }
    }
}

function Tops$AjaxLogin$LoginWindow$prototype$hide()
{
    this.loginWindow.style.display = 'none';
}

function Tops$AjaxLogin$LoginWindow$prototype$HttpRequest(url, write)
{
    var req = false;
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
            try {
                            req = new XMLHttpRequest();
            } catch(e) {
                            req = false;
            }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
            try {
                    req = new ActiveXObject("Msxml2.XMLHTTP");
                   
            } catch(e) {
                    try {
                            req = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch(e) {
                            req = false;
                    }
                    }
        }
        
        try {
          req.open('GET', url, false); 
          req.send(null); 
          if (write)
            document.write(req.responseText); 
          return req.responseText;
         
        } 
        catch (e) { 
          return ""; 
          
        }
}

function Tops$AjaxLogin$LoginWindow$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$AjaxLogin$LoginWindow$prototype$positionOnMouse()
{
    if(this.loginWindow){
        var w = getWindowInfo();
        
        this.loginWindow.style.display = 'inline';
        var width = 0;
        var height = 0;
        if(this.loginWindow.offsetWidth) {
            width = this.loginWindow.offsetWidth;
            height = this.loginWindow.offsetHeight;
        } else if(this.loginWindow.style.pixelWidth) {
            width = this.loginWindow.style.pixelWidth;
            height = this.loginWindow.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.loginWindow.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.loginWindow.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
            this.loginWindow.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
        } else if( dt >= height && dr >= width){
            this.loginWindow.style.top = (parseInt(this.mouseY) + w.scrollY - height) + "px";
            this.loginWindow.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
        } else if( db >= height && dl >= width){
            this.loginWindow.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
            this.loginWindow.style.left = (parseInt(this.mouseX) + w.scrollX - width) + "px";
        } else if( dt >= height && dl >= width){
            this.loginWindow.style.top = (parseInt(this.mouseY) + w.scrollY - height) + "px";
            this.loginWindow.style.left = (parseInt(this.mouseX) + w.scrollX - width) + "px";
        } else {
            this.loginWindow.style.top = (parseInt(this.mouseY) + w.scrollY) + "px";
            this.loginWindow.style.left = (parseInt(this.mouseX) + w.scrollX) + "px";
        }
    }

}

function Tops$AjaxLogin$LoginWindow$prototype$positionInWindow()
{
    if(this.loginWindow){
        var w = getWindowInfo();
        
        this.loginWindow.style.display = 'inline';
        
        var width = 0;
        var height = 0;
        if(this.loginWindow.offsetWidth) {
            width = this.loginWindow.offsetWidth;
            height = this.loginWindow.offsetHeight;
        } else if(this.loginWindow.style.pixelWidth) {
            width = this.loginWindow.style.pixelWidth;
            height = this.loginWindow.style.pixelHeight;
        }
        
        this.loginWindow.style.top = ((w.height / 2) - (parseInt(height)/2) + w.scrollY) + "px";
        this.loginWindow.style.left = ((w.width / 2) - (parseInt(width)/2) + w.scrollX) + "px";
        
    }
}

function Tops$AjaxLogin$LoginWindow$prototype$showRecovery()
{
    this.showView('resetpassword');
}

function Tops$AjaxLogin$LoginWindow$prototype$createUser(url)
{
    if(typeof(Tops.AjaxLogin.RegisterUserWindow) !== 'undefined' && Tops.AjaxLogin.RegisterUserWindow.getInstance())
    {
        Tops.AjaxLogin.RegisterUserWindow.getInstance().open();
        this.hide();
    } else
    {
        document.location.href = url;
    }
}

// ------------ Register Prototypes ----------------------

Tops.AjaxLogin.LoginWindow.prototype = {
    _initializeInternal: Tops$AjaxLogin$LoginWindow$_initializeInternal,
    show: Tops$AjaxLogin$LoginWindow$prototype$show,
    showView: Tops$AjaxLogin$LoginWindow$prototype$showView,
    hide: Tops$AjaxLogin$LoginWindow$prototype$hide,
    _HttpRequest:Tops$AjaxLogin$LoginWindow$prototype$HttpRequest,
    _captureMouseMove: Tops$AjaxLogin$LoginWindow$prototype$_captureMouseMove,
    positionOnMouse: Tops$AjaxLogin$LoginWindow$prototype$positionOnMouse,
    positionInWindow: Tops$AjaxLogin$LoginWindow$prototype$positionInWindow,
    showRecovery: Tops$AjaxLogin$LoginWindow$prototype$showRecovery,
    createUser: Tops$AjaxLogin$LoginWindow$prototype$createUser

};




// -------------- 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;
}
