﻿/*
* This set of functions checks the client agent for video streaming.
* If the client agent cannot stream flash, HTML5 video is generated.
*
* IMPORTANT NOTE: 
* HTML5 does not have the ability to validate tokens or use
* a lot of the security flash players provide.  As a result, this
* body of code should not be used for video that needs to be secure.
*/

function writeVideo(flashURL, httpURL, divID, height, width, flashVars) {
    if (divID == null) {
        divID = 'video';
    }

    if (height == null) {
        height = '360';
    }

    if (width == null) {
        width = '640';
    }

    var videoElement = document.getElementById(divID);
    var player = null;
    
    if (clientRequiresHTTP()) {
        player = new htmlVideoPlayer(httpURL, 'controls', 'preload', width, height);
        videoElement.innerHTML = player.generateHTML();
    }
    else {
        player = flashVideoPlayer(flashURL, width, height);
        for (i in flashVars) {
            player.addVariable(i, flashVars[i]);
        }
        player.write(divID);
    }
}

function clientRequiresHTTP() {
    agent = navigator.userAgent;
    return agent.match(/iPhone/i) ||
			agent.match(/iPod/i) ||
			agent.match(/iPad/i);
}

function htmlVideoPlayer(source, controls, preload, width, height) {
    this.source = source;
    this.controls = controls;
    this.preload = preload;
    this.width = width;
    this.height = height;
    this.generateHTML = generateHTML;

    return true;
}

function generateHTML() {
    html = '<video src=' + this.source +
			' controls=' + this.countrols +
			' autoplay=' + this.autoplay +
			' preload=' + this.autplay +
			' width=' + this.width +
			' height=' + this.height +
			'>';
    html += 'Your browser does not support HTML5 video playback.';
    html += '</video>';
    return html;
}

function flashVideoPlayer(source, width, height) {
    var player = new SWFObject('http://www.cdpe.com/assets/images/swf/player-licensed.swf', 'mpl', width, height, '9');
    player.addParam('allowscriptaccess', 'always');
    player.addParam('allowfullscreen', 'true');
    player.addParam('wmode', 'opaque');

    player.addVariable('menu', 'false');
    player.addVariable('file', source);
    player.addVariable('backcolor', '111111');
    player.addVariable('frontcolor', 'EEEEEE');
    player.addVariable('lightcolor', '666666');
    player.addVariable('screencolor', '000000');
    player.addVariable('playlistsize', '300');
    player.addVariable('skin', 'http://www.cdpe.com/assets/swf/stylish.swf');
    player.addVariable('bufferlength', '5');
    player.addVariable('streamer', 'rtmp://cp77471.edgefcs.net/ondemand');
    player.addVariable('playlist', 'none');
    player.addVariable('dock', 'true');
    player.addVariable('mute', 'false');
    player.addVariable('repeat', 'list');
    player.addVariable('smoothing', 'true');
    player.addVariable('volume', '85');
    player.addVariable('stretching', 'exactfit');

    return player;
}
