$(function () { //document ready
    $('#topics ul li, #sub-nav ul li').mouseenter(function () {
        /* retrieve text from the span within the context of this, the element which the mouse entered */
        var toolTipContent = $('span', this).text();

        /*
        console.log(toolTipContent); //check retrieved data 
        console.log($('#tool-tip').html()); //check if the tool-tip div already exists
        */
        if ($('#tool-tip-container').html() == null) {//if the tool-tip div doesn't exist
            $('#tool-tip').text("");

            var tooltip = $('<div>', {
                "id": "tool-tip",
                "text": toolTipContent
            }),
				tooltipBottom = $('<div>', {
				    "id": "tool-tip-bottom"
				}).append(tooltip),
				tooltipContainer = $('<div>', {
				    "id": "tool-tip-container"
				}).append(tooltip, tooltipBottom);
            $('body').append(tooltipContainer);
        } else {
            $('#tool-tip').text(toolTipContent);
        }

        if (toolTipContent != "") {
            var $this = $(this);
            $('#tool-tip-container').show();
            var baseWidth = $this.width();
            var toolTipWidth = $("#tool-tip-container").width();
            var leftOffset = ($this.parents("#sub-nav").length > 0) ?
                (baseWidth / 2) - (toolTipWidth / 2) :
                (baseWidth - toolTipWidth - 15);
            $('#tool-tip-container').css({
                'left': $this.offset().left + leftOffset,
                'top': $this.offset().top + 35
            });
        }


    });

    $('#topics ul li, #sub-nav ul li').mouseleave(function () {
        $('#tool-tip-container').hide();

    });


    /**********************************************************/
    $("a.external, area.external").each(function () {
        $(this).click(function () {
            $("#external-interstitial-url").attr("href", $(this).attr("href"));
            $("#external-interstitial-url").attr("target", ($(this).attr("href").indexOf("javascript:") == 0 ? "_self" : "_blank"));
            $.modalDialog.show("#external-interstitial");
            return false;
        });
    });

    $("a[href='#isi']").each(function () {
        $(this).attr("href", "javascript:jQuery.scrollBodyTo('#isi', {easing:'easeOutCubic'});");
    });
});


var social = {
    shareFacebook: function (url) {
        var shareUrl = location.href;
        if (url != undefined && url != null)
            shareUrl = url;
        window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(shareUrl), 'facebook', 'toolbar=0,status=0,width=626,height=436');
    },
    shareTwitter: function (url, msg) {
        var shareUrl = location.href;
        if (url != undefined && url != null)
            shareUrl = url;
        if (msg == undefined || msg == null)
            window.open("http://twitter.com/home?status=" + encodeURIComponent(shareUrl), "twitter");
        else
            window.open("http://twitter.com/home?status=" + encodeURIComponent(msg), "twitter");
    },
    shareDelicious: function (url) {
        var shareUrl = location.href;
        if (url != undefined && url != null)
            shareUrl = url;
        window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=' + encodeURIComponent(shareUrl) + '&amp;title=' + encodeURIComponent(document.title), 'delicious', 'toolbar=no,width=550,height=550');
    },
    shareDigg: function (url) {
        var shareUrl = location.href;
        if (url != undefined && url != null)
            shareUrl = url;
        window.open("http://digg.com/submit?url=" + encodeURIComponent(shareUrl), "digg");
    }
};
