var playList = new Array();
var root = "http://ibox.fm/";
var curSong = 0;
$(document).ready(function(){
	var flashvars = {};
	var params = {wmode: "transparent"};
	var attributes = {}; 
	swfobject.embedSWF(root+"embed.swf?as=0", "player", "300", "22", "9.0.0",root+"embed.swf", flashvars, params, attributes);
	$('.notices li.notice:even').addClass('alter');
	$('a.play').click(
		function() {
			
			if ($(this).html() == 'play'){
				$('a.play').html('play');
				$('.notices li').removeClass('playing');
				//$(this).parent().parent().parent().parent().parent().addClass('playing');
				curSong = $(this).parent().parent().parent().parent().parent().children().index($(this).parent().parent().parent().parent());
				//$(this).html('stop...');
				playSingle(curSong);
				
			} else {
				$(this).html('play');
				niftyplayer('player').stop();
			}	
		}
	);
});


function playSingle(pos){
	curSong = pos;
	currentSong = $('ul.notices li:eq('+pos+')').attr('id');
	if (currentSong == null) {
		currentSong = $('ul.notices li:eq(0)').attr('id');
	}
	$('#'+currentSong).addClass('playing');
	//alert();
	$('#song'+currentSong.replace(/notice/,'')).html('Stop....');
	if(playList[pos]) {
		url = playList[pos][2];
		sName = playList[pos][1];
		sArtist = playList[pos][0];
		
		playMp3(url,sName,sArtist);
	}
}
function playMp3(url,sName,sArtist) {
		niftyplayer('player').stop();
		$('div.control-panel-wrapper').show();
			
			//check is url of chacha
			if(url.substring(0,14) == 'http://xalo.vn') {
					id = url.replace('http://xalo.vn/','');
					 var newURL = $.ajax({
				   		type: "POST",
				   		url: root +  'index.php?action=parseurl&sid='+id,
				   		async: false
				 	}).responseText;
				 	url = newURL.replace('urlsong=','');
				}
			if(url.substring(0,16) == 'http://chacha.vn') {
				temp = url.split('id=');
				temp2 = temp[1].split('&');
				id = temp2[0];
				var newURL = $.ajax({
			   		type: "POST",
			   		url: root +  'index.php?action=parseurl&id='+id,
			   		async: false
			 	}).responseText;
			 	url = newURL;
		 	}
		 	if(url.substring(0,28) == 'http://www.nhaccuatui.com/M/') {
				id = url.replace(/http:\/\/www.nhaccuatui.com\/M\//, "");
				var newURL = $.ajax({
			   		type: "POST",
			   		url:  root +  'index.php?action=parseurl&id='+id,
			   		async: false
			 	}).responseText;
			 	url = newURL;
		 	}
		 	niftyplayer('player').loadAndPlay(url);
			niftyplayer('player').registerEvent('onSongOver', 'statusPlayer("onSongOver")');
}
function statusPlayer(state){
	if (state == 'onSongOver') {
		$('a.play').html('play');
		$('.notices li').removeClass('playing');
		if (curSong == playList.length) {
			curSong = 0
		} else {
			curSong++;
		}
		playSingle(curSong);
	} 
}
	