// Plugin img_ajustables (Ajuste y centrado de imágenes al contenedor)
// Versión 1.2
// Autor: Rodolfo Yllada
// Fecha: 17 de Diciembre de 2010
// Nota:  Se recomienda la llamada a la función dentro de jQuery(window).bind('load', function(){}); 
//        en vez de dentro de jQuery(document).ready(function(){}); ya que así nos cersioramos de que todas las imágenes
//        han sido descargadas antes de aplicarles la función.
// Ejemplo de uso: jQuery('.imagen_ajustable').each(function(){jQuery(this).AjustarImagen();});


jQuery.fn.AjustarImagen = function(ancho, alto) {
	var AnchoImagen = jQuery(this).width();
	var AltoImagen = jQuery(this).height();
	
	jQuery(this).removeAttr('width');
	jQuery(this).removeAttr('height');
	
	if ((ancho != '') && (alto != '')){	
		var AnchoContenedor = ancho;		
		var AltoContenedor = alto;
	}else{
		var AnchoContenedor = jQuery(this).parent().width();		
		var AltoContenedor = jQuery(this).parent().height();
	}
			
	if (AltoContenedor >= AnchoContenedor){
		if (AltoImagen >= AnchoImagen){
			jQuery(this).width(AnchoContenedor); //contenedor vertical e imagen vertical
					
			if (jQuery(this).height() < AltoContenedor){
				jQuery(this).height(AltoContenedor);	
				jQuery(this).css('width','');
			}
		}else{
			jQuery(this).height(AltoContenedor); //contenedor vertical e imagen apaisada
			
			if (jQuery(this).width() < AnchoContenedor){
				jQuery(this).width(AnchoContenedor);
				jQuery(this).css('height','');	
			}
		}
	}else{
		jQuery(this).width(AnchoContenedor); //contenedor apaisado
					
		if (jQuery(this).height() < AltoContenedor){
			jQuery(this).height(AltoContenedor);
			jQuery(this).css('width','');
		}				
	}
			
	AnchoImagen = jQuery(this).width();		
	AltoImagen = jQuery(this).height();
			
	var OffSetAncho = (AnchoContenedor - AnchoImagen) / 2;
	var OffSetAlto = (AltoContenedor - AltoImagen) / 2;
			
	jQuery(this).css('margin-left', OffSetAncho);
	jQuery(this).css('margin-top', OffSetAlto);
}
