var Utils = {
	/*	
	PICT_MAX_WIDTH: 80, 
	PICT_MAX_HEIGHT: 50,
	PICT_MAX_WIDTH_M: 80, 
	PICT_MAX_HEIGHT_M: 50,
	*/
	pictLoadListener: function(sImgId, sWidth, sHeight){
//		alert("pictLoadListener");
/*
		var sWidthAux = (sWidth == undefined ? this.PICT_MAX_WIDTH: sWidth);
		var sHeightAux = (sHeight == undefined ? this.PICT_MAX_HEIGHT: sHeight);
		this.PICT_MAX_WIDTH_M = sWidthAux;
		this.PICT_MAX_HEIGHT_M = sHeightAux;
		*/
	    var oImgElement = document.images[sImgId];
	    var oImg = new Image();
	    oImg.error = false;
	    oImg.onerror = function() {
	        oImg.error = true;
	    };
	    oImg.src = oImgElement.src;
	    var iTimerID;
	    /*
		if (oImg.complete && !oImg.error) {
	            clearInterval(iTimerID);
	            Utils.pictResizer(oImgElement, oImg.width, oImg.height);
	        } else if (oImg.error) {
	            clearInterval(iTimerID);
	        }*/	    
	    function pictResizerCaller() {
			//alert("pictResizerCaller");
	        if (oImg.complete && !oImg.error) {
	            clearInterval(iTimerID);
	            Utils.pictResizer(oImgElement, oImg.width, oImg.height, sWidth, sHeight);
	        } else if (oImg.error) {
	            clearInterval(iTimerID);
	        }
	    };
	    iTimerID = setInterval(pictResizerCaller, 300);
	    
	},
	pictResizer: function(oImgElement, iWidth, iHeight, iMaxWidth, iMaxHeight){
		//alert("pictResizer");
	    if (oImgElement!=null && iWidth > 0 && iHeight > 0 ) {
	        var iScaledWidth, iScaledHeight;
	        if (iWidth <= iMaxWidth && iHeight <= iMaxHeight) {
	            iScaledWidth = iWidth;
	            iScaledHeight = iHeight;
	        } else {
	            var dScaleByX = iMaxWidth / iWidth;
	            var iWidthByX = Math.floor(iWidth * dScaleByX); 
	            var iHeightByX = Math.floor(iHeight * dScaleByX); 
	    
	            var dScaleByY = iMaxHeight / iHeight;
	            var iWidthByY = Math.floor(iWidth * dScaleByY); 
	            var iHeightByY = Math.floor(iHeight * dScaleByY); 
	           
	            var bScaleByXValid = (iWidthByX<=iMaxWidth && iHeightByX<=iMaxHeight);
	            var bScaleByYValid = (iWidthByY<=iMaxWidth && iHeightByY<=iMaxHeight);
	           
	            if (bScaleByXValid && bScaleByYValid) {
	                if (dScaleByX > dScaleByY) {
	                    iScaledWidth = iWidthByX;
	                    iScaledHeight = iHeightByX;
	                } else {
	                    iScaledWidth = iWidthByY;
	                    iScaledHeight = iHeightByY;
	                }
	            } else if (!bScaleByXValid && bScaleByYValid) {
	                iScaledWidth = iWidthByY;
	                iScaledHeight = iHeightByY;
	            } else {
	                iScaledWidth = iWidthByX;
	                iScaledHeight = iHeightByX;
	            }
	        }
			//alert("casi al final");
	        oImgElement.style.width = iScaledWidth + "px";
	        oImgElement.style.height = iScaledHeight + "px";
	        oImgElement.style.left = Math.floor((iMaxWidth - iScaledWidth) / 2) + "px";
	        oImgElement.style.display = "block";
	    }
	}
};
