<!--******************** AJAX Code ******************-->
var channel = "";
var sub_channel = "";
var index = 1;

function pageFormat() {
    updatePagination();
    $('.play-hover').hide();

    $('.img').hover(function() {
        $(this).siblings('.play-hover').show();
    }, function() {
        $(this).siblings('.play-hover').hide();
    });

    $('.play-hover').hover(function() {
        $(this).show();
    }, function() {
        $(this).hide();
    });
     $('.title').truncate(40, 2);

}

function initVideoListeners(vgnextoid, locale, tabId, subsection, sourceId, i) {
    $('.tab-ajax').click(function() {
        setSelected($(this).parent());
        channel = $(this).attr('id');
        sub_channel = "";
        $('#pag-curr').text(1);
        $('#main-video').html('<div class="loading-text">Loading...</div>');
        $('#secondary').load('/mormonorg/video/videoLeftNav.html', {channel: channel}, leftNavInit);
        return false;
    });
    $('.pag-ajax').click(function() {
        index = $(this).text();
        $('#main-video').html('<div class="loading-text">Loading...</div>');
        $('#main-video').load('/mormonorg/video/videoContent.html', {channel: channel, subChannel: sub_channel, index: index, vgnextoid: vgnextoid, locale: locale, nowPlaying : sourceId}, pageFormat);
        return false;
    });

    $('.pag-left').click(function() {
        if (index > 1) {
            index = $('#pag-curr').text() - 1;
            $('#main-video').html('<div class="loading-text">Loading...</div>');
            $('#main-video').load('/mormonorg/video/videoContent.html', {channel: channel, subChannel: sub_channel, index: index, vgnextoid: vgnextoid, locale: locale, nowPlaying : sourceId}, pageFormat);
        }
        return false;
    });

    $('.pag-right').click(function() {
        var max = $('#pag-last').text();
        if (index < max) {
            index = +$('#pag-curr').text() + 1;
            $('#main-video').html('<div class="loading-text">Loading...</div>');
            $('#main-video').load('/mormonorg/video/videoContent.html', {channel: channel, subChannel: sub_channel, index: index, vgnextoid: vgnextoid, locale: locale, nowPlaying : sourceId}, pageFormat);
        }
        return false;
    });
    channel = tabId;
    sub_channel = subsection;
    if (i > 0) {
        $('#pag-curr').text(i);
    }
    leftNavInit();
}

function leftNavInit() {
    $('.left-ajax').click(function() {
        sub_channel = $(this).attr('id');
        setSelected($(this).parent());
        $('#pag-first').click();
        return false;
    });
    $('#pag-curr').click();
}

function setSelected(li) {
    li.addClass('selected');
    li.siblings().removeClass('selected');
}

function updatePagination() {
    var total = $('#video-count').text(); // total number of videos
    var display = 12; // number of videos to display on the screen
    var pages = Math.ceil(total / display);
    //      $('#pag-first').hide();
    $('#pag-curr').text(index);  // Set the current page number

    if (index < pages) {
        $('#pag-last').text(pages); // Set the last page number
        $('#pag-last').show();
    }
    else {
        $('#pag-last').text(index); // Make sure they can't paginate to a non-existant page
        $('#pag-last').hide();  // Hide the last page link - it's unnecessary.
    }

    if (index < pages - 1) {
        $('#pag-next').text((+index + 1));  // Set the next page number
        $('#pag-next').show();
        if (index < pages - 2) {
            $('#pag-uscore2').show();
        }
        else {
            $('#pag-uscore2').hide();
        }
    }
    else {
        $('#pag-next').hide();  // Hide the next page link - it's unnecessary.
        $('#pag-uscore2').hide();
    }

    if (index > 2) {
        $('#pag-prev').text((index - 1));  // Show the previous page number
        $('#pag-prev').show();
        if (index > 3) {
            $('#pag-uscore1').show();
        }
        else {
            $('#pag-uscore1').hide();
        }
    }
    else {
        $('#pag-prev').hide();  // Hide the previous page link - it's unnecessary.
        $('#pag-uscore1').hide();
    }

    if (index > 1) {
        $('#pag-first').show(); // Show the first page link
    }
    else {
        $('#pag-first').hide();
    }
}