function _smFlowPlayer() { }
_smFlowPlayer.prototype = {

    //configuration variables and default values
    logoUrl                 : "http://cdnimages.sorensonmedia.com/71478812-f460-4331-921e-8359d9994332/icons/73833eda-2d6d-4d33-8ff0-5aa0af0e45e9/file.png",
    logoLink                : "http://cnn.com",
    logoFullScreen          : false,
    logoDuration            : 0,
    thumbLocation           : "",
    autoPlayFlag            : false,
    publishingReferenceId   : "",
    fileVersionId           : "",
    fileDuration            : 0,
    targetId                : "",
    pluginHttp              : "http://static.cdn.360.sorensonmedia.com/1",
    fp                      : null,
    logoControls            : null,
    screen                  : null,
    controlBar              : null,
    configuration           : null,


    setConfig: function(jsonConfig) {
        this.configuration = jsonConfig;
        this.adjustConfigForDynamicVariables();
    },

    setConfigFromString: function(stringConfig) {
        this.configuration = $.JSON.decode(stringConfig);
        this.adjustConfigForDynamicVariables();
    },

    getConfig: function() {
        if (this.configuration == null) return {};
        this.stripDynamicVariablesFromConfig();
        var config = $.extend({}, this.configuration);
        this.adjustConfigForDynamicVariables();
        return config;
    },

    getConfigString: function() {
        if (this.configuration == null) return "";
        var c1 = $.JSON.encode( this.configuration ),
            c2 = $.JSON.encode( this.fp.getConfig() );
        this.stripDynamicVariablesFromConfig();
        var returnVal = $.JSON.encode(this.configuration);
        this.adjustConfigForDynamicVariables();
        return returnVal;
    },

    setCanvasCss: function(json) {
        if (typeof this.configuration['canvas'] == 'undefined') {
            this.configuration['canvas'] = {};
        }
        for (var i in json)
            this.configuration['canvas'][i] = json[i];
        this.displayFlowPlayer();
    },

    setPlayOverlay: function(overlayOn) {
        this.configuration['play']['opacity'] = (overlayOn) ? 1 : 0;
        this.displayFlowPlayer();
    },

    ensureDynamicVariablePathsExist: function() {
        if (typeof this.configuration['clip'] == 'undefined')
           this.configuration['clip'] = {};
        if (typeof this.configuration['canvas'] == 'undefined')
           this.configuration['canvas'] = {};
/*
        if (typeof this.configuration['plugins'] == 'undefined')
           this.configuration['plugins'] = { metrics: {} };
        else if (typeof this.configuration['plugins']['metrics'] == 'undefined')
           this.configuration['plugins']['metrics'] = {};
*/
    },

    adjustConfigForDynamicVariables: function() {
        this.ensureDynamicVariablePathsExist();
        this.configuration['playlist'] = [
            { url: this.thumbLocation,          autoPlay: true},
            { url: this.publishingReferenceId,  autoPlay: this.autoPlayFlag, fileVersionId: this.fileVersionId },
            { url: this.thumbLocation,          autoPlay: true}
        ];
        this.configuration['clip']['url'] = this.publishingReferenceId;
    },

    stripDynamicVariablesFromConfig: function() {
        this.ensureDynamicVariablePathsExist();
        this.configuration['playlist'] = [];
        this.configuration['screen'] = {};
        delete this.configuration.plugins.controls.left;
        if (this.configuration.plugins.controls.width == 412) {
            delete this.configuration.plugins.controls.width;
        }
        if (this.configuration.plugins.playListView) {
            delete this.configuration.plugins.playListView.height;
        }
        this.configuration['clip']['url'] = '';
        this.configuration['onLoad'] = '';
    },

    getCanvasBackgroundColor: function()
    {
        if (typeof this.configuration.canvas == 'undefined')
            this.configuration.canvas = {}

        if (typeof this.configuration.canvas.backgroundColor == 'undefined')
            this.configuration.canvas.backgroundColor = "#000000";

        return this.configuration.canvas.backgroundColor;
    },

    setDefaultConfig: function() {
        this.configuration = {
            play: {
                replayLabel: ""
            },
            playlist: [
                { url: this.thumbLocation, autoPlay: true },
                { url: this.publishingReferenceId, autoPlay: false, fileVersionId: this.fileVersionId },
                { url: this.thumbLocation, autoPlay: true }
            ],
            clip: {
                provider: 'rtmp',
                accelerated: false,
                autoPlay: false,
                //autoBuffering: true,
                fadeInSpeed: 1000,
                scaling: 'fit'
            },
            canvas: {
                backgroundColor : '#000000',
                backgroundGradient: 'low'
            },
            logo: {
                left: 15,
                top: 15,
                opacity: 1,
                logoDuration: 5000,
                fullscreenOnly: false,
                url: "",
                linkUrl: "",
                linkWindow: "_blank"
            },
            plugins: {
                rtmp: {
                    url: this.pluginHttp + '/flash/plugin.gm.rtmp-3.1.5.0.swf'
                },
                sorenson360: {
                    apiPath: 'http://360.sorensonmedia.com/api',
                    url: this.pluginHttp + '/flash/plugin.gm.sorenson-3.2.2.0.swf'
                },
                controls: {
                    apiPath: 'http://360.sorensonmedia.com/api',
                    url: this.pluginHttp + '/flash/flowplayer.controls-3.2.2.0.swf',
                    autoHide: 'never',
                    hideDelay: 1000,
                    borderRadius: '0px',
                    volumeSliderGradient: 'none',
                    bufferGradient: 'none',
                    tooltipTextColor: '#ffffff',
                    progressColor: '#1e5576',
                    bufferColor: '#020c13',
                    progressGradient: 'medium',
                    backgroundColor: '#222222',
                    sliderColor: '#000000',
                    volumeSliderColor: '#000000',
                    buttonColor: '#636462',
                    durationColor: '#ffffff',
                    timeColor: '#01DAFF',
                    sliderGradient: 'none',
                    buttonOverColor: '#728B94',
                    backgroundGradient: 'none',
                    tooltipColor: '#5F747C',
                    timeBgColor: '#555555',
                    height: 20,
                    opacity: 1.0,
                    play: true,
                    scrubber: true,
                    volume: true,
                    stop: false,
                    mute: true,
                    time: true,
                    fullscreen: true,
                    playerStyle: "static"
                }
            }
        };
    },

    displayFlowPlayerWithConfiguration: function (targetId, configuration, activateControls) {

        if (typeof activateControls == 'undefined') {
            activateControls = true;
        }

        //lets just see if the logo is there and url less, this is cuasing a bug atm
        if (configuration['plugins']['logo'] != null && configuration['plugins']['logo']['url'] == null) {
            configuration['plugins']['logo'] = null;
    //        configuration['logo'] = { "url" : "/images/360Logo.gif"};
        }

        if ('logo' in configuration && configuration.logo != null && 'left' in configuration.logo) {
            configuration.logo.left = ('playListView' in configuration.plugins && configuration.plugins.playListView != null)
                // TODO: get rid of this hard coded nonsense -- it is set in flowPlayerControls.js
                ? 223
                : 15;
        }

        this.configuration = configuration;
        this.fp = flowplayer(targetId, this.pluginHttp + "/flash/flowplayer-3.2.2.swf", configuration);

        if (activateControls) {
            if (this.controlBar == null) {
                this.controlBar = new _smFlowPlayerControlBar();
                this.controlBar.initialize(this);
            }

            if (this.logoControls == null) {
                this.logoControls = new _smFlowPlayerLogoControls();
                this.logoControls.initialize(this);
            }

            if (this.screen == null) {
                this.screen = new _smFlowPlayerScreen();
                this.screen.initialize(this);
            }
        }
    },

    displayFlowPlayer: function () {
        if (this.configuration == null) {
            this.setDefaultConfig();
        }
        this.displayFlowPlayerWithConfiguration(this.targetId, this.configuration);
    },

//Same as displayFlowPlayer, but strips out *many* plugns and puts in a standard image

    displayFlowPlayerShell: function() {

        if (this.configuration == null) {
            this.setDefaultConfig();
        }
        this.shellConfig['plugins']['controls'] = this.configuration['plugins']['controls'];
        this.shellConfig['plugins']['controls']['height'] = parseInt(this.shellConfig['plugins']['controls']['height'] * .7);
        //this.shellConfig['plugins']['controls']['height'] = 16;
        //this.shellConfig['plugins']['controls']['width'] = "95%";
        this.displayFlowPlayerWithConfiguration(this.targetId, this.shellConfig, false);

    },

    getControlBarComponent: function () {
        return this.controlBar;
    },


    getLogoControlsComponent: function() {
        return this.logoControls;
    },

    getScreenComponent: function() {
        return this.screen;
    },

    shellConfig: {
        play: {
            replayLabel: ""
        },
        playlist: [{url: "http://cdnimages.sorensonmedia.com/3847cd95-be8c-414c-8a28-602084bc069b/995c847a-8fe2-404d-821b-6b6561dbcc94/file-1.jpg", autoPlay: true}],
        clip:{
            accelerated: false,
            autoPlay: false,
            //autoBuffering: true,
            fadeInSpeed: 1000,
            scaling: 'fit'
        },
        plugins: {
            controls: {
                url: this.pluginHttp + '/flash/flowplayer/flowplayer.controls-3.1.5.swf',
                autoHide: 'always',
                hideDelay: 1000,
                borderRadius: '0px',
                volumeSliderGradient: 'liow',
                bufferGradient: 'none',
                tooltipTextColor: '#ffffff',
                progressColor: '#1e5576',
                bufferColor: '#020c13',
                progressGradient: 'medium',
                backgroundColor: '#222222',
                sliderColor: '#000000',
                volumeSliderColor: '#000000',
                buttonColor: '#636462',
                durationColor: '#ffffff',
                timeColor: '#01DAFF',
                sliderGradient: 'none',
                buttonOverColor: '#728B94',
                backgroundGradient: 'none',
                tooltipColor: '#5F747C',
                timeBgColor: '#555555',
                height: 20,
                opacity: 1.0,
                play: true,
                scrubber: true,
                volume: true,
                stop: false,
                mute: true,
                time: true,
                fullscreen: true
            }
        }
    },


    flowPlayer: function() {
        return this.fp;
    },

    setClip: function (url) {
        this.fp.setClip(url);
    },

    play: function () {
        this.fp.play();
    },

    pause: function () {
        this.fp.pause();
    },

    // likely no longer need getKey or getValue
    getKey: function(json)
    {
        for (var i in json)
            return i;
        return;
    },

    getValue: function(json)
    {
        for (var i in json)
            return json[i];
        return;
    },

    getControlBarHeight: function () {
        var controlBarHeight;
        try {
            controlBarHeight = parseInt(this.configuration.plugins.controls.height);
        } catch(error) {
            controlBarHeight = 0;
        }
        return controlBarHeight || 0;
    },

    getPlaylistWidth: function () {
        try {
            if (this.configuration.plugins.playListView)
                return 208;
        } catch (error) { }
        return 0;
    }
};
