﻿var pathName = window.location.pathname;
pathName = pathName.replace(/(\W|_)/g, "").toLowerCase();
// skins, video file, and cue point can be defined on video page
if (typeof skinPP == "undefined")
    var skinPP = '/js/jwplayer/FBS-PlayPause/FBS-PlayPause.zip'; // set default skin here
if (typeof skinFC == "undefined")
    var skinFC = '/js/jwplayer/FBS-FullControls/FBS-FullControls.zip'; // set default skin here
if (typeof cuePoint == "undefined")
    var cuePoint = 20 * 60 + 00; // set default cue point
if (typeof cloudFrontStreamingDistribution == "undefined")
    var cloudFrontStreamingDistribution = 'rtmp://s1kjelr53qmi1z.cloudfront.net/cfx/st';  // set default streamer
if (typeof downloadVideoUrl == "undefined")
    var downloadVideoUrl = '';
if (typeof autoStart == "undefined")
    var autoStart = "true";
if (typeof videoPreviewImageUrl == "undefined") {
    if (autoStart == "true")
        var videoPreviewImageUrl = "http://d3f3hlqdx4azlp.cloudfront.net/images/VideoPreviewImages/GlobalVideoPreviewImageLoading.png";
    else
        var videoPreviewImageUrl = "http://d3f3hlqdx4azlp.cloudfront.net/images/VideoPreviewImages/iPhoneVideoPreviewImage5.png";
}

var recordingRatio = 5; // record 5% of traffic
var recordAnalytics = false;
if (readCookie('RecordAnalytics' + pathName) == "true") {
    recordAnalytics = true;
} else if (readCookie('RecordAnalytics' + pathName) == null) {
    recordAnalytics = Math.floor(Math.random() * 100) <= recordingRatio;
    createCookie('RecordAnalytics' + pathName, recordAnalytics, 14);
}
var firstWatch = true;
var analyticsInterval = 5;
var secondsWatched = analyticsInterval;
if (readCookie('SecondsWatched' + pathName) > analyticsInterval) {
    secondsWatched = parseInt(readCookie('SecondsWatched' + pathName));
    firstWatch = false;
}
if (readCookie('SecondsWatched' + pathName) != null) {
    firstWatch = false;
}

$(function () {

    // show the order button to return visitors
    if (readCookie('orderButton' + pathName) != null) {
        $('#divOrderButton').show();
    }
    createCookie('orderButton' + pathName, 'true', 120);

    if (typeof streamingVideoFileName != "undefined") {
        // if user previously finished watching video, load full controls
        if (readCookie('finishedVideo') != null) {
            // load and start a new video player w/ full controls
            jwplayer('jwcontainer').setup({
                skin: skinFC,
                file: streamingVideoFileName,
                provider: 'rtmp',
                streamer: cloudFrontStreamingDistribution,
                image: videoPreviewImageUrl,
                autostart: autoStart,
                icons: 'false',
                stretching: 'exactfit',
                volume: '100',
                modes: [
                    { type: 'flash', src: '/js/jwplayer/player.swf' },
                    {
                        type: 'html5',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video',
                            'autostart': 'false',
                            'skin': '/js/jwplayer/FBS-FullControls/FBS-FullControls.xml',
                            'image': 'http://d3f3hlqdx4azlp.cloudfront.net/images/VideoPreviewImages/iPhoneVideoPreviewImage5.png'
                        }
                    },
                    {
                        type: 'download',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video'
                        }
                    }
                ],
                events: { // don't listen for cue point because orderButton cookie is already set
                    onReady: function () {
                        if (readCookie('VidTimer' + pathName) > 10) {
                            // resume playing where left off
                            jwplayer('jwcontainer').seek(readCookie('VidTimer' + pathName));
                        }
                    },
                    onComplete: function () {
                        $('#video').slideUp('slow');
                        $('#replay').slideDown('slow');
                    }
                },
                plugins: {
                //                    'gapro-2': {
                //                        
                //                    }
            }
        });

        // else, load play/pause player
    } else {

        // load and start a new video player w/ play/pause controls
        jwplayer('jwcontainer').setup({
            skin: skinPP,
            file: streamingVideoFileName,
            provider: 'rtmp',
            streamer: cloudFrontStreamingDistribution,
            image: videoPreviewImageUrl,
            autostart: autoStart,
            icons: 'false',
            stretching: 'exactfit',
            volume: '100',
            modes: [
                    { type: 'flash', src: '/js/jwplayer/player.swf' },
                    {
                        type: 'html5',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video',
                            'autostart': 'false',
                            'skin': '/js/jwplayer/FBS-PlayPause/FBS-PlayPause.xml',
                            'image': 'http://d3f3hlqdx4azlp.cloudfront.net/images/VideoPreviewImages/iPhoneVideoPreviewImage5.png'
                        }
                    },
                    {
                        type: 'download',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video'
                        }
                    }
                ],
            events: {
                onReady: function () {
                    if (readCookie('VidTimer' + pathName) > 10) {
                        // resume playing where left off
                        jwplayer('jwcontainer').seek(readCookie('VidTimer' + pathName));
                    }
                },
                onPlay: function () {
                    if (recordAnalytics && firstWatch) {
                        addStatCount(analyticsStub() + "start");
                        firstWatch = false;
                    }
                },
                onTime: function (evt) {
                    if (evt.position > cuePoint && evt.position < cuePoint + 1) { //cuePoint must be defined on video page
                        $('#divOrderButton').fadeIn(500);
                    }
                    if (recordAnalytics && evt.position > secondsWatched) {
                        // ie, filename.flv|flash|5|30, filename.mp4|html5|10|120
                        addStatCount(analyticsStub() + analyticsInterval + "|" + secondsWatched);
                        secondsWatched += analyticsInterval;
                    }
                },
                onComplete: function () {
                    createCookie('finishedVideo', 'true', 14);
                    $('#video').slideUp('slow');
                    $('#replay').slideDown('slow');
                },
                onError: function (evt) {
                    if (recordAnalytics && firstWatch) {
                        addStatCount((analyticsStub() + "error|" + evt.message).substr(0, 500));
                        firstWatch = false;
                    }
                }
            },
            plugins: {
            //                    'gapro-2': {
            //                        
            //                    }
        }
    });
}
}
$('#replay').click(function () {

    $('#replay').slideUp('slow');
    $('#video').slideDown('slow');

    if (typeof streamingVideoFileName != "undefined") {
        jwplayer('jwcontainer').remove();

        // load and start a new video player w/ full controls
        jwplayer('jwcontainer').setup({
            skin: skinFC,
            file: streamingVideoFileName,
            provider: 'rtmp',
            streamer: cloudFrontStreamingDistribution,
            image: videoPreviewImageUrl,
            autostart: autoStart,
            icons: 'false',
            stretching: 'exactfit',
            volume: '100',
            modes: [
                    { type: 'flash', src: '/js/jwplayer/player.swf' },
                    {
                        type: 'html5',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video',
                            'autostart': 'false',
                            'skin': '/js/jwplayer/FBS-FullControls/FBS-FullControls.xml',
                            'image': 'http://d3f3hlqdx4azlp.cloudfront.net/images/VideoPreviewImages/iPhoneVideoPreviewImage5.png'
                        }
                    },
                    {
                        type: 'download',
                        config: {
                            'file': downloadVideoUrl,
                            'provider': 'video'
                        }
                    }
                ],
            events: { // on replay, don't seek and don't listen for cue point
                onComplete: function () {
                    $('#video').slideUp('slow');
                    $('#replay').slideDown('slow');
                }
            },
            plugins: {
            //                    'gapro-2': {
            //                    }
        }
    });
}
});
});
// save player position to be able to resume (lower bounds: 10s, upper bounds: 5s before video ends)
$(window).unload(function () {
    if (typeof streamingVideoFileName != "undefined" && typeof disableResume == "undefined") {
        createCookie('SecondsWatched' + pathName, secondsWatched, 14);
        if (jwplayer().getPosition() > 10 && jwplayer().getPosition() < jwplayer().getDuration() - 5) {
            createCookie('VidTimer' + pathName, jwplayer().getPosition(), 1);
        } else {
            if (jwplayer().getPosition() >= jwplayer().getDuration() - 5) {
                createCookie('finishedVideo', 'true', 14);
            }
            createCookie('VidTimer' + pathName, '', -1);
        }
    }
});

// ie, filename.flv|flash|, filename.mp4|html5|
function analyticsStub() {
    return (jwplayer().renderingMode == "flash" ? streamingVideoFileName : downloadVideoUrl.substr(downloadVideoUrl.lastIndexOf('/') + 1))
        + "|" + jwplayer().renderingMode + "|";
}
