var mywin;
function opentchat(mylink, windowname, refocus, to)
{
	if(to)
		$.post("tchat_sql.php", { start: "true", to: to} ,
			function(data){
				//alert("Data Loaded: " + data);
			}
		);

	mylink="tchat.php";
	var mywin, href;

	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;

	mywin = window.open('', windowname, 'width=400,height=455,scrollbars=yes');

	// if we just opened the window
	if ( mywin.closed || (!mywin.document.URL) || (mywin.document.URL.indexOf("about")==0) )
	   mywin.location=href;
	else if (refocus)
	   mywin.focus();


	$('#bulle_messenger').css({height:'20px'});
	$('#bulle_messenger').css({opacity:'0'});

	return false;
}


function verifTchat() {
	$('#bulle_messenger').css({height:'20px'});
	$('#bulle_messenger').css({opacity:'0'});
	$.post("tchat_sql.php", { verifmsg: "true" },
		function(xml){
				$("root",xml).each(function(){			//on parse le xml
					$("message_preview",this).each(function(){
						var from = $("from",this).text();
						var corps = $("corps",this).text();
						alert_messenger(corps,from);
					});
					$("message_encours",this).each(function(){
						var from = $("from",this).text();
						$("#tchaticon_"+from).Pulsate(300,15);
					});
				});
		}
	);
	setTimeout('verifTchat();',15000);	//verifier les message tout les ...
}


$(document).ready(function () {	//la ou tout commence

	verifTchat();

});

function alert_messenger(text,login) {
	$('#bulle_messenger').animate({height:60,opacity:1}, "slow",
		function(){
			$('#bulle_messenger').html('<a href="#" style="color:white;text-decoration:none;"><strong>' +login+ ' dit </strong>: ' +text+ '<a/>');
			$('#bulle_messenger').css({height:'60px'});
			$('#bulle_messenger').css({opacity:'1'});
			$('#bulle_messenger').css({cursor:'pointer'});
			$('#bulle_messenger').click(function () {
				return opentchat(this, 'tchat', true, login)
			});
	 	}
	 );
}

function messenger_init() {
	$(document.body).append('<div id="bulle_messenger"></div>');
}

$(function(){
	messenger_init();
});
