/*
Title 	: Simple Carousel with Paging Using Mootools
Author 	: Nikhil Kunder (nik1409@gmail.com)
Date 	: 2008/09/12
Version : 1.0
    moocarousel_v1.0.js  is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 	Lesser General Public License for more details.
*/
var MooCarousel = new Class({
	wrapper:'',
	items:'',
	moveleft:'',
	moveright:'',
	slides:2,
	offset:350,
	currentslide:1,
	pos:0,
	ispaged:false,
	initialize: function(wrapper,items,moveleft,moveright, ns,sss,ispaged){
		this.wrapper = $(wrapper);
		this.items = $(items);
		this.moveleft = $(moveleft);
		this.moveright = $(moveright);
		this.slides = ns;
		this.offset = sss;
		this.ispaged = ispaged;
		this.parent = this.wrapper.getParent();
		this.scroll = new Fx.Scroll(this.wrapper, {offset:{'x':0, 'y':0} });
		this.dir = "right";
		var that = this;
		if(this.ispaged){
			this.carousel_paging = new Element('div').addClass('carousel_paging');
			this.carousel_paging.id= this.wrapper.id + "_p";
			for (  i = 1;  i <= parseInt(this.slides) ; i++){
				var aa = new Element('a').addClass('page');
				if(i==1) aa.className= "current";
				aa.href="javascript:void(0);";
				aa.addEvent('click', this.page.bind(this, [i, aa, this.carousel_paging]));
				aa.innerHTML = i;
				aa.injectInside(this.carousel_paging);
			}
			this.carousel_paging.injectAfter(this.parent);
			var carousel_fix = new Element('div').addClass('clearfix').injectBefore(this.carousel_paging);
			var carousel_fix = new Element('div').addClass('clearfix').injectInside(this.carousel_paging);
		}
		this.moveleft.addEvent('click', this.camoveleft.bind(this));
		this.moveright.addEvent('click', this.camoveright.bind(this));
	},
	setpos: function(page){
		this.currentslide = page + 1;
		this.pos = this.offset * page;
		this.scroll.start(this.pos);this.scroll.toLeft();
	},
	camoveleft: function(event){
		event = new Event(event).stop();
		if(this.currentslide == 1) return;
		this.currentslide--;
		if(this.ispaged){
			this.setcss('left');
			this.dir = 'left';
		}
		this.pos += -(this.offset);
		if(this.pos < 0)
		{
			this.pos = 0;
		}
		this.scroll.start(this.pos);this.scroll.toLeft();
	},
	camoveright: function(event){
		event = new Event(event).stop();
		if(this.currentslide >= this.slides) return;
		this.currentslide++;
		if(this.ispaged){
			this.setcss('right');
			this.dir = 'right';
		}
		this.pos += this.offset;
		this.scroll.start(this.pos);this.scroll.toLeft();
	},
	page: function(pagenum,o, p){
		var sss = ((pagenum-1)*this.offset) ;
		if(pagenum > this.slides) return;
		if(pagenum == 1) sss = 0;
		this.currentslide = pagenum
		this.pos = sss
		this.scroll.start(this.pos);this.scroll.toLeft();
		this.resetcss(o, p);
	},
	setcss:function(dir){
		var x = parseInt(this.currentslide)-1;
		if(x < 0 ) x = 0; if( x >9) x =9;
		var o = this.carousel_paging.getElements('a')[x];
		this.resetcss(o,this.carousel_paging);
	},
	resetcss: function(o,p){
		var cpa = p.getElements('a');
		cpa.each(function(el,i){
			el.className="page";
		});
		o.className="current";
	}
});
MooCarousel.implement(new Events);
MooCarousel.implement(new Options);
