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();
		    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() {
		       hideAZTimeout[id] = window.setTimeout('$j("#' + id + '").find("ul[class!=index]:visible").hide().end().find("a.active").removeClass("active")', 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 = $(block).find("ul#p-" + letter);
		        if (!popout.length) return false;
		        $(this).addClass("active");
		        popout.css({
		            left: $(this).position().left + $(this).outerWidth(),
		            top: $(this).position().top
		        }).show();
		        return false;
		    });
	    });   
	};
	function HidePopout() {
        $(this).hide();
	    indexLinks.filter(".active").removeClass("active");    
    }
})(jQuery);
