var SlideGallery = Class.create({

	id : null,
	imageLinks : null,
	imgCounter : null,
	prevLink : null,
	nextLink : null,
	slideElements : null,
	imgContainer : null,
	galleryIndex : null,
	gallerySize : null,
	
	// init
	initialize : function(id) {	 	
		this.id = id;
		this.gallerySize = Math.floor(id.up('.tabCont').getWidth()/122);
		this.imgContainer = id.select('.slidingCont')[0];
		if (this.imgContainer) {
			this.imageLinks = new Array();
			this.imageLinks = this.imgContainer.select('.slideEl');
			this.imgCounter = this.imageLinks.length;
			this.galleryIndex = 0;
			
			tmpLinks = this.id.select('a.slidingNav');

			this.prevLink = tmpLinks[0];
			this.nextLink = tmpLinks[tmpLinks.length-1];
	    
			if (this.imgCounter > this.gallerySize) {
				for (var i = 0; i < this.imgCounter; i++) {
					this.imageLinks[i].style.display = 'none';
				}
				this.prevLink.onclick = this.previous.bind(this);
				this.nextLink.onclick = this.next.bind(this);
				this.slideElements = new Array();
				this.imageLinks.each(function(el, i) {
					if (i < this.gallerySize) {
						this.slideElements[i] = el.cloneNode(true);
						this.slideElements[i].style.display = 'inline';
						this.imgContainer.appendChild(this.slideElements[i]);
					}
				}.bind(this));
			} else {
				new NonFunctionalLink(this.prevLink);
				new NonFunctionalLink(this.nextLink);
			}
			
		}     
	},
	
	next : function() {	  
		if (this.galleryIndex + 1 < this.imgCounter) {
			this.galleryIndex++;
		} else {
			this.galleryIndex = 0;
		}
		this.refresh();
		return false;
	},
  
	previous : function() {
		if (this.galleryIndex == 0) {
			this.galleryIndex = this.imgCounter - 1;
		} else {
			this.galleryIndex--;
		}
		this.refresh();
		return false;
	},
  
	refresh : function() {
		this.slideElements.each(function(el, i){
			el.remove();
		}.bind(this));

		this.slideElements.each(function(el, i) {
			this.slideElements[i] = this.imageLinks[(this.galleryIndex + i) % this.imageLinks.length].cloneNode(true);
			this.slideElements[i].style.display = 'inline';
			this.imgContainer.appendChild(this.slideElements[i]);
		}.bind(this));   
	}	
});

function slideGalleryInit() {
	$$('.articleSlider').each(function(element) {
		new SlideGallery(element);
	});
}

if ('observe' in document) {
	document.observe('dom:loaded', function() {
		slideGalleryInit();
	});
}
