//Smart Hover Box for Mootools 1.2 v0.11
//dedicated to the public domain
//by www.consideropen.com/blog

var smartHoverBox = function(openboxTime, closeboxTime, xOffset, yOffset, smartBoxSuffix, smartBoxClose, offsetbyrightedge) {
    var smartBoxes = $(document.body).getElements('[id$=' + smartBoxSuffix + ']');

    var closeElem = $(document.body).getElements('.' + smartBoxClose);

    var closeBoxes = function() { smartBoxes.setStyle('display', 'none'); };

// The function below was added by Tim so that the function could be called with a delay.

    var openBoxes = function(item, currentBox) {
        smartBoxes.setStyle('display', 'none');
        item.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000');

        //coordinates and size vars and math
        var windowSize = $(window).getSize();
        var windowCoords = $(window).getCoordinates();
        var windowScroll = $(window).getScroll();
        var halfWindowY = windowSize.y / 2;
        var halfWindowX = windowSize.x / 2;
        var boxSize = item.getSize();
        var inputPOS = $(currentBox).getCoordinates();
        var inputCOOR = $(currentBox).getPosition();
        var inputSize = $(currentBox).getSize();
        var inputBottomPOS = inputPOS.top; // Doug This line modified by Tim for title image.
        var inputBottomPOSAdjust = inputBottomPOS - windowScroll.y
        var inputlength = 0;
        var inputLeftPOS = inputPOS.left + xOffset;
        var inputRightPOS = inputPOS.right;
        var leftOffset = inputCOOR.x + xOffset;
        var topAdjust = "";
        var bottomAdjust = "";
        if (offsetbyrightedge == 1)
        {
            inputlength = inputPOS.width;
            if (inputlength > 460)
            {
                inputlength = 460;
            }
        }

        if(halfWindowY < inputBottomPOSAdjust)
        {
            // topAdjust = the top of the hover.
            // windowSize = the size of the window.
            topAdjust = inputPOS.top - boxSize.y - yOffset+20;
            if ((topAdjust - windowScroll.y) < 0)
            {
                item.setStyle('top', 0 + windowScroll.y);
            }
            else
            {
                item.setStyle('top', inputPOS.top - boxSize.y - yOffset+20);
                //					item.setStyle('top', windowCoordas.top);
                //	    item.setStyle('top', inputPOS.top - boxSize.y -10);
            }

            if (inputLeftPOS < halfWindowX)
            {
                item.setStyle('left', leftOffset + inputlength);
            }
            else
            {
                item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset);
            }
        }
        // This is when the hover is above the half line.
        else
        {
            bottomAdjust = inputBottomPOS + yOffset + boxSize.y;
            if ((bottomAdjust - windowScroll.y) > windowSize.y)
            {
                item.setStyle('top', windowSize.y - boxSize.y + windowScroll.y);
            }
            else
            {
                item.setStyle('top', inputBottomPOS + yOffset);
            }
            if (inputLeftPOS < halfWindowX)
            {
                rightAdjust = leftOffset+inputlength;
                if ((rightAdjust - windowScroll.x + boxSize.x) > windowSize.x)
                {
                    item.setStyle('left', windowSize.x - boxSize.x);
                }
                else
                {
                    item.setStyle('left', leftOffset + inputlength);
                }
            }
            else
            {
                item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset);
            }
        }
    };

    //closeBoxes();

    closeElem.addEvent('click', function(){ closeBoxes() }).setStyle('cursor', 'pointer');
    var closeBoxesTimer = 0;
    var openBoxesTimer = 0;

    smartBoxes.each(function(item){
        var currentBox = item.getProperty('id');
        currentBox = currentBox.replace('' + smartBoxSuffix + '', '');

        // if the mouse leaves call the close boxes function with a delay.
        $(currentBox).addEvent('mouseleave', function(){
            closeBoxesTimer = closeBoxes.delay(closeboxTime);
            if($defined(openBoxesTimer))
            {
                $clear(openBoxesTimer);
            }
        });

        item.addEvent('mouseleave', function(){
            closeBoxesTimer = closeBoxes.delay(closeboxTime);
            if($defined(openBoxesTimer))
            {
                $clear(openBoxesTimer);
            }
        });

        $(currentBox).addEvent('mouseenter', function(){
            if($defined(closeBoxesTimer))
            {
                $clear(closeBoxesTimer);
            }
        });
        item.addEvent('mouseenter', function(){
            if($defined(closeBoxesTimer))
            {
                $clear(closeBoxesTimer);
            }
        });

        item.setStyle('margin', '0');
        $(currentBox).addEvent('mouseenter', function(){
            openBoxesTimer = openBoxes.delay(openboxTime, null, [item,document.id(currentBox)]);  // This line added by Tim for delay purposes.  Previous code was tucked into the function being called.
        }).setStyle('cursor', 'pointer');
    });
}; 

