function ignore(module, item)
{
	if(!isLoggedIn()) {
		askLogin();
		return false;
	}
	
	var ele = $('ignore_button_'+item);
	
	ele.removeClassName('ignore_button');
	ele.startWaiting('nobg');
	new Ajax.Request("/ajax/fav.php", {
					onSuccess : function(r)
					{
						ele.stopWaiting();
						if(r.responseText == 'l')
						{
							ele.addClassName('ignore_button');
							askLogin();
						}
						else if(r.responseText == 'e')
						{
							ele.addClassName('ignore_button');
							alert(E_INVALID_REQUEST);
						}
						else if(r.responseText == 'v')
						{
							ele.addClassName('ignore_button');
							alert('You\'ve alreay added this item in your ignore list');
						}
						else
						{
							ele.onclick = function () {alert('You\'ve alreay added this item in your ignore list');return false;};
							ele.addClassName('ignore_button');
							$('fav_button_'+item).setStyle({'visibility': 'hidden'});
							alert('Item has been added in your ignore list');
						}
					},
					method: 'post',
					parameters: 'action=ignore&module_id='+module+'&item_id='+item
					});
}

function favRequest(module, item, action)
{
	if(!isLoggedIn()) {
		askLogin();
		return false;
	}
	
	var ele = $('fav_button_'+item);
	
	//ele.addClassName('fav_button_waiting');
	ele.removeClassName(action+'_button');
	ele.startWaiting('nobg');
	
	if(action == 'favup')
	{
		var classSucess = 'favdown_button';
		var classError = 'favup_button';
		var onClickFunction = function () { favRequest(module, item, 'favdown');return false; }
	}
	else if(action == 'favdown')
	{
		var classSucess = 'favup_button';
		var classError = 'favdown_button';
		var onClickFunction = function () { favRequest(module, item, 'favup');return false; }
	}
	
	new Ajax.Request("/ajax/fav.php", {
					onSuccess : function(resp)
					{
						ele.stopWaiting();
						ele.removeClassName(classError);
						ele.removeClassName(classSucess);
						
						if(resp.responseText == 'l')
						{
							ele.addClassName(classError);
							askLogin();
						}
						else if(resp.responseText == 'e')
						{
							ele.addClassName(classError);
							alert(E_INVALID_REQUEST);
						}
						else if(resp.responseText == 'v')
						{
							ele.onclick = onClickFunction;
							ele.addClassName(classSucess);
							alert('You\'re alreay a fan of this item');
						}
						else
						{
							ele.onclick = onClickFunction;
							ele.addClassName(classSucess);
							$('fanscount_'+item).hide();
							effect('appear', $('fanscount_'+item), '', '', 1);
							$('fanscount_'+item).update(resp.responseText);
							
							if(action == 'favdown')
								setFavButtons(item, 0);
							else if(action == 'favup')
								setFavButtons(item, 1);
						}
					},
					method: 'post',
					parameters: 'action='+action+'&module_id='+module+'&item_id='+item
					});
}

function vote(ele, info, module, item, old_avg)
{
	ele.startWaiting();
	new Ajax.Request("/ajax/rate.php", {
										onSuccess : function(r)
										{
											ele.stopWaiting();
											if(r.responseText == 'e')
											{
												currentRating = old_avg;
												alert(E_INVALID_REQUEST);
											}
											else if(r.responseText == 'v')
											{
												currentRating = old_avg;
												alert('You\'ve alreay rated this item');
											}
											else
											{
												currentRating = r.responseText;
												$('item_votes_avg').update(currentRating);
												$('item_votes_count').update(parseInt($('item_votes_count').innerHTML) + 1);
												alert('Item has been rated successfully');
											}
											initRating(item, currentRating, module, true);
										},
										method: 'post',
										parameters: 'module_id='+module+'&item_id='+item+'&rating='+info.rating
										});
	
}

function initRating(gid, avg, mid, lckd)
{
	if(isLoggedIn()) {
		ior = true;
		lor = true;
	} else {
		ior = false;
		lor = false;
	}
	$('rating-'+gid).update('');
	new Starbox('rating-'+gid, avg, {
		buttons: 30, identity: false, indicator: false, locked: lckd, max: 5, lockOnRate: lor, ignoreOnRate: ior,
		onRate: function (e, i) {
			if(isLoggedIn())
				vote(e, i, mid, gid, avg)
			else {
				//initRating(gid, avg, mid, lckd)
				askLogin();
			}
		}
	});
}

itemInfo = Class.create();
itemInfo.prototype = {
	
	Listeners: {
		mouseover: 'enterHover',
		mouseout: 'leaveHover'
	},
	
	initialize: function(id) {
		
		this.element = $('m_c_main_desc_'+id);
		this.element.setStyle({opacity: 0});
		
		this._listeners = { };
		var listener;
		
		$H(this.Listeners).each(function(pair) {
			listener = this[pair.value].bind(this);
			this._listeners[pair.key] = listener;
			this.element.observe(pair.key, listener);
		}.bind(this));
		
		/*Event.observe(this.a, 'mouseover', function(event) {
			//var elt = Event.element(event);
			//alert(elt);
			//alert(this.ele.id);
			this.ele.show();
		}.bind(this));
		
		Event.observe(this.ele, 'mouseout', function(event) {
			//var elt = Event.element(event);
			//alert(elt);
			//alert(this.ele.id);
			this.ele.hide();
		}.bind(this));*/
	},
	
	enterHover: function() {
		new Effect.Opacity(this.element, {duration: 0.2, to: 0.8, queue: 'start'});
	},
	
	leaveHover: function() {
		new Effect.Opacity(this.element, {duration: 0.0, to: 0.0, queue: 'end'});
	}
}

function showItemDesc(id)
{
	a = $('m_c_main_a_'+id);
	ele = $('m_c_main_desc_'+id);
	
	Event.observe(a, 'mouseover', function(event) {
		//var elt = Event.element(event);
		//alert(elt);
		alert(ele.id);
		ele.show();
	});
	
	/*Event.observe(ele, 'mouseout', function(event) {
		ele.hide();
	});*/
	
	//effect('blind', $('m_c_main_desc_'+id), '', '', '', '', '', '0.2');
	/*$('m_c_main_desc_'+id).show();
	
	new PeriodicalExecuter(function(pe) {
		$('m_c_main_desc_'+id).hide();
		alert(pe.element);
		pe.stop();
	}, 3);*/
	//alert('a');
	/*Event.observe(document.body, 'mouseover', function(event) {
			var elt = Event.element(event);
			//alert(elt.id);
			if(elt.tagName == 'BODY')
			{
				$('m_c_main_desc_'+id).hide();
				Event.stop(event);
			}
	});*/
	/*new PeriodicalExecuter(function(pe) {
		Event.observe(document.body, 'mouseover', function(event) {
			$('m_c_main_desc_'+id).hide();
			Event.stop(event);
			pe.stop();
		});
	}, 2);*/
	
	/*$('m_c_main_desc_'+id).observe('mouseout', function(event) {
		Event.element(event).hide();
	});*/
}

function hideItemDesc(id)
{
	Event.observe(document.body, 'mouseout', function(event) {
		alert('a');
	});
}

function setFavButtons(id, isFan, isAntiFan)
{
	favButton = $('fav_button_'+id);
	ignoreButton = $('ignore_button_'+id);
	
	if(isFan)
		ignoreButton.hide();
	else
		ignoreButton.show();
}