window.onload = init;
var navigation_active = 'ajouter';
var current_location = '';

function init()
{
	$('form_add_form').onsubmit = add_question;
	$('grand_ecart').hide();
	$('petit_ecart').hide();
	
	check_location();
	setInterval("check_location()", 10);
}

function change_location(location)
{
	parent.location.hash = location;
	current_location = location;
}

function check_location()
{
	var hash = parent.location.hash;
	hash = hash.substring(1);
	if (hash != current_location)
	{
		charge_page(hash);
		current_location = hash;
	}
}

function charge_page(url)
{
	$('alaune').update('');
	$('alaune').show();
	new Ajax.Request('/charge_page.php', {
		parameters: 'url=' + encodeURIComponent(url),
		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON();
			$('alaune').update(json.html);
			if(json.url!='') change_location(json.url);
			document.title = json.title;
		}
	});
}

function une_autre()
{
	new Effect.BlindUp('alaune_resultat', 	{ 
		duration: 0.4,
		afterFinish: function()	{
			$('alaune').update('');
			$('alaune').show();
			new Ajax.Request('/une_autre.php', {
				onSuccess: function(transport)
				{
					var json = transport.responseText.evalJSON();
					$('alaune').update(json.html);
					if(json.url!='') change_location(json.url);
					document.title = json.title;
				}
			});
		}
	});
	return false;
}

function add_question()
{
	var the = $('form_add_the').value;
	var cafe = $('form_add_cafe').value;
	
	if(the == '' || cafe == '')
	{
		alert('Veux-tu bien proposer deux options ?');
		return false;
	}
	
	if(!$('i_am_a_human').checked)
	{
		alert('Tu n\'es réellement pas un humain ?');
		return false;
	}	
	

	Effect.BlindUp('add_form', {
		duration: 0.4,
		afterFinish:function()
		{
			$('add_message').update('<h2>« '+ the +' ou '+ cafe +' ? »</h2><p>Votre participation est enregistrée.<br />Elle est soumise à validation par une haute autorité liée à des services secrets biélorusses.</p>');
			$('add_message').show();
		}
	});
								
	
	new Ajax.Request('/add.php', {
		method: 'post',
		parameters: 'cafe=' + encodeURIComponent(cafe) + '&the=' + encodeURIComponent(the)
	});

			
	return false;
}


function vote(idquestion, idreponse)
{
	Effect.BlindUp('alaune', { duration: 0.2 });
	
	$('alaune_resultat').update('Chargement...');
	$('alaune_resultat').show();

	new Ajax.Request('/vote.php', {
		method: 'post',
		parameters: 'idquestion=' + encodeURIComponent(idquestion) + '&idreponse=' + encodeURIComponent(idreponse),
		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON();
			$('alaune_resultat').update(json.html);
			if(json.url!='') change_location(json.url);
			document.title = json.title;			
		}
	});
	
	return false;
}


function navigation(e)
{
	$('navigation_' + navigation_active).className = '';
	$('navigation_' + e).className = 'active';
	$(navigation_active).hide();
	$(e).show();
	navigation_active = e;
	
	if(navigation_active == 'ajouter')
		$('footer').morph('height:170px;', { duration:0.4 });
	else
		$('footer').morph('height:250px;', { duration:0.4 });
	
	
	return false;
}
