// VARIABILE GLOBALE

// optiunile de repeat si shuffle
var repeat = "repeat list", shuffle;
var timerRest;


// pentru cautare, numarul de trackuri, melodia actuala, melodia acutala in mod shuffle
var tracks, actual_track = 0, actual_shuffle_track = 0, actual_popup_track = 0;
// pentru cautare, lista de trackuri in mod shuffle
var tracklist = new Array();

// daca face scroll
var do_not_scroll = 0;

// daca a fsot scoasa o melodie de pe playlist in timp ce e redata
var removed_while_playing = 0;

// timeoutul pentru eroare de incarcare fisier
var nextIfError;

// initializeaza playlistul
function initPlayer()
{
	if($("playerAudio"))
	{
		if($("songlist"))
		{
			tracks = $("songlist").getElementsByTagName("li").length ;
		}
		generateRandomPlaylist();
	}
}

// play melodie
function playMp3(url, title, track_order, extension, artist_id)
{             
	clearTimeout(timerRest);
	if($('restricted_' + track_order).innerHTML == 1 && url == '') {
		
		$('playerAudio').innerHTML = '<br><center>This track is not available in your country due to copyright restrictions.</center>';
		
		if($('now_playing'))
		{
			$('now_playing').innerHTML = "RESTRICTED: " + title;
		}
	
		removed_while_playing = 0;
		
		document.title = title;
		
		if($('artist_info'))
		{
			if(do_not_scroll != 1)
			{
				$('scrollAudio').scrollTop = 32 * (track_order - 1);
			}
			do_not_scroll = 0;
			if($('artist_' + track_order).innerHTML)
			{
				$('artist_info').innerHTML = $('artist_' + track_order).innerHTML;
				
				if (artist_img != '') {
                	$('artist_img').src = artist_img[artist_id].src;
				}
			}
			else
			{
				$('artist_info').innerHTML = "Unknown artist";
			}
			
			if(artist_id && !isNaN(artist_id))
			{
				showArtistLinks(artist_id);
			}
			else
			{
				$('artist_options').style.display = 'none';
			}
		}
		isPlaying(track_order);
		timerRest = setTimeout('next()', 4000);
	}else{
	
	
		if($('embed_place').innerHTML != '')
		{
			$('embed_place').innerHTML = '';
		}
		
		if($('now_playing'))
		{
			$('now_playing').innerHTML = "Acum: " + title;
		}
	
		removed_while_playing = 0;
		
		document.title = title;
		
		if($('artist_info'))
		{
			if(do_not_scroll != 1)
			{
				$('scrollAudio').scrollTop = 32 * (track_order - 1);
			}
			do_not_scroll = 0;
			if($('artist_' + track_order).innerHTML)
			{
				$('artist_info').innerHTML = $('artist_' + track_order).innerHTML;
				
				if (artist_img != '') {
                	$('artist_img').src = artist_img[artist_id].src;
				}
			}
			else
			{
				$('artist_info').innerHTML = "Unknown artist";
			}
			
			if(artist_id && !isNaN(artist_id))
			{
				showArtistLinks(artist_id);
			}
			else
			{
				$('artist_options').style.display = 'none';
			}
		}
		
		var x = url.match(/((http(s)?)|ftp):\/\/([^\/]+)/g);
		
		if(!extension)
		{
			try
			{
				$('from').innerHTML = "Gasita pe: <span>" + x[0].replace(/((http(s)?)|ftp):\/\//g,'') + "</span>";
			}
			catch(e)
			{
				
			}
		}
		
		clearTimeout(nextIfError);
		
		$("file_not_found").style.display = "none";
		
		if(actual_shuffle_track == 0)
		{
			actual_shuffle_track = 1;
		}
		actual_track = track_order;
		
		try
		{
			opener.actual_popup_track = actual_track;
			//opener.loadAudioSwf();
		}
		catch(e)
		{
			
		}	
		
		//document.getElementById("player_audio").playSound(url, title, extension);
		
		extension == "" ? extension = "MP3" : extension = extension.toUpperCase();
		
		loadAudioSwf(url, title, extension);
		isPlaying(track_order);
	}
}

// sorteaza un array
function randomOrder()
{
	return (Math.round(Math.random())-0.5);
}

// genereaza un playlist sortat aleatoriu
function generateRandomPlaylist(which)
{
	for(var i=1;i<=tracks;i++)
	{
		tracklist[i - 1] = i;
	}
	
	tracklist.sort(randomOrder);
}

// melodia urmatoare, se apeleaza din flash
function next(sursa)
{
	clearTimeout(timerRest);
	// pentru cautare
	if(repeat == "shuffle")
	{
		if(actual_shuffle_track < tracks)
		{
			var next_track = tracklist[actual_shuffle_track];
			actual_shuffle_track++;
		}
		else
		{
			var next_track = tracklist[0];
			actual_shuffle_track = 1;
		}
	}
	else 
	{
		if(actual_track < tracks)
		{
			if(repeat == "repeat item" && sursa == "auto") {
				//var next_track = actual_track;
			} else {
				var next_track = actual_track + 1;
			}
		}
		else if(repeat == "repeat list" || sursa != "auto")
		{
			var next_track = 1;
		}
		else
		{
			var next_track = 0;
		}
	}
	
	
	if(next_track != 0)
	{
		var location = $("play_" + next_track).innerHTML;
		var title = $("title_" + next_track).innerHTML;
		var extension = $('play_' + next_track).className;
		var artist_id = $('title_' + next_track).className;
		playMp3(location, title, next_track, extension, artist_id);
	}
}

// melodia precedenta
function bw()
{
	clearTimeout(timerRest);
	if(repeat == "shuffle")
	{
		if(actual_shuffle_track > 1)
		{
			var prev_track = tracklist[actual_shuffle_track - 2];
			actual_shuffle_track--;
		}
		else
		{
			var prev_track = tracklist[tracks - 1];
			actual_shuffle_track = tracks;
		}
	}
	else
	{
		if(actual_track > 1)
		{
			var prev_track = actual_track - 1;
		}
		else if(repeat != "off")
		{
			var prev_track = tracks;
		}
		else
		{
			var prev_track = 0;
		}
	}
	
	
	if(prev_track != 0)
	{
		var location = $("play_" + prev_track).innerHTML;
		var title = $("title_" + prev_track).innerHTML;
		var extension = $('play_' + prev_track).className;
		var artist_id = $('title_' + prev_track).className;
		playMp3(location, title, prev_track, extension, artist_id);
	}
}

// schimba optiunea de repeat
function playerRepeat(rep)
{
	repeat = rep;
}

// schimba optiunea de shuffle
function playerShuffle(shuf)
{
	shuffle = shuf;
}

// in caz ca nu se incarca melodia
function fileNotFound()
{
	$("file_not_found").style.display = "block";
	nextIfError = setTimeout(next, 2000);
}

// arata iconita de play
function overPlay(id)
{
	$('track_' + id).style.background = "#fff4a5";
}

function outPlay(id)
{
	if(actual_track != id)
	{
		if($('track_' + id).className == 'patru' || $('track_' + id).className == 'played4')
		{
			$('track_' + id).style.background = "#ebebeb";
		}
		else
		{
			$('track_' + id).style.background = "none";
		}
	}
}

function isPlaying(id)
{
	actual_track = id;
	for(var i=1;i<=tracks;i++)
	{
		if(i != id)
		{
			outPlay(i);
		}
		else
		{
			overPlay(i);
			if($("track_" + i).className == "patru" || $("track_" + i).className == "played4")
			{
				$("track_" + i).className = "played4";
			}
			else
			{
				$("track_" + i).className = "played";
			}
		}
	}
}

function playInPopup(url, title, track_order, extension, artist_id)
{
	if(popup_is_opened == 0)
	{
		playlistWindow = window.open(baseURL + 'index.php?module=music&action=myPlaylist&play_url=' + escape(url) + '&play_title=' + escape(title) + '&play_track=' + track_order + '&extension=' + extension + '&artist_id=' + artist_id,'Playlist','menubar=no,width=850,height=530,toolbar=no,status=no,location=yes,resizable=no');
	}
	else
	{
		$('popupDetector').playMp3(url, title, track_order, extension, artist_id);
	}
	if(actual_track > 0)
	{
		loadAudioSwf();
		isPlaying(0);
	}
}

function showDeleteImg(id)
{
	$('img_' + id).src = baseURL + 'external/templates/default/images/remove1.gif';
}

// ascunde icontia de remove daca e pe play
function hideDeleteImg(id)
{
	$('img_' + id).src = baseURL + 'external/templates/default/images/play1.gif';
}

function openPlaylist()
{
	if(popup_is_opened == 0) 
	{
		playlistWindow = window.open(baseURL + 'index.php?module=music&action=myPlaylist','Playlist','menubar=no,width=850,height=530,toolbar=no,status=no,location=yes,resizable=no');
	}
	else
	{
		$('popupDetector').focusWindow();
	}
	return false;
}


function playIndexMusic(id)
{
	if($('player_audio_c').style.display == 'none')
	{
		$('player_embed').style.display = 'none';
		top.location = '#';
		function playNow()
		{	
			playMp3($('play_' + id).innerHTML, $('title_' + id).innerHTML, id, $('play_' + id).className, $('title_' + id).className);
		}
		new Effect.Appear('player_audio_c', {duration: 0.5, afterFinish: playNow});
	}
	else
	{
		playMp3($('play_' + id).innerHTML, $('title_' + id).innerHTML, id, $('play_' + id).className, $('title_' + id).className);
	}
}

function playEmbed(id)
{
	for(i=1;i<=tracks;i++)
	{
		if(i != id)
		{
			if($('track_' + i).className == 'patru' || $('track_' + i).className == 'played4')
			{
				$('track_' + i).style.background = "#ebebeb";
			}
			else
			{
				$('track_' + i).style.background = "none";
			}
		}
		else
		{
			$('track_' + i).style.background = '#fff4a5';
		}
	}
	$('player_audio_c').style.display = 'none';
	$('player_embed').innerHTML = $('embed_' + id).innerHTML;
	new Effect.Appear('player_embed', {duration: 0.5});
}

function showArtistLinks(artist_id)
{
	$('artist_options').style.display = 'block';
	$('a_clipuri').href = baseURL + 'index.php?module=AmvcArtist&action=mediaGallery&id=' + artist_id + '&media_type=2&lang_id=' + $('a_clipuri').lang;
	$('a_poze').href = baseURL + 'index.php?module=AmvcArtist&action=mediaGallery&id=' + artist_id + '&media_type=1&lang_id=' + $('a_poze').lang;
	$('a_audio').href = baseURL + 'index.php?module=AmvcArtist&action=music&id=' + artist_id + '&lang_id=' + $('a_audio').lang;
	$('a_versuri').href = baseURL + 'index.php?module=AmvcArtist&action=lyrics&id=' + artist_id + '&lang_id=' + $('a_versuri').lang;
	$('a_biografie').href = baseURL + 'index.php?module=AmvcArtist&action=biography&id=' + artist_id + '&lang_id=' + $('a_biografie').lang;
	$('a_discografie').href = baseURL + 'index.php?module=AmvcArtist&action=discography&id=' + artist_id + '&lang_id=' + $('a_discografie').lang;
}

// initializeaza playlistul cand se incarca pagina
addEvent(window,'load', initPlayer, false);
