﻿$(function () {
    var _videoName = 'Netflix Demo Video - Home';
    var _duration = 51000;
    var _increment = _duration / 20;
    var _cuePoints = new Array();
    for (var i = 0; i < 20; i++) {
        _cuePoints[i] = (_increment * (i + 1));
    }

    //initialize flowplayer
    var player = $f('player', '/include/flowplayer/flowplayer.commercial-3.1.5.swf', { key: '#$f5c5eab7e5f14fc0001',
        clip: {
            url: 'http://c1555872.cdn.cloudfiles.rackspacecloud.com/Netflix_UI_SiteUpdate_052810.flv',
            onCuepoint: [_cuePoints, function (clip, cuepoint) {
                pageTracker._trackEvent('Videos', parseInt(cuepoint / _duration * 100) + '% watched', _videoName);
            } ],

            // track start event for this clip 
            onStart: function (clip) {
                pageTracker._trackEvent('Videos', 'Play', _videoName);
            },

            // track pause event for this clip. time (in seconds) is also tracked 
            onPause: function (clip) {
                pageTracker._trackEvent('Videos', 'Pause', _videoName, parseInt(this.getTime()));
            },

            // track stop event for this clip. time is also tracked 
            onStop: function (clip) {
                pageTracker._trackEvent('Videos', 'Stop', _videoName, parseInt(this.getTime()));
            },

            // track finish event for this clip 
            onFinish: function (clip) {
                stopVideo();
                pageTracker._trackEvent('Videos', 'Finish', _videoName);
            }
        },

        // show stop button so we can see stop events too 
        plugins: {
            controls: {
                stop: true
            }
        },

        //Needed otherwise tv image doesn't fade in firefox
        onBeforeClick: function () {
            $('.demo-thumb').click();
            return false;
        },

        // track unload event. time is also tracked 
        onBeforeUnload: function () {
            pageTracker._trackEvent('Videos', 'Stop', _videoName, parseInt(this.getTime()));
        },

        wmode: 'opaque'
    });

    //wire up watch demo thumbnail
    $('.demo-thumb').toggle(function () {
        playVideo();
    }, function () {
        stopVideo();
    });

    //Play video in IE when it's clicked
    $('.netlix-tv').click(function () {
        $('.demo-thumb').click();
    });

    //wire up channel icons
    $('#channel-icons li').each(function () {
        $(this).hover(function () {
            var id = $(this).attr('id').replace('channel', '');
            var html = $('#channel-descriptions div:nth-child(' + (parseInt(id) + 1) + ')').html();
            $('#channel-desc').html(html);
        },
        function () {
            var html = $('#channel-descriptions div:nth-child(1)').html();
            $('#channel-desc').html(html);
        });
    });

    function stopVideo() {
        $('.demo-thumb').attr('src', $('.demo-thumb').attr('src').replace(/stopdemo/, 'demo'));
        $f().unload();
        $('.netlix-tv').fadeIn('slow');
    }

    function playVideo() {
        $('.demo-thumb').attr('src', $('.demo-thumb').attr('src').replace(/demo/, 'stopdemo'));
        $('.netlix-tv').fadeOut('slow', function () {
            $f().play();
        });
    }
});


