/********************************
# Resize images MOD
# Created by: SoutlinK
# Version: 2.0
*********************************/

/********************************
# Config
*********************************/
var RESIZED_IMAGES_MAX_WIDTH = 550;
var RESIZED_IMAGES_MAX_HEIGHT = 550;
var RESIZED_IMAGES_DIALOG_MAX_WIDTH = 900;
var RESIZED_IMAGES_DIALOG_MAX_HEIGHT = 700;
//##################################//
var imgLoader = new Image();// preload image
imgLoader.src = t_imageset_path + '/ajax_loader.gif';

$(function(){	
	$("body").append('<div id="resized_image">&nbsp;</div>');
	$("#resized_image").css({
		'background': 'url(' + t_imageset_path + '/ajax_loaderb.gif) no-repeat center center)',
		'text-align': 'center',
		'vertical-align': 'middle',
		'display': 'none'
	});
});

function resize_images()
{
	$("#wrapcentre img").each(function(i){
		//Check if the image is already resized	
		if ($(this).attr("resized") == 'resized' || $(this).attr("noresize") == 'noresize' )
		{
			// The image is already resized so discart it
			return;
		}
		var image_resized = false;
		var image = $(this);
		//Now check the image dimensions
		var image_width = $(this).width();		
		//Check if the image width is too long
		if (image_width > RESIZED_IMAGES_MAX_WIDTH)
		{
			image_resized = true;
			$(this).width(RESIZED_IMAGES_MAX_WIDTH);
		}
		
		var image_height = $(this).height();
		//Check if the image height is too long
		if (image_height > RESIZED_IMAGES_MAX_HEIGHT)
		{
			image_resized = true;
			$(this).height(RESIZED_IMAGES_MAX_HEIGHT);
		}
		
		//Now style the new image resized
		if (image_resized == true)
		{
			$(this).attr("resized","resized").attr('id', 'resized_image[' + i + ']');
			//Aņadimos el borde y el texto
			$(this).before('<table><tr><td></td></tr><tr><td></td></tr></table>');
			$(this).prev()
			.css({
				'border-width': '1px',
				'border-color':	'#FFF',
				'border-style':	'dashed',
				//'border-bottom': 'none',
				'margin': '0px',
				'padding': '0px'				   
			})
			.click(function(){show_resized_image(image)});
			$(this).prev().children().children('tr:eq(0)').children()
			.addClass('ui-state-hover')
			.css({
				'cursor': 'pointer',
				'padding': '0px'
			})
			.attr('align', 'center')
			.append('<span class="ui-icon ui-icon-alert" style="float: left; margin-top: 1px;"></span>Esta imagen ha sido encogida. Pulsa sobre esta barra para verla a tama&ntilde;o real');
			$(this).prev().children().children('tr:eq(1)').children()
			.css({
				'padding':	'0px'
			})
			.append($(this));
			$(this).css({
				'cursor': 'pointer'		
			});
		}
	});
}

function show_resized_image(image)
{
	if (!image)
	{
		return false;	
	}
	
	var resized_image = new Image();
	resized_image.src = $(image).attr("src");
	//resized_image.align = 'center';
	
	var dialog_width, dialog_height;
	dialog_width = parseFloat(resized_image.width) + 100;
	dialog_height = parseFloat(resized_image.height) + 100;
	
	if (parseFloat(resized_image.width) > RESIZED_IMAGES_DIALOG_MAX_WIDTH || parseFloat(resized_image.height) > RESIZED_IMAGES_DIALOG_MAX_HEIGHT)
	{
		dialog_width = RESIZED_IMAGES_DIALOG_MAX_WIDTH;
		dialog_height = RESIZED_IMAGES_DIALOG_MAX_HEIGHT;
	}	
	
	$("#resized_image").html(resized_image);
	$("#resized_image").dialog({
		autoOpen: true,
		buttons:
		{
			"Abrir en una nueva ventana": function(){window.open(resized_image.src);},
			"Cerrar" : function(){$(this).dialog("close");}
		},
		closeOnEscape: true,
		draggable: true,
		hide: "fold",
		modal: true,
		position: "center",
		resizable: false,
		show: 'fold',
		width: dialog_width,
		height: dialog_height,
		title: '',
		close: function(){$(this).dialog("destroy");}
	});
}

$(window).load(function (){resize_images();});