
onPlayerError = function( errorCode )
{
	alert("An error occured of type:" + errorCode);
}

function onYouTubePlayerReady(playerId) {
	playControll.ytplayer = document.getElementById("ytPlayer");
	playControll.ytplayer.addEventListener("onError", "onPlayerError");
}

playControll = {
	ytplayer : null,
	startSong : '',
	init : function()
	{
		$('#kpop_btn').show('slow');
		$('#pop_btn').show('slow');
		$.post('./', {'ff' : 'music', 'fl' : 'youtube', 'action' : 'favorOne' }, 
		function( res ) {
			playControll.startSong = $.trim( res.video_id );
			tmp = '';
			if( res.thumbnail ) {
				tmp += '<div class="fl"><img src='+res.thumbnail+'></div>';
				tmp += '<div class="fl descript">' + res.description + '<br>' +  res.keywords + '</div>';
			}
			$('#tmp_description').html( tmp );
			playControll.videoReady();
			searchControll.getMusicianNews ( encodeURIComponent( res.artist ) );
		}
		, 'json' );
	},

	getPlaySong : function( obj )
	{
		strCode = $.trim( $(obj).html() );
		strArtist = obj.getAttribute('artist');
		strCode = encodeURIComponent( strCode );
		strArtist = encodeURIComponent( strArtist );
		$.post('./', {'ff' : 'music', 'fl' : 'youtube', 'action' : 'getVideo' , 'song' : strCode, 'artist' : strArtist }, 
		
		function( res ) {
			if( playControll.ytplayer ) {
				playControll.ytplayer.loadVideoById( res.video_id );
				tmp = '';
				if( res.thumbnail ) {
					tmp += '<div class="fl"><img src='+res.thumbnail+'></div>';
					tmp += '<div class="fl descript">' + res.description + '<br>' +  res.keywords + '</div>';
				}
				$('#tmp_description').html( tmp );
				searchControll.getMusicianNews ( strArtist );
			}
		}
		, 'json' );
	},

	videoReady : function()
	{
		if( !playControll.ytplayer ) {
			var params = { allowScriptAccess: "always" };
			var atts = { id: "ytPlayer" };
			swfobject.embedSWF("http://www.youtube.com/v/"+ this.startSong +"?enablejsapi=1&playerapiid=player1", "video", "700", "450", "8", null, null, params, atts);
		}
		else {
			//
			playControll.ytplayer.cueVideoById(this.startSong );
		}
	},

	getListByMusician : function( obj, search )
	{
		if( search == undefined ) {
			strCode = $.trim( $(obj).html() );
			strCode = encodeURIComponent( strCode );
		}
		else {
			strCode = $.trim( $('#search_string_input').val() );
			if( strCode == '') {
				return;
			}
			$('#search_string_input').val('');
			strCode = encodeURIComponent( strCode );
		}
		$.post('./', {'ff' : 'music', 'fl' : 'youtube', 'action' : 'getMusician' , 'artist' : strCode },
			function( res ) {
			$('#chart_list').html( res );
			searchControll.getMusicianNews ( strCode );
		}
		, 'text' );
	},

	getPlaySongByVideoId : function( videoId )
	{
		if( playControll.ytplayer ) {
			playControll.ytplayer.loadVideoById(videoId);
		}
	},

	getSearchEvent : function( e )
	{
		if( e.keyCode == 13 )
		{
			this.getListByMusician('', true);
		}
	}
}

chart = {
	chartList : function( songType )
	{
		$.post('./', {'ff' : 'music', 'fl' : 'chart', 'type' : songType }, chart.chartListResponse, 'text' );
	},

	chartListResponse : function( res )
	{
		$('#chart_list').html( res );
	},
	
	musician : function()
	{
		$.post('./', {'ff' : 'music', 'fl' : 'musician'}, chart.musicianResponse, 'text' );
	},
	
	musicianResponse : function( res )
	{
		$('#musician_square').html( res );
	}
}
