var playerWindow = null;

function openPlayer(){
	playerWindow = window.open(
		"/player/",
		"player",
		"width=820px,height=680px,toolbar=no,menubar=no,status=no,resizable=0,fullscreen=no,left=0,top=0"
	);				
}
function getPlayerWindow() {
	playerWindow = window.open("","player");
}
function selectAll(obj) {
	$(obj).find("input[type='checkbox']").each(function(){
		if(!$(this).attr("disabled")) $(this).attr("checked",true);
	});
}
function deselectAll(obj) {
	$(obj).find("input[type='checkbox']").each(function(){
		if(!$(this).attr("disabled")) $(this).attr("checked",false);
	});
}
function playSong(songId) {
	var str;
	if(typeof songId == 'object') {
		str = "{";
		$(songId).find(":checked").each(function(){
			str += $(this).val()+",";
		});
		str += "}";
		if(str == "{}") {
			$.jGrowl("재생할 곡을 선택하세요.");
			return false;
		}
	} else {
		str = "{"+songId+"}";
	}
	$.post("/player/action.php" ,{ act : "addSong" , pos : "clear" , songIds : str },
		function(data) {
			var msg = new Message(data);
			msg.print();
			if(msg.isSuccess) {
				playerWindow = window.open(
						"/player/",	"player",
						"width=820px,height=680px,toolbar=no,menubar=no,status=no,resizable=0,fullscreen=no,left=0,top=0"
				);
			}
	},"json");
}
function reserveSong(songId) {
	var str;
	if(typeof songId == 'object') {
		str = "{";
		$(songId).find(":checked").each(function(){
			str += $(this).val()+",";
		});
		str += "}";
		if(str == "{}") {
			$.jGrowl("예약할 곡을 선택하세요.");
			return false;
		}
	} else {
		str = "{"+songId+"}";
	}
	
	getPlayerWindow();
	if(!playerWindow.player) {
		alert("플레이어를 실행시켜주세요.");
		playerWindow.close();
		return false;
	}
	
	$.post("/player/action.php" ,{ act : "addSong" , pos : "last" , songIds : str },
		function(data) {
			var msg = new Message(data);
			msg.print();
			if(msg.isSuccess) {
				window.setTimeout("playerWindow.player.getPlaylist();",1000);
				$.jGrowl("예약되었습니다.");
			}
		},"json");
}
function playAlbum(albumId) {
	$.post("/player/action.php" ,{ act : "addAlbum" , albumId : albumId },
			function(data) {
				var msg = new Message(data);
				msg.print();
				if(msg.isSuccess) {
					playerWindow = window.open(
							"/player/",	"player",
							"width=820px,height=680px,toolbar=no,menubar=no,status=no,resizable=0,fullscreen=no,left=0,top=0"
					);
				}
		},"json");
}
