﻿
//Requires JQuery to be loaded first
/// <reference path="../../Script/jquery-1.2.6.min.js" />

var timerID = null;
var tabbedContentindex = 0;
var videoIndex = 0;

// Data
var tabText = new Array(4);
var pageContent = new Array(4);
var videos = new Array();

$(document).ready(function() {


    //Load Tabbed Content Data
    if (tabText.length == 4 && pageContent.length == 4) {
        for (var i = 0; i < 4; i++) {
            //Load Text into tabs
            $('#tblTabStrip .T' + (i + 1).toString() + ' div').html(tabText[i]);

            //Set Tab Click Events
            $('#tblTabStrip .T' + (i + 1).toString()).click(function() {
                //determine the index of this tab
                var index = this.className.replace('T', '') - 1;
                ShowPageByIndex(index);
            });
        }
    }

    //Hide all but the first tab.
    ShowPageByIndex(0);

    playTabs();

    //Show First Video
    ShowVideoByIndex(0);

    //--------------------------
    //-- Events
    //--------------------------

    //Set Page Area Click Event
    $('#divTabbedContent').click(pauseTabs);

    //VIDEO LINK PRECONDITION: There is at least 1 video to display

    //Set Prev / Next Link Events
    $('.PreviousLink').click(function() {
        //determine the previous index
        var prevIndex = 0;
        if (videoIndex == 0) {
            //go to last video
            prevIndex = videos.length - 1;
        }
        else {
            prevIndex = videoIndex - 1;
        }
        ShowVideoByIndex(prevIndex);
    });
    //
    $('.NextLink').click(function() {
        //determine the next index
        var nextIndex = 0;
        if (videoIndex == videos.length - 1) {
            //go to first video
            nextIndex = 0;
        }
        else {
            nextIndex = videoIndex + 1;
        }
        ShowVideoByIndex(nextIndex);
    });
});

function ShowPageByIndex(index) {
    $('#divTabbedContent .divMultiPageBox').html(pageContent[index]);
}

function ShowVideoByIndex(index) {
    var externalId = videos[index][1];
    $('#sVideoTitle').text(videos[index][0]);
    var playerMarkup = "<object width='239' height='208'><param name='movie' value='http://www.youtube.com/v/EXTERNAL_ID&hl=en&rel=0'></param><embed src='http://www.youtube.com/v/EXTERNAL_ID&hl=en&rel=0' type='application/x-shockwave-flash' width='239' height='208'> </embed></object>";
    $('#divPlayer').html(playerMarkup.replace(/EXTERNAL_ID/g, externalId));
    videoIndex = index;
}

function rotateTabs() {
    ShowPageByIndex(tabbedContentindex);

    if (tabbedContentindex == 3) {
        tabbedContentindex = 0;
    }
    else {
        tabbedContentindex++;
    }
    
    timerID = setTimeout('rotateTabs()', 10000);
}

function pauseTabs() {
    if (timerID != null) {
        clearTimeout(timerID);
        timerID = null;
    }
}

function playTabs() {
    if (timerID == null) {
        timerID = setTimeout('rotateTabs()', 100);
    }
}


