var mod_passport_favorites_loader = 
{	
	currentId: 0,
	currentAction: '',
	currentType: 0,
	currentMain: 0,
	
	addToFavorite: function(event, _type, _id, _main)
	{
		if (event != null)
		{
			_type = event.data._type;
			_id = event.data._id;
		}
		currentId = _id;
		currentType = _type;
		currentAction = 'add';
		currentMain = _main;
		$.ajax({
			url: '/passport/favorites.php',
			dataType: 'json',
			type: 'POST',
			data: {
				action:	'favorites',
				action_user: 'add',
				type:	_type,
				id: _id,
				rand:	Math.random()
			},
			success: mod_passport_favorites_loader.success,
			error: mod_passport_favorites_loader.error
		});	
	},
	
	removeFromFavorite: function(event, _type, _id, _main)
	{
		if (event != null)
		{
			_type = event.data._type;
			_id = event.data._id;
		}
		currentId = _id;
		currentType = _type;
		currentAction = 'delete';
		currentMain = _main;
		$.ajax({
			url: '/passport/favorites.php',
			dataType: 'json',
			type: 'POST',
			data: {
				action:	'favorites',
				action_user: 'delete',
				type:	_type,
				id: _id,
				rand:	Math.random()
			},
			success: mod_passport_favorites_loader.success,
			error: mod_passport_favorites_loader.error
		});	
	},
	
	success: function(data, textStatus)
	{
		if (data.status == 'ok')
		{			
			var container = $('.favorite-item-'+currentId);
			if (currentAction == 'add')
			{	
				var delImg = $('<img/>').attr({
					'width': '132',
					'height': '18',
					'title': 'Убрать из избранного',
					'src': '/_img/modules/svoi/buttons/del-favorites.gif'
				});
				
				var href = $('<a></a>')
					.attr('href','javascript:void(0)')
					.bind('click', 
						{'_type': currentType, '_id': currentId},
						mod_passport_favorites_loader.removeFromFavorite)
					.append(delImg);
					
				container.empty('');
				container.append(href);
			}
			else if (currentAction == 'delete')
			{	
				if (currentMain == 1)
				{
					container.remove();
					return;
				}
				var addImg = $('<img/>').attr({
					'width': '135',
					'height': '18',
					'title': 'Добавить в избранное',
					'src': '/_img/modules/svoi/buttons/add-favorites.gif'
				});
				
				var href = $('<a></a>')
					.attr('href','javascript:void(0)')
					.bind('click',
						{'_type': currentType, '_id': currentId}, 
						mod_passport_favorites_loader.addToFavorite)
					.append(addImg);
					
				container.empty('');
				container.append(href);
			}
		}
		
	},
	
	error: function(XMLHttpRequest, textStatus, errorThrown)
	{
		alert('Ошибка');
	}
}
