// JavaScript Document
document.domain = "myspace.cn";

var SKIN_TYPE = {
	user:"profile",
	user2:"profile2",
	artist:"artist",
	popout:"popout"
};
var MusicFlashPlayer = null;
var MyspacePlayer = {};
var loparaMeter={"userID":null, "profileID":null};
var loparaMeter_init={"userID":null, "profileID":null};
var moParameter = null;
var url_search = "";
var count = 0;
//初始化wmp

initWindowsMediaPlayer();
callInitMusicFlashPlayer();

/***********************************************************************************************/
///
/// @ 初始化
///
function callInitMusicFlashPlayer(){
	//第一次
	//alert(loparaMeter.userID);
	if(loparaMeter_init.userID == null && loparaMeter_init.profileID == null){
		if(typeof isProfile!="undefined" && isProfile){
			var local_search = window.location.search;
			loparaMeter.userID = loparaMeter_init.userID = getParameter("userID", local_search);
			loparaMeter.profileID = loparaMeter_init.profileID =  getParameter("profileID", local_search);
			//window.location.search = local_search.replace(/profileID=\w*&?/,"");
		}else{
			loparaMeter.userID = loparaMeter_init.userID = getParameter("userID", null);
			loparaMeter.profileID = loparaMeter_init.profileID =  getParameter("profileID", null);
		}
		initMusicFlashPlayer_bridge();	
		return;	
	}
	//ID换了
	//alert(loparaMeter.userID+"!="+loparaMeter_init.userID +"||" + loparaMeter.profileID+"!="+loparaMeter_init.profileID);
	if((loparaMeter.userID && loparaMeter.userID!=loparaMeter_init.userID) || (loparaMeter.profileID && loparaMeter.profileID!=loparaMeter_init.profileID)){
		loparaMeter_init.userID = loparaMeter.userID;
		loparaMeter_init.profileID = loparaMeter.profileID;
		initMusicFlashPlayer();
		if(moParameter){
			notifyPlayer(moParameter);
		}
		return;
	}
}
var isParamready = false;
var Timer2 = null;
var count2 = 0;
function initMusicFlashPlayer_bridge(){
	if(count2 == 3000){
		clearTimeout(Timer2);
		return;
	}
	if(isParamready){
		initMusicFlashPlayer();
		clearTimeout(Timer2);
	}else{
		count2++;
		Timer2 = setTimeParam(initMusicFlashPlayer_bridge);
	}
}
function initMusicFlashPlayer(){
	var lsPlayerType = getParameter("type", null);
	var lsInnerFrom = getParameter("from",location.search);
	if(typeof isProfile!="undefined" && isProfile){
		if(lsInnerFrom!=SKIN_TYPE.user2){
			createPlayerByType(lsPlayerType||"profile", loparaMeter);			
		}else{
			loparaMeter["skin"]="http://lads.myspace.cn/music/v3/skins/user_skin_4.swf";
			createPlayerByType(SKIN_TYPE.user2, loparaMeter);
		}
		
		return;
	}
	if(lsPlayerType != ""){
		createPlayerByType(lsPlayerType, loparaMeter);
	}else{
		createPlayerByType(SKIN_TYPE.popout, loparaMeter);
		//createPlayerByType(SKIN_TYPE.popout, {});
	}
}

function createPlayerByType(asPlayerType, asParaMeter){
	//swf
	var v = "v=20090612";
	if(IECore_Browser_lj()){
		v =  "v=" + (new Date()).toString().split(" ").join("");
	}
	if(asPlayerType == SKIN_TYPE.user){
		//createPlayer_User();
		var url="userid="+asParaMeter.userID+"&profileID="+asParaMeter.profileID;
		createPlayerByString("http://lads.myspace.cn/music/v3/musicPlayer.swf?"+ v, "MusicPlayer_SWF", 300, 400, url);
	}else if(asPlayerType == SKIN_TYPE.artist){
		//createPlayer_Artist();
		var url="bandtype=1&userid="+asParaMeter.userID+"&profileID="+asParaMeter.profileID;
		createPlayerByString("http://lads.myspace.cn/music/v3/musicPlayer.swf?"+ v, "MusicPlayer_SWF", 450, 400, url);
	}else if(asPlayerType == SKIN_TYPE.popout){
		//createPlayer_Popout();
		var url="userid="+asParaMeter.userID +"&profileID="+asParaMeter.profileID;
		createPlayerByString("http://lads.myspace.cn/music/v3/popoutPlayer.swf?"+ v, "MusicPlayer_SWF", 480, 480, url);
	}else if(asPlayerType == SKIN_TYPE.user2){
		//createPlayer_Popout();
		var url="userid="+asParaMeter.userID +"&profileID="+asParaMeter.profileID+"&skin="+asParaMeter.skin;
		createPlayerByString("http://lads.myspace.cn/music/v3/musicPlayer.swf?"+ v, "MusicPlayer_SWF", 300, 120, url);
	}
}

function createPlayerByString(asSwfUrl, asDomId, asWidth, asHeight, asQuery){
	var MediaPlayerSO = new SWFObject(asSwfUrl, asDomId, asWidth, asHeight, "9.0.0", "#ffffff");
	MediaPlayerSO.addParam("allowScriptAccess", "always");
	MediaPlayerSO.addParam("allowNetworking", "all");
	//MediaPlayerSO.addParam("wmode", "transparent");
	MediaPlayerSO.addParam("wmode", "opaque");
	//
	var liIndex = asQuery.indexOf("?");
	var lsQuery = (liIndex > -1) ? asQuery.substring(liIndex + 1) : asQuery;
	var lxQuery = lsQuery.split("&");
	//
	for (var i = 0; i < lxQuery.length; i++) {
		var lxKeyValue = lxQuery[i].split("=");
		MediaPlayerSO.addVariable(lxKeyValue[0], lxKeyValue[1]);
	}
	//wmp是否能播放
	if(typeof(MyspacePlayer.player.controls)=="undefined"){
		MediaPlayerSO.addVariable("wma", false); 
	}else{
		MediaPlayerSO.addVariable("wma", true); 
	}
	//
	var domId = asDomId.replace("_", "");
	
	MediaPlayerSO.write(domId);

	//获得当前swf的句柄
	MusicFlashPlayer = getSWF(asDomId);
}

var Timer = null;
function getParameter(asName, asSesrch){
	if(count == 6000){
		clearTimeout(Timer);
		return;
	}
	try{
		var lsSearch = asSesrch || window.parent.location.search;
		var reg = new RegExp("(^|&|\\?|\\s)"+ asName +"\\s*=\\s*([^&]*?)(\\s|&|$)", "i");	
		clearTimeout(Timer);
		isParamready = true;
		if (reg.test(lsSearch)) return RegExp.$2.replace(/[\x0f]/g, ";"); return "";
	}catch(e){
		count++;
		Timer = setTimeParam(getParameter, 300, asName, asSesrch);
	}

}
 function setTimeParam(aoHandler, aiTimeout, param){
	var args = Array.prototype.slice.call(arguments, 2);
	var lohandler = function(){
		aoHandler.apply(null, args);
	}
	return setTimeout(lohandler, aiTimeout);
 }
function getSWF(asName) {
  	if(navigator.userAgent.indexOf("IE") != -1) {
  		return window[asName];
	}
 	else{
 		return document[asName];
	}
}

function isReady(){
	return true;
}
/***********************************************************************************************/
///
/// @ JS 控制 FLASH 播放音乐
///
var moPlayparaMeter=null;
var count_flash_ready = 0;
var Timer_flash_ready = null;
function notifyPlayer(ao){
	if(count_flash_ready == 3000){
		clearTimeout(Timer_flash_ready);
		return false;
	}
	if (ao==null||typeof(MusicFlashPlayer)=="undefined"||MusicFlashPlayer==null || !MusicFlashPlayer.check()){
		moPlayparaMeter=ao;
		count_flash_ready++;
		Timer_flash_ready = setTimeout("notifyPlayer(moPlayparaMeter)",200);
		return false;
	}
	moPlayparaMeter=null;
	playMusic(ao);
	return true;
}
function playMusic(ao){
			var liuserID = ao.userID;
			var liplayListID = ao.playListID;
			
			var lstype=ao.type||"list";
			if (liplayListID==0){lstype="song";}
			if(lstype=="song"){  		//歌曲   playListID=0 为临时歌单
				playSong(ao.songIDs);
			}else if(lstype=="list"){		//歌单
				playSongList(liuserID,liplayListID);
			}

}
/**
 * 播放歌曲列表
 * @param axSongIDs<Array>：	歌曲列表的数据结构：bandID_songID_albumID的集合
 * js call flash
 */
function playSong(axSongIDs){
	var songIDs = [];
	for (var i=0; i<axSongIDs.length; i++){
		//lxSongIDs[lxSongIDs.length] = axSongIDs[i];
		songIDs.push(axSongIDs[i]);
	}
	try{
		if (typeof(MusicFlashPlayer)!="undefined" && MusicFlashPlayer != null){
			MusicFlashPlayer.playBySongIDList(songIDs);
		}
	}catch(e){}
}
/**
 * 播放歌单
 * @param aiUserID<Number>：	艺术家的uid(在player里面需要转跳的uid)
 * @param aiListID<Number>：	此歌单的playListID
 */
function playSongList(aiUserID, aiListID){
	try{
		if (typeof(MusicFlashPlayer) != "undefined" && MusicFlashPlayer != null){
			MusicFlashPlayer.playByPlaylist(aiUserID, aiListID);
		}
	}catch(e){}
}

//new
function setUserInfo(ao){
	if(ao.userID != null){
		loparaMeter.userID = ao.userID;
	}
	if(ao.profileID != null){
		loparaMeter.profileID = ao.profileID;
	}
	return ao;
}
/***********************************************************************************************/
///
/// @ Windows Media Player Control Model
///
function initWindowsMediaPlayer(){
	MyspacePlayer.player = {};
	MyspacePlayer.control = {};
	MyspacePlayer.events = {};
	
	//wmp
	MyspacePlayer.player = document.getElementById("MediaPlayerObject"); 
	
	MyspacePlayer.control.setURL = function(value){
		MyspacePlayer.player.URL = value;
	};
	MyspacePlayer.control.clear = function(){
		//TODO: clear all media reference.
	};
	MyspacePlayer.control.play = function(){
		if(MyspacePlayer.player.controls.isAvailable('Play')){
			MyspacePlayer.player.controls.play();
		}
	};
	MyspacePlayer.control.stop = function(){
		if(MyspacePlayer.player.controls.isAvailable('Stop')){
			MyspacePlayer.player.controls.stop();
		}
	};
	MyspacePlayer.control.pause = function(){
		if(MyspacePlayer.player.controls.isAvailable('Pause')){
			MyspacePlayer.player.controls.pause();
		}
	};
	//返回当前播放进度(秒)
	MyspacePlayer.control.getCurrentPosition = function(){
		return MyspacePlayer.player.controls.currentPosition;
	};
	//设置当前播放进度(秒)
	MyspacePlayer.control.setCurrentPosition = function(value){
		if (!isNaN(value) && (value != '')){
			MyspacePlayer.player.controls.currentPosition = value;
		}
	};
	//0-100
	MyspacePlayer.control.getVolume = function(){
		return MyspacePlayer.player.settings.volume;
	};
	//0-100
	MyspacePlayer.control.setVolume = function(value){
		if (!isNaN(value) && (value != '')){
			MyspacePlayer.player.settings.volume = value;
		}
	};
	//Boolean
	MyspacePlayer.control.mute = function(value){
		MyspacePlayer.player.settings.mute = value;
	};
	//总时长(秒)
	MyspacePlayer.control.getDuration = function(){
		return MyspacePlayer.player.currentMedia.duration;
	};
	//当前加载百分比(0-100)
	MyspacePlayer.control.getDownloadProgress = function(){
		return MyspacePlayer.player.network.downloadProgress;
	};
}

/**
 * flash call js
 */
var popupWindow_asTjs = null;
function openPlayer_asTjs(aiProfileID){
	paraMeter={"userID":0, "profileID":aiProfileID||0};
	var playerUrl="http://music.myspace.cn/player_bridge.php?profileID="+aiProfileID;
	var playerWinName="music_hide";
	var lswinvalues="alwaysRaised=yes,width=820,height=555,resizable=yes";
	if(window.open){
		if(popupWindow_asTjs!=null){
			try{
				//popupWindow_asTjs.parent.notifyPlayer(paraMeter);
				popupWindow_asTjs=window.open(playerUrl,playerWinName,lswinvalues);
				if(popupWindow_asTjs.top){
					popupWindow_asTjs.top.focus();
				}
				notifyMediate_asTjs();
			}catch(e){
				popupWindow_asTjs=window.open(playerUrl,playerWinName,lswinvalues);
				if(popupWindow_asTjs.top){
					popupWindow_asTjs.top.focus();
				}
				notifyMediate_asTjs();
			}
			popupWindow_asTjs.focus();
		}else{
			popupWindow_asTjs=window.open(playerUrl,playerWinName,lswinvalues);
			if(popupWindow_asTjs.top){
				popupWindow_asTjs.top.focus();
			}
			notifyMediate_asTjs();
		}
	}else{
		openByForm_asTjs("get",paraMeter);
	}
}
var Timer_asTjs = null;
var count_asTjs = 0;
function notifyMediate_asTjs(){
	if(count_asTjs == 3000){
		if(Timer_asTjs){clearTimeout(Timer);}
		return;
	}
	if(typeof(popupWindow.parent.window.mediateIsLoad!="undefined")&&popupWindow.parent.window.mediateIsLoad){
		popupWindow.parent.moParameter = paraMeter;
		popupWindow.parent.notifyPlayer(paraMeter);
		if(Timer_asTojs){clearTimeout(Timer);}
	}else{
		count_asTjs++;
		Timer_asTjs = setTimeout(notifyMediate,300); //需要用全局变量
	}	
}
/*第二种方式——通过临时form提交*/
/*问题是如何保证不刷新页面*/
function openByForm_asTjs(type,ao){
	var leForm=document.createElement("form");
	leForm.method="get"; //post
	
	leForm.target=playerWinName;
	leForm.action=playerUrl;
	ao.playerType="formPlayer";	
	for(var x in ao){
		var leTypeInput=document.createElement("input");
		leTypeInput.name=x;
		leTypeInput.value=ao[x];
		leForm.appendChild(leTypeInput);
	}
	document.body.appendChild(leForm);
	leForm.submit();
	document.body.removeChild(leForm);
}
function Browser_lj(){
	var userAgent = navigator.userAgent.toLowerCase(); 
	return {
		ver: (userAgent.match( /.+(?:ox|me|ra|ie|on)[\/: ]([\d.]+)/ ) || [])[1], 
		sa: /webkit/.test( userAgent ), 
		op: /opera/.test( userAgent ), 
		ie: /msie/.test( userAgent ) && !/opera/.test( userAgent ), 
		ff: /mozilla/.test(userAgent)&&!/(compatible|webkit)/.test(userAgent),
		ch: /chrome/.test(userAgent)
	}
}
function IECore_Browser_lj(){
	var browser = Browser_lj();
	if(browser.ie){
		if(typeof navigator.userProfile!="undefined" && navigator.userProfile!=null){
			return false;//IE
		}else{
			return true;
		}	
	}
	return false;
}