$(document).ready(function() {
    //$("#dialog").dialog();


    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).attr("href"), -1, -1);
    });

    $("a.glossary-item-link").live("click", function(e) {
        return ShowGlossaryTermPopup($(this).attr("href"), e.pageX, e.pageY);
    });

    function ShowGlossaryTermPopup(theHref, posX, posY) {
        var i = theHref.indexOf("#");
        if (i >= 0) {
            var Term = theHref.substring(i + 1);
            $dialog.load("/mod_cms/glossary/get_item.aspx?term=" + Term);
            $dialog.dialog("option", "title", "Glossary term: " + Term);
            if (posX >= 0 && posY >= 0) {
                $dialog.dialog("option", "position", [posX, posY]);
            }
            $dialog.dialog('open');
            return false;
        }
        else {
            return true;
        }
    }
});


