//-----------------------------------------------------------------------------------------
//paramétrage

var popupWin = null; //Popup
var popupWidth = 560;
var popupHeight = 520;
var popupUrl = "/chat/chatPopup.cfm";
var maxChats = 3;

var alertes;
var currentAlerte;
var affiched = false;
var timeout;

//-----------------------------------------------------------------------------------------
//Fonction de gestion des alertes


function checkAlerte(){
	clearTimeout(timeout);
	
	alertes = new Array();
	sendAjax("/chat/ajax_checkAlertes.cfm", self.checkAlerte_return, 'post', "pk_user="+pk_user, []);

}

function closeChat(url){

	if( !popupWin || popupWin.closed || !popupWin.doSomething) {
	}else{
		popupWin.close();
	}
	document.location = url;
}

function checkAlerte_return(text,xml,params){

	//document.getElementById("retour").innerHTML = text;

	var xml = xml.documentElement;
	var existe = xml.getAttribute('existe');
	var date = xml.getAttribute('date');

	
	if(existe=="-2"){
		document.location = "accueil.html?pagecode=accueil";
	}else{
		
		//document.getElementById("lastupd").innerHTML = "last update : "+date;
		
		if(existe=="1"){
			
			currentAlerte = new Object();
			currentAlerte.from = xml.getAttribute('from');
			currentAlerte.to = xml.getAttribute('to');
			currentAlerte.fk_chatrequest = xml.getAttribute('fk_chatrequest');
			currentAlerte.from = xml.getAttribute('from');
			currentAlerte.response = xml.getAttribute('response');
			currentAlerte.mode = xml.getAttribute('mode');
			currentAlerte.fromId = xml.getAttribute('fromId');
			currentAlerte.toId = xml.getAttribute('toId');
			currentAlerte.message = xml.firstChild.nodeValue;
			
			currentAlerte.fichier = xml.getAttribute('fichier');
			
			if(currentAlerte.fichier==""){
				currentAlerte.fichier = "/image/notset_"+mode+"/notset_"+targetsexe+"_35.jpg";		
			}else{
				currentAlerte.fichier = "/photos/35/"+currentAlerte.fichier;
			}
			
			openAlerte();
		}else{
			timeout = setTimeout("checkAlerte()",1000*timeout_time);
		}
	}
}

function openAlerte(){
	if(currentAlerte.mode=="chatRequest"){
		openChatRequestAlert(currentAlerte.fromId);
	}else if(currentAlerte.mode=="chatResponse"){
		if( !popupWin || popupWin.closed || !popupWin.doSomething) {
			sendAjax("/chat/ajax_setReaded.cfm", self.sendResponse_return, 'post', "fk_chatrequest="+currentAlerte.fk_chatrequest, []);
		}else{
			openChatResponseAlert(currentAlerte.to);
		}
		
	}
}

function closeAlerte(){
	document.getElementById("DIV_MOVE").style.width = "1px";
	document.getElementById("chatRequestAlert").style.display="none";
	document.getElementById("chatResponseAlert").style.display="none";
	document.getElementById("chatResponseText").innerHTML = "";
}

function openChatResponseAlert(to){
	document.getElementById("DIV_MOVE").style.width = "299px";
	document.getElementById("chatResponseAlert").style.display="";
	
	if(currentAlerte.response=="1"){
		document.getElementById("chatResponseText").innerHTML = currentAlerte.toId+" "+txt_accept;
	}else{
		openChatPopup("null","null","null");
		if(popupWin.closeChatWindow){ popupWin.closeChatWindow(to); }
		document.getElementById("chatResponseText").innerHTML = currentAlerte.toId+" "+txt_refuse;
	}
	
}

function openChatRequestAlert(from){
	document.getElementById("DIV_MOVE").style.width = "299px";
	document.getElementById("chatRequestAlert").style.display="";
	document.getElementById("ca_name").innerHTML = from;
	document.getElementById("ca_img").src = currentAlerte.fichier;
	document.getElementById("ca_img").style.display = "";
	//document.getElementById("chatRequestText").innerHTML = from+" souhaite dialoguer avec vous : "+currentAlerte.message;
}

function acceptChatRequest(){//ICI
	openChatPopup("openChatForDialog",currentAlerte.fromId, currentAlerte.from, currentAlerte.fk_chatrequest, currentAlerte.fichier);
	
	if(popupWin.nb == undefined || popupWin.nb < maxChats){
		//CAS 1 : la popup n'était pas ouverte avant ou le nb de chats ouverts est inférieur à 3
		sendAjax("/chat/ajax_sendResponse.cfm", self.sendResponse_return, 'post', "fk_chatrequest="+currentAlerte.fk_chatrequest+"&response=1", []);
		
		
		if(popupWin && popupWin.openChatForDialog){ //ICI
			popupWin.openChatForDialog(currentAlerte.fromId, currentAlerte.from, currentAlerte.fk_chatrequest, currentAlerte.fichier);
		}
		
		closeAlerte();
	}else{
		//CAS 2 : 3 chats sont déjà ouverts. impossible d'en ouvrir plus
		alert(txt_erreur);
	}
	
}

function declineChatRequest(){
	sendAjax("/chat/ajax_sendResponse.cfm", self.sendResponse_return, 'post', "fk_chatrequest="+currentAlerte.fk_chatrequest+"&response=2", []);
	document.getElementById("ca_img").style.display = "none";
	closeAlerte();
}

function closeResponseAlerte(){
	sendAjax("/chat/ajax_setReaded.cfm", self.sendResponse_return, 'post', "fk_chatrequest="+currentAlerte.fk_chatrequest, []);
	closeAlerte();
}

function sendResponse_return(text,xml,params){
	checkAlerte();
}

//-----------------------------------------------------------------------------------------
//Fonction de dialogue


//A appeler quand une réponse positive a été faire à une demande de chat

function openChatForDialog(toId, toPk){//ICI
	openChatPopup("openChatForDialog",currentAlerte.fromId, currentAlerte.from, currentAlerte.fk_chatrequest, currentAlerte.fichier);
	
	popupWin.openChatForDialog(toId, toPk);
}

//On désire demander quelqu'un en chat
function openChatRequestSending(toId, toPk, toImg){
	
	openChatPopup("openChatRequest", toId, toPk, "null", toImg);
	
	if(popupWin && popupWin.openChatRequest){
		if(popupWin.nb == undefined || popupWin.nb < maxChats){
			popupWin.openChatRequest(toId, toPk, toImg);
		}else{
			alert(txt_erreur);
		}
	}else{
		
	}
}


//-----------------------------------------------------------------------------------------
//Fonction génériques de gestion de la fenêtre

//Fonction permettant d'ouvrir la fenetre de chat
function openChatPopup(fctcall, toid, topk, chatrequest, toImg){
	popupWin = open( "", "popupWin", "width="+popupWidth+",height="+popupHeight );
	if( !popupWin || popupWin.closed || !popupWin.doSomething) {
		popupWin = window.open( popupUrl+"?ts="+targetsexe+"&fctcall="+fctcall+"&toid="+toid+"&topk="+topk+"&chatrequest="+chatrequest+"&toimg="+toImg, "popupWin", "width="+popupWidth+",height="+popupHeight+",toolbar=no,location=no,directories=no,status=no,menubar=0,scrollbars=no,resizable=no" );
	} else{
		popupWin.focus();
	}
}

function doSomething() {
	 popupWin.doSomething();
}


