/*
 * JAlt
 * Por Julián Solanas, (http://www.canarias7digital.com)
 * Necesaria la libreria Jquery para su utilización.
 */

// Cuando la página haya terminado de cargarse ejecuto la función.
$(document).ready(JALT_init);

// Funcion que muestra el mensaje al pasar el raton y lo elimina el quitarlo.
function JALT_init(){
	$(".jalt")
	.hover(function(){JALT_show(this)},function(){$('#ALT').remove()})
}

// Funcion que inserta la capa con el mensaje
function JALT_show(element){
	
	// Elimino las posible capas de ayuda que puedan haber quedado mostradas.
	$('#ALT').remove();
	
	// Guardo el mensaje alt lo borro y lo añado al quitar el ratón
	var text = element.title;
	element.title = '';
	$(element).bind('mouseout',function(){this.title = text;});

	// Inserto la capa
	$("body").append("<div id='ALT'>"+text+"</div>");
	
	// Obtengo el ancho del elemento para que no se muestre encima
	var arrowOffset = getElementWidth(element) + 11;
	
	// La posiciono en pantalla
	var clickElementx = getAbsoluteLeft(element) + arrowOffset; //set x position
	var clickElementy = getAbsoluteTop(element) - 3; //set y position	
	
	// Le aplico los estilo y finalmente la muestro
	$('#ALT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#ALT').show();

}

// Funcion prefabricada que obtiene el ancho de un elemento
function getElementWidth(x) {
	return x.offsetWidth;
}

// Funcion prefabricada que obtiene la posicion X absoluta de un elemento
function getAbsoluteLeft(o) {
	// Get an object left position from the upper left viewport corner
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

// Funcion prefabricada que obtiene la posicion Y absoluta de un elemento
function getAbsoluteTop(o) {
	// Get an object top position from the upper left viewport corner
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}
