/*
 *  Copyright (c) 2008 Snapvine, LLC. All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Snapvine, LLC
 *    The copyright notice above does not evidence any
 *    actual or intended publication of such source code.
*/

if (typeof(snapvine) == "undefined") { snapvine = new function() {}; }
if (typeof(snapvine.common) == "undefined") { snapvine.common = new function() {}; }
snapvine.common.Recorder = function(options) { this.initialize(options); this.show(); };

/* Class for Recorder.
 *
 * Correct usage:
 *
 */
snapvine.common.Recorder.prototype = {

    initialize: function(options) {
        this.set_options(options);
    },

    set_options: function(options) {
        this.options = {
            start_urn: null,
            on_error: null,
            on_complete: null,
            name: null,
            embed_server: null,
            width: 340,
            height: 155,
            type: null,
            uid: null,
            recorder_type: "standard"
        };

        Object.extend(this.options, options || {});

        if (this.options.on_complete != null)
        {
            this.on_complete_func = new Function(this.options.on_complete);
        }
        if (this.options.on_error != null)
        {
            this.on_error_func = new Function(this.options.on_error);
        }
    },

    unsupported_flash_tag: function() {
        w = this.options.width;
        h = this.options.height;
        var output = "<div style='width:" + ((this.options.recorder_type=='narrow'?(w-50):w)-15) + "px;height:" + (h-25) + "px;";
        output += "background-color:#eee;border:solid 2px #9cb1c2;padding: 20px 5px 5px 10px;'>";
        output += "<div style='font-weight:bold;margin-bottom:7px;'>Flash Player 9 Required</div>";
        output += "<div style='font-size:bold 8pt;'>You must have Adobe Flash Player 9 installed to use the Voice Recorder.<br/>";
        if (this.options.recorder_type=='narrow') output += "<br />";
        output += "<a style='text-decoration:underline;' target='_top' href='http://www.adobe.com/go/getflashplayer'><b>Download</b></a> the latest Flash Player from the Adobe web site.</div>";
        output += "</div>";
        return output;
    },

    show: function() {
        var flash_vars = "url=http://" + this.options.embed_server + "&type=" + this.options.type;
        if (this.options.recorder_type == "narrow")
        {
            flash_vars = "s_urn=" + this.options.start_urn;
        }

        flash_vars += "&svi=1";

        if (this.options.tab != "" && this.options.tab != null)
        {
            flash_vars += "&tab=" + this.options.tab;
        }
        if (this.options.uid != null )
        {
            flash_vars += "&uid=" + this.options.uid;
        }
        if (this.options.on_complete && this.options.recorder_type == "narrow")
        {
            flash_vars += "&on_c=$recorder.on_complete";
        }

        recorder_swf = "audio_recorder";
        if (this.options.recorder_type == "narrow") recorder_swf = "Recorder_9a";

        src = "http://" + this.options.embed_server + "/flash/" + recorder_swf + ".swf";

        // Start with the 'flash version 9 required' html
        $("div#record_embed").html(this.unsupported_flash_tag());

        var so = new SWFObject(src, "record_swf", this.options.width, this.options.height, "9", "#FFFFFF");
        so.forceEmbedTags();
        so.addParam("wmode", "transparent");
        so.addParam("play", "true");
        so.addParam("loop", "false");
        so.addParam("quality", "high");
        so.addParam("salign", "t");
        so.addParam("allowScriptAccess", "always");
        so.addParam("flashvars", flash_vars);
        so.write("record_embed");

        //alert($("div#record_embed")[0].innerHTML);
        $("div#record_embed").css({display:''});
    },

    on_complete: function(data)
    {
        if (this.options.name && data) {
            var poll_token;
            if (data[0])
                poll_token = data[0];
            else
                poll_token = data.token;
            $("input#"+this.options.name)[0].value = poll_token;
        }
        if (this.on_complete_func) {
            this.on_complete_func();
        }
    }
};


