function commonDocumentReady()
{
    $("a.jqGlossaryTermLink").click(showGlossaryPopup);
    $("a.jqGlossaryTermLink").live("click",showGlossaryPopup);
    $("a.jqGlossaryPopupClose").click(hideGlossaryPopup);
    $("div.jqPostContent").find('img').attr({'title': 'Click to view larger image'});
    $("div.jqPostContent a:has(img)").click(showLargeImage);
    $("a.jqCloseLargeImageOverlay").live("click",closeLargeImage);
    $('.jqSearchWidget').focus(	function(){
                                    if($.trim($(this).val()) == 'Search Here') {
                                            $(this).val('');
                                    }
                                })
                        .blur(	function(){
                                if($.trim($(this).val()) == '') {
                                        $(this).val('Search Here');
                                }
                        });
}

$(document).ready(commonDocumentReady);

function showLargeImage()
{
    var current = $(this);
    var currentUrl = current.attr("href");

    if(currentUrl != "#" && currentUrl != "")
    {
        $.ajax({
            url: current.attr("href"),
            dataType: "html",
            /*beforeSend: function()
                        {
                            showAjaxLoader(current.closest($("div.post")));
                        },
            complete:   hideAjaxLoader(),*/
            success: function(data)
            {
                var imageOverlay = $(data);

                if(imageOverlay.is("div.jqLargeImageOverlay"))
                {
                    $("body").find("div.jqLargeImageHome").hide();

                    imageOverlay.hide();

                    $("body").find("div#content").append(imageOverlay);

                    //imageOverlay.fadeIn(400);

                    var largeImage = imageOverlay.children("img:first");

                    largeImage.attr({'title' : ''});

                    var largeImageWidth = parseInt(largeImage.attr("width"));
                    var largeImageHeight = parseInt(largeImage.attr("height"));

                    if(largeImageWidth == 0)
                    {
                        largeImageWidth = parseInt(largeImage.css("width"));
                    }

                    if(largeImageHeight == 0)
                    {
                        largeImageHeight = parseInt(largeImage.css("height"));
                    }

                    if(largeImageWidth > 0 && largeImageHeight > 0)
                    {
                        var allowedWidth    = $(window).width() - 50;
                        var allowedHeight    = $(window).height() - 110;

                        var allowedMaxWidth     = Math.max(200, (allowedWidth - (allowedWidth%200)));
                        var allowedMaxHeight    = allowedHeight;

                        var resizeRatio         = Math.min(allowedMaxWidth/largeImageWidth, allowedMaxHeight/largeImageHeight);

                        var newWidth       = Math.min(largeImageWidth, largeImageWidth * resizeRatio);
                        var newHeight       = Math.min(largeImageHeight, largeImageHeight * resizeRatio);

                        //largeImage.height(newHeight).width(newWidth);
                        largeImage.css({
                            width : newWidth,
                            height: newHeight
                        });

                        largeImage.data("Width", largeImageWidth);
                        largeImage.data("Height", largeImageHeight);
    //                                    imageOverlay.show();
                    }

                    imageOverlay.show();

                }
                else
                {

                }
            }
        });
    }

    return false;
}

function closeLargeImage()
{
    $(this).closest("div.jqLargeImageOverlay").fadeOut('fast',function(){$(this).remove();});
                
    $("body").find("div.jqLargeImageHome").fadeIn('fast');

    return false;
}

function showGlossaryPopup()
{
    var visibleGlossaryPopups   = $("div.jqGlossaryPopupContainer:visible");
                var visibleGalleryPopupsToHide  = visibleGlossaryPopups;

                var current                 = $(this);
                var documentScrollTop       = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
                // edited for testing.
                var windowBottom = documentScrollTop + $(window).height();
                visibleGlossaryPopups.each(function(){
                var current = $(this);
                var currentOffset = current.offset();

                if(currentOffset.top > windowBottom || (currentOffset.top + current.outerHeight()) < documentScrollTop)
                {
                    visibleGlossaryPopups = visibleGlossaryPopups.not(current);
                }
                });

                var existingGlossaryPopup   = visibleGlossaryPopups.filter("[name='" + current.attr("name") + "']");

                if(visibleGlossaryPopups.filter(":last").is("[name='" + current.attr("name") + "']"))
                {
                    return false;
                }

                if(existingGlossaryPopup.length > 0)
                {
//                    existingGlossaryPopup.fadeOut(1000);
//                    visibleGlossaryPopups   = visibleGlossaryPopups.not("[title='" + current.attr("title") + "']");
                    existingGlossaryPopup.appendTo("body");
                }
                else
                {
                var pageContainer           = current.closest(".post");
                var pageContainerOffset     = pageContainer.offset();

                var glossaryPopup           = $("#jqGlossaryPopupContainer").clone(true).removeAttr("id");

                var popupTop                = 0;
                var popupLeft               = 0;
                var postID                  = current.attr("id");

                //var visibleGlossaryPopups   = $("div.jqGlossaryPopupContainer:visible");

                /*glossaryPopup.appendTo("body:first")
                             .attr("title", current.attr("title"))
                             .find(".jqGlossaryPopupContent")
                             .html(current.attr("rel"))
                             .end()
                             .find(".jqGlossaryPopupTitle")
                             .html(current.attr("title"));*/

                /*if(visibleGlossaryPopups.length > 0)
                {
                    var lastVisibleGlossaryPopup = visibleGlossaryPopups.filter(":last");
                    var lastVisibleGlossaryPopupOffset  = lastVisibleGlossaryPopup.offset();
                    popupTop                = lastVisibleGlossaryPopupOffset.top + 40;
                    popupLeft               = lastVisibleGlossaryPopupOffset.left + 40;
                }
                else
                {
                    var pageVisibleAreaBottom   = Math.min($(window).height() + documentScrollTop, pageContainerOffset.top + pageContainer.outerHeight());
                    var pageVisibleAreaTop      = Math.max(pageContainerOffset.top, documentScrollTop);
                    var pageVisibleCenter       = Math.max(pageContainerOffset.top, documentScrollTop) + ((pageVisibleAreaBottom - pageVisibleAreaTop) - glossaryPopup.outerHeight()) /2;
                    popupTop                    = Math.max(pageVisibleCenter, documentScrollTop, pageContainerOffset.top);
                    popupLeft                   = pageContainerOffset.left + (pageContainer.outerWidth() - glossaryPopup.outerWidth())/2;
                }

                if(visibleGlossaryPopups.length > 1)
                {
                    visibleGlossaryPopups.filter(":not(:last)").fadeOut(1000);
                }*/

                /*glossaryPopup.css({
                        position: "absolute",
                        'z-index' : '10000',
                        top : popupTop,
                        left: popupLeft
                }).fadeIn(1000);*/

                $.ajax({
                            type:       'post',
                            dataType:   'json',
                            url:        glossaryPopupUrl,
                            data:       {id : postID, title : current.attr("name")},
                            success:    function(reply)
                                        {
                                            if(reply)
                                            {
                                                /*var postContent = $(unescape(reply.success)).hide();
                                                thumbnailLI.append(postContent);
                                                BigMan3D.showPost(thumbnailLI, thumbnailContainer, postContent);*/
                                                glossaryPopup.appendTo("body:first")
                                                     .attr("name", current.attr("name"))
                                                     .find(".jqGlossaryPopupContent")
                                                     .html($(unescape(reply.success)))
                                                     .end()
                                                     .find(".jqGlossaryPopupTitle")
                                                     .html(current.attr("name"));






                                                    if(visibleGlossaryPopups.length == 1)
                                                    {
                                                        var lastVisibleGlossaryPopup = visibleGlossaryPopups.filter(":last");
                                                        var lastVisibleGlossaryPopupOffset  = lastVisibleGlossaryPopup.offset();
                                                        popupTop                = lastVisibleGlossaryPopupOffset.top + 40;
                                                        popupLeft               = lastVisibleGlossaryPopupOffset.left + 40;
                                                    }
                                                    else if(visibleGlossaryPopups.length > 1)
                                                    {
                                                        var firstVisibleGlossaryPopup = visibleGlossaryPopups.filter(":first");
                                                        var firstVisibleGlossaryPopupOffset  = firstVisibleGlossaryPopup.offset();
                                                        popupTop                = firstVisibleGlossaryPopupOffset.top;
                                                        popupLeft               = firstVisibleGlossaryPopupOffset.left;
                                                    }
                                                    else
                                                    {
                                                        var pageVisibleAreaBottom   = Math.min($(window).height() + documentScrollTop, pageContainerOffset.top + pageContainer.outerHeight());
                                                        var pageVisibleAreaTop      = Math.max(pageContainerOffset.top, documentScrollTop);
                                                        var pageVisibleCenter       = Math.max(pageContainerOffset.top, documentScrollTop) + ((pageVisibleAreaBottom - pageVisibleAreaTop) - glossaryPopup.outerHeight()) /2;
                                                        popupTop                    = Math.max(pageVisibleCenter, documentScrollTop, pageContainerOffset.top);
                                                        popupLeft                   = pageContainerOffset.left + (pageContainer.outerWidth() - glossaryPopup.outerWidth())/2;
                                                    }

                                                    if(visibleGalleryPopupsToHide.length > 1)
                                                    {
                                                        visibleGalleryPopupsToHide.filter(":not(:last)").hide('fast');

                                                        /*visibleGlossaryPopups.filter(":last").fadeOut(1000);
                                                        var pageVisibleAreaBottom   = Math.min($(window).height() + documentScrollTop, pageContainerOffset.top + pageContainer.outerHeight());
                                                        var pageVisibleAreaTop      = Math.max(pageContainerOffset.top, documentScrollTop);
                                                        var pageVisibleCenter       = Math.max(pageContainerOffset.top, documentScrollTop) + ((pageVisibleAreaBottom - pageVisibleAreaTop) - glossaryPopup.outerHeight()) /2;
                                                        popupTop                    = Math.max(pageVisibleCenter, documentScrollTop, pageContainerOffset.top);
                                                        popupLeft                   = pageContainerOffset.left + (pageContainer.outerWidth() - glossaryPopup.outerWidth())/2;*/
                                                    }
                                                    glossaryPopup.css({
                                                            position: "absolute",
                                                            'z-index' : '10000',
                                                            top : popupTop,
                                                            left: popupLeft
                                                    }).show('fast');
                                              }
                                        }
                    });
                }
                return false;

}
function hideGlossaryPopup()
{
    $(this).closest("div.jqGlossaryPopupContainer").hide('fast', function(){ $(this).remove(); });
    return false;
    /*var glossaryPopup           = $("#jqGlossaryPopupContainer");
    glossaryPopup.fadeOut("slow");*/
}
