//############################# 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();

var process_playlists = new Object();

function GetPlaylist( type, id )
{
	process_playlists[id] = type;
	var ajax = new Ajax();
	ajax.sendRequest('/ajax/playlist/'+type+'/'+id, null, 'GET', GetPlaylistBack);
}

function GetPlaylistBack(text)
{
	var arr = text.split('::');
	if (arr.length != 2) return;
	var type = process_playlists[ arr[0] ];
	playlists[ type+'_'+arr[0] ] = eval('('+arr[1]+')');
	PlayPause(type, arr[0]);
}

function PlayPause( type, id, rever )
{
	if (cur_playlist == type && play_index == id && rever == undefined)
	{
		StopPlay();
		return;
	}
	
	//if (type != 'track' && playlists[type+'_'+id] == undefined)
	if (playlists[type+'_'+id] == undefined)
	{
		GetPlaylist( type, id );
		return;
	}
	
	StopPlay();
	
	Play(type, id, rever);
}

function Play(playlist, index, nextprev)
{
	var obj = document.getElementById('fiestaPlayer');
	if(typeof(obj.SetVariable)=='undefined') obj = document.getElementById('embfiestaPlayer');
	
	cur_playlist = playlist;
	play_index = index;
	
	var cur_id = playlist+'_'+play_index;
	
	var url;
	//if (playlist == 'track')
	//	url = preview_url + tracks[index]['name_file'];
	//else
	{
		if (albums_indexes[cur_id] == undefined)
		albums_indexes[cur_id] = 0;
			
		if (nextprev == 'next') albums_indexes[cur_id]++;
		else if (nextprev == 'prev') albums_indexes[cur_id]--;
		if (albums_indexes[cur_id] < 0)
			albums_indexes[cur_id] = playlists[cur_id].length-1;
		if (albums_indexes[cur_id] >= playlists[cur_id].length)
			albums_indexes[cur_id] = 0;
			
		//if (basket.in_array( playlist[play_index]) )
		//	url = basket_urls[basket.get_index(playlist[play_index])];
		//else
			url = preview_url + playlists[cur_id][albums_indexes[cur_id]]['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 (play)
	{ 
		document.getElementById(cur_playlist+'_'+play_index).className = 'active';
		document.getElementById(cur_playlist+'_p_'+play_index).style.width = '0%';
		document.getElementById(cur_playlist+'_s_'+play_index).style.width = '0%';
		if (cur_playlist != 'track')
		{
			var cur_id = cur_playlist+'_'+play_index;
			var href = document.getElementById(cur_playlist+'_n_'+play_index);
			href.innerHTML = playlists[cur_id][ albums_indexes[cur_id] ]['name'];
			href.href = '/'+playlists[cur_id][ albums_indexes[cur_id] ]['name_tag'];
			href.href += '_track';
			href.href += playlists[cur_id][ albums_indexes[cur_id] ]['pk_song']+'/';
		}
	}
	else
	{
		document.getElementById(cur_playlist+'_'+play_index).className = '';
	}
}

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)
		{
			document.getElementById(cur_playlist+'_s_'+play_index).style.width = Math.ceil((this.position*100)/this.duration)+'%';
			if (parseInt(this.position) >= parseInt(this.duration))
			{	//StopPlay();
				if (cur_playlist != 'track' && albums_indexes[cur_playlist+'_'+play_index] + 1 < playlists[cur_playlist+'_'+play_index].length)
					PlayPause(cur_playlist, play_index, 'next');
				else
					StopPlay();
			}
		}
		
		if (this.bytesTotal > 0)
		{
			document.getElementById(cur_playlist+'_p_'+play_index).style.width = Math.ceil((this.bytesLoaded*100)/this.bytesTotal)+'%';
		}
	}	
};


//#################################### BUY ###############################################
var buy_list = new Object();

function Buy ( type, id, price, obj, help_title )
{
	if (!is_authorized)
	{
		open_authform( '', 'login', help_title );
		return;
	}
	if (!unlim)
//		if (!confirm('Your account will be charged at the rate of $' + price + ' for this operation.')) return;
		if(!buy_conformation(price))return;
	
	if (type == 'track')
		var url = '/basket/buySong/ajax/?pk='+id;
	else
		var url = '/basket/buyAlbum/ajax/?pk='+id;
		//alert(url);
	if (obj != undefined)
	{
		var img = document.createElement('img');
		img.src = '/images/loading_small.gif';
		obj.innerHTML = '';
		obj.appendChild(img);
		//obj.setAttribute('onclick', '');
	}
	
	buy_list[id] = new Object();
	buy_list[id]['type'] = type;
	buy_list[id]['obj'] = obj;
	buy_list[id]['price'] = price;
	
	var BuyAjax = new Ajax();
	BuyAjax.sendRequest(url, null, 'get', function(result){ BuyBack(result, price) });
}

function BuyBack( result, price )
{
	//alert(result);
	var arr = result.split('::');
	var res_limit = result.split('::');
	if (arr.length < 3 && res_limit.length < 3) return;
	
	if (arr[0] == 1)
		var id = arr[1];
	else
		var id = arr[2];
		
	if (buy_list[id] != undefined && buy_list[id]['obj'] != undefined)
	{
		var obj = buy_list[id]['obj'];
		var type = buy_list[id]['type'];
		var price = buy_list[id]['price'];
	}
	
	if (arr[0] != 1)
	{	
		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.");
			$('#album_buy_'+res_limit[2]).html('download');
			return;
		}
		else if (arr[1] == 'busy')
			alert(messages['busy']);
		else if (arr[1] == 'money') {
				//alert(messages['money']);
				checkAlloPayRedirect();
		}
			
		if (obj != undefined)
		{
			var obj = buy_list[arr[2]]['obj'];
			obj.innerHTML = 'The album has been successfully purchased! You can find it in your "Downloads" section.';
			obj.setAttribute('onclick', "Buy( 'track', "+id+", '"+price+"', this);");
		}
			
		return;
	}
	if(typeof(update_wondertime)=='function')update_wondertime();
	update_user_auth_balance(price);
	
	if (obj != undefined)
	{
		obj.className = 'downloaded';
		if (type == 'track')
		{
			obj.innerHTML = 'get mp3';
			obj.href = arr[2];
		}
		else
		{
			obj.innerHTML = 'downloaded';
			obj.href = '/'+type+id+'/';
			alert('Downloaded');
		}
	}
}

function CheckBuyButtons()
{
	if (chart_type == 'tracks')
		var basket_arr = basket;
	else
		var basket_arr = basket_albums;
	
	for (var key in data)
	{
		if (basket_arr.in_array(key))
		{
			if (chart_type == 'track')
			{
				var obj = document.getElementById('track_buy_'+key);
				obj.innerHTML = 'get mp3';
				obj.className = 'downloaded';
				obj.href = basket_urls[basket_arr.get_index(key)];
				obj.setAttribute('onclick', '');
			}
			else
			{
				var obj = document.getElementById('album_buy_'+key);
				obj.innerHTML = 'downloaded';
				obj.className = 'downloaded';
				obj.href = '/album'+key+'/';
				obj.setAttribute('onclick', '');
			}
		}
	}
}

function CheckSFLButtons()
{
	if (chart_type == 'tracks')
		var arr = savedforlater;
	else if (chart_type == 'albums')
		var arr = savedforlater_albums;
	else if (chart_type == 'artists')
		var arr = savedforlater_artists;
	else if (chart_type == 'books')
		var arr = savedforlater_albums;
		
	for (var key in data)
	{
		if (arr.in_array(key))
		{
			document.getElementById('sfl_li_'+key).className = 'active';
		}
	}
}

function ChangePlaylistsBack(todo, id)
{
	var obj = document.getElementById('playlist_button_'+id);
	obj.setAttribute('onclick', '');
	obj.innerHTML = 'added';
}

//###################################### SAVE 4 LATER #######################################
function ChangeSavedForLater(type, id)
{
	if (!is_authorized)
	{
		open_authform( '', 'login' );
		return;
	}
	
	var obj = document.getElementById('sfl_'+id);
	var img = document.createElement('img');
	img.src = '/images/loading_small.gif';
	obj.innerHTML = '';
	obj.appendChild(img);
	
	if (type == 'song')
		var arr = savedforlater;
	else if (type == 'album')
		var arr = savedforlater_albums;
	else if (type == 'artist')
		var arr = savedforlater_artists;

	var ajax = new Ajax();
	if (arr.in_array(id))
		ajax.sendRequest('/account/saveforlater/removebyitemid/'+id, null, 'GET', ChangeSFLBack);
	else
		ajax.sendRequest('/account/saveforlater/add/'+type+'/'+id, null, 'GET', ChangeSFLBack);
}

function ChangeSFLBack(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]).innerHTML = '<span class="on">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]).innerHTML = 'save for later';
		savedforlater_albums.remove_item(arr[1]);
	}
}

