﻿// 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.loginWindow = null;
    this.resetPasswordLayer = null;

    this.body = 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, loginlayerid, resetpasswordid) {
    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, loginlayerid, resetpasswordid);
}


// -------------- Object Functions ----------------------

function Tops$AjaxLogin$LoginWindow$_initializeInternal(windowid, loginlayerid, resetpasswordid) {
    this.loginWindow = document.getElementById(windowid);
    this.LoginLayer = document.getElementById(loginlayerid);
    this.resetPasswordLayer = document.getElementById(resetpasswordid);

    this.body = document.getElementById('loginBody');

    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(location) {
    if (this.LoginLayer)
        this.LoginLayer.style.display = '';
    if (this.resetPasswordLayer)
        this.resetPasswordLayer.style.display = 'none';
    if (location) {
        if (location == 'window')
            this.positionInWindow();

    } else {
        this.positionInWindow();
        //        this.positionOnMouse();
    }

    this.loginWindow.style.display = 'inline';
}

function Tops$AjaxLogin$LoginWindow$prototype$hide() {
    if (this.resetPasswordLayer)
        this.resetPasswordLayer.style.display = 'none';
    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() {
    if (this.LoginLayer)
        this.LoginLayer.style.display = 'none';
    if (this.resetPasswordLayer)
        this.resetPasswordLayer.style.display = 'inline';
}

function Tops$AjaxLogin$LoginWindow$prototype$createUser(url, registerUrl) {
    if (Tops.AjaxLogin.RegisterUserWindow && Tops.AjaxLogin.RegisterUserWindow.getInstance()) {
        Tops.AjaxLogin.RegisterUserWindow.getInstance().open(registerUrl);
        this.hide();
    } else {
        document.location.href = url;
    }
}

// ------------ Register Prototypes ----------------------

Tops.AjaxLogin.LoginWindow.prototype = {
    _initializeInternal: Tops$AjaxLogin$LoginWindow$_initializeInternal,
    show: Tops$AjaxLogin$LoginWindow$prototype$show,
    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() {
    return {
        "width": (function() {
            if (typeof window.innerWidth !== 'undefined') return window.innerWidth;
            if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
            return document.body.clientWidth;
        })(),
        "height": (function() {
            if (typeof window.innerHeight !== 'undefined') return window.innerHeight;
            if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
            return document.body.clientHeight;
        })(),
        "scrollY": (function() {
            if (typeof window.pageYOffset !== 'undefined') return window.pageYOffset;
            if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
            return document.body.scrollTop || 0;
        })(),
        "scrollX": (function() {
            if (typeof window.pageXOffset !== 'undefined') return window.pageXOffset;
            if (document.documentElement && document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
            return document.body.scrollLeft || 0;
        })()
    }
}

/*
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;
}
*/