// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

$(document).ready(function () {

    var videopath = "assets/";
    var swfplayer = videopath + "videos/flowplayer.commercial-3.2.5.swf";
    var swfcontent = videopath + "videos/flowplayer.content-3.1.0.swf";
    var swfcaptions = videopath + "videos/flowplayer.captions-3.1.4.swf";

    var videoclip;
    var player;
    var vidid;
    var captions;
    var capShow;
    var ccbutShow;

    $(".video_link").fancybox({
        'hideOnContentClick': false,
        'overlayOpacity': .6,
        'zoomSpeedIn': 400,
        'zoomSpeedOut': 400,
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',

        'callbackOnShow': function () {

            player = $f("fancy_content", { src: swfplayer, wmode: 'opaque' }, {

                play: { opacity: 0 },
                //key: '#@4e51707d209805af567', 
                key: '#@f9fb2037a1067f87bba',  //production key

                clip: {
                    url: 'http://videos.irvinecompany.com/retail/TMPSAFARIPARK.m4v',


                    onStart: function (clip) {
                        var wrap = jQuery(this.getParent());
                        var clipwidth = clip.metaData.width;
                        var clipheight = clip.metaData.height;
                        var pos = $.fn.fancybox.getViewport();
                        $("#fancy_outer").css({ width: clipwidth + 20, height: clipheight + 20 });
                        $("#fancy_outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth - 36) / 2)));
                        $("#fancy_outer").css('top', ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50) / 2)));
                        $("#fancy_right, #fancy_left").css({ height: clipheight - 60, bottom: '70px' });

                    },


                    onFinish: function () {
                        $('#fancy_close').trigger('click');
                    }
                }
            }).ipad();

            player.load();

        },
        'callbackOnClose': function () {
            $("#fancy_content_api").remove();
            $("#videoswrap").hide();
            $("#videoswrap").fadeTo("fast", 0);
        }

    });

    var sv = getQuerystring('sv');
    
    if ($.cookie('tmpVideo') == null || sv == 'true') {
        $.cookie('tmpVideo', 'hideit');

        vidid = $(".video_link").index(this);
        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 = 'videos/empty.srt';
            capShow = false;
            ccbutShow = null;
        }

        $("#video_link").trigger('click');
    } else {
        $("#videoswrap").hide();
        $("#videoswrap").fadeTo("fast", 0);
    }


    function getQuerystring(key, default_) {
        if (default_ == null) default_ = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return default_;
        else
            return qs[1];
    }

}); 
