﻿/*
    Flash mediaplayer client control
*/

CUVocalWeb.MediaPlayer = function(element) {

    CUVocalWeb.MediaPlayer.initializeBase(this, [element]);

    this._swf = "assets/mediaplayer/mediaplayer.swf";
    this._flashId = element.id;
    this._width = "550";
    this._height = "20";
    this._flashVersion = "8.0.0";
    this._expressInstallSwf = "assets/mediaplayer/expressInstall.swf";
    this._params = {};
    this._flashvars = {};
    this._attributes = {};
}

CUVocalWeb.MediaPlayer.prototype = {

    //swf(string) - the source file(swf file) of the flash media player
    get_swf: function() {
        return this._swf;
    },

    set_swf: function(value) {
        this._swf = value;
    },

    //flashId(string) - the id of the flash object element
    get_flashId: function() {
        return this._flashId;
    },

    set_flashId: function(value) {
        this._flashId = value;
    },

    //width(string) - the width of the flash object
    get_width: function() {
        return this._width;
    },

    set_width: function(value) {
        this._width = value;
    },

    //height(string) - the height of the flash object
    get_height: function() {
        return this._height;
    },

    set_height: function(value) {
        this._height = value;
    },

    //params(json object) - the parameters to be added to the embed/object element
    get_params: function() {
        return this._params;
    },

    set_params: function(value) {
        this._params = value;
    },

    //flashvars(json object) - the variables to be passed to the flash object
    get_flashvars: function() {
        return this._flashvars;
    },

    set_flashvars: function(value) {
        this._flashvars = value;
    },

    //attributes(json object) - the attributes of the flash object
    get_attributes: function() {
        return this._attributes;
    },

    set_attributes: function(value) {
        this._attributes = value;
    },

    //Override the initialize method.
    initialize: function() {

        //Check whether the swfobject.js has been loaded.
        if (!swfobject) {
            throw new Error("The MediaPlayer control needs the SWFObject 2.0.");
            return;
        }

        if (!this.get_attributes().id) {
            this.get_attributes().id = this.get_flashId();
        }

        //Embed the flash media player.
        swfobject.embedSWF(this.get_swf(), this.get_flashId(), this.get_width(), this.get_height(), this._flashVersion,
                            this._expressInstallSwf, this.get_flashvars(), this.get_params(), this.get_attributes());

        //Fix the communication bug within the form
        var flashId = this.get_flashId();
        var fixFn = function() {
            window[flashId] = $get(flashId);
        };
        swfobject.addDomLoadEvent(fixFn);


        CUVocalWeb.MediaPlayer.callBaseMethod(this, "initialize");

    },

    //Load the specific media file.
    //Parameters:
    //    vars(json object) - the new flashvars to load the new media file
    //Returns: void
    loadFile: function(vars) {
        var flashMediaPlayer = $get(this.get_attributes().id);
        flashMediaPlayer.sendEvent("stop");
        flashMediaPlayer.loadFile(vars);
        setTimeout(function() {
            flashMediaPlayer.sendEvent("playitem", 1);
        }, 500);
    },

    //Trigger the specific event of the flash media player.
    //Parameters:
    //    name(string) - the name of the event
    //    params(json object) - the parameters to be passed to the event handler
    //Returns: void
    sendEvent: function(name, params) {
        $get(this.get_attributes().id).sendEvent(name, params);
    },

    //Override the dispose method.
    dispose: function() {

        if (this._swf) {
            delete this._swf;
        }

        if (this._flashId) {
            delete this._flashId;
        }

        if (this._width) {
            delete this._width;
        }

        if (this._height) {
            delete this._height;
        }

        if (this._params) {
            delete this._params;
        }

        if (this._flashvars) {
            delete this._flashvars;
        }

        if (this._attributes) {
            delete this._attributes;
        }

        CUVocalWeb.MediaPlayer.callBaseMethod(this, "dispose");
    }

};

CUVocalWeb.MediaPlayer.registerClass("CUVocalWeb.MediaPlayer", Sys.UI.Control);
