// @date 18.08.2008
// @version 0.4.0

if (!CD3) var CD3 = {};
CD3.Slider = Class.create({
	initialize: function(container, prev, next, options){
		this.options = Object.extend({
			scrollBy:		0,
			scrollType:		'horizontal',
			event:			'click',
//			beforeSlide:	Prototype.emptyFunction,
//			afterSlide:		Prototype.emptyFunction,
			effectOptions:	{
				duration: 0.8, 
				queue: {scope: 'slider', limit:1},
				beforeStart: this.beforeSlide.bind(this),
				afterFinish: this.afterSlide.bind(this)
			}
		}, options || {});
		
		this.container	= $(container);
		this.scroll		= this.options.scrollType == 'vertical' ? ['top', 'offsetHeight'] : ['left', 'offsetWidth'];
		this.prevBtn	= this.bindEvent(prev, 1);
		this.nextBtn	= this.bindEvent(next, -1);
		this.sliding	= false;
		
		var pos = parseInt(this.container.style[this.scroll[0]]) || 0;
		
		if (pos == 0)
			this.prevBtn.style.visibility = 'hidden';
			
		if (this.container[this.scroll[1]] - (this.options.scrollBy - pos) < 1)
			this.nextBtn.style.visibility = 'hidden';
	},
	bindEvent: function(a, moveBy){
		return $(a).observe(this.options.event, this.slide.bind(this, moveBy));
	},
	slide: function(moveBy){
		if (this.sliding !== false) return;
		
		var side 		= this.container,
			property	= parseInt(side.style[this.scroll[0]]) || 0,
			offset		= side[this.scroll[1]];
		
		moveBy *= this.options.scrollBy;
	
		if ((moveBy > 0 && (property > -1 || !property)) || (moveBy < 0 && offset + moveBy + property < 0))
			return;
		
		this.prevBtn.style.visibility = !(property+moveBy < 0) ? 				'hidden' : 'visible';
		this.nextBtn.style.visibility = !(offset + moveBy*2 + property > 1) ?	'hidden' : 'visible';
		this.sliding = moveBy = moveBy < 0 ? Math.max(moveBy, - (offset + property + moveBy)) : Math.min(moveBy, -property)
				
		if (this.options.scrollType == 'vertical')
			Effect.MoveBy(side, moveBy, 0, this.options.effectOptions);
		else
			Effect.MoveBy(side, 0, moveBy, this.options.effectOptions);
	},
	beforeSlide: function (){
		if (this.options.beforeSlide)
			this.options.beforeSlide.call(this);
	},
	afterSlide: function(){
		this.sliding = false;
		if (this.options.afterSlide)
			this.options.afterSlide.call(this);
	},
	position: function(){
		return Math.round(parseInt(this.container.style.left) / -this.options.scrollBy);
	}
});