function abrir(nombre, direccion, ancho, alto){
     var opciones = "fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + ancho +
                 ",height=" + alto +
                 ",left=0"
                 ",top=0";
     var ventana = window.open(direccion,nombre,opciones,false);
}

var AsesorRex = new Class
({
	jx: null,
	initialize: function() {
	
		var estado = $('asesor-estado');
		
		if(estado != null) {
			var en = $('asesor-estado').getParent();
			if(en != null) {
				en.addEvent('click', function(ev)
				{
					new Event(ev).stop();
					abrir("chat", "http://www.newsrex.com.ar/asesor/index.php?salas/ingresar", 610,460);
				});
			}
		}
		this.jx = new Request.JSON(
		{
			url: "client.php", 
			method: 'post',
			link: 'chain',
			onComplete: function(respuesta){
				this.process(respuesta);
			}.bind(this)
		});
		this.check();
		this.check.periodical(10000, this);
	},
	check: function() {
		this.jx.send();
	},
	process: function(respuesta) {
	
		var cont = $('invitacion-asesor');
		if(respuesta != null) 
		{
			var estado = $('asesor-estado');
			
			if(respuesta.invitado != 0 && respuesta.visto == 'no') {
				
				if(cont == null) {
					this.createInvitation(respuesta.tkn);
				}
			}else{
				if(cont != null) {
					this.removeInvitation(cont);
				}
			}
			
			if(estado != null) {
				if(respuesta.estado == 0) {
					estado.set('src', 'img/ba_asesor_offline.gif');
				}else{
					estado.set('src', 'img/banner_asesor.jpg');
				}
			}
		}
		var cont = $('invitacion-asesor');
		
		if(cont != null) {
			var ww = window.getSize();
			var cw = cont.getSize();
			
			var px = (ww.x / 2) - (cw.x / 2);
			var py = ((ww.y / 2) + window.getScroll().y) - (cw.y / 2);
			
			if(cont != null) {
				cont.setStyles(
				{
					'position': 'absolute',
					'top': py.toInt() + 'px',
					'left': px.toInt() + 'px'
				});
			}
		}
		
	},
	createInvitation: function(tkn)
	{
		cont = new Element('div', {'class' : 'invitacion-asesor', 'id' : 'invitacion-asesor', 'styles': {'position': 'relative', 'width': '200px'}});
		var img = new Element('img', {'src': 'img/chat-asesor.gif', 'border':'0'});
		
		var cerrar = new Element('img', {'src': 'img/delete-comment-red.gif', 'border':'0'});
		
		var enl = new Element('a', {'href' : '#', 'target' : '_blank', 'styles': {'position': 'absolute', 'top': '0px', 'left': '0px'}});
		var enl2 = new Element('a', {'href' : '#', 'target' : '_blank', 'styles': {'position': 'absolute', 'top': '0px', 'right': '0px', 'z-index': '20'}});
		
		enl.addEvent('click', function() {
			this.jx.send("visto=si");
			this.removeInvitation(cont);
			abrir("chat", "http://www.newsrex.com.ar/asesor/index.php?salas/invitar/" + tkn, 610,460);
		}.bind(this));
		
		enl2.addEvent('click', function(ev) {
			new Event(ev).stop();
			this.jx.send("visto=si");
			this.removeInvitation(cont);
			abrir("chat", "http://www.newsrex.com.ar/asesor/index.php?salas/invitar/" + tkn, 610,460);
		}.bind(this));
		
		enl.adopt(img);
		enl2.adopt(cerrar);
		cont.adopt(enl);
		cont.adopt(enl2);
		
		var body = $(document.body);
		body.adopt(cont);
	},
	removeInvitation: function(cont)
	{
		cont.dispose();
	}	
});

var asesor;

window.addEvent('domready', function()
{
	asesor = new AsesorRex();
});
