function StartGlossary($) {
    //convert terms to links
    for (value in GlossaryItems) {
        //alert(GlossaryItems[value]);
        var find = GlossaryItems[value];
        var rp = new RegExp("([\\s])(" + find + ")(?![\\d\\w])", "gi");
        $("#pageContent :not(a), #pageContentHome :not(a)").replaceText(
            rp,
            '$1<a class="glossary-item-link" href="#$2">$2<\/a>');
    }

    var $dialog = $('<div></div>')
    		    .html('Vitamin D Glossary')
    		    .dialog({
    		        autoOpen: false,
    		        title: 'Glossary term:',
    		        minHeight: 10
    		    });

    //$
    $("a.glossary-search").click(function() {

        var theHref = $(this).attr("href");
        var theSearch = $("input#searchterms").val();
        $(this).attr("href", theHref + "s=" + escape(theSearch));

    });

    $("#everything").live("click", function() {
        $dialog.dialog("close");
    });

    $("a.glossary-item-link-internal").live("click", function(e) {
        return ShowGlossaryTermPopup($(this).text(), $(this).attr("href"), -1, -1, e, $dialog);
    });

    $("a.glossary-item-link").live("click", function(e) {
        return ShowGlossaryTermPopup($(this).text(), $(this).attr("href"), 0, 0, e, $dialog);
    });

}

function ShowGlossaryTermPopup(theText, theHref, posX, posY, e, $dialog) {
    var i = theHref.indexOf("#");
    if (i >= 0) {
        var Term = theHref.substring(i + 1);
        $dialog.load("/mod_cms/glossary/get_item.aspx?term=" + escape(Term));
        $dialog.dialog("option", "title", "Glossary term: " + theText);
        if (posX >= 0 && posY >= 0) {
            $dialog.dialog("option", "position", {
                my: "left top",
                at: "right bottom",
                of: e,
                offset: "15 15"
            });
        }
        $dialog.dialog('open');
        return false;
    }
    else {
        return true;
    }
}

