﻿/// <reference path="jQuery/jquery-1.3.2.min-vsdoc.js"/>
/// <reference path="jQuery/plugins/jQuery.Timer.js" />

var popularTimer;
var currentPopularPage = 1;
var TimerName = "PopularVideoTimer";

$(document).ready(function()
{
    styleTabsForIE6();
});

$(function()
{
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    function EndRequestHandler(sender, args)
    {
        styleTabsForIE6();
    }
});

function styleTabsForIE6()
{
    if (($.browser.msie && $.browser.version == 6) && $("#bmwTabs").length)
    {
        $("#bmwTabs li a[id *=\"Featured\"]").css("margin-left", "0px");
    }
}

function ClearPopularTimer()
{
    $(document).stopTime(TimerName);
    $(document).stopTime();
    popularTimer = null;
}

function ResetPopularVids()
{
    ClearPopularTimer();
    currentPopularPage = 1;
}

function StartPopularVidsCycle()
{
    ClearPopularTimer();
    if (null == popularTimer)
    {
        popularTimer = $(document).oneTime(29000, TimerName, NextPopularPage);
    }
}

function NextPopularPage()
{
    ClearPopularTimer();
    if (null == popularTimer)
    {
        currentPopularPage = currentPopularPage + 1;
        if (currentPopularPage > 5)
        {
            currentPopularPage = 1;
        }
        ShowPageGroup();
    }
}

function PreviousPopularPage()
{
    ClearPopularTimer();
    if (null == popularTimer)
    {
        currentPopularPage = currentPopularPage - 1;
        if (currentPopularPage < 1)
        {
            currentPopularPage = 5;
        }
        ShowPageGroup();
    }
}

function ShowPageGroup()
{
    var itemCount = $("#FeatureVidContainer").children("[id*='featureContainer']", this).size();

    var lastToShow = (currentPopularPage * 3) - 1;
    var firstToShow = lastToShow - 2;
    var secondToShow = firstToShow + 1;

    if (!$.browser.msie)
    {
        $.each($("#FeatureVidContainer").children("[id*='featureContainer']", this), function() { $(this).fadeTo(750, 0); });

        popularTimer = $(document).oneTime(750, TimerName, function()
        {
            $("#FeatureVidContainer").children("[id*='featureContainer']", this).css("display", "none");
            $("#FeatureVidContainer").children("[id*='featureContainer']", this).filter(":eq(" + firstToShow + "),:eq(" + secondToShow + "),:eq(" + lastToShow + ")").fadeTo(750, 1).css("display", "block");
            if (!$.browser.msie)
            {
                $("#FeatureVidContainer .videoContainer").fadeTo(750, 1);
            }
        });
    }
    else
    {
        $("#FeatureVidContainer").children("[id*='featureContainer']", this).css("float", "left")
        $.each($("#FeatureVidContainer").children("[id*='featureContainer']", this), function() { $(this).fadeTo(750, 0); });
        $("#FeatureVidContainer .videoContainer").fadeTo(750, 0);

        popularTimer = $(document).oneTime(750, TimerName, function()
        {
            $("#FeatureVidContainer").children("[id*='featureContainer']", this).css("display", "none");
            $("#FeatureVidContainer").children("[id*='featureContainer']", this).filter(":eq(" + firstToShow + "),:eq(" + secondToShow + "),:eq(" + lastToShow + ")").fadeTo(750, 1).css("display", "block");
            $("#FeatureVidContainer .videoContainer").fadeTo(750, 1);
        });
    }

    $(".popularVidsPage").html(currentPopularPage + " of " + (itemCount / 3));
    popularTimer = $(document).oneTime(6000, TimerName, NextPopularPage);
}

function PagePopularVideos(previousPage)
{
    if (previousPage)
    {
        PreviousPopularPage()
    }
    else
    {
        NextPopularPage();
    }
}

