var ddImagesSlider = new Class({
	
	initialize: function(containerId, prevId, nextId) {

		this.container = $(containerId);
		this.containerId = containerId;
		
		this.prevLink = $(prevId);
		this.nextLink = $(nextId);

		this.loadImages();
	
		this.size = 3;
		this.index = 0;
		
		this.currentMarginLeft = 10;
		this.startMarginLeft = this.currentMarginLeft;
		this.width = 285;
		
		this.prevLink.addEvent('click', function(){
			this.prevLink.blur();
			this.prev();
		}.bind(this));
		
		this.nextLink.addEvent('click', function(){
			this.nextLink.blur();
			this.next();
		}.bind(this));
		
	},
	
	loadImages: function() {
		this.links = $$('#' + this.containerId + ' a.imgPop');
		this.images = $$('#' + this.containerId + ' img');		
	},
	
	prev: function(){
		
		if (this.index < this.size) {
			var links2move = this.size;
			for (var c = this.links.length-links2move; c < this.links.length; c++) {
				this.links[c].dispose().inject(this.links[0], 'before');
			}
			this.container.setStyle('margin-left',this.startMarginLeft-this.width);
			this.currentMarginLeft = this.startMarginLeft-this.width;
			this.loadImages();
			this.index = 0;
		}
		else {
			this.index -= this.size;
		}
			
		// do the slide
		var slideEffect = new Fx.Morph(this.container, {duration: 1000, transition: Fx.Transitions.Sine.easeInOut});
		 
		slideEffect.start({
		    'margin-left': [this.currentMarginLeft, this.currentMarginLeft+this.width]
		});	
		
		this.currentMarginLeft += this.width;			
	},
	
	next: function(){
		
		if (this.index+this.size >= this.links.length) {
			for (var c = this.size-1; c >= 0; c--) {
				this.links[c].dispose().inject(this.links[this.links.length-1], 'after');
			}
			this.container.setStyle('margin-left',this.startMarginLeft);
			this.currentMarginLeft = this.startMarginLeft;
			this.loadImages();
		}
		else {
			this.index += this.size;
		}
			
		// do the slide	
		
		var slideEffect = new Fx.Morph(this.container, {duration: 1000, transition: Fx.Transitions.Sine.easeInOut});
		 
		slideEffect.start({
		    'margin-left': [this.currentMarginLeft, this.currentMarginLeft-this.width]
		});	
		
		this.currentMarginLeft -= this.width;
				
			
	}	
	
});

window.addEvent('domready', initImagesSlider);
function initImagesSlider() {
	homeImagesSlider = new ddImagesSlider('homeImagesContainer', 'homeImagesPrev', 'homeImagesNext');
	
}

