var agt = navigator.userAgent.toLowerCase();
var isflash = !(agt.indexOf("msie") != -1);
function _cookie(name,val) 
{
	var date=new Date; 
	var year=date.getFullYear(); 
	var hour=date.getHours(); 
	var cookie="";
	hour=hour+3; 
	date.setHours(hour); 
	cookie=name+"="+val+";expires="+date.toGMTString()+";"; 
	cookie +="domain=99.cn;";
	cookie +="path="+"/;";
	document.cookie=cookie;
}
if(!$.isIE()){
	_cookie("domain","99.cn");
}
var play = {
	p:null,
	lrc:[],
	list:null,
	/*当前播放索引*/
	playIndex:null,
	/*装载播放对象*/
	dragVolume:null,
	dragTop:null,
	initPlayList:function(){
		var g = $("#playList").find("li").element();
		if(g == null){
			return false;
		}
		if(g.length){
			this.list = g;
			for(var n  in g){
				g[n].id = n;
			}
		}else{
			this.list = [g];
			g.id = 0;
		}
	},
	init:function(){
		/*插入播放器控件*/
		this.p;
		if(!isflash){
			this.p = $("#playObject").element();
		}else{
			this.p = mp3player;
		}
		this.controls = this.p.controls;
		this.settings = this.p.settings;
		this.volume = this.settings.volume; 
		this.setVolumeValue(100);
		this.dragVolume = $("#play_dragVolume");
		this.dragTop = $("#play_dragVolume").get("offsetTop");
		this.panel();
		this.initPlayList();
		this.modeMessage = $("#playModeTool").html().split(",");
		$("#playModeTool").html(this.modeMessage[0]);
		this.mode = 0;
	},
	/*初始化面板*/
	panel:function(){
		/*计算偏移值*/
		if($.isIE()){
			this.s = document.body.offsetLeft;
		}else{
			this.s = $.getOffset($("#f").element()).l;
		}
		/*设置进度拖拽*/
		var ma
		var mi
		if($.isIE()){ 
			ma = 216;
			mi = 6;
		}else{
			ma = 230;
			mi = 22;
		}
		$("#play_drag").drag({lock:"y",max:ma+this.s,min:this.s+mi,mouseup:function(){
			play.setPlay(play.d*(($(this).get("offsetLeft")-play.s)/210));
			play.b = true;
			play.setLrcPosition();

		}});
		/*设置音量拖拽*/
		var ma1
		var mi1
		if($.isIE()){
			ma1 = 218;
			mi1 = 178;
			ml1 = 175; 
		}else{
			ma1 = 223;
			mi1 = 178;
			ml1 = 125;
		}
		$("#play_dragVolume").drag({lock:"y",max:ma1+this.s,min:mi1+this.s,mouseup:function(){
			play.setVolumeValue(144-(this.offsetLeft-play.s-236)/(195-236)*100);
		}}).css({left:ml1+this.s+play.volume-6,top:play.dragTop-20});
	},
	/*格式化Irc日期时间*/
	getTime:function(str)
	{
		var time =0;
		var ta = str.split(":");
		if(ta.length<2)
			return time;
		if(ta[1].indexOf(".")>0){
			var tb = ta[1].split(".");
			time = ta[0]*60*1000+tb[0]*1000+tb[1]*10;
		}
		else
			time =ta[0]*60*1000+ta[1]*1000;
		return time/1000;
	},
	
	/*设置IRC位置*/
	setLrcPosition:function(){
		var g; 
		try{
			var i =parseInt(play.c);
			var h = $("#lrc"+i);
			while(h.element().innerHTML.length<0){
				i++;
				h = $("#lrc"+i);
			}
			$("#lrc div").css({color:"#000"});
			for(var n=i;n>0;n--){
				if($("#lrc"+n)){
					$("#lrc"+n).css({color:"#ccc"});
				}
			}
			g = h.css({color:"#ff9900"}).get("offsetTop");
		}catch(e){}
			if(g){
				if(g>100){
				var k
				if($.isIE()){
					k = 70;
				}else{
					k = 218;
				}
				$("#lrc").set("scrollTop",g-k);
			}
		}
	},
	/*显示设置播放进度*/
	show:function(){
		 var g;
		 var h=0;
		/*当前播放到时间*/
		 try {
		 	if(isflash){
				mp3Interval();
			}
			play.c = play.controls.currentPosition;
			/*播放总时长*/
			play.d = play.p.currentMedia.duration;
			if(!isflash){
				g = play.c/play.d*210;
				h = 8;
			}else{
				g = play.p.playbackPercent/100*210
				h = 6;
			}
			var l;
			if($.isIE()){
				l = document.body.offsetLeft;
			}else{
				l = $.getOffset($("#f").element()).l;
			}
			 
			/*设置播放拖动位置*/
			$("#play_drag").change({left:g+l+h},0);
			/*设置播放进度条*/
			$("#play_position").change({width:g},0);
			/*控制Irc显示*/
			play.setLrcPosition();
			/*设置播放器信息*/
			$("#state").html(play.getStateMessage()+play.formatTime(play.c)+" / "+play.formatTime(play.d)+"");
			/*如果播放完毕调整到下一首*/
			/*
			if(!$.isIE()){
				play.p.PlayState = play.p.getPlayState();
			}*/
			if(play.p.PlayState==1){
				if(play.v){
					play.playCount(play.getPlay(play.playIndex,0),play.d,play.d)
					this.v = true;
				}
				/*顺序播放*/
				if(play.mode == 0){
					play.playNext();
				}
				/*随机播放*/
				if(play.mode == 1){
					/*随机取得音乐索引*/
					play.playThis(Math.floor(Math.random()*play.list.length+1));
				}
				/*单曲循环*/
				if(play.mode == 2){
					play.playThis(play.playIndex);
				}
			}
		}catch (e){
		}
	},
	/*获取播放状态*/
	getStateMessage:function(){
		switch (this.p.PlayState){
			case 0:return '正在确定歌曲位置';break;
			case 1:return '停止';break;
			case 2:return '暂停.';break;
			case 3:return '正在播放.';break;
			case 4:return '向前';break;
			case 5:return '向后';break;
			case 6:return '缓冲.';break;
			case 7:return '等待中';break;
			case 8:return '完毕';break;
			case 9:return '正在确定媒体位置';break;
			default:return'...';
		}
	},
	/*格式化日期*/
	formatTime:function(a){    
	    var g=Math.floor(a/60);if(g<10){g="0"+g};
		var h=Math.floor(a%60);if(h<10){h="0"+h};
		return  g+":"+h;
	},
	/*设置播放图片*/
	setPlayImg:function(a){
		$("#play_play").set("src","http://jj.99.cn/imgs/play_"+a+".gif");
	},
	v:true,
	/*停止音乐*/
	doStop:function(){	
		play.playCount(play.getPlay(play.playIndex,0),play.c,play.d);
		play.setPlayUrl(0);

		this.v = false;
		//clearInterval(this.time);
		this.controls.stop();
	
		clearInterval(this.time); 
		var l;
		if($.isIE()){
			l = document.body.offsetLeft;
		}else{
			l = $.getOffset($("#f").element()).l;
		}
		this.setPlayImg("play");
		$("#play_position").change({width:0},0);
		$("#play_drag").change({left:l+11},0);	
		$("#state").html("停止 00:00 / 00:00");
	},
	/*暂停音乐*/
	doPause:function(){
		this.controls.pause();
		this.setPlayImg("play");
	},
	/*播放到指定*时间*位置*/
	setPlay:function(a){
		if(!isflash){
			this.controls.CurrentPosition = a ;
		}else{
			play.p.setCurrentPosition(a);
		}
	},
	/*开始播放*/
	doPlay:function(){
		/*播放音乐*/
		this.controls.play();
		/*显示播放信息*/
		this.time = setInterval(this.show,1000);
		/*设置播放图片为暂停图片*/
		this.setPlayImg("pause");
		
	},
	/*设置音量值*/
	setVolumeValue:function(a){
	    this.settings.volume = a;
	},
	/*设置静音*/
	setVolume:function(){
		if(this.p.settings.volume>0){
			this.setVolumeValue(0);
			$("#play_volume").set("src","http://jj.99.cn/imgs/play_volume_0.gif");
		}else{
			this.setVolumeValue(this.volume);
			$("#play_volume").set("src","http://jj.99.cn/imgs/play_volume.gif");
		}
	},
	/*播放当前音乐*/
	playThis:function(a){
		play.setPlayUrl(a*1);
	},
	/*播放下一首*/
	playNext:function(){
		play.setPlayUrl(play.playIndex*1+1);
	},
	/*播放上一首*/
	playPrevious:function(){
		play.setPlayUrl(play.playIndex*1-1);
	},
	/*获取播放音乐数组*/
	getPlay:function(a,b){
		/*如果到底，从头开始播放*/ 
		if(a == this.list.length){
			a = 0;
		}
		/*如果到头，从尾开始播放*/
		if(a < 0){
			a = this.list.length-1;
		}
		/*设置播放歌曲为当前索引*/
		this.playIndex = a;
		$("#playList").find("li").css("");
		return $(this.list[a]).css("playSelect").find("input").value().split("|")[b];
	},
	/*设置Irc内容*/
	setLrc:function(){
		var g = $("#lrc").html();
		var h = g.split("[");
		var k;
		this.lrc = [];
		for (var n=h.length-1;n>0;n--)
		{
			i = h[n].split("]");
			for(var m=0;m< i.length-1; m++){
				var j= parseInt(this.getTime(i[m].replace("[","")));
				if(!isNaN(j)){	
					if(i[i.length-1]){
						k = i[i.length-1];
					}
					this.lrc[j] = k;
				}		
			}
		} 
		for(var n; n<this.lrc.length; n++){
			if(this.lrc[n])
			this.lrc[n] = "<span id='lrc"+n+"'>"+this.lrc[n]+"</span>"
		}

		var j = this.lrc.join("");

		if(j.length>0){
			$("#lrc").html(this.lrc.join(""));
		}
	},
	/*请求统计*/
	count:function(a){
		/*统计
		var script=document.createElement("script");
		script.type="text/javascript";
		script.id="count"
		script.src=a+"&r="+Math.random(); 
		document.getElementsByTagName("body")[0].appendChild(script);	
		*/
		$("#countIframe").set("src",a+"&r="+Math.random())
	},
	playCount:function(a,b,c){
		var g="/count/count.do?id="+a+"&t=3";
		if(b>0){
			g +="&s="+Math.round(b)+"&a="+Math.round(c)
		}
		this.count(g);
	},
	id:null,
	/*歌曲转换后切换文字图片*/
	lrcIndex:true,
	changePlay:function(){
	 
		var g = play.c
		if(this.id==null){
			this.id = this.getPlay(this.playIndex,0)
		}
		if(g!=null){
			if( g!=0){
				this.playCount(this.id,g,play.d)
			}
		}
		//document.title  = this.getPlay(this.playIndex,7)+"在线试听_"+this.getPlay(this.playIndex,7)+"LRC歌词_"+this.getPlay(this.playIndex,3)+"《"+this.getPlay(this.playIndex,5)+"》_九九音乐"
		$("#playAlbum").find("img").set("src",this.getPlay(this.playIndex,2)).set("alt",this.getPlay(this.playIndex,5));
		$("#playAlbum").find("a").set("href",this.getPlay(this.playIndex,6))
		$("#playSongName").html(this.getPlay(this.playIndex,7));
		$("#playAlbumName").html(playAlbumName+"<a href="+this.getPlay(this.playIndex,6)+">"+this.getPlay(this.playIndex,5)+"</a>");
		$("#playArtistName").html(playArtistName+"<a href="+this.getPlay(this.playIndex,4)+">"+this.getPlay(this.playIndex,3)+"</a>");


		if(!this.lrcIndex){
			$.ajax({type:"GET",url:"/ajax/lyric/"+this.getPlay(this.playIndex,0)+"/",random:false},function(){
				/*歌词不为空*/
				if(this.length>312){
					$("#lrc").html("<pre>"+this+"</pre>");
				}else{
					/*歌词为空*/
					$("#lrc").html("");
				}
				$("#lrc").set("scrollTop",0);
				play.setLrc();
			})
		}else{
			$("#lrc").set("scrollTop",0);
			play.setLrc();
			this.lrcIndex  = false;
		}
		$("#playTool").html("<a href='/lyric/"+this.getPlay(this.playIndex,0)+"/' target='_blank'>"+this.getPlay(this.playIndex,7)+"歌词</a>&nbsp;&nbsp;"+"<a href='/lrc/"+this.getPlay(this.playIndex,0)+".lrc' target='_blank'>LRC歌词</a>");
		$("#playListPanel").set("scrollTop",this.playIndex*18);
		$("#d").html($("#breadcrumb_"+this.getPlay(this.playIndex,0)).html());
		 
	 
		if($("#aIframe").element()){
			$("#aIframe").element().src="http://99.cn/ling/?f=12042&name="+encodeURI(this.getPlay(this.playIndex,7));
		}
		if($("#bIframe").element()){
			$("#bIframe").element().src="http://99.cn/ling/?f=12042&ids=02&viewName=ivr&name="+encodeURI(this.getPlay(this.playIndex,7));
		}
		this.id = this.getPlay(this.playIndex,0)
		this.playCount(this.id)
	},
	setPlayUrl:function(a){	
		/*设置播放地址*/
		this.p.Url = this.getPlay(a,1);
		/*播放歌曲*/
		this.doPlay();
		this.changePlay();
		return false;
	},
	/*删除歌曲*/
	delPlayList:function(a){
		var g = a.parentNode.parentNode.id;
		if(g  == play.playIndex){
			play.playNext();
		}
		/*删除歌曲*/
		$("#playList").element().removeChild(this.list[a.parentNode.parentNode.id]);
		/*重新装载播放列表*/
		this.initPlayList();
	},
	/*装载cookie歌曲*/
	load:true,
	loadPlay:function(){ 
		$.setCookie("win","true",30000);
		if($.getCookie("playList").length > 1 && play.load == true){
			var h = $.getCookie("playList").split('_').length-1;
			play.load = false;
			/*原始音乐列表*/
			var g = play.list.length;
			var j = "";		
			for(var o = 0 ;o<play.list.length;o++){
				j += "_"+$(play.list[o]).find("input").value().split("|")[0];
			}
			/*设置原始音乐列表到cookie*/
			$.setCookie("_playList",j,30000);
			/*请求新添加的音乐*/
			$.ajax({type:"GET",url:"/ajax/playlist/"},function(){
				 /*清除cookie*/
				 $.setCookie("playList",0,30000);
				 $.setCookie("_playList",null,30000);
			
				 if($.indexOf(this,"99.cn")){
					 /*取得音乐数据*/
					 if($.getCookie("append")=="true"){
						$("#playList").append(this);
						$.setCookie("append",null,30000);
						
					 }else{
						$("#playList").prepend(this);
						play.initPlayList();
						window.focus();
						/*重新设置播放索引*/
						if($.getCookie("play")!="now"){
							play.playIndex+=h;
						}else{
							play.playIndex = 0;
						}
						if(play.playIndex>g){
							play.playIndex = g;
						}
					 }
					 if($.getCookie("play")=="now" || g==0){
						 play.setPlayUrl(0);
						 $.setCookie("play",null,30000);
					 }
				 }
				 play.load = true;
				
			});
		};
	},
	mode:0,
	modeMessage:null,
	setMode:function(a,b){
		this.mode = a;
		$("#playModeTool").html($(b).html());
	},
	/*播放模式窗口开启*/
	setModeDialogue:function(a){
		var g = $(a);
		var h = this.modeMessage;
		var i = "";
		/*组装播放模式*/
		for(var n= 0;n<h.length;n++){
			if (g.html() != h[n]){
				i+= "<li onclick='play.setMode("+n+",this)'>"+h[n]+"</li>";
			}
		}
		/*浮动居左边距离*/
		var left = -1;
		/*是IE7进行距离调整*/
		if($.isBrowser("ie7")){
			//left = document.body.offsetLeft;
		}
		/*打开模式窗口*/
		$(document.body).dialogue({listener:a,type:"menu",html:"<div id='playMode'><ul>"+i+"</ul></div>",change:({top:-43,left:left,width:68,height:158})});
	},
	/*清除播放列表*/
	playClear:function(){
		play.doPause();
		var g = $("#playList").find("li").element();
		if(g){	
			if(g.length>0){
				for (var n= 0;n<g.length;n++){	
					$("#playList").element().removeChild(g[n]);
				}
			}else{
				$("#playList").element().removeChild(g);
			}
			for(var o = 0 ;o<play.list.length;o++){
				$(play.list[o]).find("input").value("");
			}
			play.initPlayList();
		}
	}
};

$(window).bind("load",function(){
	/*播放按钮事件绑定*/
});
$(window).bind("unload",function(){
	//play.P.close();
	play.doStop();
});
$(window).bind("resize",function(){
	play.panel();
})

var jsReady = false;
var swfReady = false;
/* 调用这些函数的目的是检查页面是否已初始化以及 JavaScript 是否可用 */
function isReady(){
    return jsReady;
}
/* 由 <body> 标签的 onload 事件调用 */
function pageInit(){
    /* 记录 JavaScript 已准备就绪。*/
    jsReady = true;
}
function setSWFIsReady(){
	/*装载播放器*/
	play.init();
	/*设置播放ID开始播放*/
	play.setPlayUrl(0);
	/*监控是否有新歌曲加入*/
	setInterval(play.loadPlay,1000);
	swfReady = true;
}
FlashObject = function(idname) {
	if ($.isIE()){return document.getElementById(idname);}else{return document.getElementById(idname+"_ff");}
}
Mp3Player = function(){
	this.volume = 100;
	this.settings = this;
	this.controls = this;
	this.currentMedia = this;
	this.currentPosition = 0;
	this.duration = 0;
	this.PlayState = 0;
	this.Url = "";
	this.playbackPercent = 0;
	this.interval = function(){
		if(flash.getVolume()!=this.volume){
			flash.setVolume(this.volume);
		}
		this.PlayState = flash.getPlayState();
		this.currentPosition = flash.getCurrentPosition()/1000;
		this.playbackPercent = flash.getPlaybackPercent();
		this.duration = flash.getDuration()/1000;
	};
	this.setCurrentPosition = function(i){
		flash.setCurrentPosition(i);
	};
	this.stop = function(){
		this.PlayState = 1;
		flash.doStop();
	};
	this.pause = function(){
		this.PlayState = 2;
		flash.doPause();
	};
	this.setPlayState = function(i){
		flash.setPlayState(i);
	};
	this.getPlayState = function(){
		flash.getPlayState();
	};
	this.play = function(){
		this.PlayState = 0;
		flash.setPlayState(0);
		flash.setUrl(this.Url);
		flash.doPlay();
	};
}
function mp3Interval(){
	mp3player.interval();
}
var mp3player = new Mp3Player();
pageInit();