﻿//<using>
//  nui/nui.js
//  nui/prototype.js
//  nui/lang.js
//  nui/util.js
//  nui/widget.js
//</using>

/**
 * All kinds of players in NUI.
 * Copyright 2007, NetRanking.cn
 */
 
/**
 * MediaPlayer. 
 * ATTENTION: this widget doesn't inherent from NUI.Widget.Base
 * @class NUI.Widget.MediaPlayer
 */
NUI.Widget.MediaPlayer = (function(){
    var g_dom = NUI.Util.DomService,
        g_sb = NUI.Util.StringService.StringBuilder,
        g_path = NUI.Util.WindowService.Path,
        g_htmlTmplt = (function() {
            var sb = new g_sb();
            sb.append(
                "<div onclick=\"try{NUI.Util.EventService.cancelBubble();}catch(ex){}\" ",
                "onmousedown=\"try{NUI.Util.EventService.cancelBubble();}catch(ex){}\" style=\"display:inline;\">",
                "<object classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\" width=\"{WIDTH}\" height=\"{HEIGHT}\">",
                "<param name=\"src\" value=\"{SRC}\" />",
                "<param name=\"url\" value=\"{SRC}\" />",
                "<param name=\"width\" value=\"{WIDTH}\" />",
                "<param name=\"height\" value=\"{HEIGHT}\" />",
                "<embed type=\"application/x-mplayer2\" src=\"{SRC}\" width=\"{WIDTH}\" height=\"{HEIGHT}\"></embed>",
                "<param name=\"AutoStart\" value=\"0\">",
                "</object>",
                "</div>"
            );
            return sb.toString();
        })(),
        g_type = {
            VIDEO : "video",
            AUDIO : "audio",
            FLASH : "flash"
        };
    
    /**
     * Check the media type is legal or not
     * @param vType {string} The media type
     * @return {boolean}
     */
    function _checkType(vType) {
        var retval = false;
        NUI.Util.ObjectService.searchObject(g_type, function(vName) {
            retval = (vType == g_type[vName]);
            return retval;
        });
        return retval;
    }
    
    return {
        /**
         * Media's type.
         */
        VIDEO : g_type.VIDEO,
        AUDIO : g_type.AUDIO,
        FLASH : g_type.FLASH,
        
        /**
         * Render the media player.
         * @param vMedia {object} The media to play
         *      vMedia = {
         *          src : string,           // media's src
         *          type : string           // MediaPlayer.AUDIO or MediaPlayer.VIDEO or MediaPlayer.FLASH
         *      } 
         * @param vAttributes {object} OPTIONAL. The attributes of popup. 
         *      vAttributes = {
         *          width : int,
         *          height : int
         *      }
         * @return {string} the html string of the player
         */
        render : function(vMedia, vAttributes) {
            if(!vMedia) {
                return "";
            }
            var m_src = vMedia.src, m_type = vMedia.type;
            if(!_checkType(m_type)) {
                return "";
            }
            var m_width = 360, m_height = (m_type == g_type.AUDIO) ? 45 : 270;
            if(vAttributes) {
                m_width = (vAttribute.width > 0) ? vAttribute.width : m_width;
                m_height = (vAttribute.height > 0) ? vAttribute.height : m_height;
            }//.replace(/&/gm, "%26")
            
            var retval = "";
            if(m_type == g_type.FLASH) {
                var p = new SWFObject(m_src, "myflashplayer", m_width + "px", m_height + "px", "9", "#FFFFFF");
                p.addVariable("width", m_width + "px");
                p.addVariable("height", m_height + "px");
                try {
                    var con = g_dom.createNode("div");
                    p.write(con);
                    retval = con.innerHTML;
                } catch(ex) {
                }
            } else {
                retval = g_htmlTmplt.supplant({
                    SRC : m_src,
                    WIDTH : m_width,
                    HEIGHT : m_height
                });
            }
            return retval;
        }
    };
})();

