var videoPlayer = (function() {

    var videoWidth, videoHeight, videoContainer, video, videoCompleteFunc, initialized = false;

    var pause = function(func) {
        video.pauseVideo();
    };

    var play = function() {
        video.playVideo();
    };

    var ensureVolume = function() {
        setVolume(0.5);
    };

    var sizeFlashVideo = function() {
        $(videoContainer).css({width: $(window).width(), height: $(window).height()});
    };

    var swap = function(videoFile,opts) {
        initialized = false;
        $('#video-container').empty().html('<div id="flash-video"></div>');
        injectFlashVideo(videoFile,opts);
    };

    var setOnEnded = function(func) {
	videoCompleteFunc = func;
    };

    var injectFlashVideo = function(filename,opts) {
        var width = 800, height = 600;
        if (opts) {
            if (opts.aspect && opts.aspect == '16:9') {
                height = 450;
            }
        }
	var flashvars = {
	    videoPath: 'http://runhappy.s3.amazonaws.com/video/' + filename + '.flv',
            volume: 0.3,
            width: width,
            height: height
	};
	var params = {
            wmode: "opaque",
	    menu: "false",
	    scale: "noScale",
	    allowFullscreen: "false",
	    allowScriptAccess: "always",
	    bgcolor: "#0164ac"
	};
	var attributes = {};
        swfobject.embedSWF("/swf/fs_video_player.swf", "flash-video", "100%", "100%", "10", "inc/j/expressInstall.swf", flashvars, params, attributes);
        video = el('flash-video');
    };

    var videoComplete = function() {
        videoCompleteFunc();
    };

    var init = function(opts) {
        videoContainer = el('video-container');
        injectFlashVideo('loop');
        sizeFlashVideo();
        $(window).resize(sizeFlashVideo);
    };

    return {
        init: init,
        play: play,
        pause: pause,
        swap: swap,
        setOnEnded: setOnEnded,
        videoComplete: videoComplete
    };

}());


function videoComplete() {
    videoPlayer.videoComplete();
}