function Music() { this.init(); } (function () { var pages = [], panels = [], selectedItem = null; Music.prototype = { total: 70, pageSize: 10, dataUrl: "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.search.common", playerUrl: "http://box.baidu.com/widget/flash/bdspacesong.swf", init: function () { var me = this; domUtils.on($G("J_searchName"), "keyup", function (event) { var e = window.event || event; if (e.keyCode == 13) { me.dosearch(); } }); domUtils.on($G("J_searchBtn"), "click", function () { me.dosearch(); }); }, callback: function (data) { var me = this; me.data = data.song_list; setTimeout(function () { $G('J_resultBar').innerHTML = me._renderTemplate(data.song_list); }, 300); }, dosearch: function () { var me = this; selectedItem = null; var key = $G('J_searchName').value; if (utils.trim(key) == "") return false; key = encodeURIComponent(key); me._sent(key); }, doselect: function (i) { var me = this; if (typeof i == 'object') { selectedItem = i; } else if (typeof i == 'number') { selectedItem = me.data[i]; } }, onpageclick: function (id) { var me = this; for (var i = 0; i < pages.length; i++) { $G(pages[i]).className = 'pageoff'; $G(panels[i]).className = 'paneloff'; } $G('page' + id).className = 'pageon'; $G('panel' + id).className = 'panelon'; }, listenTest: function (elem) { var me = this, view = $G('J_preview'), is_play_action = (elem.className == 'm-try'), old_trying = me._getTryingElem(); if (old_trying) { old_trying.className = 'm-try'; view.innerHTML = ''; } if (is_play_action) { elem.className = 'm-trying'; view.innerHTML = me._buildMusicHtml(me._getUrl(true)); } }, _sent: function (param) { var me = this; $G('J_resultBar').innerHTML = '
'; utils.loadFile(document, { src: me.dataUrl + '&query=' + param + '&page_size=' + me.total + '&callback=music.callback&.r=' + Math.random(), tag: "script", type: "text/javascript", defer: "defer" }); }, _removeHtml: function (str) { var reg = /<\s*\/?\s*[^>]*\s*>/gi; return str.replace(reg, ""); }, _getUrl: function (isTryListen) { var me = this; var param = 'from=tiebasongwidget&url=&name=' + encodeURIComponent(me._removeHtml(selectedItem.title)) + '&artist=' + encodeURIComponent(me._removeHtml(selectedItem.author)) + '&extra=' + encodeURIComponent(me._removeHtml(selectedItem.album_title)) + '&autoPlay=' + isTryListen + '' + '&loop=true'; return me.playerUrl + "?" + param; }, _getTryingElem: function () { var s = $G('J_listPanel').getElementsByTagName('span'); for (var i = 0; i < s.length; i++) { if (s[i].className == 'm-trying') return s[i]; } return null; }, _buildMusicHtml: function (playerUrl) { var html = ' 12) return s.substring(0, 5) + '...'; if (!s) s = " "; return s; }, _rebuildData: function (data) { var me = this, newData = [], d = me.pageSize, itembox; for (var i = 0; i < data.length; i++) { if ((i + d) % d == 0) { itembox = []; newData.push(itembox) } itembox.push(data[i]); } return newData; }, _renderTemplate: function (data) { var me = this; if (data.length == 0) return '
' + lang.emptyTxt + '
'; data = me._rebuildData(data); var s = [], p = [], t = []; s.push('
'); p.push('
'); for (var i = 0, tmpList; tmpList = data[i++];) { panels.push('panel' + i); pages.push('page' + i); if (i == 1) { s.push('
'); if (data.length != 1) { t.push('
' + (i) + '
'); } } else { s.push('
'); t.push('
' + (i) + '
'); } s.push('
'); s.push('
' + lang.chapter + '' + lang.singer + '' + lang.special + '' + lang.listenTest + '
'); for (var j = 0, tmpObj; tmpObj = tmpList[j++];) { s.push(''); } s.push('
'); s.push('
'); } t.reverse(); p.push(t.join('')); s.push('
'); p.push('
'); return s.join('') + p.join(''); }, exec: function () { var me = this; if (selectedItem == null) return; $G('J_preview').innerHTML = ""; editor.execCommand('music', { url: me._getUrl(false), width: 400, height: 95 }); } }; })();