/**
 * IMoveOpenEffectGrow - Plugin di apertura
 * 
 * alfredo.cerutti@intercom.it
 * 
 * 
 * 
 */

IMoveOpenEffectGrowOptions = Object.extend({
    resizeSpeed: 7,
	startSize: {
		height: 350,
		width: 500
	},
	showDetails : true,
	animateDetails: false			
});
	
var IMoveOpenEffectGrow = Class.create();

IMoveOpenEffectGrow.prototype = {

	initialize: function(conf){ 
        if (IMoveOpenEffectGrowOptions.resizeSpeed > 10) IMoveOpenEffectGrowOptions.resizeSpeed = 10;
        if (IMoveOpenEffectGrowOptions.resizeSpeed < 1)  IMoveOpenEffectGrowOptions.resizeSpeed = 1;
	
		if (typeof conf == 'undefined') conf = {};
		this.resizeSpeed = conf.resizeSpeed || IMoveOpenEffectGrowOptions.resizeSpeed;	
		this.startSize = conf.startSize || IMoveOpenEffectGrowOptions.startSize;
		this.showDetails = conf.showDetails || IMoveOpenEffectGrowOptions.showDetails;
		this.animateDetails = conf.animateDetails || IMoveOpenEffectGrowOptions.animateDetails;
	},
	
	_setParent:function(parent) {	
		this.parent = parent;
		
		this.parent.resizeSpeed = this.resizeSpeed;
	},
	

	run: function(width, height) {
        // get current width and height
        var widthCurrent  = this.parent.imove_outerImageContainer.getWidth();
        var heightCurrent = this.parent.imove_outerImageContainer.getHeight();

		if (this.parent.max_height && this.parent.max_width) {			
	        width = this.parent.max_width;
	        height = this.parent.max_height;	
		}
		
        // get new width and height
    	var widthNew  = (width  + IMoveOptions.borderSize * 2);
    	var heightNew = (height + IMoveOptions.borderSize * 2);

        // scalars based on change from old to new
        var xScale = (widthNew  / widthCurrent)  * 100;
        var yScale = (heightNew / heightCurrent) * 100;

        // calculate size difference between new and old image, and resize if necessary
        var wDiff = widthCurrent - widthNew;
        var hDiff = heightCurrent - heightNew;

        if (hDiff != 0) new Effect.Scale(this.parent.imove_outerImageContainer, yScale, {scaleX: false, duration: this.parent.resizeDuration, queue: 'front'}); 
        if (wDiff != 0) new Effect.Scale(this.parent.imove_outerImageContainer, xScale, {scaleY: false, duration: this.parent.resizeDuration, delay: this.parent.resizeDuration}); 

        // if new and old image are same size and no scaling transition is necessary, 
        // do a quick pause to prevent image flicker.
        var timeout = 0;
        if ((hDiff == 0) && (wDiff == 0)){
            timeout = 100;
            if (Prototype.Browser.IE) timeout = 250;   
        }

        (function(){
			// posiziono i controlli
			var nord_sud = {
				height: '13',
				width: '21'
			};
			var ovest_est = {
				height: '21',
				width: '13'
			};

            this.parent.imove_ovest.setStyle({ 
				top: Math.ceil((this.parent.max_height - (ovest_est.height/2)) /2) + 'px',
				left: (1+ IMoveOptions.borderSize)+'px'		
			});
            this.parent.imove_est.setStyle({ 
				top: Math.ceil((this.parent.max_height - (ovest_est.height/2)) /2) + 'px',
				right:(ovest_est.width/2)+'px'				 
			});			
			this.parent.imove_nord.setStyle({ 
				left: Math.ceil((this.parent.max_width - nord_sud.width) /2) + 'px',
				top: (1 +IMoveOptions.borderSize)+'px'
			});
			this.parent.imove_sud.setStyle({ 
				bottom: (1+(nord_sud.height/2)) +'px',
				left: Math.ceil((this.parent.max_width - (nord_sud.width/2)) /2) + 'px'
			});			


            this.parent.imove_imageDataContainer.setStyle({ width: widthNew + 'px' });

			this.parent.imove_Image_div.setStyle({height: height +'px', width: width +'px'});

            this.parent.showImage();
        }).bind(this).delay(timeout / 1000);		
	}
}
