var favValues = new Array();
var favIds = new Array();
ajax.methods['favourites'] = function() {}

function checkFav(songsIds)
{
	var songsString = songsIds.join(',');
	ajax.setProcessMethod('favourites');
	ajax.technique = 'synchronous';
	ajax.setParameter('songs',songsString);
	ajax.setParameter('check','true');						
	ajax.makeCustomRequest('/scripts/favourites/favourites.php');
	ajax.technique = 'asynchronous';
	favValues = ajax.responseText.split(',');
	favIds = songsIds;
}

function fav(obj, cl, del)
{
	var parentDiv = document.getElementById(obj);
	var fileId = parseInt(parentDiv.id.replace('fav_', ''));
	var className = arguments[1] ? arguments[1] : 'fav';
	var mode = 'add';

	for(var i = 0; i < favValues.length; i++)
	{
		if(String(favIds[i]) == fileId)
		{
			mode = favValues[i].match('true') ? 'remove' : 'add';
		}
	}
	parentDiv.className = className + '_' + mode;
	parentDiv.onclick = function()
	{
		favClick(this,fileId,className); 
		if(del) removeButton(del);
	}
}

function favClick(obj, fileId, cl)
{
	ajax.technique = 'synchronous';
	ajax.setProcessMethod('rating');
	ajax.setParameter('id', fileId);
	ajax.makeCustomRequest('/scripts/favourites/favourites.php');
	if(ajax.responseText.match('unlogged'))
	{
		window.location.href = 'http://' + location.host + '/login.php';
	}
	if(ajax.responseText.match('added'))
	{
		obj.className = cl + '_remove';
	}
	else if (ajax.responseText.match('removed'))
	{
		obj.className = cl + '_add';
	}
	else if (ajax.responseText.match('error')) {};
	ajax.technique = 'asynchronous';
}

function removeButton(obj)
{
	var div = document.getElementById(obj);
	var sep = document.getElementById(obj + '_sep');
	div.parentNode.removeChild(div);
	sep.parentNode.removeChild(sep);
}
