Effect.AScale = Class.create(Effect.Base, {
  initialize: function(element, percent) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    
	var options = Object.extend({
      scaleX: true,
      scaleY: true,
      scaleContent: true,
      scaleFromCenter: true,
      scaleMode: 'box',        // 'box' or 'contents' or { } with provided values
      scaleFrom: 100.0,
	  scaleTo: percent,
	  direction: direction,
	  originals: '',
		evm: 'mouseonn'
	  //restoreAfterFinish: true
    }, arguments[2] || { });
    this.start(options);
  },

  setup: function() {
	this.originals = this.options.originals;
	this.evm = this.options.evm;

    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;
    
    var fontSize = this.element.getStyle('font-size') || '100%';
    ['em','px','%','pt'].each( function(fontSizeType) {
      if (fontSize.indexOf(fontSizeType)>0) {
        this.fontSize     = parseFloat(fontSize);
        this.fontSizeType = fontSizeType;
      }
    }.bind(this));
    
    this.factor = (this.options.scaleTo - this.options.scaleFrom)/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 currentScale = (this.options.scaleFrom/100.0) + (this.factor * position);
    if (this.options.scaleContent && this.fontSize)
      this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType });
    this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale);
  },
  finish: function(position) {
	if (this.evm == 'mouseoff') {
		this.originalStyle['top'] = this.originals[0];
		this.originalStyle['left'] = this.originals[1];
		this.originalStyle['width'] = this.originals[2];
		this.originalStyle['height'] = this.originals[3];
		this.restoreAfterFinish = true;
	}
	else {
		this.restoreAfterFinish = false;
	}
    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);
  }
});




Event.observe(
window,
'load',
function() {
	$$('.animate').each(function(item) {
		
			item.iw = item.offsetWidth;
			item.ih = item.offsetHeight;

			Event.observe(item, 'mouseover', function() {
					this.direction = 1;
				}
			);

			Event.observe(item, 'mouseout', function() {
					this.direction = -1;
				}
			);
		}
	);
}
);


function animation() {
	$$('.animate').each(function(item) {
			ww = item.offsetWidth;
			hh = item.offsetHeight;

			var d = { };

			if (item.direction == 1)
			{
				ww = ww * 1.05;
				hh = hh * 1.05;
				d.width = ww + 'px';
				d.height = hh + 'px';
			}

			if (item.direction == -1)
			{
				if (ww > item.iw)
				{
					ww = ww / 1.1;
					hh = hh / 1.1;
					d.width = ww + 'px';
					d.height = hh + 'px';
				} else {
					d.width = item.iw + 'px';
					d.height = item.ih + 'px';
					item.direction = 0;
				}
			}

			if (ww > item.iw * 1.20)
			{
				item.direction = 0;
			}

			item.setStyle(d);

		}
	);
	setTimeout('animation()', 50);
}

animation();

