// JScript File

IManager.Hooked = null;
IManager.orgHandlerMove = null;
IManager.orgHandlerDown = null;
IManager.orgHandlerUp = null;
IManager.orgHandlerKey = null;
IManager.DebugEnabled = true;
IManager.DebugVerboseEnabled = false;
IManager.cursorX = 0;
IManager.cursorY = 0;
IManager.GlobalTooltipId = "uniquePageToolTip";
IManager.CurrentTipRequest = null;
IManager.ToolTipsEnabled = true;
IManager.ToolTipContentId = 0;
IManager.ToolTips = new Array();

function IManager() {
    if (IDebug) IDebug.Message('IManager Installed');
    this.dragElement = null;
    this.tooltip = null;
    this.toolparent = null;
    this.sizeMode = 0;
    this.sizeElement = null;
    this.lastKey = null;
    this.unique = 0;
    return this;
}

function IToolltip(element, realContent, displayContent) {
    if (IDebug) IDebug.Message('ITooltip created');
    this.xmlHttp = ILib.GetHttpRequest();
    IManager.CurrentTipRequest = this.tipId = 'IManager.Tooltip-' + (IManager.ToolTipContentId++);
    IManager.ToolTips[this.tipId] = this;
    this.tipElement = element;
    this.displayContent = displayContent;
    this.tipContent = realContent;
    this._load();
    return this;
}

IManager.Hook = function() {
    if (!IManager.Hooked || IManager.Hooked == null) {

        ILib.LoadResourceFile('jscss','IManager', 'css');
        ILib.LoadResourceFile('jscss','ToolTip', 'css');

        if (IDebug && IManager.DebugEnabled) IDebug.Message('Install IManager');
        IManager.Hooked = new IManager();

        IManager.orgHandlerMove = document.onmousemove;
        IManager.orgHandlerDown = document.onmousedown;
        IManager.orgHandlerUp = document.onmouseup;
        IManager.orgHandlerKeyPress = document.onkeypress;
        IManager.orgHandlerKeyDown = document.onkeydown;

        document.onmousemove = IManager.OnMouseMove;
        document.onmousedown = IManager.OnMouseDown;
        document.onmouseup = IManager.OnMouseUp;
        document.onkeypress = IManager.OnKeyPress;
        document.onkeydown = IManager.OnKeyDown;

    }
}

IManager.OnKeyPress = function (ev) {
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnKeyPress', ev);

    var evt = ILib.GetEvent(ev);
    var src = ILib.GetEventElement(ev);
    if (IDebug && IManager.DebugEnabled) IDebug.Message('IManager.OnKeyPress : keyCode=' + evt.keyCode + ', altKey=' + evt.altKey);

    if ((evt.keyCode == 68 || evt.keyCode == 100) && src && (src.tagName == 'BODY' || src.tagName == 'DIV')) {
        IManager.DebugEnabled = !IManager.DebugEnabled;
        IDebug.Enabled = IManager.DebugEnabled;
        IDebug.Show(IManager.DebugEnabled);
    }

    if (IManager.orgHandlerKeyPress) {
        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager.Bubble onkeypress', IManager.orgHandlerKeyPress);
        try {
            eval(IManager.orgHandlerKeyPress());
        } catch (e) {
            alert('onkeypress bubble failed : ' + e.message ? e.message : e);
        }
    }
}

IManager.OnKeyDown = function (ev) {

    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnKeyDown', ev);

    var evt = ILib.GetEvent(ev);
    var src = ILib.GetEventElement(ev);
    if (IDebug && IManager.DebugEnabled) IDebug.Message('IManager.OnKeyDown : keyCode=' + evt.keyCode + ', altKey=' + evt.altKey);

    if (evt.keyCode == 27) {
        var popupDiv = getElement('popupHolder');
        if (!popupDiv && parent.document)
            popupDiv = parent.document.getElementById('popupHolder');
        if (popupDiv) {
            popupDiv.style.display = "none";
            popupDiv.innerHTML = "";
        }
    }

    if (IManager.orgHandlerKeyDown) {
        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager.Bubble onkeydown', IManager.orgHandlerKeyDown);
        try {
            eval(IManager.orgHandlerKeyDown());
        } catch (e) {
            alert('onkeydown bubble failed : ' + e.message ? e.message : e);
        }
    }
}

IManager.OnMouseMove = function(ev) {
    if (IDebug && IManager.DebugVerboseEnabled) IDebug.EventMessage('IManager.OnMouseMove', ev);

    src = ILib.GetEventElement(ev);
    evt = ILib.GetEvent(ev);
    IManager.cursorX = evt.clientX;
    IManager.cursorY = evt.clientY;
    drag = IManager.Hooked.dragElement ? IManager.Hooked.dragElement : findParentElementByClass(src, 'iwDrag');
    if (drag) {
        if (!IManager.Hooked.dragElement) {
            if (IDebug && IManager.DebugEnabled) {
                IDebug.ElementMessage('Over Draggable Element', drag);
                IDebug.ObjectMessage('Over Draggable Element', IManager.Hooked);
            }
        }
        IManager.Hooked._dragCheck(drag, ILib.GetEvent(ev));
    }
    else if (src && IManager.ToolTipsEnabled) {
        var ttn = null;
        try {
            ttn = findParentElementByClass(src, 'tooltipClass');
        } catch (e) {
            if (IDebug && IManager.DebugEnabled) {
                IDebug.Message('error in findParentElementByClass ' + e.message);
            }
        }
        if (IDebug && IManager.DebugEnabled)
            IDebug.ElementMessage('Over Tooltip Element', ttn);
        IManager.Hooked._tooltip(ttn, ILib.GetEvent(ev));
    }
    else if (IManager.ToolTipsEnabled) {
        IManager.Hooked._tipoff();
    }

    if (IManager.orgHandlerMove) {
        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager.Bubble onmousemove', IManager.orgHandlerMove);
        try {
            eval(IManager.orgHandlerMove());
        } catch (e) {
            alert('onmousemove bubble failed : ' + e.message ? e.message : e);
        }
    }
}

IManager.OnMouseDown = function(ev) {
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnMouseDown', ev);

    src = ILib.GetEventElement(ev);
    drag = findParentElementByClass(src, 'iwDrag');
    if (drag) {
        IManager.Hooked._beginDrag(drag, ILib.GetEvent(ev));
    }

    if (IManager.orgHandlerDown) {
        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager.Bubble onmousedown', IManager.orgHandlerDown);
        try {
            eval(IManager.orgHandlerDown());
        } catch (e) {
            alert('onmousedown bubble failed : ' + e.message ? e.message : e);
        }
    }
}

IManager.OnMouseUp = function(ev) {
    var evt = ILib.GetEvent(ev);
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnMouseUp', ev);

    IManager.Hooked._endDrag(evt);

    if (IManager.orgHandlerUp) {
        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager.Bubble onmouseup', IManager.orgHandlerUp);
        try {
            eval(IManager.orgHandlerUp());
        } catch (e) {
            alert('onmouseup bubble failed : ' + e.message ? e.message : e);
        }
    }
}

IManager.BusyToolTip = function() {
}

IManager.Navigate = function(url) {
    IManager.BusyToolTip();
    top.location = url;
}

IManager.prototype._tooltip = function (element, ev) {
    if (!element && this.tooltip) {
        this._tipoff();
        return;
    }
    else if (element && element != this.toolparent) {
        this._tipoff();
        this.tooltip = findChildByClass(element, 'tooltipContentInner');
        if (this.tooltip) {
            // We create an additional element as a child of the main
            // document body to avoid any problems with positioning
            // when inside other positional containers
            docTip = document.getElementById(IManager.GlobalTooltipId);
            if (!docTip) {
                docTip = document.createElement('DIV');
                docTip.className = 'tooltipContent';
                docTip.style.display = 'none';
                docTip.id = IManager.GlobalTooltipId;
                document.body.insertBefore(docTip, document.body.firstChild);
            }
            // Doing this means that the first attempt tp
            // get the acctual size of the tip in the next section
            // will get the real rendered size - and will avaoid drawing flicker
            docTip.style.position = 'absolute';
            docTip.style.left = '-2048px';
            docTip.style.top = '-2048px';
            docTip.innerHTML = this.tooltip.innerHTML;
            displayContent = findDescendantByClass(docTip, '_ttmain');

            if (element.getAttribute("content") && element.getAttribute("content") != "" && element.getAttribute("content") != null) {
                realContent = findDescendantByClass(this.tooltip, '_ttmain');
                if (realContent) {
                    try {
                        new IToolltip(element, realContent, displayContent);
                    } catch (e) {
                        realContent.innerHTML = "Failed to load tooltip data : <br/><br/>" + e.message;
                        displayContent.innerHTML = "Failed to load tooltip data : <br/><br/>" + e.message;
                    }
                }
            }
            //            else
            //                IManager.CurrentTipRequest = null;

            this.tooltip = docTip;
            displayContent.style.display = 'block';
            this.tooltip.style.display = 'block';
            this.toolparent = element;
            this._ty = (parseInt(ev.clientY) - getElementTop(element));
            this._tx = getElementScreenLeft(element) - (isIE() ? 12 : 12);
        }
    }
    if (this.tooltip) {
        this._tipposition(ev.clientX, ev.clientY);
    }
}

IManager.prototype._tipposition = function(clientX,clientY) {
    //        if (ev.clientX >= 0 && ev.clientY >= 0) 
    {
        scrollTop = ILib.GetDocumentScrollTop();
        scrollLeft = ILib.GetDocumentScrollLeft();
        newTop = (parseInt(clientY) + 8 + scrollTop);
        newLeft = ((parseInt(clientX) - this._tx) + scrollLeft);

        if (IDebug && IManager.DebugEnabled) IDebug.ObjectMessage('IManager._tipposition : scrollTop=' + scrollTop + ', clientY=' + clientY + ', height=' + getElementHeight(this.tooltip) + ', InnerHeight=' + ILib.GetInsideWindowHeight(), this);
        
        if ((newTop + getElementHeight(this.tooltip)) > (ILib.GetInsideWindowHeight() + scrollTop))
            newTop = newTop - getElementHeight(this.tooltip);
        if ((newLeft + getElementWidth(this.tooltip)) > (ILib.GetInsideWindowWidth() + scrollLeft))
            newLeft = newLeft - getElementWidth(this.tooltip) - (isIE() ? 12 : 12);

        this.tooltip.style.left = (newLeft < scrollLeft ? scrollLeft : newLeft) + 'px';
        this.tooltip.style.top = (newTop < scrollTop ? scrollTop : newTop) + 'px';

        this.tooltip.style.position = 'absolute';
        this.tooltip.style.display = 'block';
    }
}

IManager.prototype._tipoff = function() {
    if (this.tooltip)
        this.tooltip.style.display = 'none';
    this.tooltip = null;
    this.toolparent = null;
}

IManager.prototype._beginDrag = function(element, ev) {
    if (!this.dragElement) {
        var drag = findParentElementByClass(element, 'iwDragTarget');
        if (!drag) drag = element;
        IDebug.ElementMessage('Drag Start', drag);
        if (isIE()) {
            element.onselectstart = function() { return false; };
            document.body.onselectstart = function() { return false; };
        }
        else
            element.style.userSelect = 'none';
        drag.style.position = 'absolute';

        //    	this._grabx = parseInt(ev.clientX) - getElementLeft(drag);
        //      	this._graby = parseInt(ev.clientY) - getElementTop(drag);

        this._grabx = parseInt(ev.screenX) - drag.offsetLeft;
        this._graby = parseInt(ev.screenY) - drag.offsetTop;

        if (IDebug && IManager.DebugEnabled)
            IDebug.Message('Drag Init : x=' + getElementLeft(drag) + ',' + ev.clientX + ',' + this._grabx +
    	                ', y=' + getElementTop(drag) + ',' + ev.clientY + ',' + this._graby);
        this._width = drag.offsetWidth;
        this._height = drag.offsetHeight;
        this._left = drag.offsetLeft;
        this._top = drag.offsetTop;

        ev.returnValue = false;
        ev.cancelBubble = true;

        this.dragElement = drag;
    }
}

IManager.prototype._endDrag = function(ev) {
    if (this.dragElement) {
        if (isIE())
            document.body.onselectstart = null;
        if (IDebug && IManager.DebugEnabled) IDebug.ElementMessage('Drag Stop', this.dragElement);
    }

    this._grabx = 0;
    this._graby = 0;
    this._width = 0;
    this._height = 0;
    this._left = 0;
    this._top = 0;

    this.dragElement = null;
}

IManager.prototype._dragCheck = function(element, ev) {
    if (!this.dragElement) {
        var drag = findParentElementByClass(element, 'iwDragTarget');
        if (!drag) drag = element;
        element.style.cursor = 'move';
        if (IDebug && IManager.DebugEnabled) IDebug.ElementMessage('Drag Target', drag);

    }
    else {
        this._drag(element, ev);
    }
}

IManager.prototype._drag = function(element, ev) {
    if (this.dragElement) {
        try {
            if (ev.clientX >= 0 && ev.clientY >= 0) {
                //			this.dragElement.style.left = (parseInt(ev.clientX) - this._grabx)+'px';
                //			this.dragElement.style.top = (parseInt(ev.clientY) - this._graby)+'px';
                this.dragElement.style.left = (parseInt(ev.screenX) - this._grabx) + 'px';
                this.dragElement.style.top = (parseInt(ev.screenY) - this._graby) + 'px';
                if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('Drag Element', ev);
            }
            else
                this._endDrag(ev);
        } catch (e) {
            alert('drag failed : ' + e.message ? e.message : e);
        }
        ev.returnValue = false;
        ev.cancelBubble = true;
    }
}

IToolltip.prototype._load = function() {
    this.tipContent.innerHTML = "...Loading...";
    try {
        this.xmlHttp.open("GET", this.tipElement.getAttribute("content"), true);
        this.xmlHttp.onreadystatechange = new Function("IManager.TipResponse('" + this.tipId + "')");
        this.xmlHttp.setRequestHeader('Content-Type', 'text/html');
        this.xmlHttp.setRequestHeader('Cache-Control', 'no-cache');
        this.xmlHttp.setRequestHeader('Pragma', 'no-cache');
        this.xmlHttp.send('');
    }
    catch (e) {
        this.tipContent.innerHTML = "Failed to request tooltip content: <br/>" + e.messsage + "<br/>" + e.description;
    }
}

IManager.TipResponse = function (tipId) {
    var tip = IManager.ToolTips[tipId];
    if (tip != null) {
        try {
            switch (tip.xmlHttp.readyState) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    // The Response
                    tip.tipContent.innerHTML = tip.xmlHttp.responseText;
                    tip.tipContent.style.display='none';
                    tip.tipElement.content = "";
                    delete IManager.ToolTips[tipId];
                    if (IManager.CurrentTipRequest == tipId) {
                        tip.displayContent.innerHTML = tip.tipContent.innerHTML;
                        IManager.Hooked._ty = (parseInt(IManager.cursorY) - getElementTop(tip.displayContent));
                        IManager.Hooked._tx = getElementScreenLeft(tip.displayContent) - (isIE() ? 12 : 12);
                        IManager.Hooked._tipposition(IManager.cursorX, IManager.cursorY);
                    }
                    break;
                default:
                    break;
            }
        } catch (e) {
        }
    }
}

// And auto hook up the IManager
IManager.Hook();


