var horizontal;

window.addEvent('domready',function(){
	
	var re = [];
	$$('.bodyGallery.').each(function(el, i) {

		el.getElement('.carousel').set('id', 'galleryID_'+i);

		horID = el.getElement('.carousel').get('id');
		
		var re = new Fx.Scroll.Carousel(horID,{
			mode: 'horizontal',
			onStart: function(){
				re.getCurrent().tween('color','#000');
			},
			onComplete: function(){
				re.getCurrent().tween('color','#fff');
			}
		});

		if ( re.elements.length > 1 ) {
			var pagination = el.getElement('.imgShift');
			pagination.empty();
			
			var paging = new Element("ul").injectInside(pagination).addClass("paging");
			for(i=0;i<re.elements.length;i++) {
				if ( ( i != 0) ) new Element("li", {'html' : ' l '}).injectInside(paging);
				var w = new Element("li").injectInside(paging);

				new Element("a", {
					'href' : 'javascript:void(0);',
					'html' : parseInt(i+1),
					'class' : (( i == 0) ? "onCurrent" : ""),
					'events' : {
						'click' : function() {
							
							this.index = parseInt(re.currentIndex+1);
							this.page = parseInt(this.get('html'));
							
							this.getParent().getParent().getElements('a').removeClass('onCurrent');
							this.addClass('onCurrent');

							if ( this.index < this.page ) {
									re.currentIndex = this.page - 1;
									re.toNext();
							} else if ( this.index > this.page ) {
									re.currentIndex = this.page - 1;
									re.toPrevious();
							}
														
							return false;
						}
					}
				}).injectInside(w);
			}
		} 
	});
});
