AlfaImageTransformInfos = new Object();

// uncomment the following method to activate automatic image resizing/cropping
//jQuery(document).ready(
//
//	/*
//	This function gets loaded when all the HTML, not including the portlets, is
//	loaded.
//	*/
//
//    function() {
////    	alert("document ready");
//    	window.setTimeout("transformImages()", 250);
//    }
//);

//jQuery(document).last(
//
//		/*
//		This function gets loaded when everything, including the portlets, is on
//		the page.
//		*/
//
//		function() {
//			alert("document last");
//		}
//	);

function transformImages() {
	var mediaobjects = jQuery("img.mediaobject");
	jQuery.each(mediaobjects, function(i, mediaobject) {
		var moclassid = jQuery(mediaobject).attr("class").match(/itemid_([\w-]*)/)
		if (moclassid) {
			var transinfo = AlfaImageTransformInfos[moclassid[1]];
			var transformtype = transinfo["transformtype"];
			if (transformtype == 'rezize') {
				resizePictureGeneric(mediaobject, transinfo["resizeParent"], transinfo["parentSelector"]);
			} else if (transformtype == 'crop') {
				cropPictureGeneric(mediaobject, transinfo["resizeParent"], transinfo["parentSelector"], 
						transinfo["cropX0"], transinfo["cropX1"], transinfo["cropY0"], transinfo["cropY1"])
			}
		}
	});
}

function resizePictureGeneric(item, doParentResize, parentselector) {
	var jitem = jQuery(item);
	
	var height = item.height;
	var width = item.width;
	if (height <= 0 || width <= 0) {
		return;
	}
	var maxwidth = jitem.css('max-width').replace(/px/g, '');
	if (maxwidth <= 0 || maxwidth == 'none') {
		maxwidth = width;
	}
	var maxheight = jitem.css('max-height').replace(/px/g, '');
	if (maxheight <= 0 || maxheight == 'none') {
		maxheight = height;
	}
	
	if (width == maxwidth || height == maxheight) {
		if (doParentResize) {
			// Don't change css properties of image if image is already sized correctly
			jitem.parents(parentselector).css("width", maxwidth + "px");
		}
	} else if(width >= height) {
		item.height = Math.round(maxwidth*height/width);
		var newwidth = maxwidth;
		item.width = newwidth;
		if (doParentResize) {
			jitem.parents(parentselector).css("width", newwidth + "px");
		}
	} else if(height > width ) {
		item.height = maxheight;
		var newwidth = Math.round(maxheight/height*width);
		item.width = newwidth;
		if (doParentResize) {
			jitem.parents(parentselector).css("width", newwidth + "px");
		}
	} 
}

function cropPictureGeneric(item, doParentResize, parentselector, x0, x1, y0, y1) {
	//alert("crop");
	var jitem = jQuery(item);
	
	var height = item.height;
	var width = item.width;
//	alert(width + "x" + height);
	if (height <= 0 || width <= 0) {
		return;
	}
	
	var maxwidth = jitem.css('max-width').replace(/px/g, '');
	if (maxwidth <= 0 || maxwidth == 'none') {
		maxwidth = width;
	}
	var maxheight = jitem.css('max-height').replace(/px/g, '');
	if (maxheight <= 0 || maxheight == 'none') {
		maxheight = height;
	}
	var cropwidth = maxwidth;
//	alert(maxwidth + "x" + maxheight);
	
	if ((x1-x0)*width >= (y1-y0)*height) {
		var newwidth = Math.round(maxwidth/(x1-x0)*100);
		var newheight = Math.round(newwidth*height/width);
		item.width = newwidth;
		item.height = newheight;
		jitem.css("width", Math.round(maxwidth/(x1-x0)*100) + "px");
		jitem.css("max-width", Math.round(maxwidth/(x1-x0)*100) + "px");
		jitem.css("max-height", Math.round(newwidth*height/width) + "px");
		jitem.css("margin-left", -1*Math.round(newwidth*x0/100) + "px");
		jitem.css("margin-top", -1*Math.round(newheight*y0/100) + "px");
		var cropbox = jitem.parents(".imagecrop");
		cropbox.css("width", maxwidth + "px");
		cropbox.css("height", Math.round(newheight*(y1-y0)/100) + "px");
		
	} else {
		var newheight = Math.round(maxheight/(y1-y0)*100);
		var newwidth = Math.round(newheight*width/height);
		item.width = newwidth;
		item.height = newheight;
		jitem.css("width", Math.round(newheight*width/height) + "px");
		jitem.css("max-width", Math.round(newheight*width/height) + "px");
		jitem.css("max-height", Math.round(maxheight/(y1-y0)*100) + "px");
		jitem.css("margin-left", -1*Math.round(newwidth*x0/100) + "px");
		jitem.css("margin-top", -1*Math.round(newheight*y0/100) + "px");
		var cropbox = jitem.parents(".imagecrop");
		cropwidth = Math.round(newwidth*(x1-x0)/100);
		cropbox.css("width", cropwidth + "px");
		cropbox.css("height", maxheight + "px");
	}
	
	if (doParentResize) {
		jitem.parents(parentselector).css("width", cropwidth + "px");
	}
}