// @date 21.07.2008
// @version 0.2.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',
			effectOptions:	{
				duration: 0.8, 
				queue: {scope: 'slider', limit:1}, 
				afterFinish: function() { this.sliding = false; }.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';
		else if (-this.options.scrollBy + pos + this.container[this.scroll[1]] < 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) 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 < 10))
			return;
		
		this.prevBtn.style.visibility = !(property+moveBy < 0) ? 				'hidden' : 'visible';
		this.nextBtn.style.visibility = !(offset + moveBy*2 + property > 1) ?	'hidden' : 'visible';
		this.sliding = true;
		
		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);
	}
});

Event.observe(window, 'load', function(){
	var slide = $('slided_menu');
	var width = $$('#slided_menu li').length * 197;
	
	if (width <= 986)
		return $('menu_right_btn').style.visibility = 'hidden';
	
	
	slide.style.width = width + 'px';
	new CD3.Slider(slide, 'menu_left_btn', 'menu_right_btn', { 
		scrollBy:	986,
		event:		'mouseover'
	});
});




