function vB_AJAX_Favs_Init(favstableid)
{
	if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
	{
		var link_list = fetch_tags(fetch_object(favstableid), 'a');
		for (var i = 0; i < link_list.length; i++)
		{															 
			if (link_list[i].id && link_list[i].id.substr(0, 4) == 'fav_')
			{
				link_list[i].onclick = fav_click;
			}
		}
	}
}

function vB_AJAX_Fav(objid)
{
	this.objid = objid;
	this.mediaid = null;
	this.xml_sender = null;

	if (this.objid && this.objid.substr(0, 4) == 'fav_' && (match = this.objid.match(/^fav_(\d+)$/)))
	{
		this.mediaid = match[1];
	}
	
	var me = this;

	this.resolve = function()
	{
		if (!this.mediaid) {
			return false;
		}
		this.xml_sender = new vB_AJAX_Handler(true);
		this.xml_sender.onreadystatechange(this.onreadystatechange);
		this.xml_sender.send('media.php?do=favs&mediaid=' + PHP.urlencode(this.mediaid), 'do=favs&ajax=1&mediaid=' + PHP.urlencode(this.mediaid));
	}

	this.onreadystatechange = function()
	{
		if (me.xml_sender.handler.readyState == 4 && me.xml_sender.handler.status == 200)
		{
			if (me.xml_sender.handler.responseXML)
			{
				// var obj = fetch_object(me.objid);
				var obj_remove = fetch_object('icon_remove_fav_' + me.mediaid);
				var obj_add = fetch_object('icon_add_fav_' + me.mediaid);
				var fav = me.xml_sender.fetch_data(fetch_tags(me.xml_sender.handler.responseXML, 'fav')[0]);
				switch (fav) {
					case 'added':
						obj_remove.style.display = '';
						obj_add.style.display = 'none';
						alert('Media was added to your favourites');
					break;
					case 'removed':
						obj_remove.style.display = 'none';
						obj_add.style.display = '';
						alert('Media was removed from your favourites');
					break;
					case 'notloggedin':
						alert('Please log-in to add/remove audio to/from my favorites');
						window.top.location.reload();
					break;
				}
			}
			if (is_ie)
			{
				me.xml_sender.handler.abort();
			}
		}
	}
}


function fav_click(e)
{
	var fav = new vB_AJAX_Fav(this.id);
	fav.resolve();
	return false;
}
