$(document).ready(function(){

	// Slider
	function slide(etat)
	{
		var suite;
		
		if(etat == 1 || etat == 2 || etat == 3)
		{
			suite = etat + 1;
		}
		else
		{
			suite = 1;
		}
		
		$('#ts'+etat).animate({top: '257px'}, 400);
		$('#ts'+suite).delay(1900).animate({top: '213px'}, 400);
		$('#is'+etat).delay(400).animate({left: '-625px'}, 1500).delay(1500).animate({left: '625px'}, 0);
		$('#is'+suite).delay(400).animate({left: '0px'}, 1500);
		
		setTimeout(function(){ slide(suite) }, 5900);
	}
	
	if($('#is1'))
	{
		setTimeout(function(){ slide(1) }, 4000);
	}
	
	// Tchat
	var ancien_scrollheight;
	
	function tchat()
	{
		if($('#messages_tchat') && $('#messages_tchat').is(':visible'))
		{
			// Chargement des messages
			$.ajax({
				url: 'http://www.next-nintendo.com/tchat-recup.html',
				success: function(msg)
				{
					$('#messages_tchat').html(msg);
				}
			});
			
			var elem = $('#messages_tchat');
			if(elem[0].scrollHeight - elem.scrollTop() != elem.outerHeight() && ancien_scrollheight != elem[0].scrollHeight)
			{
				elem.scrollTop(elem[0].scrollHeight);
				ancien_scrollheight = elem[0].scrollHeight;
			}
		}

		// Rechargement regulier
		setTimeout(function(){ tchat() }, 1000);
	}
	
	if($('#messages_tchat'))
	{
		tchat();
	}
	
	// Form d'envoi
	function envoi_tchat()
	{
		var message = $('#input_tchat').val();
		$.post('http://www.next-nintendo.com/tchat-envoi.html', {message:message});
		$('#input_tchat').val('');
	}
	
	$('#bouton_tchat').click( function() { 
	
		envoi_tchat();
	
	} );
	$('#input_tchat').keydown( function(event) {

		if (event.keyCode == '13')
		{
			envoi_tchat();
		}

	});
	
	// Systeme de fermeture/ouverture
	$('#tete_tchat').click( function() { 

		if($.cookie('tchat') == 0)
		{
			$('#corps_tchat').animate({height: '450px'}, 1500);
			$('#messages_tchat').show();
			$.cookie('tchat', 1, { expires: 7, path: '/' });
			$('#tete_tchat span').text('Fermer');
		}
		else
		{
			$('#corps_tchat').animate({height: '30px'}, 1500);
			$('#messages_tchat').hide();
			$.cookie('tchat', 0, { expires: 7, path: '/' });
			$('#tete_tchat span').text('Ouvrir');
		}
	
	} );
	
	// Insertion valide W3C du bouton facebook
	if($('#jaime'))
	{
		var url = $('#jaime').text();
		$('#jaime').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+url+'&amp;layout=standard&amp;show_faces=false&amp;width=600&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:600px; height:23px;" allowTransparency="true"></iframe>');
		$('#jaime').toggle();
	}
	
	// Slide d'identification en haut du site
	$('#connexion').click( function() { 
	
		$('#ident_slide').animate({right: '-151', opacity: 0}, 0).toggle().animate({right: '-1', opacity: 1}, 1000);
	
	} );
});
	
	// Formulaires
	function insert_form(debut, fin, id)
	{
		var input = document.getElementById(id);

		input.focus();

		if(typeof document.selection != 'undefined')
		{
			var range = document.selection.createRange();
			var insText = range.text;
			range.text = debut + insText + fin;
			range = document.selection.createRange();

			if (insText.length == 0)
			{
				range.move('character', -fin.length);
			}
			else
			{
				range.moveStart('character', debut.length + insText.length + fin.length);
			}
			range.select();
		}
		else if(typeof input.selectionStart != 'undefined')
		{
			var start = input.selectionStart;
			var end = input.selectionEnd;
			var insText = input.value.substring(start, end);
			input.value = input.value.substr(0, start) + debut + insText + fin + input.value.substr(end);
			var pos;
			if (insText.length == 0)
			{
				pos = start + debut.length;
			}
			else
			{
				pos = start + debut.length + insText.length + fin.length;
			}
			input.selectionStart = pos;
			input.selectionEnd = pos;
		}
		else
		{
			var pos;
			var re = new RegExp('^[0-9]{0,3}$');
			while(!re.test(pos))
			{
				pos = prompt("insertion (0.." + input.value.length + "):", "0");
			}
			if(pos > input.value.length)
			{
				pos = input.value.length;
			}

			var insText = prompt("Veuillez taper le texte");
			input.value = input.value.substr(0, pos) + debut + insText + fin + input.value.substr(pos);
		}
	}

	function insert_lien(id)
	{
		var input = document.getElementById(id);

		var lien = prompt('Entrez le lien (il doit commencer par http://, https:// ou ftp://)');

		if(lien == '' || lien == null)
		{
			alert('Votre lien ne doit pas être vide');
		}
		else
		{
			var titre_lien = prompt('Entrez le texte du lien');

			if(titre_lien == '' || titre_lien == null)
			{
				alert('Votre titre ne doit pas être vide');
			}
			else
			{
				input.focus();
				input.value = input.value + '[lien url=' + lien + ']' + titre_lien + '[/lien] ';
			}
		}
	}

	function effacement_zone(valeur, valeur_initiale, id_zone)
	{
		if((valeur == valeur_initiale))
		{        
			document.getElementById(id_zone).value='';
		}
	}

	// Images
	function redim(img)
	{
		widthmax = 360;

		if(img.width > widthmax)
		{
			img.height = widthmax * img.height / img.width;
			img.width = widthmax;  
			img.style.cursor = "pointer";
			img.setAttribute("onclick", "window.open(\""+img.src+"\");");
		}
	}

	function redim2(img)
	{
		widthmax = 120;

		if(img.width > widthmax)
		{
			img.height = widthmax * img.height / img.width;
			img.width = widthmax;  
			img.style.cursor = "pointer";
			img.setAttribute("onclick", "window.open(\""+img.src+"\");");
		}
	}
