﻿$(document).ready(function ()
{
    var href = document.location.href.toLowerCase();

    // The querystring category will be set if the CMS tool is being used, to show the correct tab
    var queryStringCategory;

    if (href.indexOf("?") > 0)
    {
        // try and get the category before you cut the href
        queryStringCategory = href.substring(href.indexOf("?") + 1);
        href = href.substring(0, href.indexOf("?"));
    }

    href += (href.lastIndexOf("/") == href.length - 1 ? "default.aspx" : "");
    if (href.indexOf("/connect.aspx") > 0 || href.indexOf("/connectdetail.aspx") > 0)
    {
        $(".top_bar_link").attr("class", "top_bar_link");
        $(".top_bar_link a.Connect").parent().addClass("selected");
    }

    if (queryStringCategory == null)
    {

        // highlight selected page
        $(".top_nav_container a").each(function ()
        {
            var link = $(this).attr("href").toLowerCase();
            if (href.indexOf(link) > -1)
            {
                $(this).addClass("selected");
            }
        });
    }

    // highlight selected page
    href = document.location.href.toLowerCase();
    $(".portal_nav .nav").each(function ()
    {
        if ($("a", this).length > 0)
        {
            var link = "/" + $("a", this).attr("href").toLowerCase();
            if (href.indexOf(link) > -1)
            {
                bsPortal.selectedPage = this.id;
                $(this).addClass("selected");
            }
        }
    });

    // site search
    $(".bs_search_box").keypress(function (e)
    {
        if (e.keyCode == 13) { bs.siteSearch(); return false; }
    });

    // home page video nav
    $(".vid_nav .prev").click(function () { bs.prevNextVid(-1); bs.cancelRotation(); });
    $(".vid_nav .next").click(function () { bs.prevNextVid(1); bs.cancelRotation(); });
    $(".vid_nav .dot").click(bs.dotVid);

    // show the first you tube video
    $(".feature .panel:first").fadeIn();

    bs.rotateVideo();

    //    $("ul .wpc_video_tab").live("click", function ()
    //    {
    //        var i = $(this).closest("li").index();
    //        i = (i == 0 ? 1 : 0);
    //        $("ul.wpc_videos li").hide();
    //        $("ul.wpc_videos li:eq(" + i + ")").show();
    //    });


});

bs =
{
    videoTimeout: null,

    siteSearch: function ()
    {
        var keyword = $(".bs_search_box").val();
        if (keyword.length > 0)
        {
            //document.location = "http://www.bizspark.com/search/results.aspx?k=" + keyword;
            document.location = "http://search.microsoft.com/Results.aspx?S100=on&OtherSite=www.microsoft.com%2Fbizspark%2F&q=" + keyword;
        }
        return false;
    },
    showVid: function (i)
    {
        var vid = $(".feature .panel:visible");
        if (vid.length > 0)
        {
            vid.hide();
        }
        var panel = $(".feature .panel:eq(" + i + ")");
        if (panel.length > 0)
        {
            panel.fadeIn();
            var newVid = $(".vid", panel).html();
            $(".videos .video").html(newVid);
            $(".vid_nav .dot").removeClass("dot_on");
            $(".vid_nav .dot:eq(" + i + ")").addClass("dot_on");
        }
    },
    prevNextVid: function (inc)
    {
        var vid = $(".feature .panel:visible");
        var max = $(".feature .panel").length;
        var i = vid.index();
        i += inc;
        if (i < 0)
        {
            i = max - 1;
        }
        else if (i >= max)
        {
            i = 0;
        }
        bs.showVid(i);
    },
    dotVid: function ()
    {
        var i = $(this).index() - 1;
        bs.showVid(i);
        bs.cancelRotation();
    },
    rotateVideo: function ()
    {
        bs.videoTimeout = setTimeout(function ()
        {
            bs.prevNextVid(+1);
            if ($(".feature .panel:visible").index() > 0)
            {
                bs.rotateVideo();
            }
        }, 10000);
    },
    cancelRotation: function ()
    {
        if (bs.videoTimeout != null)
        {
            clearTimeout(bs.videoTimeout);
            bs.videoTimeout = null;
        }
    },

    // Pass in items to track in an array
    // item.WTQueryParameter
    // item.Value
    webTrendsTracking: function (itemsToTrack)
    {

        try
        {
            dcsMultiTrack(arguments);
        } catch (err) { }

    },

	showWaiting: function () {
        $.blockUI(
         {
             message: "Please wait..." 
             ,css: { padding: '25px' } 
         });
    },

    hideWaiting: function () {
        $.unblockUI();
    }

}

