this.photoGallery = function(){	
	
	//Comprobamos si estamos en galería de fotos o de noticias
	var galeria_fotos = true;
	$(".photoAlbumEntry").each( function(e) {
		if ( $(this).is('.photoAlbumFolder') ) {
			galeria_fotos = false;
		}
	});
	
	if ( galeria_fotos ) {
	
		//Desactivamos tooltips
		$(".photoAlbumEntry a").each(function(e){
			this.title = "";
		});
		
		$(".photoAlbumEntryWrapper img").each(function(e){
			this.t = this.title;
			this.title = "";
		});
		
		$(".photoAlbumEntry img").click( function(el){
		
			var etitle = (this.title != "") ? this.title : "";
			if ( etitle == "" ) {
				etitle = (this.t != "") ? this.t : "";
			}
			
			changeImageGalleryElement(this.src, etitle, this.alt);
			return false;
		});
		
		//Desactivamos comportamiento por defecto
		$(".photoAlbumEntry a").click( function(e){
			return false;
		});
	}
	else {
		//Espacio para los textos
		$(".photoAlbumEntry").css('height', 'auto');
	}
};

this.changeImageGalleryElement = function(src, etitle, ealt){
	
	var href_img_aux = src.split("/image_thumb");
	var href_img = href_img_aux[0];
	
	//Reseteamos la galeria
	$("#gallery_info_box").fadeOut(200).remove();
	$("#gallery_photo_container").fadeOut(200).remove();
	$("#gallery_info").attr("src", "gallery_buttons_info.gif");
	
	var botones = 
		"<div id='gallery_buttons'>" +
			"<img src='gallery_buttons_close.gif' id='gallery_close' alt='Close' onclick='galleryClose()' /> " +
			"<img src='gallery_buttons_prev.gif' id='gallery_prev' alt='Previus' onclick='galleryPrevElement()' /> " +
			"<img src='gallery_buttons_next.gif' id='gallery_next' alt='Next' onclick='galleryNextElement()' /> " +
			"<img src='gallery_buttons_info.gif' id='gallery_info' alt='Info' onclick='galleryShowInfo()' /> " +
		"</div>";
	
	var galleryInfo = "<div id='gallery_info_box'>" + ealt + "<p>" + etitle + "</p> </div>"; 
	
	var html_content = "<div id='gallery_photo_container'> <img id='gallery_photo' src='" + href_img + "' alt='" + etitle + "' /></div>";
	$("body").append(html_content + botones + galleryInfo);	
		
	applyMask();
	fixGalleryImageSize( $(window).width(), ($(window).height() * 0.9) );
	putTheButtons();
}

this.galleryClose = function (e) {
	$('#mask').fadeOut(200).hide();
	$("#gallery_buttons").fadeOut(200).remove();
	$("#gallery_info_box").fadeOut(200).remove();
	$("#gallery_photo_container").fadeOut(200).remove();	
}

this.galleryShowInfo = function (e) {
	
	if ( $("#gallery_info_box").css("display") == 'none' ) {
		 $("#gallery_info_box").fadeTo(200, 0.9);
		 $("#gallery_info").attr("src", "gallery_buttons_info-push.gif");
	}
	else {
		$("#gallery_info_box").fadeOut(200);
		$("#gallery_info").attr("src", "gallery_buttons_info.gif");
	}
}


this.galleryPrevElement = function (e) {
	var gallery_foto_list = $("#gallery_foto_list").text();
	
	var photos = gallery_foto_list.split(":#:");
	var actual = $("#gallery_photo").attr("src"); // + "/image_thumb";
		
	var aux = 0;
	var prev_element = -1;
	
	for (var i = 0; i < photos.length; i++){
	
		var photo_aux = photos[i].split(":$:");
		
		if ( photo_aux[0] == actual ){
			prev_element = aux-1;
		}
		aux++;
	}
	
	if ( prev_element < 0 )
		prev_element = photos.length - 1;
	
	var prev_element_data = photos[prev_element].split(":$:");
	changeImageGalleryElement( prev_element_data[0], prev_element_data[1], prev_element_data[2]);
	
}

this.galleryNextElement = function (e) {
	
	var gallery_foto_list = $("#gallery_foto_list").text();
	
	var photos = gallery_foto_list.split(":#:");
	var actual = $("#gallery_photo").attr("src");
		
	var aux = 0;
	var next_element = photos.length;
	
	for (var i = 0; i < photos.length; i++){
	
		var photo_aux = photos[i].split(":$:");
		
		if ( photo_aux[0] == actual ){
			next_element = aux+1;
		}
		aux++;
	}
	
	if ( next_element >= photos.length )
		next_element = 0;
	
	var prev_element_data = photos[next_element].split(":$:");
	changeImageGalleryElement( prev_element_data[0], prev_element_data[1], prev_element_data[2]);
 
}

this.applyMask = function() {
	var maskHeight = $("#portal-main-content-wrapper").height(); 
	var maskWidth = $(window).width();

	$('#mask').css({'width':maskWidth,'height':maskHeight});	
	$('#mask').fadeTo(5, 1);		
}

this.fixGalleryImageSize = function(window_width, window_height) { 
	
	//Ajustamos el tamaño si es superior a la pantalla	
	var photo_width  = $("#gallery_photo_container").width();
	var photo_height = $("#gallery_photo_container").height();
		
	if ( window_width < photo_width ){
		var new_width = window_width*0.9;
		var new_height = new_width * photo_height / photo_width; 
		
		$("#gallery_photo")
			.css("width", new_width+"px")
			.css("height", new_height+"px");
		
		photo_width = new_width;
		photo_height = new_height;	
	}
	
	if ( (window_height*0.8) < photo_height ){
		var new_height = window_height*0.7;
		var new_width =  parseInt( new_height * photo_width / photo_height ); 
		
		$("#gallery_photo")
			.css("width", new_width+"px")
			.css("height", new_height+"px");
		
		photo_width = new_width;
		photo_height = new_height;
	}
	
	//Centramos
	var top_offset =  ( window_height - $("#gallery_photo_container").height() )/2 ;
	var left_offset = ( window_width  - $("#gallery_photo_container").width()  )/2 ;
	
	$("#gallery_photo_container")
		.css("top", top_offset + "px")
		.css("left", left_offset + "px")
		.fadeIn(200)
	;
	
}

this.putTheButtons = function() { 

	var window_width  = $(window).width();
	var window_height = ($(window).height() * 0.9);
	
	var top_offset  = ( window_height - $("#gallery_photo_container").height() )/2 ;
	var left_offset = ( window_width  - $("#gallery_photo_container").width()  )/2 ;
	
	var photo_width = $("#gallery_photo_container").width();
	
	$("#gallery_buttons")
		.css("top", "35%")
		.css("left", "80%")
		.fadeIn(200)
		;
	
	$("#gallery_info_box")
		.css("top", top_offset + ($("#gallery_photo").height() * 0.78) + "px")
		.css("left", left_offset + 70 + "px" )
		.css("width", (photo_width - 90) + "px" )
		;
	
	$("#gallery_prev").hover( function(e){
		this.src = 'gallery_buttons_prev-hover.gif';
	},
	function(e){
		this.src = 'gallery_buttons_prev.gif';	
	});
	
	$("#gallery_next").hover( function(e){
		this.src = 'gallery_buttons_next-hover.gif';
	},
	function(e){
		this.src = 'gallery_buttons_next.gif';	
	});
	
	$("#gallery_close").hover( function(e){
		this.src = 'gallery_buttons_close-hover.gif';
	},
	function(e){
		this.src = 'gallery_buttons_close.gif';	
	});
	
	$("#gallery_info").hover( function(e){
		this.src = 'gallery_buttons_info-hover.gif';
	},
	function(e){
		if ( $("#gallery_info_box").css("display") == 'none' )
			this.src = 'gallery_buttons_info.gif';
		else
			this.src = 'gallery_buttons_info-push.gif';
	});
}

function preloadGallery() {
	//Precarga imágenes
	var gallery_foto_list = '';
	$(".photoAlbumEntryWrapper img").each( function(){
		var new_src = this.src.replace('/image_thumb','');
		jQuery.preLoadImages(new_src);
		gallery_foto_list = gallery_foto_list + ':#:' + new_src + ":$:" + this.title + ":$:" + this.alt;
		
		
	});	
	
	if ( gallery_foto_list != '' ){
		var gallery_foto_list_aux = '<div id="gallery_foto_list" style="display: none">' + gallery_foto_list.substring(3) + '</div>';
		$("body").append(gallery_foto_list_aux);
	}
	
	//Desactivamos comportamiento por defecto
	$(".photoAlbumEntry a").unbind();
}
