var adSlider = Class.create({
  initialize: function(maskID, divsClass, itemsClass, movBtnClass ) {
	try{
		this.startPoint = Position.cumulativeOffset($(maskID))[0];	
		this.bAutoSlide = false;
		this.movBtnClass= '.'+movBtnClass;
		this.itemsClass = itemsClass;
		this.divsClass  = divsClass;
		this.container	= $(divsClass);
		this.slides		= $$('.'+this.divsClass+' < div[class~='+this.itemsClass+']');
		this.timeout	= {};
		this.observeBtn();
		this.startAutoSlide();
	}
	catch(e){
		// console.log("adSlider started with error: "+e);	
		}
	},
	
	fx: function (o,iX)
	{
	   new Effect.Move(o, { 
			x: iX, 
			y: 0,
			duration:0.95,
			transition: Effect.Transitions.easeInBack
		});
	},

	moveTo: function(objNo)
	{
		diff_x =  this.startPoint - (Position.cumulativeOffset( this.slides[objNo] )[0]) ;
		this.fx(this.container, diff_x );
		this.fxBtn(objNo);
	},

	fxBtn: function (objNo)
	{
		el = $$(this.movBtnClass)[objNo];
		if(el.hasClassName('slideBtn_selected') )return false;
		try{$$('.slideBtn_selected')[0].removeClassName('slideBtn_selected')}catch(e){}
		el.addClassName('slideBtn_selected');	
	},
	
	autoSlide: function(startID)
	{
		o = this;
		if(! this.bAutoSlide ) return;
	
		if (isNaN(startID) ) startID = 0;
		nextID = (startID+1) % this.slides.length;		
		
		this.moveTo(startID);
		o.timeout	=	setTimeout( "o.autoSlide("+nextID	+")", 3500);
	},
	
	startAutoSlide: function ()
	{
		this.bAutoSlide = (this.bAutoSlide) ? false: true;
		this.autoSlide();
	},

	observeBtn : function ()
	{
		o = this;
		$$(this.movBtnClass).each( function(el){
			Event.observe(el, 'click', function(){
				clearTimeout(o.timeout);
				//o.fxBtn(el.id);
				o.moveTo(el.id);		
				o.timeout	=	setTimeout( "o.autoSlide("+nextID	+")", 7000);
			})
		});
	}
});	
Effect.Transitions.easeInBack = function(pos)
{
	var s = 1.70158;	
	return (pos)*pos*((s+1)*pos - s);
}
//document.observe("dom:loaded", function(){o=new adSlider();});
Event.observe(window, 'load', function(){
	o=new adSlider('mask', 'allNews','item', 'movBtn');
	}
);
