﻿function srabcb_flash_video(source) {
    var vid = source.replace('#', '');

    if (parseInt(vid) > 0 && document.location.href.indexOf('videos/all') != -1) {
        // Variables and data
        var vobj = $('#vid-' + vid);
        var finalPath = "/WorkArea/DownloadAsset.aspx?id=" + vid;

        // Video details
        var title = $(vobj).find('.title').text();
        var author = $(vobj).find('.author').text();
        var added = $(vobj).find('.added').text();
        var runtime = $(vobj).find('.runtime').text();

        // Change address (for bookmarking)
        document.location.href = document.location.href.replace(/#([0-9]+)/, '') + '#' + vid;

        // Change HTML
        $('#all_videos tr').removeClass('active');
        $('#all_videos #vid-' + vid + ':visible').addClass('active');
        $('#video-player').empty().append('<div id="flash-video-player"/>');
        $('#video-player')
            .prepend('<h1 class="pageTitle">' + title + '</h1>')
            .append('<div class="details"><p></p></div>');
        $('#video-player .details p')
            .append('<strong>Runtime:</strong> ' + runtime + '<br/>')
            .append('<strong>Posted by:</strong> ' + author + '<br/>')
            .append('<strong>Added:</strong> ' + added + '<br/>')
            .append('<br/><a href="#" class="shareVideo">Share this video</a>');

        // Share Video (thickbox)
        $('#video-player a.shareVideo').click(function() {
            tb_show('', '/shared/sendToFriend.aspx?KeepThis=true&TB_iframe=true&height=250&width=350');
            return false;
        });

        // Configure Flash
        var flashvars = {
            flvPath: "",
            videoSrc: finalPath,
            fullscreenpage: "true",
            fsreturnpage: "true"
        };
        var params = {
            allowfullscreen: "true",
            bgcolor: "#eeeeee",
            quality: "best",
            wmode: "transparent"
        };
        var attributes = { id: 'videoGallery', name: 'videoGallery' };

        // Load Flash
        if (userHasFlashVersion) {
            swfobject.embedSWF("/_ui/srabcbsha08/swf/videoPlayer.swf", "flash-video-player", "480", "345", flashVersion, expressInstall, flashvars, params, attributes);
        }
        else {
            $('#flash-video-player').empty().append(noFlashContent);
        }
    }
    else if (parseInt(vid) > 0) {
        if (document.location.href.indexOf('individuals') != -1) {
            document.location.href = '/individuals/videos/all/#' + vid;
        }
        else {
            document.location.href = '/employers/videos/all/#' + vid;
        }
    }
}

function srabcb_flash_video_recent() {
    var flashvars = {};
    var params = {
        allowfullscreen: "true",
        bgcolor: "#eeeeee",
        quality: "best",
        wmode: "transparent"
    };
    var attributes = {};

    // Load Flash
    if (userHasFlashVersion) {
        swfobject.embedSWF("/_ui/srabcbsha08/swf/videoScroll.swf", "flash-recent-videos", "680", "210", flashVersion, expressInstall, flashvars, params, attributes);
    }
    else {
        $('#flash-recent-videos').empty().append(noFlashContent);
    }
}

function srabcb_video_table() {
    $('#all_videos').each(function() {

        // Insert links
        $('#content .allVideos h2').after('<span class="headRight"></div>');
        $('#content .allVideos .headRight').append('<a href="#" class="prev">Previous</a>');
        $('#content .allVideos .headRight').append('<a href="#" class="next">Next</a>');

        // Pagination
        var curPage = 0;
        var numPerPage = 10;
        var $table = $(this);
        var length = $table.find('tbody tr').length;

        var repaginate = function() {
            if (curPage == 0) {
                $('.allVideos .prev').hide();
            }
            $table.attr('class', 'page-' + curPage);
            $table.find('tbody tr')
                .show()
                .slice(0, curPage * numPerPage).hide()
                .end()
                .slice((curPage + 1) * numPerPage, length).hide()
                .end()
        }

        // Init
        repaginate();
    });
}


$(document).ready(function() {
    // Recent videos
    if ($('#flash-recent-videos').length) {
        srabcb_flash_video_recent();
    }

    if ($('#flash-video-player').length) {
        // Switch videos
        $('#all_videos tr td a').each(function() {
            $(this).unbind().click(function() {
                srabcb_flash_video($(this).attr('href'));
            });
        });
        // Switch to video onload
        if (document.location.href.indexOf('#') != -1) {
            srabcb_flash_video(document.location.href.split('#')[1]);
        }
    }

    // Paginate table
    if ($('#all_videos').length) {
        srabcb_video_table();
    }
});