if (Object.isUndefined(MenuFlotante)) { var MenuFlotante = { } }

MenuFlotante = Class.create({
	initialize: function() {
		var e = Prototype.emptyFunction;
		this.ie = Prototype.Browser.IE;
		
		this.options = Object.extend({
			selector: '.contextmenu',
			className: 'menuFlotante',
			pageOffset: 25,
			fade: false,
			zIndex: 100,
			beforeShow: e,
			beforeHide: e,
			beforeSelect: e
		}, arguments[0] || { });
		
		this.shim = new Element('iframe', {
			style: 'position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);display:none',
			src: 'javascript:false;',
			frameborder: 0
		});
		
		this.options.fade = this.options.fade && !Object.isUndefined(Effect);
		this.container = new Element('div', {className: this.options.className, style: 'display:none'});
		var list = new Element('ul');
		this.options.menuItems.each(function(item) {
			list.insert(
				new Element('li', {className: item.separator ? 'separator' : ''}).insert(
					item.separator 
						? '' 
						: Object.extend(new Element('a', {
							href: '#',
							title: item.name,
							className: (item.className || '') + (item.disabled ? ' disabled' : ' enabled')
						}), { _callback: item.callback })
						.observe('click', this.onClick.bind(this))
						.observe('contextmenu', Event.stop)
						.update(item.name)
				)
			)
		}.bind(this));
		
		$(document.body).insert(this.container.insert(list).observe('contextmenu', Event.stop));
		if (this.ie) { $(document.body).insert(this.shim) }
		
		document.observe('click', function(e) {			
			if (this.container.visible()) {
				this.options.beforeHide(e);
				if (this.ie) this.shim.hide();
				this.container.hide();
			}
		}.bind(this));
		/*
		$('cntRightViajeros').invoke('observe', 'mouseout' , function(e){
			if (this.container.visible()) {
				this.options.beforeHide(e);
				if (this.ie) this.shim.hide();
				this.container.hide();
			}
		}.bind(this));
		*/
		$$(this.options.selector).invoke('observe', 'mouseover' , function(e){
			/*if (this.container.visible()) {
				this.options.beforeHide(e);
				if (this.ie) this.shim.hide();
				this.container.hide();
			}
			else {
			*/
			
				$$('.desktop').each(Element.hide); //eliminamos los otros elementos mostrados
				this.show(e);
			//}
		}.bind(this));
	},
	show: function(e) {
		e.stop();
		this.options.beforeShow(e);
		var x = Event.pointer(e).x,
			y = Event.pointer(e).y,
			vpDim = document.viewport.getDimensions(),
			vpOff = document.viewport.getScrollOffsets(),
			elDim = this.container.getDimensions(),
			elOff = {
				left: ((x + elDim.width + this.options.pageOffset) > vpDim.width 
					? (vpDim.width - elDim.width - this.options.pageOffset) : x) + 'px',
				top: ((y - vpOff.top + elDim.height) > vpDim.height && (y - vpOff.top) > elDim.height 
					? (y - elDim.height) : y) + 'px'
			};
		this.container.setStyle(elOff).setStyle({zIndex: this.options.zIndex});
		if (this.ie) { 
			this.shim.setStyle(Object.extend(Object.extend(elDim, elOff), {zIndex: this.options.zIndex - 1})).show();
		}
		this.options.fade ? Effect.Appear(this.container, {duration: 0.25}) : this.container.show();
		this.event = e;
	},
	onClick: function(e) {
		e.stop();
		if (e.target._callback && !e.target.hasClassName('disabled')) {
			this.options.beforeSelect(e);
			if (this.ie) this.shim.hide();
			this.container.hide();
			e.target._callback(this.event);
		}
	}
})


function newArrayMenuFlotanteViajero(id){
	var aMenu = [
	  {
	    name: 'Ver pasaporte',
	    className: '', 
	    callback: function() {
		  irPagina(id);
	    }
	  },{
	    name: 'Añadir viajero',
	    className: '', 
	    callback: function() {
		  addAmigo(id);
	    }
	  },{
	    name: 'Enviar Mensaje',
	    className: '', 
	    callback: function() {
			showEnviarMensaje(id);
	    }
	  },{
	    separator: true
	  },{
	    name: 'Eliminar viajero',
	    className: '',
	    callback: function() {
	      delAmigo(id);
	    }
	  }
	];
	return aMenu;
}

function newArrayMenuFlotanteOtroViajero(id){
	
	var aMenu = [
	  {
	    name: 'Ver pasaporte',
	    className: '', 
	    callback: function() {
		  irPagina(id);
	    }
	  },{
	    name: 'Añadir viajero',
	    className: '', 
	    callback: function() {
		  addAmigo(id);
	    }
	  },{
	    name: 'Enviar Mensaje',
	    className: '', 
	    callback: function() {
			showEnviarMensaje(id);
	    }
	  }
	];
	return aMenu;
}

function newArrayMenuFlotanteAmigo(id){
	var aMenu = [
	  {
	    name: 'Ver pasaporte',
	    className: '', 
	    callback: function(){
			irPagina(id);
		}
	  },{
	    name: 'Enviar Mensaje',
	    className: '', 
	    callback: function() {
			showEnviarMensaje(id);
	    }
	  }
	];
	return aMenu;
};

/*
 * Crea un nuevo menu asociado al id y que se activara en el classM
 */

function newMenu(idM, classM, tipo){
	if ($(idM) == null ) {
		return;
	}
	if(tipo == 'amigo')
		var myMenuItems = newArrayMenuFlotanteAmigo($F(idM));
	else if(tipo == 'viajero') 
		var myMenuItems = newArrayMenuFlotanteViajero($F(idM));
	else if(tipo == 'otroviajero') 
		var myMenuItems = newArrayMenuFlotanteOtroViajero($F(idM));
		
	new MenuFlotante({
	  selector: classM, // context menu will be shown when element with id of "contextArea" is clicked
	  className: 'menu desktop', // this is a class which will be attached to menu container (used for css styling)
	  menuItems: myMenuItems // array of menu items
	});
};

var TXTasunto;
var TXTcontenido;

function onFocusAsunto(){
	if($('asunto').value == TXTasunto){
		$('asunto').update('');
	}
}

function onBlurAsunto(){
	if($('asunto').value == "" ){
		$('asunto').update(TXTasunto);
	}
}

function onFocusContenido(){
	if($('contenido').value == TXTcontenido){
		$('contenido').update('');
	}
}

function onBlurContenido(){
	if($('contenido').value == ""){
		$('contenido').update(TXTcontenido);
	}
}

function showEnviarMensaje(id){
	var titulo = "Mensaje privado de " + $F('userAlias');
	TXTasunto = titulo;
	TXTcontenido = 'Escribe tu mensaje ...';
	var txt ="<a href=\"javascript:Dialog.closeInfo();\">cerrar</a>";
	txt += "<textarea id='asunto' style='width:250px' onFocus=\"javascript:onFocusAsunto();\" onBlur=\"javascript:onBlurAsunto();\">"+titulo+"</textarea>";
	txt += "<textarea id='contenido' rows='6' style='width:372px; height:100px' onFocus=\"javascript:onFocusContenido();\" onBlur=\"javascript:onBlurContenido();\">" + TXTcontenido + "</textarea>";
	txt += "<a class='botonDialog' href=\"javascript:enviarMensaje(" + id+", $F('asunto'), $F('contenido') )\">Enviar Mensaje</a>"
	Dialog.info(txt, {className: "alert", width:400, height:175	});
}

function enviarMensaje(id, asunto, contenido){
	id += "";
	if(id.blank()){
		Dialog.closeInfo();
		Dialog.info("Debe elegir un destinatario.<br><a class='botonDialog' href='javascript:Dialog.closeInfo();'>Cerrar</a>", {className: "alert", width:250, height:100});
		return;
	}
	if(asunto.blank()){
		Dialog.closeInfo();
		Dialog.info("Debe escribir un asunto.<br><a class='botonDialog' href='javascript:Dialog.closeInfo();'>Cerrar</a>", {className: "alert", width:250, height:100});
		return;
	}
	if(contenido.blank()){
		Dialog.closeInfo();
		Dialog.info("Escriba algun mensaje.<br><a class='botonDialog' href='javascript:Dialog.closeInfo();'>Cerrar</a>", {className: "alert", width:250, height:100});
		return;
	}
	
	var url = '/comunidad/php/mensajes.php';
	var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: {'accion': 'send', 'idDest': id, 'asunto': asunto, 'contenido': contenido},
        onComplete: function(t){
			if (!t.responseText.blank())
				Dialog.alert(t.responseText, {className: "alert", width:250, height:100, okLabel: "Cerrar"});
        },
		on409: function(t) {
			Dialog.alert(t.statusText, {className: "alert", width:250, height:100, okLabel: "Cerrar"});
		},
		on500: function(t) {
			Dialog.alert(t.statusText, {className: "alert", width:250, height:100, okLabel: "Cerrar"});
		}
	});
	Dialog.closeInfo();
}

function delAmigo(id){
	var url = '/comunidad/php/delamigo.php';
 	var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: {'accion': 'del', 'id': id},
        onComplete: function(t){
			if (!t.responseText.blank()) {
				Dialog.info(t.responseText, {className: "alert", width:250, height:100, okLabel: "Cerrar"});
				setTimeout(infoTimeout, 1000);
				if ( $('viaj_favo_'+id) )
					$('viaj_favo_'+id).remove();
			}
        }
	});
}
var timeout = 1; // 1 segundos

function addAmigo(id) {
	var url = '/comunidad/php/addamigo.php';
    var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: {'accion': 'add', 'id': id},
        onComplete: function(t){
			if (!t.responseText.blank()) {
				Dialog.info(t.responseText, { width:250, height:100, okLabel: "Cerrar"});
				setTimeout(infoTimeout, 1000);
			}
        },
		on409: function(t) {
			Dialog.info(t.statusText, { width:250, height:100, okLabel: "Cerrar"});
			setTimeout(infoTimeout, 1000);
		},
		on500: function(t) {
			Dialog.info(t.statusText, { width:250, height:100, okLabel: "Cerrar"});
			setTimeout(infoTimeout, 1000);
		}
    });
}

function infoTimeout() { 
	timeout--; 
	if (timeout > 0) {
		setTimeout(infoTimeout, 1000);
	} else {
		Dialog.closeInfo();
		timeout = 1;
	}
} 

function irPagina(id){
	var url = '/comunidad/php/redirigir.php';
    var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: {'accion': 'usuId', 'id': id},
        onComplete: function(t){
			if (!t.responseText.blank()) {
				document.location.href=t.responseText;
			}
        }
    });
}


