var curPlaing = 0;

$(function(){
	$('a').click(aOnClick);
	 
	handlePlayList();	
});

function aOnClick(){
	
	if($(this).hasClass('no-ajax') || !isPlaying) return true;
	
	$('#mainTPL').block({
		message: 'Загрузка... ',
		overlayCSS: {opacity: '0.1'}
	});
	
	$.post($(this).attr('href'), {
		simpleMode: 1
	}, function(data){
		$('#mainTPL').html(data).unblock();
		$('#mainTPL a').click(aOnClick);
	});

	return false;
}


function playMP3File(title, link, control){
	$(control).hide().next().show();
	playMP3(link);
	$('#curItemTitle').html(title);
	$('#mp3-load-ind').show();
}


var myListener = new Object();
                
/**
 * Initialisation
 */
myListener.onInit = function()
{
	this.position = 0;
};
/**
 * Update
 */
myListener.onUpdate = function()
{


	//document.getElementById("info_playing").innerHTML = this.isPlaying;
	//document.getElementById("info_url").innerHTML = this.url;
	//document.getElementById("info_volume").innerHTML = this.volume;
	//document.getElementById("info_position").innerHTML = this.position;
	//document.getElementById("info_duration").innerHTML = this.duration;
	//document.getElementById("info_bytes").innerHTML = this.bytesLoaded + "/" + this.bytesTotal + " (" + this.bytesPercent + "%)";
	
	isPlaying = (this.isPlaying == "true");
	if(isPlaying){
		$('#stop-button').show();
		$('#mp3-load-ind').hide();
	}else{
		$('#stop-button').hide();
		if(this.bytesLoaded < this.bytesTotal) $('#mp3-load-ind').show();
	}
	
};

function getFlashObject()
{
	return document.getElementById("myFlash");
}
function playMP3(mp3)
{
	$('#flashContainer').show();
	getFlashObject().SetVariable("method:setUrl", mp3);
	getFlashObject().SetVariable("method:play", "");
	getFlashObject().SetVariable("enabled", "true");
	

}

function pause()
{
	getFlashObject().SetVariable("method:pause", "");
	$('#stop-button').hide();
}
function stopMP3()
{
   document.getElementById("myFlash").SetVariable("method:stop", "");
	$('#stop-button').hide();
}
function setPosition()
{
	var position = document.getElementById("inputPosition").value;
	getFlashObject().SetVariable("player:jsPosition", position);
}
function setVolume()
{
	var volume = document.getElementById("inputVolume").value;
	getFlashObject().SetVariable("player:jsVolume", volume);
}


function send2playlist(id){
	$('#playListContent').block({
		message: 'Обновляем плейлист...'
	});	
	$.post(AJAXPath + 'add2playlist.php', {
		id: id, plID: curPlayListID
	}, renderPlayList);
}

function renderPlayList(data){
	$('#playListContent').html(data);
	$('#playListContent').unblock();
	handlePlayList();
}

function handlePlayList(){
	$('#playListContent div.songItem').dblclick(selectSong);
}

function selectSong(){
	playSongID(getSongID(this));
	return false;
}

function getSongID(div){
	return parseInt($(div).attr('id').replace('si', ''));
}

function playSongID(id){
	$.get(AJAXPath + 'getPlayListItem.php?id='+id, function(data){
		playMP3(data);
	});
	visualSelect(id);
}

function plNext(){
	var curSong = $('#playListContent .selectedSong:first');
	if(!$(curSong).length)
		plFirst();
	var nextSong = $(curSong).next();
	if(!$(nextSong).length) return plFirst(); // какая-то хуйня
	if(!$(nextSong).hasClass('songItem')) return plFirst(); // последний итем
	playSongID(getSongID(nextSong));
	return true;
}

function plPrev(){
	var curSong = $('#playListContent .selectedSong:first');
	if(!$(curSong).length)
		plLast();
	var prevSong = $(curSong).prev();
	if(!$(prevSong).length) return plLast(); // какая-то хуйня
	if(!$(prevSong).hasClass('songItem')) return plLast(); // последний итем
	playSongID(getSongID(prevSong));
	return true;
	
}

function plFirst(){
	var firstSong = $('#playListContent .songItem:first');
	if(!$(firstSong).length) return false; // no songs
	playSongID(getSongID(firstSong));
	return true;
}

function plLast(){
	var firstSong = $('#playListContent .songItem:last');
	if(!$(firstSong).length) return false; // no songs
	playSongID(getSongID(firstSong));
	return true;	
}

function visualSelect(id){
	$('#playListContent div.songItem').removeClass('selectedSong');
	$('#playListContent #si'+id).addClass('selectedSong');
}

function doLogin(f){
	$(f).block({
		message: 'Пытаемся войти...'
	});
	$.post(AJAXPath + 'login.php', $(f).serialize(), function(data){
		$(f).unblock();
		if(data == 'OK'){
			window.location = window.location;
		}else
			alert(data);
	});
	return false;
}

function plDelSelected(){	
	
	$.post(AJAXPath + 'delSongs.php', $('#plListForm').serialize(), function(data){
		$('#plListForm').unblock();
		if(data == 'OK'){
			$('#plListForm input:checked').parents('div.songItem').fadeOut(function(){
				$(this).remove();
			});
		}else alert(data);
	});
	
	$('#plListForm').block({
		message: 'Удаляем композиции...'
	});		
}