﻿// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer
$(document).ready(function () {

    var videoclip;
    var player;
    var vidid;
    var captions = '';
    var capShow = false;
    var ccbutShow = false;
    var swfplayer = '/js/jquery.flowplayer/flowplayer-3.1.5.swf';

    $(".video_link").hover(function () {
        vidid = $(".video_link").index(this);
        videoclip = $(".video_link").eq(vidid).attr("name");
    });

    $(".video_link").fancybox({
        'hideOnContentClick': false,
        'overlayOpacity': .6,
        'zoomSpeedIn': 400,
        'zoomSpeedOut': 400,
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        'onComplete': function () {
            if (videoclip == 'image') {
                $("#fancybox-right, #fancybox-left").css({ height: $("#fancybox-outer").height(), bottom: '0' });
            } else {
                player = $f("fancybox-content", { src: swfplayer, wmode: 'opaque' }, {
                    play: { opacity: 0 },
                    clip: {
                        autoPlay: true,
                        autoBuffering: true,
                        url: videoclip + '',
                        //captionUrl: videopath+captions+'',
                        onStart: function (clip) {
                            var wrap = jQuery(this.getParent());
                            var clipwidth = clip.metaData.width;
                            var clipheight = clip.metaData.height;
                            var pos = $.fn.fancybox.getViewport();
                            $("#fancybox_outer").css({ width: clipwidth + 20, height: clipheight + 20 });
                            $("#fancybox_outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth - 36) / 2)));
                            $("#fancybox_outer").css('top', ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50) / 2)));
                            $("#fancybox_right, #fancy_left").css({ height: clipheight - 60, bottom: '70px' });
                        }
                    },
                    onFinish: function () {
                        // $('#fancybox-close').trigger('click');
                    }
                });
                player.load();
            }
        },
        'onClosed': function () {
            $("#fancybox-content-api").remove();
        }
    });

    $('#fancybox-right').click(function () {
        vidid++;
        videoclip = $(".video_link").eq(vidid).attr("name");
        if ($(".video_link").eq(vidid).hasClass('captions')) {
            captions = $(".video_link").eq(vidid).attr("name") + '.srt';
            capShow = true;
            ccbutShow = { width: 20, height: 15, right: 5, bottom: 32, label: 'CC' };
        } else {
            captions = '';
            capShow = false;
            ccbutShow = null;
        }
    });

    $('#fancybox-left').click(function () {
        vidid--;
        videoclip = $(".video_link").eq(vidid).attr("name");
        if ($(".video_link").eq(vidid).hasClass('captions')) {
            captions = $(".video_link").eq(vidid).attr("name") + '.srt';
            capShow = true;
            ccbutShow = { width: 20, height: 15, right: 5, bottom: 32, label: 'SUBS' };
        } else {
            captions = '';
            capShow = false;
            ccbutShow = null;
        }
    });
}); 
