(function(){
    window.top.MusicPlayer = {
        Constants : {
            DiskHost : "http://" + window.location.host//  "http://XXX.com"
        },
        Messages : {
            defaultTitle : "播放器",
            atLeastOne : "至少要选择一个歌曲",
            initialFail: "初始化播放器控件失败,或未识别浏览器。",
            failBrowser: "播放器只支持IE或Firefox浏览器",
            failPlugIn:  "播放器缺失相关插件"
        },
        //标记
        Flag : {
            PlayState : PlayState,
            BrowserFlag : BrowserFlag
        },         
        PlayControl : null,    
        MusicWin : null,
        LoadpageCallback : null,
        dispose : function(){
            if(top.MusicPlayer.PlayControl){
                top.MusicPlayer.PlayControl.dispose();
                top.MusicPlayer.PlayControl = null;
            }
            top.MusicPlayer.MusicWin = null;
            top.MusicPlayer.LoadpageCallback = null;
        },
        musicPlayInitialLoad : null,
        //是否显示播放器
        validShowUI : function(isValidUserSetting /* 是否验证用户设置 */){
            var validCode = 0;  //0 显示;1 浏览器不支持;2 插件不支持;3 用户设置不显示
            //浏览器
            var browserFlag = BrowserFlag.autoDetect();
            //alert(browserFlag);
            if(browserFlag != "ie" && browserFlag != "ff"){
                validCode = 1;
            }
            //用户设置
            if(validCode == 0 && isValidUserSetting){
                //todo
            }
            return validCode;
        },
        //加载播放器页面
        loadMusicPlayerPage : function(isRefresh, loadpageCallback){
            if(isRefresh){
                top.$("#playerframe").parent().remove();
            }
            if(top.$("#playerframe").length == 0) {
                top.MusicPlayer.LoadpageCallback = loadpageCallback;
                top.$("<div style='position:absolute;z-index:999;top:45px;right:17px;'><iframe id='playerframe' src='"
                    + top.MusicPlayer.Constants.DiskHost
                    + "/Html/MusicPlayer/default.html' style='width:212px;height:22px;' scrolling='no' frameborder='0'></iframe></div>")
                .appendTo(top.document.body);
            }
        },
        removeMusiPlayerPage: function(){            
            window.top.MusicPlayer.dispose();
            top.$("#playerframe").remove();
        },
        appendSongList : function(list /* [{FileId:xx,Name:yy}] */){
            var action = function(){            
                var oldMax = top.MusicPlayer.PlayControl.getPlayList().length;
                top.MusicPlayer.PlayControl.appendPlayList(list, function(){
                    window.top.MusicPlayer.loadPlayListUI(false, function(){
                        var newIndex = 0;
                        if(top.MusicPlayer.PlayControl.getPlayList().length > 0){
                            newIndex = oldMax;
                        }
                        //播放新添加歌曲
                        top.MusicPlayer.PlayControl.playLoop(newIndex);
                    });                
                    var $ = top.MusicPlayer.MusicWin.$;
                    $(".pl-ft").hide();
                    $("#controlPlaylist").show();
                });
            };
            if(!top.MusicPlayer.PlayControl){
                top.MusicPlayer.loadMusicPlayerPage(true, action);
                return;
            }
            action();
        },
        //显示播放列表
        loadPlayListUI : function(stopload, loadCallback){
            var $ = top.MusicPlayer.MusicWin.$;
            //加载播放列表
            var listbody = $("#listbody").empty();
            var htmlTemplate = "<dl class='pl-current'>"
                + "<dt><i class='mp-icon mp-status'></i><span>播放列表</span></dt>"
                + "<dd><ul></ul></dd></dl>";
            htmlTemplate = $(htmlTemplate).appendTo(listbody);
            var ul = htmlTemplate.find("dd>ul");
            
            var renderUI = function(){
                var pc = top.MusicPlayer.PlayControl;
                var playlist = pc.getPlayList();
                $.each(playlist, function(index){
                    var li = $("<li><input type='checkbox' />{0}</li>".format(this.Name));
                    li.data("data", this);  //缓存
                    li.data("index", index);
                    li.click/*(function(){    //单击选中行
                        var cb = $(this).find(":checkbox")[0];
                        cb.checked = !cb.checked;
                    }).dblclick*/(function(){ //双击播放此歌曲
                        pc.playLoop($(this).data("index"), function(){
                        });
                    });
                    li.find(":checkbox").click(function(e){
                        e.stopPropagation();
                    });
                    ul.append(li);
                });
                top.MusicPlayer.highlightPlay(!pc.isPlaying);
            };
            
            if(!stopload){
                top.MusicPlayer.PlayControl.loadPlayList(function(pc){
                    renderUI();
                    if(loadCallback){
                        loadCallback();
                    }
                });
            }
            else {
                renderUI();
            }
        },
        highlightPlay : function(clear){
            var $ = top.MusicPlayer.MusicWin.$;
            if($("#listbody").data("status") == "play"){                    
                var ul = $("#listbody>dl>dd>ul");
                ul.find("li").removeClass("pl-hover");
                if(!clear){                        
                    ul.find("li:eq(" + top.MusicPlayer.PlayControl.getCurrentPlayIndex() + ")").addClass("pl-hover");
                }
            }
        },
        renderPlugIn : function(config){
            var html = "<object id='{0}' width='370' height='45' {1}> " +
                "<param name='rate' value='1' /> " +
                "<param name='balance' value='0' /> " +
                "<param name='currentPosition' value='0' /> " +
                "<param name='defaultFrame' /> " +
                "<param name='playCount' value='1' /> " +
                "<param name='autoStart' value='0' /> " +
                "<param name='currentMarker' value='0' /> " +
                "<param name='invokeURLs' value='0' /> " +
                "<param name='baseURL' /> " +
                "<param name='volume' value='50' /> " +
                "<param name='mute' value='0' /> " +
                "<param name='uiMode' value='invisible' /> " +
                "<param name='stretchToFit' value='0' /> " +
                "<param name='windowlessVideo' value='0' /> " +
                "<param name='enabled' value='-1' /> " +
                "<param name='enableContextMenu' value='0' /> " +
                "<param name='fullScreen' value='0' /> " +
                "<param name='SAMIStyle' /> " +
                "<param name='SAMILang' /> " +
                "<param name='SAMIFilename' /> " +
                "<param name='captioningID' /> " +
                "<param name='enableErrorDialogs' value='0' /> " +
                "<param name='_cx' value='6482' /> " +
                "<param name='_cy' value='6350' /> " +
            "</object> ";
            //插件html描述
            var plugInHtml = null;
            config = jQuery.extend({
                id: "Player",
                browserFlag: null,
                doc : document
            }, config);
            
            config.browserFlag = config.browserFlag || BrowserFlag.autoDetect();
            
            if(config.browserFlag){
                //插件html
                switch(config.browserFlag){
                    case BrowserFlag.IE: {
                        //ActiveX
                        plugInHtml = "classid=\"clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6\"";
                        break;
                    }
                    case BrowserFlag.FF: {
                        //FF PlugIn WMP11
                        plugInHtml = "type=\"application/x-ms-wmp\"";
                        break;
                    }
                }
            }
            
            if(plugInHtml){
                config.doc.write(html.format(config.id, plugInHtml));
                //注册一个全局变量
                //config.doc.parentWindow[config.id] = config.doc.getElementById(config.id);
            }
        },
        //初始化
        initialPlayer : function(winPlayer){            
            var $ = winPlayer.$;
            top.MusicPlayer.MusicWin = winPlayer;
            //初始化播放器插件,绑定事件,加载歌曲,直接播放
            var playControl = new PlayControl("Player");
            
            var plug = $("object");
            if(plug.length == 0 || !plug[0].controls){
                $(".player").hide().remove();
                $("#msg").html(window.top.MusicPlayer.Messages.failPlugIn);
                return;
            }
            
            window.top.MusicPlayer.PlayControl = playControl;
            $("#aClose").click(function(e){
                top.MusicPlayer.removeMusiPlayerPage();
                e.preventDefault();
            });     
            if(playControl){
                //常用方法
                var loadPlayListUI = top.MusicPlayer.loadPlayListUI;
                var highlightPlay = top.MusicPlayer.highlightPlay;
                var replaceClass = function(id, oldcss, newcss){
                    var temp = $("#" + id);
                    temp.removeClass(oldcss);
                    if(!temp.hasClass(newcss)){
                        temp.addClass(newcss);
                    }
                };
                /******** 绑定事件 ******/
                
                //------播放事件
                //播放前
                playControl.onBeforePlay = function(music) {
                    if(music){
                        replaceClass("aPlayOrPause", "mp-paly", "mp-pause");
                        replaceClass("iStatus", "mp-loading", "mp-flash");
                    }                    
                };
                //播放後
                playControl.onAfterPlay = function(res){
                    var music = null;
                    var playIndex = playControl.getCurrentPlayIndex();
                    if(res.resultCode == 0){
                        music = playControl.getPlayList()[playIndex];
                    }
                    if(music){
                        $("#currentMediaInfo").html(music.Name);                        
                        highlightPlay();
                    }
                    if(playControl.isPlaying){
                        winPlayer.addDiskBehavior(7005, 11, 20);
                    }
                };
                //暂停
                playControl.onPause = function() {
                    replaceClass("aPlayOrPause", "mp-pause", "mp-paly");
                    replaceClass("iStatus", "mp-flash", "mp-loading");
                };
                //停止前
                playControl.onStop = function() {
                    $("#currentMediaInfo").html(top.MusicPlayer.Messages.defaultTitle);
                    $("#aCurrentProgress").width(0);                    
                    replaceClass("aPlayOrPause", "mp-pause", "mp-paly");
                    replaceClass("iStatus", "mp-flash", "mp-loading");
                    highlightPlay(true);
                }
                //显示时间
                playControl.onDisplayPlaying = function(cp, cpStr, media) {                
                    var maxLength = 128;
                    if(media != null){                    
                        //var name = playControl.getPlayList()[playControl.getCurrentPlayIndex];
                        //$("#currentMediaInfo").html(media.name);
                        var actual = (maxLength * cp) / media.duration;
                        if(isNaN(actual)){
                            actual = 0;
                        }
                        $("#aCurrentProgress").width(actual);
                    }
                    else {
                        $("#currentMediaInfo").html(top.MusicPlayer.Messages.defaultTitle);
                        $("#aCurrentProgress").width(0);
                    }
                };               
                
                $("#aPlayOrPause").click(function(e) {
                    playControl.togglePlayPause(true);
                    e.preventDefault();
                });
                $("#aPrev").click(function(e) {
                    playControl.prev();
                    e.preventDefault();
                });
                $("#aNext").click(function(e) {
                    playControl.next();
                    e.preventDefault();
                });
                
                var sid = top.UserData.ssoSid;
                
                var showClassInfo = function(cid){
                    $("#listbody>dl").removeClass("pl-current");                    
                    var fragment = $("#listbody>dl[classid='" + cid + "']")
                        .addClass("pl-current");

                    if(!fragment.data("isload")){
                        var config = {
                            name : sid,
                            cid  : cid
                        };
                        $.postXml({
                            url : "/Ajax/GetMusicList.ashx",
                            data: top.MusicPlayer.MusicWin.XmlUtility.parseFromJson(config),
                            success: function(result){                    
                                if(result.resultCode == 0){
                                    var ul = $("#listbody>dl[classid='" + cid + "']>dd>ul");
                                    ul.empty();
                                    $.each(result.resultData.File.Rows, function(){
                                        var file = this;
                                        var li = "<li><input type='checkbox' /><label>{0}</label></li>"
                                            .format(file.SRCFILENAME);
                                        $(li).data("data", file)
                                            .appendTo(ul);                                                        
                                    });                                    
                                    fragment.data("isload", true);
                                }
                            },
                            error: function(error){
                                //todo 处理错误信息
                                alert(error);
                            }
                        });
                    }
                };
                
                //-------外观行为
                //显示添加列表
                $("#aAdd").click(function(e){
                    var isExpand = (window.top.$("iframe#playerframe").css("height") == "22px"
                        || $("#listbody").empty().data("status") == "play");
                    
                    if(isExpand){
                        //加载音乐盒的内容
                        var listbody = $("#listbody").empty().data("status", "class");
                        //加载全部专辑
                        $.postXml({
                            url : "/Ajax/GetMusicList.ashx",
                            data: top.MusicPlayer.MusicWin.XmlUtility.parseFromJson({
                                name            : sid,
                                dontloadfile    : "yes",
                                clsinfo         : "yes"
                            }),
                            success: function(result){                    
                                if(result.resultCode == 0) {
                                    var allClass = result.resultData.Class;
                                    allClass.Rows.push({
                                        CLASSID : "",
                                        CLASSNAME : "我的音乐"
                                    });
                                    $.each(allClass.Rows, function(index){
                                        var data = this;
                                        var fragment = "<dl classid='{1}'>" +
                                            " <dt><i class='mp-icon mp-status'></i><span>{0}</span></dt>  " +
                                            " <dd><ul></ul></dd></dl> ";
                                        fragment = $(fragment.format(data["CLASSNAME"], data["CLASSID"]))
                                            .data("data", data)
                                            .appendTo($("#listbody"));
                                        
                                        fragment.find("dt").click(function(){
                                            var dl = $(this).parent();
                                            $("#controlRecord>:checkbox").attr("checked", false);
                                            if(dl.hasClass("pl-current")){
                                                dl.removeClass("pl-current");
                                            }
                                            else {
                                                showClassInfo(dl.attr("classid"));
                                            }
                                        });                                    
                                        //再加载第一张专辑歌曲
                                        if(index == 0){                                        
                                            showClassInfo(data["CLASSID"])
                                        }
                                    });
                                }
                                else {
                                    //服务器抛出异常
                                    alert(result.errorMsg);
                                }
                            },
                            error: function(error){
                                //todo 处理错误信息
                                alert(error);
                            }
                        });
                        window.top.$("iframe#playerframe").css("height","215px");    
                        $(".pl-ft").hide();
                        $("#controlRecord").show();
                    }
                    else {
                        window.top.$("iframe#playerframe").css("height","22px");
                    }
                    e.preventDefault();
                });
                //添加列表的全选
                $("#controlRecord>:checkbox").click(function(){
                    var checked = this.checked;
                    $("#listbody>dl.pl-current>dd>ul>li>:checkbox").attr("checked", checked);
                });
                //添加列表的取消
                $("#aRecordCancel").click(function(){
                    window.top.$("iframe#playerframe").css("height","22px");
                });
                //把选中的添加到播放列表
                $("#aRecordAdd").click(function(){
                    var checkedItem = $("#listbody :checked");
                    if(checkedItem.length == 0){
                        top.FloatingFrame.alert(top.MusicPlayer.Messages.atLeastOne);
                        return;
                    }    
                    var checkedlist = [];
                    checkedItem.each(function(){
                        var li = $(this).parent();
                        var data = li.data("data");
                        checkedlist.push({
                            Name    :data["SRCFILENAME"],
                            FileId  :data["FILEID"]
                        });
                    });
                    top.MusicPlayer.PlayControl.appendPlayList(checkedlist, function(){
                        loadPlayListUI();
                        $(".pl-ft").hide();
                        $("#controlPlaylist").show();
                    });
                });
                //显示播放列表
                $("#aToggleList").click(function(e){
                    var isExpand = (window.top.$("iframe#playerframe").css("height") == "22px"
                        || $("#listbody").data("status") == "class");
                    if(isExpand) {
                        $("#listbody").data("status", "play");
                        loadPlayListUI();
                        window.top.$("iframe#playerframe").css("height","215px");    
                        $(".pl-ft").hide();
                        $("#controlPlaylist").show();
                    }
                    else {
                        window.top.$("iframe#playerframe").css("height","22px");
                    }   
                    e.preventDefault();
                });
                //删除所选的歌曲
                $("#aPlayListRemove").click(function(e){
                    var li = $("#listbody>dl>dd>ul>li");
                    
                    if(li.find(":checked").length == 0){
                        return;
                    }
                    var playing = top.MusicPlayer.PlayControl.isPlaying;
                    
                    if(li.find(":checkbox").length == li.find(":checked").length){
                        //删除全部,相等于清空
                        $("#aPlayListClear").click();
                    }
                    else {
                        var removeIds = [];
                        var indexs = [];
                        li.find(":checked").each(function(){
                            var temp = $(this).parent();
                            removeIds.push(temp.data("data").Id);
                            indexs.push(temp.data("index"));                            
                        });
                        top.MusicPlayer.PlayControl.removePlayList(removeIds, function(){
                            loadPlayListUI(false, function() {
                                if(playing){
                                    var currentIndex = top.MusicPlayer.PlayControl.getCurrentPlayIndex();
                                    var replacePlay = false;
                                    $.each(indexs, function(){
                                        if(this == currentIndex) {                              //如果删除了正在播放的歌曲
                                            top.MusicPlayer.PlayControl.play(currentIndex);     //再次播放当前位置歌曲
                                            replacePlay = true;
                                            return false;           
                                        }
                                    });
                                    if(!replacePlay){
                                        
                                    }
                                }                                
                            });                            
                        });
                    }
                });
                //清空播放列表
                $("#aPlayListClear").click(function(e){                  
                    top.MusicPlayer.PlayControl.clearPlayList(function(){
                        //停止播放                          
                        top.MusicPlayer.PlayControl.stop();
                        var listbody = $("#listbody").empty();
                        var htmlTemplate = "<dl class='pl-current'>"
                            + "<dt><i class='mp-icon mp-status'></i><span>播放列表</span></dt>"
                            + "<dd><ul></ul></dd></dl>";
                        htmlTemplate = $(htmlTemplate).appendTo(listbody);
                    });                    
                });
            }
        }        
    };

    var PlayState = {
        //0=初始化,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
        Initial     :0,
        Stop        :1,
        Pause       :2,
        Play        :3,
        Buffer      :6,
        Connecting  :9,
        Ready       :10
    };

    var BrowserFlag = {
        IE : "ie",
        FF : "ff",
        Opera : "opera",
        Safari : "safari",
        Chorme : "chorme",
        autoDetect : function(){
            if($.browser.safari) return BrowserFlag.Safari;
            if($.browser.opera) return BrowserFlag.Opera;
            if($.browser.msie) return BrowserFlag.IE;
            if($.browser.mozilla) return BrowserFlag.FF;
        }
    }

    
    var PlayControl = function(element, browserFlag){
        var tempObj = this;
        
        this.__wmp = (typeof element == "string")?top.MusicPlayer.MusicWin.document.getElementById(element):element;
        this.__browserFlag = browserFlag || BrowserFlag.autoDetect();
        this.__playlist = [];
        this.__currentPlayIndex = 0;
        this.__tasks = [];
        //-----定义事件
        //显示当前时间;function(currentPosition:double, currentPositionString:string, media)
        this.onDisplayPlaying = null;
        //播放前;function(obj:{Name:xxx, Ur;yyy})
        this.onBeforePlay = null;
        //播放後
        this.onAfterPlay = null
        //暂停
        this.onPause = null;
        //停止
        this.onStop = null;
        //显示弹出信息
        this.onAlert = function(msg){
            //alert(msg);
        };
        //是否正在播放中
        this.isPlaying = false;
        //音量改变时;function(volumn:int)
        this.onVolumnChanged = null;
        //销毁
        this.dispose = function() {
            tempObj.stop();
            tempObj.stopTimer();
            for(var name in tempObj){
                tempObj[name] = null;
            }
        };
        
        this.__isInitialValid = function(){
            var isValid = this.__wmp && this.__browserFlag;
            if(!isValid && this.onAlert){
                this.onAlert(top.MusicPlayer.Messages.initialFail);
            }
            return isValid;
        };
        if(this.__isInitialValid()){         
            
            //-------公开属性
            this.getCurrentPlayIndex = function(){
                return this.__currentPlayIndex;
            };
            this.setCurrentPlayIndex = function(index){
                if(index < 0){
                    index = 0;
                }
                else if(index >= this.__playlist.length){
                    index = this.__playlist.length - 1;
                }
                this.__currentPlayIndex = index;
            };
            //音量
            this.getVolumn = function(){
                return this.__wmp.settings.volume;
            };
            //是否静音
            this.getMute = function(){
                return this.__wmp.settings.mute;
            }
            //媒体信息
            this.currentMedia = function(){
                return this.__wmp.currentMedia;
            };
            //获取WMP对象
            this.getWmp = function(){
                return this.__wmp;
            };
            //播放是否全部循环
            this.isRepeatAll = true;
            //是否播放列表
            this.isPlayLoop = false;
            //定时器
            this.internalTimer = null;  
            //读取播放列表
            this.loadPlayList();
            
            //启动定时器
            this.startTimer = function(){
                if(this.internalTimer == null){                
                    var thisObj = this;
                    thisObj.internalTimer = window.setInterval(function(){
                        for(var i=0; i < thisObj.__tasks.length;i++) {
                            thisObj.__tasks[i](thisObj);
                        }
                    }, 1000);
                }
            };
            
            //停止定时器
            this.stopTimer = function(){
                if(this.internalTimer != null){
                    window.clearInterval(this.internalTimer);
                    this.internalTimer = null;
                }
            };
            
            //-------初始化设置
            this.__tasks.push(function(sender){
                //显示播放时间
                if(sender.onDisplayPlaying){
                    sender.onDisplayPlaying(sender.__wmp.controls.currentPosition
                        , sender.__wmp.controls.currentPositionString
                        , sender.currentMedia());
                }
            });
            this.__tasks.push(function(sender){
                
                //播放列表
                if(sender.isPlayLoop){                
                    if(sender.__wmp.playState == PlayState.Ready || sender.__wmp.playState == PlayState.Stop){
                        if(sender.__currentPlayIndex >= sender.__playlist.length - 1 && !sender.isRepeatAll){
                            //是否为最后一首
                            sender.__currentPlayIndex = 0;
                            sender.stop();
                        }
                        else {
                            //选下一首
                            sender.next();
                        }
                    }
                }
            });
        }
    };

    /*===== 播放列表 =====*/

    //设置播放列表。
    //元素为{Name:xxx, Id:yyy, FileId:zzz}
    PlayControl.prototype.setPlayList = function(playlist){
        if(playlist){
            this.__playlist = playlist;
        }
    };
    
    //加载播放列表
    PlayControl.prototype.loadPlayList = function(callback){    
        var playControl = this;
        ajax({
            "name": top.UserData.ssoSid,
            "act" : "getlist"
        }, function(res){
            if(res.resultCode == 0){
                res = res.resultData;
                playControl.__playlist = top.$.map(res, function(ele){
                    return {
                        "Id"        : ele.Id,
                        "FileId"    : ele.FileId,
                        "Name"      : ele.FileName
                    }
                });                
                if(callback){
                    callback(playControl);
                }
            }
            else {
                alert(res.errorMsg);
            }
        });
    };

    //获取播放列表
    PlayControl.prototype.getPlayList = function(){
        return this.__playlist;
    };

    //添加一个歌曲到播放列表
    PlayControl.prototype.appendPlayList = function(songlist, callback){
        var fids = $.map(songlist, function(ele){
            return ele.FileId;
        });
        if(fids != null && fids.length > 0){
            var playControl = this;
            //往播放列表插入数据        
            ajax({
                "name": top.UserData.ssoSid,
                "act" : "append",
                "fids": fids.join(",")
            }, function(res){
                if(res.resultCode == 0){
                    res = res.resultData;                    
                    if(callback){
                        callback(playControl);
                    }
                }
                else {
                    alert(res.errorMsg);
                }                            
            });
        }
    };
    
    PlayControl.prototype.removePlayList = function(idlist, callback){        
        if(idlist != null && idlist.length > 0){
            var playControl = this;
            //往播放列表插入数据        
            ajax({
                "name": top.UserData.ssoSid,
                "act" : "remove",
                "ids": idlist.join(",")
            }, function(res){
                if(res.resultCode == 0){
                    res = res.resultData;
                    var indexOf = function(arr, ele){
                        var index = -1;
                        $.each(arr, function(i){
                            if(this == ele){
                                index = i;
                                return false;
                            }
                        });
                        return index;
                    };
                    
                    var currentPlayId = playControl.__playlist[playControl.__currentPlayIndex].Id;
                    var newIndex = 0;
                    var index = 0;
                    
                    playControl.__playlist = $.grep(playControl.__playlist, function(ele){
                        if(indexOf(idlist, ele.Id) >= 0){
                            return false;
                        }
                        if(ele.Id == currentPlayId){
                            newIndex = index;
                        }
                        index++;
                        return true;
                    });                    
                    
                    //fix currentPlayIndex                    
                    playControl.__currentPlayIndex = newIndex;

                    if(callback){
                        callback(playControl);
                    }
                }
                else {
                    alert(res.errorMsg);
                }                            
            });
        }
    };

    //清除所有歌曲列表
    PlayControl.prototype.clearPlayList = function(callback){
        var playControl = this;
        ajax({
            "name": top.UserData.ssoSid,
            "act" : "empty"
        }, function(res){
            if(res.resultCode == 0){                
                this.__playlist = [];
                this.__currentPlayIndex = 0;
                if(callback){
                    callback(playControl);
                }
            }
            else {
                alert(res.errorMsg);
            }
        });
    };

    /*===== 播放控制 =====*/

    //播放列表,循环播放
    PlayControl.prototype.playLoop = function(startIndex, callback){
        this.isPlayLoop = true;
        this.play(startIndex, callback);
    };

    //播放当前曲目
    PlayControl.prototype.play = function(index, callback){  
        if(index == null){
            index = this.__currentPlayIndex;
        }    
        //启动定时器
        this.startTimer();
        
        var max = this.__playlist.length;
        if(index >= max || index < 0){
            index = 0;
        }
        var pc = this;
        if(this.__playlist[index] != null){
            if(this.playpause) {
                this.__currentPlayIndex = index;
                this.__wmp.controls.play();
                this.isPlaying = true;
                this.playpause = false;
                if(callback){
                    callback(pc);
                }
            }
            else {
                //Ajax生成文件Url
                ajax({
                    "name"  : top.UserData.ssoSid,
                    "act"   : "url",
                    "fid"   : this.__playlist[index].FileId
                }, function(res){
                    if(res.resultCode == 0){
                        if(res.resultData.Code == 1){
                            var url = res.resultData.Url;
                            pc.__wmp.URL = url;
                            pc.__currentPlayIndex = index;
                            pc.__wmp.controls.play();
                            pc.isPlaying = true;
                        }
                    }
                    else {
                        alert(res.errorMsg);
                    }                    
                    if(callback){
                        callback(pc);
                    }
                    
                    if(pc.onAfterPlay){                        
                        pc.onAfterPlay(res);
                    }
                });
            }
        }
        if(pc.onBeforePlay != null) {
            pc.onBeforePlay(pc.__playlist[index]);
        }
    }

    //暂停当前播放
    PlayControl.prototype.pause = function(){
        if(this.__wmp.playState == PlayState.Play){
            //停止定时器
            this.stopTimer();
            this.__wmp.controls.pause();
            this.playpause = true;
            this.isPlaying = false;
            if(this.onPause != null) {
                this.onPause(this.__playlist[this.__currentPlayIndex]);
            }
        }
    }

    //暂停时播放,播放时暂停
    PlayControl.prototype.togglePlayPause = function(isPlayLoop){        
        switch(this.__wmp.playState){
            case PlayState.Play : {
                this.pause();
                break;
            }
            case PlayState.Initial:
            case PlayState.Pause:
            case PlayState.Stop:
            case PlayState.Ready:{
                if(isPlayLoop){
                    this.playLoop();
                }
                else {
                    this.play();
                }
            }
        }
    };

    //停止当前播放
    PlayControl.prototype.stop = function(){
        //停止定时器
        this.stopTimer();
        this.__wmp.controls.stop();
        this.__isPlayLoop = false;  
        this.isPlaying = false;  
        if(this.onStop != null) {
            this.onStop();
        }
    }

    //播放下一首
    PlayControl.prototype.next = function(){
        if(this.__playlist.length == 0){
            this.stop();
        }
        
        this.stopTimer();
        var index = this.__currentPlayIndex;
        index++;
        if(index >= this.__playlist.length || index < 0){
            index = 0;
        }
        
        var temp = this;
        window.setTimeout(function(){
            if(temp && temp.play){
                temp.play(index);
            }
        }, 500);
    }

    //播放上一首
    PlayControl.prototype.prev = function(){
        if(this.__playlist.length == 0){
            this.stop();
        }
        
        this.stopTimer();
        var listLenght = this.__playlist.length;
        var index = this.__currentPlayIndex;
        index--;
        if(index >= listLenght || index < 0){
            index = listLenght - 1;
        }    
        var temp = this;
        window.setTimeout(function(){    
            if(temp && temp.play){
                temp.play(index);
            }
        }, 500);
    }

    //控制音量
    PlayControl.prototype.setVolumn = function(volumn){
        if(volumn == null) {
            volumn = 50;
        }
        else if(volumn < 0){
            volumn = 0;
        }
        else if(volumn > 100){
            volumn = 100;
        }
        this.__wmp.settings.volume = volumn;
        if(this.onVolumnChanged != null) {
            this.onVolumnChanged(volumn);
        }
    };

    //增加音量
    PlayControl.prototype.addVolumn = function(step){
        this.setVolumn(this.getVolumn() + step);
    }

    //减少音量
    PlayControl.prototype.minusVolumn = function(step){
        this.setVolumn(this.getVolumn() - step);
    }

    //设置静音
    PlayControl.prototype.setMute = function(isMute){
        this.__wmp.settings.mute = isMute;
    };

    //静音启动/停止
    PlayControl.prototype.toggleMute = function(){
        this.__wmp.settings.mute = !this.__wmp.settings.mute;
    };
    
    var ajax = function(data, successCallback){
        top.MusicPlayer.MusicWin.$.ajax({
            type    :"POST",
            async   : true,
            dataType: "json",
            url     : top.MusicPlayer.Constants.DiskHost + "/Ajax/ExecMusicPlayList.ashx",
            data    : data,
            success : successCallback
        });
    };
    
    var FF = top.FloatingFrame;    
    var validCode = window.top.MusicPlayer.validShowUI(true);    
    
    window.top.MusicPlayer.loadMusicPlayerPage(true, function(){
        var jq = window.top.MusicPlayer.MusicWin.$;
        switch(validCode){
            case 1:{
                jq(".player").hide().remove();
                jq("#msg").html(window.top.MusicPlayer.Messages.failBrowser);
                break;
            }
            case 3:{
                break;
            }
        }
    });
    
})();