var clrzSlider = new Class({
	Implements: [Options,Events],
	options:{ 
		perpage:1,
		setpage:0,
		containerwidth:0,
		container:'slide_container',
		items:'#contentslider div.item',
		contentslider:'contentslider',
		btnnext:'div.carouselbar_content .next a',
		btnprev:'div.carouselbar_content .previous a',
		layout:'horizontal',
		windowscroll:1,
		autostart:false,
		duration:800,
		marge:0
		},
	
	
	initialize: function(options)
	{
		
		this.setOptions(options);
		this.wscroll = new Fx.Scroll(window);
		if(!$(this.options.container))
			return;
	
		if(this.options.layout=='horizontal')
		{
			this.prop = 'width';
			this.moove = 'margin-left';
		}
		else
		{
			this.prop = 'height';
			this.moove = 'margin-top';
		}
		
		
		this.ulwidth = 0;
		this.items = $$(this.options.items);
		this.container = $(this.options.container);
		this.contentslider = $(this.options.contentslider);
		
		
		this.is_under = 0;	
		this.ulid = [];	
		this.fxlist = new Fx.Morph(this.contentslider,{duration:this.options.duration,transition: Fx.Transitions.Quint.easeOut,wait:false});
		
		var maclass = this;	
		
		
		this.items.each(function(el,i)
		{
			if(maclass.options.layout=='horizontal')
				maclass.ulwidth = maclass.ulwidth+el.getWidth()+maclass.options.marge;
			else
				maclass.ulwidth = maclass.ulwidth+el.getHeight()+maclass.options.marge;
				
				
			maclass.ulid[i]=el.getProperty('id');
			
			
		
		});	
		
		this.itemwidth = (this.ulwidth/this.items.length);
		var containerW;
			if(this.options.containerwidth)
				containerW=this.options.containerwidth;
			else
				containerW=this.itemwidth*this.options.perpage;
				
		var obj	= {};
		obj[this.prop] = containerW;
		obj['overflow']='hidden';
		
		this.container.setStyles(obj);
		this.containerwidth = (this.itemwidth*this.options.perpage);
		this.contentslider.setStyle(this.prop,this.containerwidth);
		this.maxpage = Math.ceil(this.items.length/this.options.perpage)-1;
		
		
		
		/*console.log(this.maxpage);
		console.log(this.items.length);
		console.log(this.options.perpage);
		console.log('-----------');*/
		this.contentslider.setStyle(this.prop,(this.ulwidth));	
		this.initevents();
		
		
		this.fireEvent('initialize');


		this.goto(0);
		if(this.options.autostart)
		this.start();
		 	
		
	},
	
	
	initevents:function()
	{
		
		var maclass = this;
		this.container.addEvents({mouseenter:function(){maclass.is_under=1},mouseleave:function(){maclass.is_under=0;}});	
		
		$$(this.options.btnnext).addEvent('click',function(e){
		
		new Event(e).stop();
		
		
		maclass.scrollmenext();
		});
		
		$$(this.options.btnprev).addEvent('click',function(e){
		
		new Event(e).stop();
		
		
		maclass.scrollmeprev();
		});	
		
		
		
	},
	
	scrollmenext:function()
	{
		
		if(this.setpage>=this.maxpage)
		this.setpage=0;
		else		
		this.setpage=this.setpage+1;
		this.goto();
		
		if(this.windowscroll==1)
			this.wscroll.toElement(this.container);	
		
	},	
	
	scrollmeprev:function()
	{
		
		if(this.setpage<=0)
		this.setpage=this.maxpage;
		else
		this.setpage=this.setpage-1;
		this.goto();
		if(this.windowscroll==1)
			this.wscroll.toElement(this.container);	
		
	},
	
	goto:function(page)
	{
		
		var maclass = this;
		if($chk(page))
		this.setpage = page;
		var obj = {};				
		obj[this.moove] =-(this.setpage*this.containerwidth);
		this.fxlist.start(obj).chain(function(){ maclass.fireEvent('gotocomplete'); });
		
		this.fireEvent('goto');
	
	},
	
	
	
	
	loadactivepage:function(item)
	{
		$(item).addClass('current');
		 this.setpage =  Math.floor(ulid.indexOf(item)/perpage);
		
		this.goto();
	},

	
	
	
	autostart:function()
	{
		
		if(!this.is_under)
			this.scrollmenext();
		
	},
	
	start:function()
	{
		
		this.autostart.periodical(5000,this);	
		
		
	}
		
		
	
	
});
