﻿// Checks if the browser is on apple device using HTML5 player rather than a Flash player
function PlayerPicker(videoContainer, siteRoot, cloudFrontDomain, awsRoot, videoSource, videoExtension, width, height, posterUrl, autoStart, wmodeSetting) {
    if ((navigator.userAgent.match(/iPad/i) != null) ||
		(navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)) {

        // Generate code for HTML5 Player
        var video = $('<video />');
        video.attr('width', width);
        video.attr('height', height);
        video.attr('preload', 'auto');
        video.attr('controls', 'controls');
        video.bind('play', function () {
            registerVideoEvent('Play - HTML5', videoSource);
        });

        if (posterUrl.length > 0)
            video.attr('poster', posterUrl);

        if (autoStart)
            video.attr('autoplay', 'autoplay');

        var source = $('<source />');
        source.attr('type', 'video/mp4');
        source.attr('codecs', 'avc1.42E01E, mp4a.40.2');
        source.attr('src', awsRoot + videoSource + videoExtension);
        video.append(source);
        $('#' + videoContainer).append(video);        
        
    } else {
        // Adds in flowplayer (flash) video player
        $('#' + videoContainer).css('width', width + 'px')
							   .css('height', height + 'px');

		var videoPlaylist = ['mp4:' + videoSource];
		if (posterUrl.length > 0) {
			videoPlaylist = [{ url: posterUrl, scaling: 'scale' }, 
							 { url: 'mp4:' + videoSource } ];
			if (!autoStart) {					 
				var startUpImage = $('<img />').attr('src', posterUrl)
											   .css('width', width + 'px')
											   .css('height', height + 'px')
											   .css('cursor', 'pointer');
				$('#' + videoContainer).append(startUpImage);
				autoStart = true;
			}
		 } 
		
        $f(videoContainer, {
            src: siteRoot + '/ClientScript/FlowPlayer/flowplayer-3.2.7.swf',
            wmode: wmodeSetting
        }, {
            clip: {
                provider: 'rtmp',
                autoPlay: autoStart
            },
            plugins: {
                rtmp: {
                    url: siteRoot + '/ClientScript/FlowPlayer/flowplayer.rtmp-3.2.3.swf',
                    netConnectionUrl: cloudFrontDomain
                }
            },
            canvas: { backgroundColor: '#000' },
            playlist: videoPlaylist
        });
        registerVideoEvent('Play - FLV', videoSource);
    }
}

var registerVideoEvent = function (actionType, videoSource) {
    //alert(mojoPageTracker);
    //Google Analytics tracking
    _gaq.push(['_setAccount', mojoPageTracker._getAccount()]);
    _gaq.push(['_trackEvent', 'Videos', actionType, videoSource]);
};
