﻿HoverManager = function()
{   
    //Current DIV attributes
    var _divID = 'divHover'; 
    var _caption = "";
    var _text = "";
    var _height;
    var _width;
    var _horizontalAlign;
    var _persistent;
    
    
    //Defaults - set in loadDefaults()
    var _defaultWidth;
    var _defaultHeight;
    var _defaultShowDelay;
    var _defaultHideDelay;
    var _defaultPersistent;
    var _defaultHorizontalAlign;
        
    var _browser = new BrowserInfo();
    var _windowLoaded = false;
    var _mouseCaptured = false;
    var _offsetX = 15;
    var _offsetY = 15;
    var _div;
    var _frame;
    var _timerID = 0;
         
    if(window.addEventListener)
    {
        window.addEventListener("load", onWindowLoad, false);
    }
    else if(window.attachEvent)
    {
        window.attachEvent("onload", onWindowLoad);
    }



    //Public Functions    
    this.show = show;
    function show(caption, text, width, height, horizontalAlignment, persistent, delay)
    {
        if(!_windowLoaded)
        {
            return true;
        }
        
        clearDelay();
        
        if(width == null) width = _defaultWidth;
        if(height == null) height = _defaultHeight;
        if(horizontalAlignment == null) horizontalAlignment = _defaultHorizontalAlignment;
        if(persistent == null) persistent = _defaultPersistent;
        if(delay == null) delay = _defaultShowDelay;
        
        if(delay > 0)
        {
            setDelay(function(){show(caption, text, width, height, horizontalAlignment, persistent, 0);}, delay);
            return true;
        }
                
        _caption = caption;
        _text = text;
        _height = height;
        _horizontalAlignment = horizontalAlignment;
        _width = width;
        _persistent = persistent;
        
        createHover(caption, text, width, height);
        positionHover(true);
        displayHover(true);
        
        return true;
    }

    this.hide = hide;
    function hide(delay)
    {
        if(!_windowLoaded)
        {
            return true;
        }
        
        clearDelay();
        
        if(delay == null) delay = _defaultHideDelay;
        
        if(delay > 0)
        {
            setDelay(function(){hide(0);}, delay);
            return true;
        }
        
        if(typeof(_div.innerHTML) != 'undefined') 
	    {
		    _div.innerHTML = '';
	    }             
	    
        displayHover(false);
        
        return true;
    }    
    
    this.positionHover = positionHover;
    function positionHover(canMove)
    {
        if(!_windowLoaded)
        {
            return true;
        }
        
        if(!_persistent || canMove)
        {
	        var widthFix = 0;
        	
	        if(_frame.innerWidth) widthFix = 18; 
	        
	        var windowWidth = getWindowWidth(); 
	        var horizontalScroll = getScrollLeft();
	        var x = getHorizontalPosition(windowWidth, horizontalScroll, widthFix);            
		    var innerHeight = getWindowHeight();	        
	        var verticalScroll = getScrollTop();
	        var y = getVerticalPosition(innerHeight, verticalScroll);

	        _div.style.left = x + 'px';
	        _div.style.top = y + 'px';
	    }
    }
    
    this.isVisible = isVisible;
    function isVisible()
    {
        return (_div.style.visibility == "visible");  
    }
    
    this.setDebugStatus = setDebugStatus;
    function setDebugStatus()
    {
        var status = "ScrollTop: " + getScrollTop() + ", ScrollLeft: " + getScrollLeft();
        status += " - MouseX: " + HoverManager.mouseX + ", MouseY: " + HoverManager.mouseY;
        status += " - Width: " + getWindowWidth() + ", Height: " + getWindowHeight();    
        status += ' - Hover Height: ' + _div.offsetHeight;
        window.status = status;
    }
    //End Public Functions

    
    
    //Private Functions    
    function createHover(caption, text, width, height)
    {
        var html = '<div style="border:1px solid #d0d0d0;width:' + width + 'px;' + (height > 0 ? 'height:' + height + 'px' : '') + '">';
        
        if(caption != null && caption.length > 0)
        {
            html += '<div class="GridItem" style="text-align:center;">';
            html += caption;
            html += '</div>';
        }
        
        html += '<div class="GridAltItem" style="text-align:' + _horizontalAlign + '">';
        html += text;
        html += '</div>';
        html += '</div>';
            
	    if(typeof(_div.innerHTML) != 'undefined') 
	    {
		    _div.innerHTML = '';
		    _div.innerHTML = html;
	    } 
    }

    function displayHover(display)
    {
        if(display)
        {
            _div.style.visibility = "visible";    
        }
        else
        {
            _div.style.visibility = "hidden";              
        }
    }

    function getWindowWidth() 
    {
	    var width;
    	
	    if(_windowWidth)
    	{
    	    width = _windowWidth;
    	}
    	else if(_frame.innerWidth) 
	    {   
	        width = _frame.innerWidth;
	    }
	    else if(_frame.document.documentElement && typeof(_frame.document.documentElement.clientWidth) == 'number') 
	    {
		    width = _frame.document.documentElement.clientWidth;
	    }
    	
	    return width;			
    }
    
    function getWindowHeight()
    {
        var innerHeight;
        
        if (_frame.innerHeight) 
        {
	        innerHeight = _frame.innerHeight;
        } 
        else if(_frame.document.documentElement && typeof(_frame.document.documentElement.clientHeight)=='number') 
        { 
	        innerHeight = _frame.document.documentElement.clientHeight; 
        }
        
        return innerHeight;
    }
    
    function getScrollTop()
    {
        return _frame.document.documentElement.scrollTop; 
    }
    
    function getScrollLeft()
    {
        return _browser.isIE4 ? _frame.document.documentElement.scrollLeft : _frame.pageXOffset; 
    }

    function getHorizontalPosition(browserWidth, horizontalScroll, widthFix) 
    {
	    var x;
	    var width = parseInt(_width);

        //Center the hover over
        x = HoverManager.mouseX + _offsetX - (width / 2);
        
        if(x < horizontalScroll)
        {
            //Make sure the left edge doesn't go off the left side of the screen
            x = horizontalScroll + _offsetY;
        }
        else if((x + width + horizontalScroll) > (browserWidth - _offsetY))
        {
            //Make sure the right edge doesn't go off the right side of the screen
            x = (browserWidth - width - _offsetY + horizontalScroll);
        }
	    
	    return x;
    }

    function getVerticalPosition(browserHeight, verticalScroll) 
    {
	    var y;
	    var height = _browser.isNetscape4 ? _div.clip.height : _div.offsetHeight;
	   
        //Put hover below the mouse
		y = HoverManager.mouseY + _offsetY + verticalScroll;
		
		/*
		if((y + height - verticalScroll) > (browserHeight - _offsetY))
		{
		    //Position it above the cursor if the bottom edge goes below the bottom of the screen
		    y = HoverManager.mouseY - height - _offsetY + verticalScroll;
		}
		*/
		
	    return y;
    }

    function setDelay(fx, timeout)
    {
        clearDelay();
	    _timerID = setTimeout(fx, timeout);
    }
    
    function clearDelay()
    {
        if(_timerID > 0)
        {
            clearTimeout(_timerID);
        }
        
        _timerID = -1;
    }
    
    function captureMouse()
    {
        if(!_mouseCaptured)
        {
            var fxCurrent = new FunctionInfo(document.onmousemove);
            var fxString = 'HoverManager.onMouseMove();' + fxCurrent.getBody();
    
            document.onmousemove = new Function(fxString);
            
            _mouseCaptured = true;
        }
    }
    
    function loadDefaults()
    {
        _defaultWidth = 200;
        _defaultHeight = -1;
        _defaultHorizontalAlign = "center";
        _defaultShowDelay = 250;
        _defaultHideDelay = 0;
        _defaultPersistent = false;
        
        _height = _defaultHeight;
        _width = _defaultWidth;
        _horizontalAlign = _defaultHorizontalAlign;
        _persistent = _defaultPersistent;
    }
    
    function onWindowLoad()
    {
        _div = document.getElementById(_divID);
        _frame = self;
        
        captureMouse();    
        loadDefaults();
        
        _windowLoaded = true;    
    }
}

HoverManager.mouseX = 0;
HoverManager.mouseY = 0;
HoverManager.instance = null;
HoverManager.create = function()
{
    var hover = HoverManager.instance;
    
    if(hover == null)
    {
        hover = new HoverManager();
        HoverManager.instance = hover;
    }
    
    return hover;
}
HoverManager.onMouseMove = function(e)
{
    var e = (e) ? e : event;

    if (e.pageX) 
    {
        HoverManager.mouseX = e.pageX;
        HoverManager.mouseY = e.pageY;
    }		
    else if (e.clientX) 
    {
        HoverManager.mouseX = e.clientX + document.body.scrollLeft;
        HoverManager.mouseY = e.clientY + document.body.scrollTop;
    } 
}  