var	morphTMP	= Class.create({

	options			: null,
	width			: 0,
	currentEffect	: null,
	elAnimate		: null,
	state			: 1,

	initialize	: function(elAction, elAnimate, options) {

		elAction		= $(elAction);
		this.elAnimate	= $(elAnimate);

		this.options = Object.extend({
			transitionOpen	: Effect.Transitions.Elastic,
			transitionClose	: Effect.Transitions.linear,
			durationOpen	: 1,
			durationClose	: 0.3,
			fixedSize		: null,
			eventClose		: 'mouseout',
			eventOpen		: 'mouseover',
			callBackOpen	: Prototype.emptyFunction,
			callBackClose	: Prototype.emptyFunction,

			startClose		: true,
			afterFinish		: Prototype.emptyFunction
		}, options || {});

		if(this.options.fixedSize !== null) {
			this.width		= this.options.fixedSize;
		} else {
			this.width		= this.elAnimate.getWidth();
		}

		if(this.options.startClose === true) {
			this.close();
//			this.elAnimate.setStyle({width: '0px', overflow: 'hidden'});
			this.state = 0;
		}

		elAction.observe(this.options.eventOpen, this.open.bind(this));
		elAction.observe(this.options.eventClose, this.close.bind(this));

	},

	open		: function() {

		if( this.options.eventOpen !== this.options.eventClose || this.state == 0) {
			if(this.currentEffect != null) {
				this.currentEffect.cancel();
			}

			this.currentEffect	= new Effect.Morph(this.elAnimate,
					{
						duration: this.options.durationOpen,
						style: 'width: ' + this.width + 'px',
						transition: this.options.transitionOpen,
						afterFinish: function(){this.currentEffect = null;this.state = 1;this.options.afterFinish();}.bind(this)
					}
				);

			this.options.callBackOpen.bind(this)();
		}

	},

	close		: function() {
//alert('ok');
		if( this.options.eventOpen !== this.options.eventClose || this.state == 1) {

			
			if(this.currentEffect != null) {
				this.currentEffect.cancel();
			}

			this.currentEffect	= new Effect.Morph(this.elAnimate,
					{
						duration: this.options.durationClose,
						style: 'width: 0px',
						transition: this.options.transitionClose,
						afterFinish: function(){this.currentEffect = null;this.state = 0;this.options.afterFinish();}.bind(this)
					}
				);

			this.options.callBackClose.bind(this)();
		}
	}

});


Object.extend(Effect.Transitions, {
	// adapted from ElasticIn
  Elastic: function(pos) {
    return -1*Math.pow(2,-7*pos)*Math.sin((pos*4-1)*(2*Math.PI)/2)+1;
  },
	// adapted from EaseInOut
  EaseFromTo: function(pos) {
    return -0.5*((pos-=2)*pos*pos*pos-2);
  },
	// adapted from EaseIn
  EaseFrom: function(pos) {
    return Math.pow(pos,4);
  },
	// adapted from EaseOut
  EaseTo: function(pos) {
    return Math.pow(-1*pos,4);
  },
	// adapted from ExpoEaseIn
  ExpoEaseTo: function(pos) {
    return -1*Math.pow(2,-10*pos)+1;
  },
	// adapted from BackEaseInOut
  SwingFromTo: function(pos) {
    var s=1.70158;
    if((pos/=0.5)<1) return 0.5*(pos*pos*(((s*=(1.525))+1)*pos-s));
    return 0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos+s)+2);
  },
	// adapted from BackEaseIn
  SwingFrom: function(pos) {
    var s=1.70158;
    return pos*pos*((s+1)*pos-s);
  },
	// adapted from BackEaseOut
  SwingTo: function(pos) {
    var s=1.70158;
    return(pos-=1)*pos*((s+1)*pos+s)+1;
  },
	// adapted from EaseOutBounce
  Bounce: function(pos) {
    if(pos<(1/2.75)) {
      return (7.5625*pos*pos);
    } else if(pos<(2/2.75)) {
      return (7.5625*(pos-=(1.5/2.75))*pos+.75);
    } else if(pos<(2.5/2.75)) {
      return (7.5625*(pos-=(2.25/2.75))*pos+.9375);
    } else {
      return (7.5625*(pos-=(2.625/2.75))*pos+.984375);
    }
  },
	// newly created based on EaseOutBounce
  BouncePast: function(pos) {
    if(pos<(1/2.75)) {
      return (7.5625*pos*pos);
    } else if(pos<(2/2.75)) {
      return 2-(7.5625*(pos-=(1.5/2.75))*pos+.75);
    } else if(pos<(2.5/2.75)) {
      return 2-(7.5625*(pos-=(2.25/2.75))*pos+.9375);
    } else {
      return 2-(7.5625*(pos-=(2.625/2.75))*pos+.984375);
    }
  }
});
