﻿$(function () {
    AddWordDefLogic();
    AddHideShowLogic();
    fixRequired();
});

function fixRequired() {
    $('.required > span.r').remove()
    $('.required').prepend('<span class="r">* </span>');
    setTimeout('fixRequired();', 1000);
}

function AddWordDefLogic() {
    $('.pageInfo a.define').mouseenter(function (e) {
        var x = e.pageX;
        var y = e.pageY;
        var def = $(this).attr('title');
        if (def.length == 0) {
            // see if you can get it from the href
            def = $(this).attr('href');
            // Clear the actual href
            $(this).attr('href', '');
        }
        if (def.length > 0) {
            $.ajax({
                url: "/GetDefinition.aspx?word=" + def,
                success: function (data) {
                    PostDefinition(data, x, y);
                }
            });
        }
    });
    $('.pageInfo a.define').mouseout(function () { $('.tooltip').remove(); });
}

function PostDefinition(word,x,y) {
    $('.pageInfo').append('<div class="tooltip">' + word + '</div>');
    $('.tooltip').css('position', 'absolute').css('top', (y/2) + 'px').css('left', (x/2) + 'px').show();
}

function AddHideShowLogic() {
    $('.expandLink').click(function () {
        var idLink = $(this).attr('id');
        var bodyLink = '#' + idLink.replace('el', 'eb');
        var displayState = $(bodyLink).css('display').toLowerCase();
        $('.expandBody').hide();
        if (displayState == 'none') { $(bodyLink).show(); $(document).scrollTop($(this).pageY); }
        else { $(bodyLink).hide(); }
    });
}

