//
//  xat ajax with prototype
//  xat.js
//  (cc) January 2010 
//
//  Albert Serra - From Barcelona
// 	Senior PHP Developer
//  info@albertserra.cat
//	http://www.albertserra.cat/
// 	http://www.senior-php-developer.com/
//


// UPDATER

var xat_interval = new Hash;
function xat_button_click(button) {
	if($(button.id).hasClassName('xat_barra_button_active')) {
		$(button.id).removeClassName('xat_barra_button_active');
		$('dialog_'+button.id).hide();
		if(xat_interval.get(button.id)) clearInterval(xat_interval.get(button.id));
	}else{
		$(button.id).addClassName('xat_barra_button_active');
		if(xat_interval.get(button.id)) clearInterval(xat_interval.get(button.id));
		xat_interval.set(button.id,setInterval("xat_update('"+button.id+"')",5000));
		xat_update(button.id);
		$('dialog_'+button.id).show();
		$('input_msg_'+button.id).focus();
	}
}

function xat_update(button_id) {
	new Ajax.Updater('xat_dialog_content_'+button_id, 'xat_dialog.php?admin='+button_id, {
	  method: 'get',
	  onComplete: function(transport){
		  if($('dialog_down_'+button_id).hasClassName('do_it')) xat_down(button_id);
	   }
	});
}

function xat_dialog_submit(form) {
	return false;
}

function xat_down(button_id) {
	Position.prepare();
	container_y = Position.cumulativeOffset($('xat_dialog_content_'+button_id))[1];
	element_y = Position.cumulativeOffset($('dialog_down_'+button_id))[1];
	new Effect.Scroll('xat_dialog_content_'+button_id, {x:0, y:(element_y-container_y)});
	return false;
}

//
// SENDING MESSAGE
//

function xat_send_msg(form) {
	new Ajax.Request(form.action,{
		 method: 'post',
		 parameters: form.serialize(true),
		 onSuccess: function(transport){
			var resposta = transport.responseText; 
			$('xat_dialog_content_'+form.admin.value).innerHTML = $('xat_dialog_content_'+form.admin.value).innerHTML+resposta;
			form.msg.value = ''; // resetejem el input
			xat_down(form.admin.value);
		 }			 
	});
	
	return false;
}

//
// PING
//

var i_xat_ping = setInterval("xat_ping()",5000);
function xat_ping() {
	new Ajax.Request('xat_ping.php',{
		 method: 'get',
		 onSuccess: function(transport){
			// marquem missatges nous
			var msgs_hash = new Hash(transport.responseText.evalJSON()); 
			 // alert(hash);
			msgs_hash.each(function(pair) {  // pair.key = admin_id, pair.value = [inline,nb_new_msgs]
				var dades = $A(pair.value.evalJSON());
				
				var online = dades[0]; // 1 o 0
				if($(pair.key).hasClassName('online') && online == 0) {
					// passem a offline
					$(pair.key).removeClassName('online');
					$(pair.key).addClassName('offline');
					$('xat_state_'+pair.key).innerHTML='(offline)';
				}
				else if($(pair.key).hasClassName('offline') && online == 1) {
					// passen a online
					$(pair.key).removeClassName('offline');
					$(pair.key).addClassName('online');
					$('xat_state_'+pair.key).innerHTML='(online)';
				}
				
				// alert();
				// missatges nous?
				var nbmsgs = dades[1];
				if($('new_msg_'+pair.key) && parseInt(nbmsgs) > 0) { 
					$('new_msg_'+pair.key).update(parseInt(nbmsgs));
					$('new_msg_'+pair.key).show();
					pulse(pair.key);
				} else $('new_msg_'+pair.key).hide();
			})
		 }			 
	});
}

var nbpulses = new Hash();
function pulse(key) {
	if(!nbpulses.get(key)) nbpulses.set(key,0);
	
	if(!(parseInt(nbpulses.get(key))%3)) {
		Effect.Pulsate('new_msg_'+key, { pulses: 3, duration: 2 });
	}
	nbpulses.set(key,parseInt(nbpulses.get(key))+1);
}




