function chat_open()
{
	$('#close_chat').html('Close miniChat') ;	
	$('#close_chat').unbind('click') ;
	$('#main_content').animate({
		width: '635'
	}, 100);
	
	$('#chat_express_div').slideToggle('fast', function() {
		$('#close_chat').attr('onclick','').click(function(){chat_close()});
	}) ;
	document.getElementById('div_chat_msg').scrollTop = document.getElementById('div_chat_msg').scrollHeight;
	chat_toggle() ;
}

function chat_close()
{
	$('#close_chat').html('Open Chat express<div id="chat_new_msg"></div>') ;	
	$('#close_chat').unbind('click') ;
	$('#main_content').animate({
		width: '890'
	}, 100);

	$('#chat_express_div').slideToggle('fast', function() {
		$('#menu_content').animate({height:'20'},1) ;
		$('#close_chat').attr('onclick','').click(function(){chat_open()});
	}) ;

	chat_toggle() ;
}

function chat_toggle()
{
	$.ajax({
		url: "/scripts/chat_set_toogle.php",
		type: "GET",
		cache: false,
		success: function(html) {

		}
	}) ;	
}

function chat_logout()
{
	$.ajax({
		url: "/scripts/chat_logout.php",
		success: function(res){
			$("#div_chat_login").show() ;
			$("#div_chat_add_msg").hide() ;
		}
	}) ;	
}

function chat_close_new_msg()
{
	$("#chat_new_msg").hide() ;
}

function chat_get_if_new_msg()
{
	$.ajax({
		url: "/scripts/chat_get_if_new_msg.php",
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data) {
			if(data.result == 1) {
				chat_get_msg() ;
				/* Si la chat est ferme, alors on met le text dans la div */
				if(!$('#chat_express_div').is(":visible")) {
					$("#chat_new_msg").show() ;
					$("#chat_new_msg").html(data.txt) ;
					setTimeout("chat_close_new_msg()", 5000) ;
				}
			}
			setTimeout("chat_get_if_new_msg()", 5000) ;
		}
	}) ;
}

function chat_get_msg()
{
	$.ajax({
		url: "/scripts/chat_get_msg.php",
		type: "GET",
		cache: false,
		success: function(html) {
			$("#div_chat_msg").html(html) ;
			document.getElementById('div_chat_msg').scrollTop = document.getElementById('div_chat_msg').scrollHeight;
		}
	}) ;
}

function chat_login()
{
	var loginChart = 0 ;
	if(document.getElementById('cbx_chat_charte').checked == true) {
		loginChart = 1 ;
	}
	
	$.ajax({
		url: "/scripts/chat_login.php",
		type: "POST",
		data: ({chat_login : document.getElementById('txt_chat_login').value,chat_charte : loginChart}),
		dataType: "json",
		success: function(json){
			var loginResult = json.result ;
			if(loginResult == '-1') {
				alert("Merci d'accepter la charte") ;
			}
			if(loginResult == '0') {
				alert("Le login doit contenir plus de deux caracteres") ;
			}
			if(loginResult == '1') {
				$("#div_chat_login").hide() ;
				$("#div_chat_add_msg").show() ;
			}
		}
	}) ;

}

function chat_add_msg()
{
	$.ajax({
		url: "/scripts/chat_add_msg.php",
		type: "POST",
		data: ({chat_msg : document.getElementById('txt_chat_msg').value}),
		dataType: "html",
		success: function(html){
			document.getElementById('txt_chat_msg').value = '' ;
			chat_get_msg() ;
		}
	}) ;
}

