// script.aculo.us EffectResize.js

// Copyright(c) 2007 - Pedro de la Lastra http://www.thebluetrack.com
//
// Effect ScaleTo.js is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

// Modified by Pedro de la Lastra to work with Prototype 1.4.0_rc3

/* Helper Effect for scaling elements...
 */
Effect.ScaleTo = Class.create(Effect.Base, {
  initialize: function(element, amount_x, amount_y) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      scaleFromCenter: false,
      scaleMode: 'box',        // 'box' or 'contents' or { } with provided values
      scaleXFrom: this.element.getWidth(),
      scaleXTo: this.element.getWidth() + amount_x,
      scaleYFrom: this.element.getHeight(),
      scaleYTo: this.element.getHeight() + amount_y
    }, arguments[2] || { });
    this.start(options);
  },
  setup: function() {
    this.restoreAfterFinish = this.options.restoreAfterFinish || false;
    this.elementPositioning = this.element.getStyle('position');
    
    this.originalStyle = { };
    ['top','left','width','height','fontSize'].each( function(k) {
      this.originalStyle[k] = this.element.style[k];
    }.bind(this));
      
    this.originalTop  = this.element.offsetTop;
    this.originalLeft = this.element.offsetLeft;
    
    this.factorX =  (this.options.scaleXTo - this.options.scaleXFrom)/100;
    this.factorY =  (this.options.scaleYTo - this.options.scaleYFrom)/100;
    
    this.dims = null;
    if (this.options.scaleMode=='box')
      this.dims = [this.element.offsetHeight, this.element.offsetWidth];
    if (/^content/.test(this.options.scaleMode))
      this.dims = [this.element.scrollHeight, this.element.scrollWidth];
    if (!this.dims)
      this.dims = [this.options.scaleMode.originalHeight,
                   this.options.scaleMode.originalWidth];
  },
  update: function(position) {
    var currentScaleX = (this.options.scaleFromX/100.0) + (this.factorX * position);
    var currentScaleY = (this.options.scaleFromY/100.0) + (this.factorY * position);
	 
    this.setDimensions(this.dims[0] * currentScaleY, this.dims[1] * currentScaleX);
  },
  finish: function(position) {
    if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle);
  },
  setDimensions: function(height, width) {
    var d = { };
    if (this.options.scaleX) d.width = width.round() + 'px';
    if (this.options.scaleY) d.height = height.round() + 'px';
    if (this.options.scaleFromCenter) {
      var topd  = (height - this.dims[0])/2;
      var leftd = (width  - this.dims[1])/2;
      if (this.elementPositioning == 'absolute') {
        if (this.options.scaleY) d.top = this.originalTop-topd + 'px';
        if (this.options.scaleX) d.left = this.originalLeft-leftd + 'px';
      } else {
        if (this.options.scaleY) d.top = -topd + 'px';
        if (this.options.scaleX) d.left = -leftd + 'px';
      }
    }
    this.element.setStyle(d);
  }
});