/* $Id: ImgResize.js 84386 2009-10-01 11:47:48Z k.reimer $
 * Copyright (C) 2006 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */
 
 /**
 * @fileoverview
 *
 * Contains the ImgResize class. Does not depend on any other class.
 *
 * @author Andreas Kornetka <a.kornetka@iplabs.de>
 * @version $Revision: 84386 $
 */

/**
 * Constructor for an ImgResize Object.
 *
 * @class <p>The ImageResize class provides the abilty of resizing and centering images and reduces additional amount of thumbs
 *  	  on the server.</p>
 *
 * @constructor
 * @param {Integer} maxWidth
 *            The maximum width for thumb images. Inevitable parameter!
 * @param {Integer} maxHeight
 *            The maximum height for thumb images. Inevitable parameter!
 * @param {String} verticalAlign
 *            The vertical align values can be: 'top', 'middle' (default), 'bottom' 
 * @param {String} textAlign
 *            The text align values can be: 'left', 'center' (default), 'right' 
 * @jscram.public class="ImgResize"
 */
function ImgResize(maxWidth, maxHeight, verticalAlign, textAlign) {
	/* private: */
	this.__maxWidth      = maxWidth;
 	this.__maxHeight     = maxHeight;
 	if(typeof verticalAlign != 'string') {verticalAlign = new String();} 
 	switch(verticalAlign.toLowerCase()) {
 		case 'top':
 			this.__verticalAlign = 'top';
 			break;
 		case 'bottom':
 			this.__verticalAlign = 'bottom';
 			break;
 		default:
 			this.__verticalAlign = 'middle';
 	}
 	if(typeof textAlign != 'string') {textAlign = new String();} 
 	switch(textAlign.toLowerCase()) {
 		case 'left':
 			this.__textAlign = 'left';
 			break;
 		case 'right':
 			this.__textAlign = 'right';
 			break;
 		default:
 			this.__textAlign = 'center';
 	}	
}

/**
 * Calculates the optimal width & height and the left, top margin (position) of an image toward its rectangle.
 * 
 * @param {Integer} width
 *            Width of the thumb or the origanal image - keeping the aspect ratio matters. Inevitable parameter!
 * @param {Integer} height
 *            Height of the thumb or the origanal image - keeping the aspect ratio matters. Inevitable parameter!
 * @param {Integer} rotation
 *            Actual rotation of the image. Valid values are: none or 0, 90, 180, 270
 * @return
 *            returns an object with .width, .height .left and .top members
 */  
ImgResize.prototype.calculate = function(width, height, rotation) {
    var calculatedWidth, calculatedHeight, calculatedLeft = 0, calculatedTop = 0;
    if(rotation == 90 || rotation == 270) {
        calculatedHeight = width;
        width            = height;
        height           = calculatedHeight;
    }
    if((width / height) <= (this.__maxWidth / this.__maxHeight)) {
        calculatedHeight = this.__maxHeight;
        calculatedWidth  = width / (height / this.__maxHeight);
    } else {
        calculatedWidth  = this.__maxWidth;
        calculatedHeight = height / (width / this.__maxWidth);
    }
	switch(this.__verticalAlign) {
		case 'middle':
			calculatedTop = (this.__maxHeight - calculatedHeight) / 2;
			break;
		case 'bottom':
			calculatedTop = this.__maxHeight - calculatedHeight;
			break;
	}
	switch(this.__textAlign) {
		case 'center':
			calculatedLeft = (this.__maxWidth - calculatedWidth) / 2;
			break;
		case 'right':
			calculatedLeft = this.__maxWidth - calculatedWidth;
			break;
	}
    return {'width': Math.floor(calculatedWidth), 'height': Math.floor(calculatedHeight), 'left': Math.floor(calculatedLeft), 'top': Math.floor(calculatedTop)};		
}

/**
 * Resizes and positions an image.
 * 
 * @param {String} id
 *			  Id reference as string. Optional a second id can be added by seperating both values with a space.
 *			  If the later id references to one valid parentnode, then the positioning of that image will be
 *			  appied there (with the use of css margin - so be cautious). Inevitable parameter!
 * @param {Integer} width
 *            Width of the thumb or the origanal image - keeping the aspect ratio matters. Inevitable parameter!
 * @param {Integer} height
 *            Height of the thumb or the origanal image - keeping the aspect ratio matters. Inevitable parameter!
 * @param {Integer} rotation
 *            Actual rotation of the image. Valid values are: none or 0, 90, 180, 270
 */  
ImgResize.prototype.compute = function(id, width, height, rotation) {
	var calculated, img, out;
	id = id.split(' '); 
    img = document.getElementById(id[0]);
    out = document.getElementById(id[1]);
    if(img) {
		calculated = this.calculate(width, height, rotation);
        img.style.width  = calculated.width + 'px';
        img.style.height = calculated.height + 'px';
        if(out) {img = out;}
        img.style.marginLeft = calculated.left + 'px';
        img.style.marginTop  = calculated.top + 'px';
	}
}

/**
 * Loads an image from the given URL and when loading has finished, resizes and positions the loaded image.
 * 
 * @param {String} id
 *			  Id reference to the image object as string.
 * @param {String} url
 *            The URL to the image.
 */  
ImgResize.prototype.loadImage = function(id, url) {
	var img = new Image();
	img.docImgId = id;
	img.instance = this;
	img.onload = this.__processLoadedImage;
	img.src = url;
}

ImgResize.prototype.__processLoadedImage = function(img) {
	if (this.instance)
		return this.instance.__processLoadedImage(this);

	// scale and position the image
	this.compute(img.docImgId, img.width, img.height, 0);

	// assign to document image
	var docImg = document.getElementById(img.docImgId);
	docImg.src = img.src;
}