// JScript File

IManager.Hooked = null;
IManager.orgHandlerMove = null;
IManager.orgHandlerDown = null;
IManager.orgHandlerUp = null;
IManager.DebugEnabled = true;
IManager.ToolTipsEnabled = true;

function IManager()
{
    if (IDebug) IDebug.Message('IManager Installed');
    this.dragElement = null;
    this.tooltip = null;
    this.toolparent = null;
    this.sizeMode = 0;
    this.sizeElement = null;
    return this;
}

IManager.Hook = function()
{
    if (!IManager.Hooked || IManager.Hooked == null)
    {
        if (IDebug && IManager.DebugEnabled) IDebug.Message('Install IManager');
        IManager.Hooked = new IManager();        
        
        IManager.orgHandlerMove = document.onmousemove;
        IManager.orgHandlerDown = document.onmousedown;
        IManager.orgHandlerUp = document.onmouseup;
    
        document.onmousemove = IManager.OnMouseMove;
        document.onmousedown = IManager.OnMouseDown;
        document.onmouseup = IManager.OnMouseUp;

    }
}

IManager.OnMouseMove = function(ev)
{
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnMouseMove',ev);
    
    src = getEventElement(ev);
    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,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.description);
          }
        }    
        if (IDebug && IManager.DebugEnabled)
            IDebug.ElementMessage('Over Tooltip Element',ttn);
        IManager.Hooked._tooltip(ttn,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.description?e.description:e);
        }
    }    
}

IManager.OnMouseDown = function(ev)
{
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnMouseDown',ev);
    
    src = getEventElement(ev);
    drag = findParentElementByClass(src,'iwDrag');
    if (drag)
    {
        IManager.Hooked._beginDrag(drag,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.description?e.description:e);
        }
    }    
}

IManager.OnMouseUp = function(ev)
{
    var evt = getEvent(ev);
    if (IDebug && IManager.DebugEnabled) IDebug.EventMessage('IManager.OnMouseUp',ev);
    
    IManager.Hooked._endDrag(getEvent(ev));
    
    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.description?e.description: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,'tooltipContent');
        var eh = getElementTop(element);
        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 = getElement('$$tootip$$');
            if (!docTip)
            {
                docTip = document.createElement('SPAN');
                docTip.className = 'tooltipContent';
                docTip.style.display = 'none';
                docTip.id = '$$tooltip$$';
                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;
            
            this.tooltip = docTip;
            this.tooltip.style.display = 'block';
            this.toolparent = element;
        	this._ty = (parseInt(ev.clientY) - getElementTop(element));
        	this._tx = getElementScreenLeft(element) - (isIE()?20:20);
        }
    }
    if (this.tooltip)
    {
        
//        if (ev.clientX >= 0 && ev.clientY >= 0) 
        {
			scrollTop = isIE()?document.documentElement.scrollTop:window.pageYOffset;
			scrollLeft = isIE()?document.documentElement.scrollLeft:window.pageXOffset;
        	newTop = (parseInt(ev.clientY) + 8 + scrollTop);
        	newLeft = ((parseInt(ev.clientX) - this._tx) + scrollLeft);
        	if ((newTop+getElementHeight(this.tooltip)) > (getInsideWindowHeight() + scrollTop))
        	    newTop = newTop - getElementHeight(this.tooltip);
        	if ((newLeft+getElementWidth(this.tooltip)) > (getInsideWindowWidth() + scrollLeft))
        	    newLeft = newLeft - getElementWidth(this.tooltip) - (isIE()?20:20);

	        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.description?e.description:e);
		}
    	ev.returnValue = false;
   		ev.cancelBubble = true;
    }
}

// And auto hook up
IManager.Hook();

