// Gaia Ajax Widgets Copyright (C) 2007  Frost Innovation AS. details at http://ajaxwidgets.com/
/* 
 * Gaia Ajax Widgets, an Ajax Widget Library
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * This program is distributed under either GPL version 2 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Frost Innovation AS
 * read the details at http://ajaxwidgets.com/
 */




/* Helper Effect for resizing elements...
 */
Effect.ReSize = Class.create();
Effect.ReSize.prototype = Object.extend(new Effect.Base(), {
  initialize: function(element) {
    this.element = element;
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({ amount: 100, direction: 'vert', toSize:null }, arguments[1] || {});
    if( options.direction == 'vert' )
      this.originalSize = options.originalSize || parseInt(this.element.style.height);
    else
      this.originalSize = options.originalSize || parseInt(this.element.style.width);

    if( options.otherEl ){
      if( options.direction == 'vert' ){
        if( options.otherEl.length != 'undefined'){
          this.originalSizeOtherEl = new Array();
          options.otherEl.each(function(idx){
            this.originalSizeOtherEl.push(parseInt(idx.style.height));
          }.bind(this));
        } else {
          this.originalSizeOtherEl = options.originalSizeOtherEl || parseInt(options.otherEl.style.height);
        }
      } else {
        if( options.otherEl.length != 'undefined'){
          this.originalSizeOtherEl = new Array();
          options.otherEl.each(function(idx){
            this.originalSizeOtherEl.push(parseInt(idx.style.width));
          }.bind(this));
        } else {
          this.originalSizeOtherEl = options.originalSizeOtherEl || parseInt(options.otherEl.style.width);
        }
      }
    }

    if( options.toSize != null )
      options.amount = options.toSize - this.originalSize;
    if( options.otherElToSize != null ){
      if( options.otherEl.length != 'undefined'){
        options.amountOtherEl = new Array();
        for( var idx = 0; idx < options.otherEl.length; idx++ ){
          if( !options.otherElToSize.length || options.otherElToSize.length == 1 ){
            options.amountOtherEl.push(options.otherElToSize - this.originalSizeOtherEl[idx]);
          } else {
            options.amountOtherEl.push(options.otherElToSize[idx] - this.originalSizeOtherEl[idx]);
          }
        }
      } else {
        options.amountOtherEl = options.otherElToSize - this.originalSizeOtherEl;
      }
    }

    this.start(options);
  },

  setup: function() {
    // Prevent executing on elements not in the layout flow
    if(this.element.style.display == 'none') { this.cancel(); return; }
  },

  update: function(position) {
    if( this.options.direction == 'vert' ){
      Element.setStyle(this.element, {height: this.originalSize+(this.options.amount*position)+'px'});
    } else {
      Element.setStyle(this.element, {width: this.originalSize+(this.options.amount*position)+'px'});
    }
    if( this.options.otherEl ){
      if( this.options.direction == 'vert' ){
        if( this.options.otherEl.length != 'undefined' ){
          for(var idx = 0; idx < this.options.otherEl.length; idx++){
            Element.setStyle(this.options.otherEl[idx], {height: this.originalSizeOtherEl[idx]+(this.options.amountOtherEl[idx]*position)+'px'});
          }
        } else {
          Element.setStyle(this.options.otherEl, {height: this.originalSizeOtherEl+(this.options.amountOtherEl*position)+'px'});
        }
      } else {
        if( this.options.otherEl.length != 'undefined' ){
          for(var idx = 0; idx < this.options.otherEl.length; idx++){
            Element.setStyle(this.options.otherEl[idx], {width: this.originalSizeOtherEl[idx]+(this.options.amountOtherEl[idx]*position)+'px'});
          }
        } else {
          Element.setStyle(this.options.otherEl, {width: this.originalSizeOtherEl+(this.options.amountOtherEl*position)+'px'});
        }
      }
    }
  },

  finish: function(){
    if( this.options.direction == 'vert' ){
      Element.setStyle(this.element, {height: this.originalSize+this.options.amount+'px'});
      if( this.options.otherEl ) {
        if( this.options.otherEl.length != 'undefined' ){
          for(var idx = 0; idx < this.options.otherEl.length; idx++){
            var newSize;
            if(!this.options.otherElToSize.length)
              newSize = this.options.otherElToSize;
            else
              newSize = this.options.otherElToSize[idx];
            Element.setStyle(this.options.otherEl[idx], {height: newSize+'px'});
          }
        } else {
          Element.setStyle(this.options.otherEl, {height: this.options.otherElToSize+'px'});
        }
      }
    } else {
      Element.setStyle(this.element, {width: this.originalSize+this.options.amount+'px'});
      if( this.options.otherEl ) {
        if( this.options.otherEl.length != 'undefined' ){
          for(var idx = 0; idx < this.options.otherEl.length; idx++){
            Element.setStyle(this.options.otherEl[idx], {width: this.options.otherElToSize[idx]+'px'});
          }
        } else {
          Element.setStyle(this.options.otherEl, {width: this.options.otherElToSize+'px'});
        }
      }
    }
  }
});

// Static method to go directly without animations...!!
Effect.ReSize.Direct = function(element, options){
  switch(options.direction){
    case 'vert':
      var oldHeight = parseInt(element.style.height, 10);
      oldHeight += options.amount;
      element.style.height = (oldHeight+'px');
      break;
    case 'horz':
      var oldWidth = parseInt(element.style.width, 10);
      oldWidth += options.amount;
      element.style.width = (oldWidth+'px');
      break;
    default:
      throw "Unsupported enum in Effect.ReSize.Direct...!!";
  }
}

Effect.ReSize.browserFinishedLoading = true;
