var hideAZTimeout = {};

(function($) {
    $.fn.healthAZpopout = function(opts) {
        $.fn.healthAZpopout.defaults = {};
        var opts = $.extend({}, $.fn.healthAZpopout.defaults, opts);

        return this.each(function(idx) {
            var id = $(this).attr("id");
            if (!id) id = $(this).attr("id", "popout-123").attr("id");
            $(this).show();
            var popouts = $(this).find("ul[class!=index]").hide().appendTo($("body"));
            var block = this;
            var indexLinks = $(this).find("ul.index :not(li:last) a");
            $(this).click(function() {
                window.clearTimeout(hideAZTimeout[id]);
                indexLinks.removeClass("active");
                popouts.filter(":visible").hide();
            });
            $(popouts).mouseleave(function() {
                var popout = $(this);
                //hideAZTimeout[id] = window.setTimeout('$j("#' + id + '").find("ul[class!=index]:visible").hide().end().find("a.active").removeClass("active")', 500);
                hideAZTimeout[id] = window.setTimeout(function() {
                    $(popout).hide();
                    $(block).find("a.active").removeClass("active");
                    //$("#' + id + '").find("ul[class!=index]:visible").hide().end().find("a.active").removeClass("active")', 500);
                }, 500);
            });
            $(this).find("ul.index :not(li:last) a").click(function() {
                window.clearTimeout(hideAZTimeout[id]);
                $(popouts).filter(":visible").hide();
                indexLinks.filter(".active").removeClass("active");
                var href = $(this).attr("href");
                href = href.split("#");
                if (href.length < 2) return false;
                var letter = href[href.length - 1];
                var popout = $("#p-" + letter);
                if (!popout.length) return false;
                $(this).addClass("active");
                popout.css("height", null);
                popout.css({
                    left: -9999,
                    top: -9999
                }).show();
                var wH = $(window).height();
                var pH = popout.outerHeight();
                var sT = $(window).scrollTop() + 10;
                var tT = $(this).offset().top;
                var tH = $(this).outerHeight(true);
                var hT = wH - 20;

                if (pH > hT) {
                    pH = hT - 10;
                    popout.css("height", pH);
                }

                var tOffset = tT + tH - pH;
                if (tOffset < sT) {
                    //alert("will get cut off on top");
                    tOffset = tT;
                    if (tOffset + pH > wH) {
                        //alert("will get cut off on bottom");
                        tOffset = sT;
                    }
                }
                popout.css({
                    "left": $(this).offset().left + $(this).outerWidth(),
                    "top": tOffset
                });
                return false;
            });
        });
    };
    function HidePopout() {
        $(this).hide();
        indexLinks.filter(".active").removeClass("active");
    }
})(jQuery);

