//############################# PLAYER #################################

var splitter = '::';

var cur_playlist;
var cur_album;
var play_index = -1;
//var old_play_index = 0;
var albums_playlists = new Object;
var albums_indexes = new Object();

var album_playlist;

var playlists = new Object();

function PlayPause(playlist, id)
{
	if (cur_playlist == playlist && play_index == id)
	{
		StopPlay();
		return;
	}
	if (play_index >= 0) StopPlay();
	
	Play(playlist, id);
}

function PlayPauseAlbum(playlist, id, nextprev)
{
	if (playlist == 'albums' && playlists['album_'+id] == undefined)
	{
		GetAlbumPlaylist(id);
		return;
	}
	
	if (albums_indexes[id] == undefined)
		albums_indexes[id] = 0;
	else
	{
		if (nextprev == 'next') albums_indexes[id]++;
		else if (nextprev == 'prev') albums_indexes[id]--;
		if (albums_indexes[id] < 0)
			albums_indexes[id] = playlists['album_'+id].length-1;
		if (albums_indexes[id] >= playlists['album_'+id].length)
			albums_indexes[id] = 0;
	}
	cur_album = id;
	
	PlayPause('album_'+id, albums_indexes[id]);
}

function Play(playlist, index)
{
	var obj = document.getElementById('fiestaPlayer');
	if(typeof(obj.SetVariable)=='undefined') obj = document.getElementById('embfiestaPlayer');
	
	cur_playlist = playlist;
	play_index = index;
	
	var url;
	if (playlist == 'tops')
		url = preview_url + tops[index]['name_file'];
	else
	{
		//if (basket.in_array( playlist[play_index]) )
		//	url = basket_urls[basket.get_index(playlist[play_index])];
		//else
			url = preview_url + playlists[playlist][index]['name_file'];
	}
	
	DisplayPlay(true);
	
	obj.SetVariable("method:setUrl", url);
	
	obj.SetVariable("method:play", "");
	obj.SetVariable("enabled", "true");
}

function StopPlay()
{
	//old_play_index = play_index;
	DisplayPlay(false);
	play_index = -1;
	var obj = document.getElementById('fiestaPlayer');
	if(typeof(obj.SetVariable)=='undefined') obj = document.getElementById('embfiestaPlayer');
	obj.SetVariable("method:stop", "");
}

function DisplayPlay(play)
{
	if (play_index < 0) return;
	
	if (cur_playlist == 'tops')
	{
		if (play)
		{
			document.getElementById('tops_player').className = 'art_player active';
			document.getElementById('top_player_status').style.width = '0%';
			document.getElementById('top_player_preload').style.width = '0%';
		}
		else
		{
			document.getElementById('tops_player').className = 'art_player';
		}
	}
	else
	{
		if (play)
		{
			document.getElementById('player_'+cur_playlist).className = 'art_player active';
			document.getElementById('status_'+cur_playlist).style.width = '0%';
			document.getElementById('preload_'+cur_playlist).style.width = '0%';
			var href = document.getElementById('playing_'+cur_playlist);
			href.innerHTML = playlists[cur_playlist][play_index]['name'];
			href.href = '/'+playlists[cur_playlist][play_index]['name_tag']+'_track'+playlists[cur_playlist][play_index]['pk_song']+'/';
		}
		else
		{
			document.getElementById('player_'+cur_playlist).className = 'art_player';
		}
	}
}

var myListener = new Object();
/**
 * Initialisation
 */
myListener.onInit = function()
{
	this.position = 0;
};
/**
 * Update
 */

myListener.onUpdate = function()
{
	if (this.isPlaying == 'false')
	{
		DisplayPlay(false);
		play_index = -1;
	}
	else
	{
		if (this.duration > 0)
		{
			if (cur_playlist == 'tops')
				document.getElementById('top_player_status').style.width = Math.ceil((this.position*100)/this.duration)+'%';
			else
				document.getElementById('status_'+cur_playlist).style.width = Math.ceil((this.position*100)/this.duration)+'%';

			if (parseInt(this.position) >= parseInt(this.duration))
				StopPlay();
		}
		
		if (this.bytesTotal > 0)
		{
			if (cur_playlist == 'tops')
				document.getElementById('top_player_preload').style.width = Math.ceil((this.bytesLoaded*100)/this.bytesTotal)+'%';
			else
				document.getElementById('preload_'+cur_playlist).style.width = Math.ceil((this.bytesLoaded*100)/this.bytesTotal)+'%';
		}
	}	
};


function GetAlbumPlaylist(id)
{
	var ajax = new Ajax();
	ajax.sendRequest('/ajax/playlist/album/'+id, null, 'GET', GetAlbumPlaylistBack);
}

function GetAlbumPlaylistBack(text)
{
	var arr = text.split('::');
	if (arr.length != 2) return; 
	playlists['album_'+arr[0]] = eval('('+arr[1]+')');
	PlayPauseAlbum('albums', arr[0]);
}





//########################################## TOPS ##########################################
var last_top;

function ChangeTopTrack(id)
{
	var cur = document.getElementById('top_item_'+id);
	if (cur == undefined) return;
	
	var last = document.getElementById('top_item_'+last_top);
	if (last != undefined) last.className = '';
	
	last_top = id;
	
	cur.className = 'active';
	
	document.getElementById('top_track_name').innerHTML = tops[id]['name'];
	var href = document.getElementById('top_album_name');
	href.innerHTML = tops[id]['album_name'];
	href.href = '/'+tops[id]['album_tag']+'_album'+tops[id]['fk_album']+'/';
	var img = document.getElementById('tops_cover');
	img.src = 'http://img.mp3fiesta.com/covers/'+tops[id]['fk_artist'].substr(0, 2)+'/'+tops[id]['fk_artist']+'/alb_'+tops[id]['fk_album']+'_sm.jpg';
	img.width='75';
	
	if (play_index >= 0 && play_index != last_top) PlayPause('tops', last_top);
}

//###################################### SAVE 4 LATER #######################################
function ChangeSavedForLaterAlbum(album_id)
{
	if (!is_authorized)
	{
		open_authform( '', 'login' );
		return;
	}
	
	var obj = document.getElementById('sfl_'+album_id);
	var img = document.createElement('img');
	img.src = '/images/loading_small.gif';
	obj.innerHTML = '';
	obj.appendChild(img);
	
	var ajax = new Ajax();
	if (savedforlater_albums.in_array(album_id))
		ajax.sendRequest('/account/saveforlater/removebyitemid/'+album_id, null, 'GET', ChangeAlbumSFLBack);
	else
		ajax.sendRequest('/account/saveforlater/add/album/'+album_id, null, 'GET', ChangeAlbumSFLBack);	
}

function ChangeAlbumSFLBack(result)
{
	var arr = result.split(splitter);
	if (arr.length < 2) return;
	
	if (arr[0] == 'added') 
	{
		document.getElementById('sfl_li_'+arr[1]).className = 'active';
		//document.getElementById('sfl_'+arr[1]).className = 'save';//.innerHTML = 'album saved for later';
		document.getElementById('sfl_'+arr[1]).innerHTML = '<span class="on">album saved for later</span>';
		savedforlater_albums[savedforlater_albums.length] = arr[1];
	}
	else if (arr[0] == 'removed')
	{
		document.getElementById('sfl_li_'+arr[1]).className = '';
		//document.getElementById('sfl_'+arr[1]).className = '';//.innerHTML = 'save album for later';
		document.getElementById('sfl_'+arr[1]).innerHTML = 'save album for later';
		savedforlater_albums.remove_item(arr[1]);
	}
}

function CheckButtons()
{
	for (var i=0; i<albums.length; i++)
	{
		if (savedforlater_albums.in_array(albums[i]))
		{
			document.getElementById('sfl_li_'+albums[i]).className = 'active';
			//document.getElementById('sfl_'+albums[i]).className = 'save';//.innerHTML = 'album saved for later';
			//document.getElementById('sfl_'+albums[i]).innerHTML = '<span class="on">album saved for later</span>';
		}
			
		if (basket_albums.in_array(albums[i]))
		{
			var href = document.getElementById('buy_'+albums[i]);
			href.innerHTML = 'get album';
			href.setAttribute("onclick", "");
			href.href="/album"+albums[i]+"/";
		}
	}
}

//###################################### BUY ALBUM ############################################
function buy_any_from_linker()
{
	is_buy_album_or_track_by_linker = 0;
	
	var linker = window.location.href.split('/');
	var linker_s = '';
	for( i in linker )
		if( i>3 && linker[i].match(/^[1-9][0-9]*/) )
		{
		  linker_s = linker[i];
		  break;
		}  
		  
	if( !linker_s.length )
		return;
	
	linker_s = linker_s + "";
	linker_s = linker_s.replace(':', '.');	
	linker_s = linker_s.split('_', 3);
		
	if( linker_s[2] && linker_s[2] == 'song' )
		BuySong(linker_s[0], linker_s[1]);
	else	
		BuyAlbum(linker_s[0], linker_s[1]);
}
function BuyAlbum(album_id, price, help_title)
{
	if (!is_authorized)
	{
		var price_link = price + "";
		var backward = album_id + '_' + price_link.replace('.', ':');			
		//self.location = '/user/auth/?backward='+location.search;
		open_authform( backward, 'login', help_title );
		return;
		//http://mp3panda.com/user/auth/?backward=%2Falbum2263002%2FTaylor-Swift%2FFearless%2F&pk_song=24498371
	}
	if (!unlim)
//		if (!confirm('Your account will be charged at the rate of $' + price + ' for this operation.')) return;
		if(!buy_conformation(price))return;
		
	var obj = document.getElementById('buy_'+album_id);
	var img = document.createElement('img');
	img.src = '/images/loading_small.gif';
	obj.innerHTML = '';
	obj.appendChild(img);
	//obj.setAttribute("onclick", "");
		
	var ajax = new Ajax();
	ajax.sendRequest('/basket/buyAlbum/ajax/?pk='+album_id, null, 'GET', function(result){ BuyAlbumBack(result, price) });
}

function BuyAlbumBack(result, price)
{
	var arr = result.split(splitter);
	if (arr[0] != 1)
	{
		var res_limit = result.split('::');
		if( res_limit[1] == 'limit' )
		{
			alert("Sorry, due to technical reasons and in order to provide better service and high speed downloads we limit daily download by 500 tracks per day. Sorry for inconvenience.");
			var obj = $('a#buy_'+res_limit[2]);
			$(obj).html('download album');
		}
		else if (arr[1] == 'busy')
			alert(messages['busy']);
		else if (arr[1] == 'money') {
				//alert(messages['money']);
				checkAlloPayRedirect();
		}
		
		return;
	}
	var href = document.getElementById('buy_'+arr[1]);
	href.innerHTML = 'get album';
	href.setAttribute("onclick", "");
	href.href="/album"+arr[1]+"/";	
	if(typeof(update_wondertime)=='function')update_wondertime();
	
	update_user_auth_balance(price);
	alert('The album has been successfully purchased! You can find it in your "Downloads" section.');
}


function ChangePlaylistAlbumBack(todo, id)
{
	var obj = document.getElementById('playlist_button_'+id);
	obj.setAttribute('onclick', '');
	obj.innerHTML = 'added';
}

