function sendReport(type, id, complaint, comment, loading_id, reponse_message_id)
{
	if ($(loading_id))
		$(loading_id).style.visibility = 'visible';
	if ($(reponse_message_id))
		$(reponse_message_id).setText('');

	new Ajax('/?page=questions/ajax_report',
			{
				data : { target_type : type, target_id : id, complaint : complaint, submit_comment : comment },
				onSuccess : function()
							{
								if ($(reponse_message_id))
								{
									switch(this.response['text'])
									{
										case 'ok' :
											$(reponse_message_id).setText('Demande enregistrée.');
										break;
										case 'error-1' :
											$(reponse_message_id).setText('Paramètres insuffisants.');
										break;
										case 'error-2' :
											$(reponse_message_id).setText('Vous devez être un membre connecté.');
										break;
										case 'error-3' :
											$(reponse_message_id).setText('Erreur de base de donnée, réessayez plus tard.');
										break;
										default :
											$(reponse_message_id).setText('Erreur de connexion, réessayez plus tard.');
										break;
									}
								}
							},
				onFailure : function()
							{
								if ($(reponse_message_id))
									$(reponse_message_id).setText('Erreur de connexion, réessayez plus tard.');
							},
				onComplete : function()
							{
								if ($(loading_id))
								$(loading_id).style.visibility = 'hidden';
							}
			}
	).request();
}

current_tooltip = '';
current_tooltip_element = null;
tooltip_timeout = null;
function showTooltip(element, id, x_offset, y_offset)
{
	if (current_tooltip && current_tooltip!=id)
		hideTooltip(current_tooltip);

	if (current_tooltip == id && current_tooltip_element == element)
		hideTooltip(id);
	else
	{
		if (element)
		{
			var position = offsetPos(element);
	
			var x = position[0] + x_offset;
			var y = position[1] + y_offset;

			$(id).style.left = x+'px';
			$(id).style.top = y+'px';
		}
		appearShow(id, 0.9);
		current_tooltip = id;
		current_tooltip_element = element;
		tooltip_timeout = setTimeout('hideTooltip(\''+id+'\', 0.9)', 30*1000);
	}
}
function hideTooltip(id)
{
	clearTimeout(tooltip_timeout);
	appearHide(id, 0.9);
	current_tooltip = '';
	current_tooltip_element = null;
}

current_parent = -1;
current_category = -1;
parent_level = new Array();

function changeBaseCategory(select, id_sector)
{
	if (select.options[select.selectedIndex].value)	
	{
		var select_value = select.options[select.selectedIndex].value.split('-');
		
		if ($('category-list-'+current_parent))
			$('category-list-'+current_parent).remove();
		if ($('category-list-0'))
			$('category-list-0').remove();
		current_parent = current_category = -1;

		switch (select_value[0])
		{
			case 'cat' :
				selectCategory(select_value[1], 0, select.options[select.selectedIndex].text, 'category');
				chercherCategories(select_value[1], 0, id_sector, select.options[select.selectedIndex].text);
				getCategoryQuestions(select_value[1]);
			break;
			case 'jeu' :
				selectCategory(select_value[1], 0, select.options[select.selectedIndex].text, 'game');
				chercherCategories(0, 0, id_sector, select.options[select.selectedIndex].text, select_value[1]);
				getGameQuestions(select_value[1]);
			break;
		}
	}
	else
		selectCategory('','','category');
}
function toggleCategory(id)
{	
	if ($('category_'+id))
	{
		if ($('category_'+id).hasClass('category_list_element_exp'))
			$('category_'+id).toggleClass('category_list_exp_selected');
		else
			$('category_'+id).toggleClass('category_list_selected');
	}
}

function chercherCategories(id_parent, level, id_sector, parent_name, id_game)
{
	if (id_parent == current_parent)
	{
		if ($('category-list-'+id_parent))
			$('category-list-'+id_parent).remove();
		current_parent = -1;
	}
	else
	{
		parent_level[id_parent] = level;

		if (parent_level[current_parent] == level && $('category-list-'+current_parent))
			$('category-list-'+current_parent).remove();

		current_parent = id_parent;
		$('category-loading').style.visibility = 'visible';
		if (!id_game)
			id_game=0;
		new Ajax('/?page=questions/ajax_get_categories',
					{
						method : 'post',
						encoding : 'ISO-8859-15',
						data : {id_parent : id_parent, id_sector: id_sector, id_game : id_game},
						onSuccess : function()
						{
							if (this.response['xml'])
							{
								var html = '';
	
								var id;
								var name; 
								var haschild;
								// Categories
								var categories = this.response['xml'].getElementsByTagName('category');
								for(i=0;i<categories.length;i++)
								{
									id = categories[i].getElementsByTagName('id')[0].firstChild.nodeValue;
									name = categories[i].getElementsByTagName('name')[0].firstChild.nodeValue;
									haschild = categories[i].getElementsByTagName('haschild').length > 0;
									if (name != '_root')
									{
										html += '<li id="category_'+id+'" class="category_list_element'+(haschild ? ' category_list_element_exp' : '')+'" onclick="selectCategory('+id+', '+id_parent+', \''+(parent_name ? parent_name+' &raquo; ' : '')+name+'\', \'category\')';
										if (haschild)
											html += ';chercherCategories('+id+', '+(level+1)+', '+id_sector+', \''+(parent_name ? parent_name+' &raquo; ' : '')+name+'\')';
										html += '">'+name;
										html += '</li>';
									}
								}
	
								if (!$('category-list-'+id_parent))
								{
									if (html)
										$('categories-slide').innerHTML += '<ul class="category_list" id="category-list-'+id_parent+'">'+html+'</ul>';
								}
								else
								{
									$('category-list-'+id_parent).setHTML(html);
									$('category-list-'+id_parent).style.display = 'block';
								}
							}
	
						},
						onComplete : function() {$('category-loading').style.visibility = 'hidden';}
					}).request();
	}
}

function getCategoryQuestions(id_category)
{
	getQuestions('category', id_category, 0, 3);
}
function getGameQuestions(id_game)
{
	getQuestions('game', id_game, 0, 3);
}
function getQuestions(type, id_type, offset, limit)
{
	if ($('question-list'))
	{
		id_category = id_game = id_sector = '';
		switch(type)
		{
			case 'category' : id_category = id_type; break;
			case 'game' : id_game = id_type; break;
			case 'sector' : id_sector = id_type; break;
		}
		$('question-list').setHTML('');

		if ($('recherche-loading'))
			$('recherche-loading').style.visibility = 'visible';

		new Ajax('/?page=questions/ajax_get_questions',
				{
					method  : 'post',
					encoding : 'ISO-8859-15',
					data : { id_category : id_category, id_game : id_game, id_sector : id_sector, offset : offset, limit : limit},
					onSuccess : function()
								{
									var html = '';
	
									var questions = this.response['xml'].getElementsByTagName('question');
									if (questions.length)
									{
										html += '<ul>';
										for(i=0;i<questions.length;i++)
										{
											text = questions[i].getElementsByTagName('text')[0].firstChild.nodeValue;
											url = questions[i].getElementsByTagName('url')[0].firstChild.nodeValue;
		
											html += '<li><a title="Consulter les r&eacute;ponses" href="'+url+'">'+text+'</a></li>';
										}
										html += '</ul>';
										$('question-list').setHTML(html);
										$('questions-existantes').style.display = 'block';
									}
									else
										$('questions-existantes').style.display = 'none';
								},
					onFailure : function() { $('questions-existantes').style.display = 'none'; },
					onComplete : function()	{ $('recherche-loading').style.visibility = 'hidden'; }
				}
	
		).request();
	}
}

function xpGain(element, xp)
{
	if (xp)
	{
		var position = offsetPos(element);
		var element_width = element.offsetWidth;
	
		x = position[0] + element_width/2;
		y = position[1];
		$('gain-xp').style.left = x+'px';
		$('gain-xp').style.top = y+'px';
		$('gain-xp').setText((xp > 0 ? '+' : '')+xp+' XP');
//		$('gain-xp').style.display = 'block';
		var fx = new Fx.Styles('gain-xp', 
					{
						duration : 800,
						onComplete : function()
									{
	//									appearHide('gain-xp');
									}
					});
	
//		fx.start({'top' : [y, y-40] });
//		appearHide('gain-xp', 1, 800);
	}
}
