function fav_formatter(type)
{ 
	$("span#" + type + ".fav").each(function(i,o) {
		var obj = $(this);
		var id = obj.attr('rel');

		if(fav_get_status(type, id)) {
			obj.html("<a title='Click to remove from your favorites' onclick=\"fav_msg_remove('"+type+"','"+id+"')\" href=\"javascript:void(0);\"><img src=\"/img/bg/fav_on.png\"></a>").show();
		} else {
			obj.html("<a title='Click to add to your favorites' onclick=\"fav_msg_add('"+type+"','"+id+"')\" href=\"javascript:void(0);\"><img src=\"/img/bg/fav_off.png\"></a>").show();
		}
	}); 
}

function fav_formatter_textonly(type)
{ 
	$("span#" + type + ".fav-text").each(function(i,o) {
		var obj = $(this);
		var id = obj.attr('rel');
		if(fav_get_status(type, id)) {
			obj.html("<a title='Click to remove from your favorites' onclick=\"fav_msg_remove('"+type+"','"+id+"')\" href=\"javascript:void(0);\" style=\"text-decoration:none;\"><strong style=\"color:Orange;\">*</strong></a>").show();
		} else {
			obj.html("<a title='Click to add to your favorites' onclick=\"fav_msg_add('"+type+"','"+id+"')\" href=\"javascript:void(0);\" style=\"text-decoration:none;\"><strong style=\"color:#ccc;\">*</strong></a>").show();
		}
	}); 
}


function fav_msg_add(type,id) {
	
	var obj = $("span.fav[rel="+id+"]");
	obj.html('<img src="/img/bg/ajax-loader.gif">');
	 
	var data = '';
	$.getJSON("/fav/ad", {id:id, type:type}, function(data) {
		obj.html("<a title='Click to remove from your favorites' onclick=\"fav_msg_remove('"+type+"','"+id+"');\" href=\"javascript:void(0);\"><img src=\"/img/bg/fav_on.png\"></a>").show();
		var data = data['msg'];
		$.jGrowl(data);
	}, data);
}

function fav_msg_remove(type,id, t) {
	
	var obj = $("span.fav[rel="+id+"]");
	obj.html('<img src="/img/bg/ajax-loader.gif">');
	 
	var data = '';
	$.getJSON("/fav/de", {id:id, type:type}, function(data) {
		obj.html("<a title='Click to add to your favorites' onclick=\"fav_msg_add('"+type+"','"+id+"');\" href=\"javascript:void(0);\"><img src=\"/img/bg/fav_off.png\"></a>").show();
		var data = data['msg'];
		$.jGrowl(data);
	}, data);
}

function fav_get_status(type, id) {
	try {
		var fav_ids = fav_get_cookie_list(type);
		for (var i=0; i<fav_ids.length; i++) if (fav_ids[i] == id) return true;
	} catch(e) {
		window.console.log(e);
	}
	return false;
}

function fav_type_count_total(type) {
	return fav_get_cookie_list(type).length;
}

function fav_type_count_page(type) {
	var count = 0;
	$("span#" + type + ".fav").each(function(i,o) {
		if(fav_get_status(type, $(this).attr('rel'))) { 
			count++;
		}
	});
	return count;	
}

function fav_get_cookie_list(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			var value = c.substring(nameEQ.length,c.length);
			return value.replace(/[\[\"\]\ ]/g, '').split(",");
		}
	}
	return [];
}