/*
* Floater.js #20070604, 
*
* date   : 2008-11-02
* author : this upgraded from mootools 1.1 to 1.2 by odop, originally http://www.yangkun.pe.kr, 
* email  : yangkun7@gmail.com
* license : MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

var Floater = new Class({
	Extends: Fx.Tween,

	options: {
		duration: 1000,
		transition: Fx.Transitions.Sine.easeInOut,
		wait: false,
		initTop: 0,
		initLeft: 30,
		leftGuider: null,
		bottom: null
	},
	
	initialize: function(floater, options){
		this.parent(floater, 'top', options);
		this.isPlay = true;

		this.options.initTop = options.initTop;
		this.options.leftGuider = options.leftGuider;

		this.set('top', this.options.initTop);
		this.adjustLeft();
		this.prevTop = this.options.initTop;
		if (options.bottom) this.bottomBound = $(options.bottom).getTop() - this.element.getPosition().y;
		
		window.addEvent('scroll', this.adjustTop.bind(this));
		window.addEvent('resize', this.adjustLeft.bind(this));
	},

	adjustTop: function(){
		if (this.isPlay)
		{
			var newTop = this.computeTop();
			if (newTop <= this.options.initTop) newTop = this.options.initTop;
			if ($defined(this.bottomBound) && newTop >= this.bottomBound) newTop = this.bottomBound;
		
			if (this.prevTop != newTop)
			{
				this.start('top', newTop);
				this.prevTop = newTop;
			}
		}
	},

	computeTop: function(){
		return window.getScrollTop() + this.options.initTop;
	},

	adjustLeft: function() {
		if (this.isPlay) {
			if (this.options.leftGuider) this.set('left', this.options.initLeft + $(this.options.leftGuider).getPosition().x);
		}
	},

	fix: function(){
		this.stop();
		this.set(this.options.initTop);
		this.prevTop = this.options.initTop;
		this.isPlay = false;
	},

	play: function(){
		this.isPlay = true;
		this.adjustTop();
	}
});


Floater.implement(new Events);

Floater.KeepTop = Floater.extend({
	computeTop: function(){
		return window.getScrollTop();
	}
});

Floater.KeepBottom = Floater.extend({
	computeTop: function(){
		return window.getSize().y+window.getScrollTop()-50;
	}
});