var lastMessageIdShoutbox = 0;
var refTimeOut;
var MODE_SHOUTBOX_POPUP = 1;
var MODE_SHOUTBOX_BLOC = 0;
var modeShoutbox = 0;

function refreshShoutbox()
{
	ajaxSendFunction(url_site+"inc/action/shoutbox.action.php", "action=refresh&lastid="+lastMessageIdShoutbox, refreshShoutboxResponse);
}

function refreshShoutboxResponse(response)
{
	analyseAjaxResponse(response);
}

//on descend le scroll en bas
function scrollDownShoutbox()
{
	document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
}

//on envoit un nouveau message à la shoutbox
function sendMessageShoutbox()
{
	ajaxSendFunction(url_site+"inc/action/shoutbox.action.php", "action=post&lastid="+lastMessageIdShoutbox+'&texte='+formatMsgShoutbox(document.getElementById('msg_shoutbox').value)+'&idanimal='+document.getElementById('id_animal_envoi_shoutbox').value, sendMessageShoutboxResponse);

	//on vide le champ de message
	document.getElementById('msg_shoutbox').value = '';
}

//on formate le message à envoyer à la shoutbox
function formatMsgShoutbox(msg)
{
	//msg = msg.replace('"','\"');
	//if(navigator.appName != "Netscape")
	msg = msg.replace(/&/g,'');
	msg = encodeURIComponent(msg);
	//msg = msg.replace(/#/g,'%23');
	return msg;
}

//on récupere la reponse du serveur
function sendMessageShoutboxResponse(response)
{
	analyseAjaxResponse(response);	
}

//on analyse et traite la réponse ajax
function analyseAjaxResponse(response)
{
	var refreshTime = 0;
	var node;
	var newMessage = false;
	var divMsgs = document.getElementById('messages');
	
	//instanciation du parser
	var xml = instanciateXmlParser(response);
	
	
	//pour chaque element apres la racine
	for(i=0; xml.firstChild.childNodes[i]; i++)
	{	
		node = xml.firstChild.childNodes[i];
		
		//c'est le temps de refresh
		if(node.nodeName == 't')
		{
			refreshTime = node.firstChild.nodeValue;
		}
		//c'est un nouveau message
		else if(node.nodeName == 'message')
		{
			//on ajoute le nouveau message
			addMessageHtml(node);
			if(modeShoutbox == MODE_SHOUTBOX_BLOC && divMsgs.childNodes.length > 10)
				removeFirstMessageHtml();
			if(node.getAttribute('id') > lastMessageIdShoutbox)
			{
				lastMessageIdShoutbox = node.getAttribute('id');
			}
			newMessage = true;
		}
	}
	
	if(newMessage)
		scrollDownShoutbox();
	
	//on met à jour le temps de refresh
	if(refreshTime != 0)
	{
		clearTimeout(refTimeOut);
		refTimeOut = setTimeout("refreshShoutbox()",refreshTime);
	}
}

function addMessageHtml(node)
{
	var divMsgs = document.getElementById('messages');
	var divMsg = document.createElement('div');
	if(session_id_user==node.getAttribute('user'))
	{
		divMsg.className = 'msgShutboxPerso';
	}
	else
	{
		divMsg.className = 'msgShutbox';
	}
	var dateMessage = new Date();
	dateMessage.setTime(node.getAttribute('timestamp')*1000);
	var dateMinutes = dateMessage.getMinutes();
	if(dateMinutes < 10)
		dateMinutes = '0'+dateMinutes;
	var html = '';


	html += '<div class="picShutbox textOrange tresTresPetitText">';
	if(modeShoutbox==1)
	{
		if(node.getAttribute('idPhotoAnimal')!="")
		{
			html += '	<span class="curseurHand commeLiens" onclick="if(window.opener && !window.opener.closed){window.opener.location.href=\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\';}else{window.open(\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\');};">';
			html += '		<img src="'+url_site+'photos/galeries/'+node.getAttribute('idPhotoAnimal')+'_SMALL.jpg" alt="'+node.getAttribute('nomAnimal')+'" title="'+node.getAttribute('nomAnimal')+'" width="26" />';
			html += '	</span><br />';
		}
		else
		{
			html += '	<span class="curseurHand commeLiens" onclick="if(window.opener && !window.opener.closed){window.opener.location.href=\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\';}else{window.open(\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\');};">';
			html += '		<img src="'+url_site+'images/charteV1/picto/avatar-defaut_small.jpg" alt="'+node.getAttribute('nomAnimal')+'" title="'+node.getAttribute('nomAnimal')+'" width="26" />';
			html += '	</span><br />';
		}
	}
	else
	{
		if(node.getAttribute('idPhotoAnimal')!="")
		{
			html += '	<a href="'+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'" target="_blank">';
			html += '		<img src="'+url_site+'photos/galeries/'+node.getAttribute('idPhotoAnimal')+'_SMALL.jpg" alt="'+node.getAttribute('nomAnimal')+'" title="'+node.getAttribute('nomAnimal')+'" width="26" />';
			html += '	</a><br />';
		}
		else
		{
			html += '	<a href="'+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'" target="_blank">';
			html += '		<img src="'+url_site+'images/charteV1/picto/avatar-defaut_small.jpg" alt="'+node.getAttribute('nomAnimal')+'" title="'+node.getAttribute('nomAnimal')+'" width="26" />';
			html += '	</a><br />';

		}

	}
	html += '		'+dateMessage.getHours()+':'+dateMinutes;
	html += '	</div>';
	html += '	<div class="txtShutbox pasTropPetitText lienBleu">';
	if(modeShoutbox==1)
	{
		html += '	<span class="curseurHand commeLiens" onclick="if(window.opener && !window.opener.closed){window.opener.location.href=\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\';}else{window.open(\''+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'\');};">';
		html += 		node.getAttribute('nomAnimal');
		html += '	</span> : ';
	}
	else
	{
		html += '	<a href="'+url_site+'animal/chien/'+node.getAttribute('idAnimal')+'-'+node.getAttribute('nomAnimal')+'">'+node.getAttribute('nomAnimal')+'</a> : ';
	}
	html += '		'+node.getAttribute('texte');
	html += '	</div>';
	divMsg.innerHTML = html;
	divMsgs.appendChild(divMsg);
}

function removeFirstMessageHtml()
{
	var divMsgs = document.getElementById('messages');
	removeNode(divMsgs.firstChild);	
}

function openPopupShoutbox()
{
	//on prévient le serveur qu'on passe en mode shoutbox
	ajaxSendFunction(url_site+"inc/action/shoutbox.action.php", "action=changemode", nothing);

	if (document.getElementById("bouton_ouverture_form")) 
	{
		document.getElementById("bouton_ouverture_form").style.display="none";
	}
	if (document.getElementById("formulaireShoutbox")) 
	{
		document.getElementById("formulaireShoutbox").style.display="none";
	}
					
	//on cache les bouton d'ouverture de la popup
	document.getElementById('img_agrandir_shoutbox').style.display = 'none';
	document.getElementById('bouton_agrandir_shoutbox').style.display = 'none';				
	
	desactivate();
	
	//on annule le refresh
	clearTimeout(refTimeOut);
					
	var Hauteur=535;
	var Largeur=770;
	var Dessus=(screen.height/2)-(Hauteur/2); 
	var Gauche=(screen.width/2)-(Largeur/2); 
	
	window.popupShoutbox = window.open(url_site+'popup_shoutbox.php', "popupshoutbox", "width="+Largeur+",height="+Hauteur+",top="+Dessus+",left="+Gauche);
	//window.popupShoutbox.getElementById("formulaireShoutbox").style.display="block";				
}

function desactivate()
{
	var divMsgs = document.getElementById('messages');
	var html = '';
	
	html += '<br /><br />';
	html += '<p class="centerDiv">	';
	html += '	Cliquez ci-dessous pour réactiver la shoutbox dans cet encart.<br />';
	html += '	<br /><br />';
	html += '	<span class="curseurHand" onclick="reactivate()">';
	html += '		<img src="'+url_site+'images/charteV1/bouton/btn_reactiver.gif" alt="R�activer" title="R�activer">';
	html += '	</span>';
	html += '</p>';
	divMsgs.innerHTML = html;
}

function reactivate()
{
	//on prévient le serveur qu'on réactive
	ajaxSendFunction(url_site+"inc/action/shoutbox.action.php", "action=changemode", nothing);

	if (document.getElementById("bouton_ouverture_form")) 
	{
		document.getElementById("bouton_ouverture_form").style.display="block";
	}
	if (document.getElementById("formulaireShoutbox")) 
	{
		//document.getElementById("formulaireShoutbox").style.display="block";
	}
					
	//on cache les bouton d'ouverture de la popup
	if(document.getElementById('img_agrandir_shoutbox'))
		document.getElementById('img_agrandir_shoutbox').style.display = 'block';
	if(document.getElementById('bouton_agrandir_shoutbox'))
		document.getElementById('bouton_agrandir_shoutbox').style.display = 'block';
	
	//on récupere les nouveau messages
	document.getElementById('messages').innerHTML='';
	lastMessageIdShoutbox = 0;
	refreshShoutbox();
}
