<?
require_once('../init.php');
?>
var dim = -85;
var pop = null;
var popInterval = null;
var noticeInterval = null;

function apri(nick) {
	alert("nick " + nick );
}

function openChat(nick, first) {
	chiudiControInvito(nick);
	document.Helper.addUser(nick, first);
}

function chiudiControInvito(nick) {
	var copai = new AJAX('risp_ajax_contro.php');
	var params = {ric: nick};
	copai.doPost(params);
	chiudiPopup();
}

function scriviDB(nick) {
	var db = new AJAX("interroga_db.php");
	var params = {caller: nick};
	db.doPost(params);
}

function addUserError(user, data) {
	pop = window.open("chat/popup.php?nick=" + escape(user) + (data ? "&first=1" : ""), "chat_" + user, "top="+((screen.availHeight)-(403))+",left="+((screen.availWidth/2)-(700/2))+", width=700, height=403, status=no, menubars=no, scrollbars=no, resizable=no");
	//pop.resizeTo(700,403);
}

function addUserSuccess(user, data) {
	//pop.document.getElementById('ajax').innerHTML = 'Lo cambio'; 
	//var popupDiv = document.getElementById("header");
	//popupDiv.innerHTML = "RIFATTO " + user;
	insRichiedenteLocale(user);
}

function insRichiedenteLocale(nick)
{
//document.getElementById('ajax').innerHTML = 'Lo cambio';
errore[0]='Errore interrogazione database!';
errore[1]='N';
var ai = new AJAX("js/risp_ajax_locale.php", execCode);
ai.doPost('ajax=yes&id1='+nick);
}



function chiudiInvito(nick,stato) {
  var ai = new AJAX("risp_ajax_chat.php");
  var params = { notified: "yes", nick: nick , rifiutato: stato};
  ai.doPost(params);
  chiudiPopup();
}

function chiudiPopup() {
  var popupDiv = document.getElementById("popup");
  popupDiv.innerHTML = "";
  popupDiv.style.display = "none";
  stopBlinkIt();
}

function chiudiOnline() {

  var popupDiv = document.getElementById("alert");
  popupDiv.innerHTML = "";
  popupDiv.style.display = "none";
}

function rifiutaChat(nick) {
  chiudiInvito(nick,'si');
}

function accettaChat(nick) {
  scriviDB(nick);
  openChat(nick, false);
  chiudiInvito(nick,'no');
}

function blinkIt() {
	window.blinko = new blink('chat_req', 250);
	window.blinko.start();
	
	if(pop.location)
		pop.focus();
	else
		this.focus();
		
}

function changeTitle() {
	if (pop.location){
		pop.document.title = "nuovo title";
	}
}

function stopBlinkIt() {
	if(window.blinko)
		window.blinko.stop();
	blink.count = 0;
}

function popitup(url) {
	newwindow=window.open(url,'name','height=200,width=150');
	if (window.focus) {newwindow.focus()}
	return false;
}

function muoveRichiesta() {
	var popupDiv = document.getElementById("popup");
	if (dim <= 320) {
		dim = dim + 85;
		popupDiv.style.marginLeft = dim + "px";
	}
	else clearInterval(popInterval);
}

function muoveAvviso(nick) {
	var popupDiv = document.getElementById("popup");
	if (dim <= 320) {
		dim = dim + 85;
		popupDiv.style.marginLeft = dim + "px";
	}
	else {
		clearInterval(noticeInterval);
		//setTimeout('chiudiOnline()', 5000);
	}
}

function rifiutiChatControl(text) {
	if(text != ""){
		eval("var roba = " + text + ";");
		//for(var p in roba)
		//	alert(p + " -- " + roba[p]);
		var message = '<p align="center">' + roba.username +' ha rifiutato il tuo invito </p> <p align="center"><a href="javascript:chiudiRifiutoAjax(\''+roba.username+'\');">chiudi</a></p>';
		if(pop && !pop.closed) {
			pop.document.getElementById("ajax2").innerHTML = message;
			pop.document.getElementById("ajax2").style.display = 'block';
		}
	}
}

function richiesteVideoControl(text) {
	if (text != "") {
	//alert(text);
		var message = text;
      	var mioDiv = document.getElementById("reg_video");
   		mioDiv.innerHTML = message;
	}

}

function richiesteChatControl(text) {
//if(pop && !pop.closed) alert('Pop presente');
	//		alert('ci arrivo ' + text);
if (text != "") {
		try{
			//if(typeof(blink) == "function" && blink.count == 0)
			//blinkIt();
			//eval(text);
//			alert('ci sono' +  text)
			var message = text;
/*
			eval("var user = " + text + ";");
			var message = '';
			if(user.cancellato == 'si')
				message = '<p align="center">' + user.username +' ha chiuso la chat dopo averti invitato.Vuoi invitarlo/a tu?  </p>' + '<p align="center"><a href="javascript:openChat(\'' + user.username + '\',true);"><?= _('Accetta'); ?></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + '<a href="javascript:rifiutaCh	at(\'' + user.username + '\');"><?= _('Rifiuta'); ?></a></p>';
			else
				message = '<p align="center"><?= _('Vuoi chattare con'); ?> ' + user.username + '</p>' + '<p align="center"><a href="javascript:accettaChat(\'' + user.username + '\');"><?= _('Accetta'); ?></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + '<a href="javascript:rifiutaChat(\'' + user.username + '\');"><?= _('Rifiuta'); ?></a></p>';
*/
			if(pop && !pop.closed){
				//pop.document.getElementById("ajax").innerHTML = message;
				pop.document.getElementById("ajax").innerHTML = message;
      			pop.document.getElementById("ajax").style.display = "block";
				clearInterval(popInterval);
				//eval(pop.document.getElementById("ajax2").innerHTML = '<script type="text\/javascript">alert("Ciao")<\/script>');
				//if(window.focus)
				pop.focus();
  			}
			else {
			/*
      			var popupDiv = document.getElementById("popup");
   				popupDiv.innerHTML = message;
   				popInterval = setInterval("muoveRichiesta()",100);
      			popupDiv.style.display = "block";
      			popupDiv.style.visibility = "visible";
      			popupDiv.style.textAlign = "center";
			*/
			var popupDiv = document.getElementById("popup");
   			popupDiv.innerHTML = message;
   			//noticeInterval = setInterval("muoveAlert()",100);
      		popupDiv.style.display = "block";
      		popupDiv.style.visibility = "visible";
			//pageScroll();
      		popupDiv.style.textAlign = "center";

			}
		}catch(e){ }
    }
}

function getRichiesteChat() {
  var ai = new AJAX("risp_ajax_chat.php", richiesteChatControl);
  ai.doGet();
}

function getVideoAnnunci() {
  var ai = new AJAX("risp_ajax_video.php", richiesteVideoControl);
  ai.doGet();
}


function getRifiutiChat() {
  var ai = new AJAX("risp_ajax_rifiuti.php", rifiutiChatControl);
  ai.doGet();
}

getRifiutiChat();
getRichiesteChat();
setInterval("getRifiutiChat()", 8000); 
setInterval("getRichiesteChat()", 5000); 

