this.photoGalleryMobile = 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 comportamiento por defecto
		$(".photoAlbumEntry a").click( function(e){
			return false;
		});
	
		//Aplicamos efecto album de fotos
		$(".photoAlbumEntry img").click( function(e){
			changeImageGalleryMobileElement(this);
			return false;
		});
	}
	else {
		//Espacio para los textos
		$(".photoAlbumEntry").css('height', 'auto');
	}

};


this.changeImageGalleryMobileElement = function( el ){
	
	var new_href = el.src.replace('/image_thumb', '');

	$("#gallery_photo_container").remove();
	$("#gallery_buttons_mobile").remove();
	document.getElementById('portal-footer-dock').style.display = 'none';
	
	var botones = 
		"<div id='gallery_buttons_mobile'>" +
			"<img src='gallery_buttons_prev.gif' id='gallery_prev' alt='Previus' onclick='galleryMobilePrevElement()' /> " +
			"<img src='gallery_buttons_next.gif' id='gallery_next' alt='Next' onclick='galleryMobileNextElement()' /> " +
			"<img src='gallery_buttons_info.gif' id='gallery_info' alt='Info' onclick='galleryMobileInfo()' /> " +
			"<img src='gallery_buttons_close.gif' id='gallery_close' alt='Close' onclick='galleryMobileClose()' /> " +
		"</div>";	
	
	var html_content = "<div id='gallery_photo_container'> <img id='gallery_photo' src='" + new_href + "/image_preview' alt='" + el.title + "' /> </div>";
	$("body").append(botones + html_content);
	
	var info = '<div id="gallery_info_box" onclick="gallery_info_box_close()"> '+ el.alt + "<p>" + el.title + ' </div>';
	$("#gallery_photo_container").append(info);
	
	document.getElementById('gallery_photo_container').style.display = 'block';
	fixGalleryImageMobileSize(screen.width, screen.height);
	
	applyMaskMobile();
	
}

this.galleryMobileInfo = function (e) { 
	
	if ( $("#gallery_info_box").css("display") == 'none' ) {
		 $("#gallery_info_box").css('display', 'block');
		 $("#gallery_info").attr("src", "gallery_buttons_info-push.gif");
	}
	else {
		$("#gallery_info_box").css('display', 'none');
		$("#gallery_info").attr("src", "gallery_buttons_info.gif");
	}			

}

this.galleryMobileClose = function (e) {
	$('#mask').fadeOut(200).hide();
	$("#gallery_buttons_mobile").fadeOut(200).remove();
	$("#gallery_info_box").fadeOut(200).remove();
	$("#gallery_photo_container").fadeOut(200).remove();
	
	document.getElementById('portal-footer-dock').style.display = 'block';
}

this.galleryMobilePrevElement = function (e) {

	var photos = $(".photoAlbumEntry img");
	var actual = $("#gallery_photo").attr("src").replace("image_preview", "image_thumb");

	var aux = 0;
	var prev_element = -1;

	photos.each( function(e) {	
		
		if ( this.src == actual){
			prev_element = aux-1;
		}
		aux++;
	});

	if ( prev_element < 0 )
		prev_element = photos.length - 1;

	changeImageGalleryMobileElement(photos[prev_element]);
}



this.galleryMobileNextElement = function (e) {

	var photos = $(".photoAlbumEntry img");
	var actual = $("#gallery_photo").attr("src").replace("image_preview", "image_thumb");

	var aux = 0;
	var next_element = photos.length;

	photos.each( function(e) {		
	
		if ( this.src == actual){
			next_element = aux+1;
		}
		aux++;
	});

	if ( next_element >= photos.length )
		next_element = 0;

	changeImageGalleryMobileElement(photos[next_element]); 
}



this.applyMaskMobile = function() {

	var maskHeight = $("#portal-main-content-wrapper").height(); 
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight});	
	$('#mask').fadeTo(5, 1);		

}



this.fixGalleryImageMobileSize = 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 ;
	
	if ( screen.height > screen.width ){
		$("#gallery_photo_container")
			.css("top", (top_offset-30) + "px")
			.css("left", (left_offset) + "px")
			.fadeIn(200)
		;
	}
	else {
		$("#gallery_photo_container")
			.css("top", (top_offset+30) + "px")
			.css("left", (left_offset-20) + "px")
			.fadeIn(200)
		;
	}
	
	document.getElementById("gallery_buttons_mobile").style.display = 'block';
}
