﻿function showHover() {
    clearHideHoverTimeout();

    window._hoverOptions = window._hoverOptions ? window._hoverOptions : {};

    hideHover();

    window._hoverOptions = window._newHoverOptions;

    var $hover = $("#" + window._hoverOptions.hoverId).show();
    if ($hover.length) {
        //alert($("#" + window._hoverOptions.contentId).outerHTML());
        var $content = $("#" + window._hoverOptions.contentId);
        $("#hover_content_div").append($content.show());

        //$hover_content_div.css("height", $content.height() + "px");
        //$content.hide();

        $(document.body).addClass("ie6-hide-selects");
        var vTargetOffset = $(window._hoverOptions.hoverTarget).offset();

        //alert(vTargetOffset.top + "," + vTargetOffset.left);

        var $hoverArrow = $("#hover_arrow");
        var vWindowWidth = $(window).innerWidth();
        var vWindowHeight = $(window).innerHeight();
        var vScrollTop = $(window).scrollTop();
        var vScrollLeft = $(window).scrollLeft();

        var vWidth = $hover.width();
        var vHeight = $hover.height();

        var vTop = 0;
        var vLeft = 0;
        var vOffsetY = 0;
        var vOffsetX = 0;
        var vArrowTop = 0;
        var vArrowLeft = 0;
    
        var vClassName = $hoverArrow.attr("className");
        var display = window._hoverOptions.position.display;
        if (display.indexOf("top") == 0 || display.indexOf("bottom") == 0) {
            $hoverArrow.attr("className", "hover_arrow_bottom");
            //we need to do this to force the iepngfix to run for IE 6
            $hoverArrow.css("backgroundImage", "url('/Portals/1/Skins/Main/images/hover_arrow_bottom.png')");

            if (display.indexOf("top") == 0) {
                vTop = vTargetOffset.top + window._hoverOptions.hoverTarget.offsetHeight - vOffsetY + 10;
            }
            else {
                vTop = vTargetOffset.top - vHeight - $hoverArrow[0].offsetHeight;
            }

            var newDisplay = display.replace("top", "").replace("bottom", "");
            if (vHeight <= $hoverArrow[0].offsetWidth) {
                newDisplay = "middle";
            }
            switch (newDisplay) {
                case "left":
                    vLeft = vTargetOffset.left;
                    vArrowLeft = window._hoverOptions.position.x;
                    break;
                case "middle":
                    vLeft = vTargetOffset.left + (window._hoverOptions.hoverTarget.offsetWidth - vWidth) * 0.5;
                    vArrowLeft = vWidth * 0.5 - $hoverArrow[0].offsetWidth * 0.5;
                    break;
                case "right":
                    vLeft = (vTargetOffset.left + window._hoverOptions.hoverTarget.offsetWidth) + (window._hoverOptions.hoverTarget.offsetWidth - vWidth) * 0.5 - vOffsetX;
                    vArrowLeft = vWidth - window._hoverOptions.position.x;
                    break;
            }

            
            
            /*
            if (vArrowLeft > vWidth - $hoverArrow[0].offsetWidth * 0.5) {
                vArrowLeft = vWidth - $hoverArrow[0].offsetWidth * 0.5;
            }
            else if (vArrowLeft < 8) {
                vArrowLeft = 8;
            }

            if (vLeft + vWidth - vScrollLeft > vWindowWidth) {
                vLeft = vWindowWidth - vWidth + vScrollLeft;
            }
            if (vLeft < vScrollLeft) {
                vLeft = vScrollLeft;
            }
*/

            vArrowTop = vHeight - window._hoverOptions.position.y;

            //outside the top bounds of the window
            if (vTop - vScrollTop < 0 && vTargetOffset.top - vScrollTop < vHeight + $hoverArrow[0].offsetHeight) {
                var spaceTop = (vTargetOffset.top - vScrollTop);
                var spaceBottom = (vWindowHeight + vScrollTop) - (vTargetOffset.top + window._hoverOptions.hoverTarget.offsetHeight);
                if (spaceBottom > spaceTop) {
                    vTop = vTargetOffset.top + window._hoverOptions.hoverTarget.offsetHeight + window._hoverOptions.position.y + $hoverArrow[0].offsetHeight * 0.5;
                    vArrowTop = -$hoverArrow[0].offsetHeight + window._hoverOptions.position.y;
                }
            }
            if (vTop >= vTargetOffset.top) {
                $hoverArrow.attr("className", "hover_arrow_top");
                //we need to do this to force the iepngfix to run for IE 6
                $hoverArrow.css("backgroundImage", "url('/Portals/1/Skins/Main/images/hover_arrow_top.png')");
            }
        }
        else {
            $hoverArrow.attr("className", "hover_arrow_right");
            //we need to do this to force the iepngfix to run for IE 6
            $hoverArrow.css("backgroundImage", "url('/Portals/1/Skins/Main/images/hover_arrow_right.png')");

            vOffsetY = 12;
            vOffsetX = $hoverArrow[0].offsetWidth * 0.5;

            var newDisplay = display.replace("left", "").replace("right", "");
            if (vHeight <= $hoverArrow[0].offsetHeight + vOffsetY) {
                newDisplay = "middle";
            }
            switch (newDisplay) {
                case "top":
                    vTop = vTargetOffset.top - (vHeight * 0.5) + vOffsetY;
                    vArrowTop = vHeight * 0.5 - $hoverArrow[0].offsetHeight;
                    break;
                case "middle":
                    vTop = vTargetOffset.top - (vHeight - window._hoverOptions.hoverTarget.offsetHeight) * 0.5;
                    vArrowTop = (vHeight - $hoverArrow[0].offsetHeight) * 0.5;
                    break;
                case "bottom":
                    vTop = (vTargetOffset.top + window._hoverOptions.hoverTarget.offsetHeight) - vHeight + vOffsetY;
                    vArrowTop = vHeight - $hoverArrow[0].offsetHeight + window._hoverOptions.position.y;
                    break;
            }

            vLeft = vTargetOffset.left - vWidth - vOffsetX - 10;
/*
            if (vTop + vHeight - vScrollTop > vWindowHeight) {
                var vOldTop = vTop;
                vTop = vWindowHeight - vHeight + vScrollTop;
                vArrowTop += vOldTop - vTop;
            }
            if (vTop < vScrollTop) {
                var vOldTop = vTop;
                vTop = vScrollTop;
                vArrowTop += vOldTop - vTop;
            }

            if (vArrowTop > vHeight - $hoverArrow[0].offsetHeight - 16) {
                vArrowTop = vHeight - $hoverArrow[0].offsetHeight - 16;
            }
            else if (vArrowTop < 16) {
                vArrowTop = 16;
            }
*/
            vArrowLeft = vWidth - window._hoverOptions.position.x;

            var boundsLeft = vTargetOffset.left + window._hoverOptions.hoverTarget.offsetWidth + vOffsetX + 10;

            //outside the left bounds of window
            if (vLeft - vScrollLeft < 0 && boundsLeft + vWidth - vScrollLeft < vWindowWidth) {
                var spaceLeft = (vTargetOffset.left - vScrollLeft);
                var spaceRight = (vWindowWidth + vScrollLeft) - (vTargetOffset.left + window._hoverOptions.hoverTarget.offsetWidth);
                if (spaceRight > spaceLeft) {
                    vLeft = vTargetOffset.left + window._hoverOptions.hoverTarget.offsetWidth + vOffsetX + 10;
                    vArrowLeft = -$hoverArrow[0].offsetWidth + window._hoverOptions.position.x;
                }
            }
            /*
            if (vLeft + vWidth - vScrollLeft > vWindowWidth) {//outside the right bounds of window
                vLeft = vTargetOffset.left - vWidth - vOffsetX - 10;
                vArrowLeft = vWidth - window._hoverOptions.position.x;
            }
            */
            if (vLeft >= vTargetOffset.left) {
                $hoverArrow.attr("className", "hover_arrow_left");
                //we need to do this to force the iepngfix to run for IE 6
                $hoverArrow.css("backgroundImage", "url('/Portals/1/Skins/Main/images/hover_arrow_left.png')");
            }
        }

        var jHover = $(".hover");
        if (jHover.supersleight) {
            jHover.supersleight({ shim: "/Portals/1/Skins/Main/images/blank.gif" });
        }

        $hoverArrow.css({ top: vArrowTop + "px", left: vArrowLeft + "px" });
        $hover.css({ top: vTop + "px", left: vLeft + "px" });
    }
}

function setShowHoverTimeout(evt, options) {
    clearHideHoverTimeout();

    options = (typeof (options) == "object") ? options : {};
    if (options.contentId && options.hoverId) {
        evt = evt ? evt : window.event;
        options.hoverTarget = evt.currentTarget ? evt.currentTarget : evt.srcElement;
        options.timeout = (options.timeout != null) ? options.timeout : 125;

        //set up default positioning
        options.position = (typeof(options.position) == "object") ? options.position : {};
        options.position.display = (options.position.display ? options.position.display : "BottomLeft").toString().toLowerCase();
        options.position.x = options.position.x ? options.position.x : 2;
        options.position.y = options.position.y ? options.position.y : 2;
        
        window._newHoverOptions = options;
        if (options.timeout) {
            window._showHoverTimeoutId = setTimeout("showHover();", options.timeout);
        }
        else {
            showHover();
        }
    }
}

function clearShowHoverTimeout() {
    clearTimeout(window._showHoverTimeoutId);
}

function setHideHoverTimeout(options) {
    clearShowHoverTimeout();
    window._hoverOptions = window._hoverOptions ? window._hoverOptions : {};
    if (window._hoverOptions.timeout) {
        window._hideHoverTimeoutId = setTimeout("hideHover(true);", window._hoverOptions.timeout);
    }
    else {
        hideHover(true);
    }
}

function clearHideHoverTimeout() {
    clearTimeout(window._hideHoverTimeoutId);
}
function hideHover(hide) {
    window._hoverOptions = window._hoverOptions ? window._hoverOptions : {}
    if (hide || window._hoverOptions.hoverId && window._hoverOptions.hoverId != window._newHoverOptions.hoverId) {
        $("#" + window._hoverOptions.hoverId).hide();
    }
    if (hide || window._hoverOptions.contentId && window._hoverOptions.contentId != window._newHoverOptions.contentId) {
        $(document.body).append($("#" + window._hoverOptions.contentId).hide());
    }
    $(document.body).removeClass("ie6-hide-selects");
}

$.fn.outerHTML = function() {
    return $('<div>').append(this.eq(0).clone()).html();
};
