﻿$().ready(function () 
{
    baba.masterInit();
    baba.init();    
});

baba =
{
    menu: "",

    masterInit: function ()
    {
        // scenario flyout
        $(".scenario_menu .btn").click(function ()
        {
            $(".scenario_menu .btn").removeClass("selected");
            var flyout = $(this).next(".flyout");
            if (!flyout.is(":visible"))
            {
                $(".flyout").hide();
                flyout.slideDown();
                $(this).addClass("selected");
            }
            else
            {
                flyout.slideUp();
            }
        });

        $(".scenario_menu .menu_item").hover(
            function ()
            {
                if (baba.menu != null && baba.menu != "")
                {
                    window.clearTimeout(baba.menu);
                }
            },
            function ()
            {
                if ($(".flyout:visible").length > 0)
                {
                    baba.menu = window.setTimeout(baba.slowMenu, 500);
                }
            }
        );

        // menu links - navigate to the learning page
        $(".scenario_menu .flyout li").click(function ()
        {
            var id = $(".CampaignId", this).text();
            document.location = "Learning.aspx?campaignid=" + id;
        });

        // search
        $(".search_btn").click(baba.siteSearch);

        // site search
        $(".baba_search_box").keypress(function (e)
        {
            if (e.keyCode == 13) { baba.siteSearch(); return false; }
        });

    },

    init: function ()
    {
        // More / less buttons
        $(".more_less a.more").click(function (e)
        {
            e.preventDefault();
            var p = $(this).closest(".Desc");
            $(".ellipsis", p).hide();
            $(".more", p).hide();
            $(".more_expand", p).fadeIn();
            $(".less", p).fadeIn();
        });
        $(".more_less a.less").click(function (e)
        {
            e.preventDefault();
            var p = $(this).closest(".Desc");
            $(".less", p).hide();
            $(".more_expand", p).fadeOut();
            $(".ellipsis", p).fadeIn();
            $(".more", p).fadeIn();
        });


        // learning
        $(".transcript_div a").click(function (e)
        {
            e.preventDefault();
            // replace the transcript with the content from the video item
            var transcriptContent = $(this).parent().find(".trans_content").html();
            $(".lightbox > .transcript_body").html(transcriptContent);

            $(".lightbox").show();
            $(".shadow").show();
        });

        $(".lightbox .close a").click(function (e)
        {
            e.preventDefault();
            $(".lightbox").hide();
            $(".shadow").hide();
        });

        // Expando
        // this takes a paragraph, and if its length is greater than the expansionCharacterCount
        // then it breaks it and adds the expand or collapse buttons.
        $(".Expando").each(function ()
        {
            var expansionCharacterCount = 150;

            // Check if the object specifies the amount of characters to expand on
            var expandoAttribute = $(this).attr("ExpandoCharacters");
            if (expandoAttribute)
            {
                expansionCharacterCount = expandoAttribute;
            } //if (expandoAttribute) {

            var text = $(this).html();
            if (text.length > expansionCharacterCount)
            {
                var partA = text.substring(0, expansionCharacterCount);
                var partB = text.substring(expansionCharacterCount);
                var combined = partA + "<span class=\"ellipsis\">...</span><span class=\"more_expand\">" + partB + "</span>";
                $(this).html(combined);
                var parent = $(this).parent();
                $(".more_less>a.more", parent).show();
            }
        });

        //Setup twitter links
        $(".TwitterCount")
        .each(function ()
        {
            baba.getTwitterCount(this);
        }
        );

        $(".FacebookCount")
        .each(function ()
        {
            baba.getFacebookCount(this);
        }
        );
    },

    siteSearch: function ()
    {
        var keyword = $(".baba_search_box").val();
        if (keyword.length > 0)
        {
            document.location = "http://search.microsoft.com/Results.aspx?S100=on&OtherSite=www.microsoft.com%2Fvisualstudio%2Fbuildabetterapp%2F&q=" + keyword;
        }
        return false;
    },

    // Pass in items to track in an array
    // item.WTQueryParameter
    // item.Value
    webTrendsTracking: function (itemsToTrack)
    {

        try
        {
            dcsMultiTrack(arguments);
        } catch (err) { }

        //console.log(arguments);

    }, // trackTest


    getTwitterCount: function (obj)
    {
        var url = $(obj).attr("url");
        $.getJSON('http://api.tweetmeme.com/url_info.jsonc?url=' + url + '&callback=?'
        , function (data)
        {
            if (data.status != "failure")
            {
                $(obj).html(data.story.url_count);
            } //if
        })
    },

    getFacebookCount: function (obj)
    {
        var url = $(obj).attr("url");
        $.getJSON("http://graph.facebook.com/" + url + "&callback=?"
         , function (data)
         {
             if (data.shares != NaN)
             {
                 $(obj).html(data.shares);
             }
         })
    },

    slowMenu: function ()
    {
        $(".flyout").slideUp();
        $(".scenario_menu .btn").removeClass("selected");
    }

};



