

/**
 * Javascript de la administración
 *
 * @category javascript
 * @copyright Copyright 2008, Isertia
 * @filesource
 * @package javascript
 * @version 1.0 2008/10/01
 */


/**
 * Ruta web de instalación. Constante
 */
var INSTALATION_DIR = $('base').attr('href');


/**
 * Mostrar símbolo de carga 
 * @param e object Elemento al que superponer el cargador
 */
function loading(e)
{
	var lwidth = $(window).width();
	var lheight = $(window).height();
	var container = 'body';
	var ltop = 0;
	var lleft = 0;
	var lposition = 'absolute';
	if(e)
	{
		lwidth = $(e).outerWidth();
		lheight = $(e).outerHeight(true);
		container = e;
		ltop = $(e).offset().top;
		lleft = $(e).offset().left;
		lposition = 'absolute';
	}

	//Crea loading si no existe
	if($('#loading').length < 1)
	{
		$(container).before('<div id="loading"></div>');
		$('#loading').css({opacity: '0.5', left: lleft, top: ltop, width: lwidth, height: lheight, position: lposition});
	}
	return true;
}
function loadingEnd()
{
	$('#loading').remove();
	return true;
}


/**
 * Cargar lightbox con parámetros 
 */
function loadLb()
{
	$('a[rel*=lightbox]').lightBox(
	{
		imageLoading: INSTALATION_DIR+'/img/lightbox-ico-loading.gif',
		imageBtnClose: INSTALATION_DIR+'/img/lightbox-btn-close.gif',
		imageBtnPrev: INSTALATION_DIR+'/img/lightbox-btn-prev.gif',
		imageBtnNext: INSTALATION_DIR+'/img/lightbox-btn-next.gif',
		imageBlank: INSTALATION_DIR+'/img/lightbox-blank.gif',
		containerResizeSpeed: 250,
		txtImage: lng.image,
		txtOf: lng.of,
		keyToPrev: lng.lightbox_prev,
		keyToNext: lng.lightbox_next
	});
}


/**
 * JS propio de la página
 */
function loadPage()
{
	//Ajax para paginación de noticias | contenidos | portfolio
	$('.previous_next a').livequery('click', function(event)
	{
		event.preventDefault();
		loading('#column_two');
		$('#column_two').load($(event.target).attr('href')+' #column_two > *', function(response)
		{
			$('.column_two').replaceWith($(response).find('.column_two'));
			loadingEnd();
		});
	});
	
	//Ajax para paginación de eventos
	$('.previous_next_services a').livequery('click', function(event)
	{
		event.preventDefault();
		loading('#column_one');
		$('#column_one').load($(event.target).attr('href')+' #column_one > *', function(response)
		{
			$('.column_one').replaceWith($(response).find('.column_one'));
			loadingEnd();
		});
	});
	
	//Compartir
	$("#share").livequery('mouseenter', 
		function()
		{
			$("#share ol").animate({
				'marginLeft': 0
			});
		}
	);
	$('#share').livequery('mouseleave',
		function()
		{
			$("#share ol").animate({
				'marginLeft': -314
			});
		}
	);
	//El hover, ocultar al click
	$('#hover').livequery('click', function(e)
	{
		$("#email, #hover").toggle();
	});
	//Enviar por email
	$("#share a.email, #email .close").livequery('click', function(e)
	{
		e.preventDefault();
		$("#email").toggle();
		$('#hover').toggle().css({'opacity': 0.6, 'height': $(document).height()});
		return false;
	});
	//Enviar email
	$("#email .send").livequery('click', function(e)
	{
		e.preventDefault();
		loading('body');
		var arr = { 'share' : 'compartir' };
		var mail  = 'mail='  + $("#email #mail").attr("value");
		var title = 'title=' + $("#email #title").attr("value");
		var url   = 'url='   + $("#email #url").attr("value");
		var str   = INSTALATION_DIR+'/public/ajax/share_link.php?'+mail+'&'+ title+'&'+url;
		$.get(str, arr, function(data)
		{
			loadingEnd();
			if(data == 'ok')
			{
				alert('Mensaje enviado');
				$("#email, #hover").toggle();
			}
			else
			{
				alert('Error al enviar el mensaje. Por favor, vuelva a intentarlo');	
			}
		});
		return false
	});
	
	//Compartir externo
	$("#share a.extern").livequery('click', function(e)
	{
		e.preventDefault();
		window.open(this.href, this.title);
		return false;
	});
	
	//Añadir a favoritos
	$("#share a.favorites").livequery('click', function(e)
	{
		e.preventDefault();
		var url   = this.href;  
		var title = this.title;  
		if (window.sidebar)// Mozilla Firefox Bookmark
		{
			window.sidebar.addPanel(title, url,"");  
		}
		else if( window.external || document.all ) // IE Favorite
		{
			window.external.AddFavorite( url, title);  
		}
		else if(window.opera) // Opera 7+
		{
			$("a.jQueryBookmark").attr("href",url);
			$("a.jQueryBookmark").attr("title",title);
			$("a.jQueryBookmark").attr("rel","sidebar");
			return false; // do nothing - the rel="sidebar" should do the trick  
		}
		else // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)  
		{
			alert('Desafortunadamente, este navegador no soporta la acción solicitada. Por favor, añade a favoritos esta página manualmente.');  
		}
		return false;
	});

}


/**
 * Cargar el slider jFlow
 */
function loadFlow()
{
	$("#multimedia_controls").jFlow({
		slides: "#multimedia_wrapper",
		controller: ".control",
		slideWrapper : "#multimedia",
		selectedWrapper: "active",
		width: "430px",
		height: "305px",
		duration: 400,
		prev: "#multimedia_controls .prev",
		next: "#multimedia_controls .next"
	});	
}


/**
 * Cargar archivo css
 * @param file string Nombre del css a cargar
 */
jQuery.getCss = function(url, callback)
{	
	var head = document.getElementsByTagName('head')[0];//Head
	var script = document.createElement("link");
	$(script).attr({type: 'text/css', href: url, rel: 'stylesheet', media: 'screen'});//Código css
	
	// Attach handlers for all browsers
	script.onload = script.onreadystatechange = function(){};

	//Añadir a cabecera
	head.appendChild(script);
	
	return undefined;
};


/**
 * Defuscador de email
 * Uso: $(enlace elemento).defuscate();
 */ 
jQuery.fn.defuscate = function()
{
	return this.each(function(){
	var email = String($(this).html()).replace(/\s*\(.+\)\s*/, "@");
	$(this).before('<a href="mailto:' + email + '">' + email + "</a>").remove();
	});
}; 


/**
 * Cargar funcionalidades js tras cargar DOM
 */
$(document).ready(function()
{
	$(".email a").defuscate();
	
	//LiveQuery. Bind eventos tras ajax automáticamente
	loadPage();
	//Carga de lightbox
	loadLb();
	//jFlow	
	loadFlow();
		
	//Buscador: Selección del tipo de resultados
	var desplegado=0;	
	$("#header #searcher ul li").click(function(event)
	{
		$("#header #searcher ul li").removeClass("active");
		$(this).addClass("active");													
		$("#header #searcher #tipo").val(event.target.id);
	});
	
	$("#header #searcher input").hover(function(event)
	{
		$("#header #searcher ul").show(0);
		desplegado=1;
	},
	function(event)
	{
	});
	
	$("#header #searcher ul").hover(function(event)
	{															
	},
	function(event)
	{
		$("#header #searcher ul").hide(0);									
		desplegado=0;
	});
	
	$("#header #searcher").hover(function(event)
	{															
	},
	function(event)
	{
		$("#header #searcher ul").hide(0);									
		desplegado=0;
	});
	
	//Borrar contenido input
	$('#header #searcher input').livequery('click', function(event)
	{
		if($(this).val() == 'Buscar')
		{
			$(this).val('');
		}
	});
});