(function($){

    var activeGroupId = '';
    var activeGroupName = '';

    var activeDefinitionId = '';
    var activeDefinitionName = '';

    var activeTerm;

    $(function() {
        $("#overlay #overlay-content").addClass("glossary-overlay");
        $("#overlay #overlay-content").html('<div id="overlay-definition"></div><div style="float:left;margin:10px 0 0 0"><a href="#" id="printDefinitionButton"><img src="/common/images/btn-printDefinition.gif" width="97" height="19" alt="Print this Definition" border="0" /></a><a href="#" onclick="window.open(\'/tips-and-know-how/glossary?print=true\', \'print\', \'width=670,scrollbars=yes\');return false"><img src="/common/images/btn-printAll.gif" width="62" height="19" alt="Print All Definitions" border="0" hspace="5"/></a></div><img width="48" height="11" border="0" alt="Close Definition" class="glossary-close" src="/common/images/btn-glossary-close.png"/>');

        $("img.glossary-close").click(function() { $("#overlay").hide() });

        $("#glossary-anchors a").click(
			function() {
                $("#overlay").hide();
                activeGroupId = $(this).attr("id");
                activeGroupName = activeGroupId.substring(activeGroupId.indexOf("-") + 1, activeGroupId.length);
                if(activeGroupName == 'viewAll') {
                    $("div.glossary-group-inner:not(div.glossary-group-inner:first)").addClass("glossary-group-border");
                    $("h3#glossarySectionTitle").text("View All");
                    $("div.glossary-group").show();
				} else {
					$("div.glossary-group").hide();
                    $("div.glossary-group-inner").removeClass("glossary-group-border");
                    if (activeGroupName != 'numeric') {
                        $("h3#glossarySectionTitle").text(activeGroupName);
                    } else {
                        $("h3#glossarySectionTitle").text('#');
                    }
                    $("#glossary-" + activeGroupName).show();
                }
			}
		);

		$("div.glossary-group a").click(
			function() {
				activeDefinitionId = $(this).attr("id");
                activeDefinitionName = activeDefinitionId.substring(activeDefinitionId.indexOf("-") + 1, activeDefinitionId.length);
				$("#overlay #overlay-content #overlay-definition").html($("#glossaryDefinition-" + activeDefinitionName).html());
                activeTerm = encodeURIComponent($("#overlay-definition span").text());
                $("#overlay-content #printDefinitionButton").click(function() {
                    popPrintWindowForSingleDefinition();
                    return false;
                });

                var position = $(this).position();
                if ($("#overlay").height() + position.top > $(window).height() + $(window).scrollTop() - 40) {
                    $("#overlay").css("top", (position.top - $("#overlay").height()) + "px");
                } else {
                    $("#overlay").css("top", (position.top + 15) + "px");
                }
                $("#overlay").css("left", (position.left + 15) + "px");
                $("#overlay").show();
                return false;
            }
		);
		
    });

    function popPrintWindowForSingleDefinition() {
        window.open('/tips-and-know-how/glossary?print=true&term=' + activeTerm, 'print', 'width=670,scrollbars=yes');
    }
})(jQuery);

