Slidr = new Class({
	
	moveSize : -126,
	size : 0,
	moving : 0, 
	animationDuration : 500,

	initialize : function(container,left,right,first,last,images,captions,links,position){
			this.container = $(container);
			this.images = images;
			this.lengths = lengths;
			this.captions = captions;
			this.links = links;
			
			this.first = '<table align="left" class="tampon first" width="100%" height="100%" cellpadding="0" cellmargin="0"><tr><td align="center">'+first+'</td></tr></table>';
			this.last = '<table class="tampon last" width="100%" height="100%" cellpadding="0" cellmargin="0"><tr><td align="center">'+last+'</td></tr></table>';
		
			this.show = position;
			
			this.position = (position-1);
			this.left = $(left);
			this.right = $(right);
			this.left.addEvent('click', this.move.pass('left',this));
			this.right.addEvent('click', this.move.pass('right',this));
			
			this.overflow = new Element('div').setStyle('margin-left', (this.position*this.moveSize)+'px')
				.setProperty('class','overflow')
				.setProperty('id','overflowdiv').injectInside(this.container);
				
			this.addImages(true, 5);
			//this.injectInside($E('body'));
	},
	injectInside : function(elm){
		this.left.setStyle('visibility',(this.position >= 1 && links.length > 1)?'visible':'hidden');
		this.right.setStyle('visibility',((this.position) < this.images.length && links.length > 1)?'visible':'hidden');
	},
	addImages : function(limit, numlimit, newsrc){
				if(limit){
					//new Element('a').injectInside(this.overflow).set('html',this.first);	
					for(i=0; i < (numlimit+this.position+1);i++){
							if(i >= (this.position-2) && i < this.links.length){							
								new Element('a').setStyle('text-decoration','none').setProperty('href','#').injectInside(this.overflow).set('html','<div style="cursor:pointer;float:left;position:relative;" onclick="document.location.href=\''+this.links[i]+'\';return false;"><img src="'+this.images[i]+'" /><div style="position:absolute;right:3px;top:3px;background:#000000;color:#ffffff;">'+this.lengths[i]+'</div></div>');
								//if(i >= (this.links.length-1)) new Element('a').injectInside(this.overflow).set('html',this.last);
							} else
								if(i < this.links.length){
									new Element('a').setStyle('text-decoration','none').setProperty('href','#').injectInside(this.overflow).set('html','<img src="" />');
								}
						this.size += this.moveSize;
					}
					//if((this.links.length)==0) new Element('a').injectInside(this.overflow).set('html',this.last);				
				} else {
					//console.log(newsrc + ' - ' + this.links.length);
					if(newsrc < (this.links.length))
					{new Element('a').setStyle('text-decoration','none').setProperty('href',this.links[newsrc]).injectInside(this.overflow).set('html','<div onclick="document.location.href=\''+this.links[i]+'\';return false;" style="cursor:pointer;float:left;position:relative;"><img src="'+this.images[newsrc]+'" /><div style="position:absolute;right:3px;top:3px;background:#000000;color:#ffffff;">'+this.lengths[newsrc]+'</div></div>');}
					else{
					//console.log(newsrc-(this.links.length))
					//if(newsrc-(this.links.length)<1)new Element('a').injectInside(this.overflow).set('html',this.last);
					this.right.setStyle('visibility',((newsrc-(this.links.length))==0)?'hidden':'visible');
					this.finish == 1;
					}
					this.size += this.moveSize;
				}
			this.overflow.setStyle('width',Math.abs(this.size)+'px');			
	},
	move : function(direction){
			if(this.moving) return;
			var start = this.moveSize * this.position;
			var finish = this.moveSize*((direction == 'right')?(++this.position):(--this.position))
			new Fx.Tween(this.overflow, {
				duration: this.animationDuration, 
				onStart: function(){this.moving = true;}.bind(this),
				onComplete: function(){
					this.moving = false;
					if(direction == "right"){
						if(this.images[this.position-1] != null){
							if(this.finish != 1)this.addImages(false, 1, this.position+5);
						}
					} else {
						if(this.images[this.position+6] != null)
						$('overflowdiv').getElements('img')[this.position+6].getParent().dispose();
						else $$('.last').each(function(ID){$(ID).dispose();});
											
						
						if(this.images[this.position-5] != null){
							$('overflowdiv').getElements('img')[this.position-5].src = this.images[this.position-5];
						}
					}
				}.bind(this)
				}).start('margin-left',start,finish);
			this.left.setStyle('visibility',(this.position <= 0)?'hidden':'visible');
			this.right.setStyle('visibility',(this.position >= (this.images.length))?'hidden':'visible');
		}
	
});
