var slideshow = new Class({
    
    initialize: function(container){

        this.Identifier = $(container);
		if (this.Identifier != undefined)
		{
			this.BtnLeft = this.Identifier.getChildren('a.slider_button_left')[0];
			this.BtnRight = this.Identifier.getChildren('a.slider_button_right')[0];		
			this.Wrapper = this.Identifier.getChildren('div.elements')[0];
			this.Container = this.Wrapper.getChildren('ul')[0]; 
			this.Items = this.Container.getChildren('li'); 
			this.WrapperWidth = parseInt(this.Wrapper.getStyle("width")); 
			this.ContainerWidth = this.WrapperWidth*this.Items.length;
			if (this.ContainerWidth != undefined)
			{
				this.Container.setStyle("width",this.ContainerWidth);               
			}
			this.Delay = 6000; 
			this.Duration = parseInt(this.Delay/5);
			
			this.Images = this.Container.getElements("img.identifier");
				
			
			slideshow = this; 
			
			this.initThumbButtons(); 
			this.initButtons();        

			this.timer = this.run.periodical(this.Delay,this);  
		}
	},  
    
    run: function()
    {   
        
        this.CurrentPos = (parseInt(this.Container.left)) ? parseInt(this.Container.left) : 0; 

      /* 
	  erstes element nach hinten stellen
	  if (this.CurrentPos < 0) 
        {
            this.CurrentPos = 0; 
            this.tmp = this.Container.getChildren('li')[0];
            this.Container.getChildren('li')[0].destroy();
            this.Container.setStyle("left",0);
            this.tmp.inject(this.Container);
        }*/ 
        
		this.NewPos = this.CurrentPos+(this.WrapperWidth*-1); 
	//	console.log(this.NewPos); 
        this.NewPos = (this.NewPos < (this.ContainerWidth-this.WrapperWidth)*-1) ? 0 : this.NewPos; 
       // console.log(this.CurrentPos +" " +this.ContainerWidth +" " +this.WrapperWidth + " " + this.NewPos);
        slideshow.Container.morph({
            duration: this.Duration, 
            "left": this.NewPos
        });

    },
    
    scrollFwd: function()
    {
        clearInterval(this.timer);
		
		this.CurrentPos = parseInt(this.Container.getStyle("left")); 
		this.NewPos = this.CurrentPos+(this.WrapperWidth*-1); 
        this.NewPos = (this.NewPos < (this.ContainerWidth-this.WrapperWidth)*-1) ? 0 : this.NewPos; 
		

		
        slideshow.Container.morph({
            duration: this.Duration, 
            "left": this.NewPos
        });  
        this.timer = this.run.periodical(this.Delay,this);   
    },
	scrollBwd: function()
    {
        clearInterval(this.timer);
		
		this.CurrentPos = parseInt(this.Container.getStyle("left")); 
		this.NewPos = this.CurrentPos+(this.WrapperWidth); 
        this.NewPos = (this.NewPos > 0) ? 0 : this.NewPos; 
		
        
		
        this.Container.morph({
            duration: this.Duration, 
            "left": this.NewPos
        });  
        this.timer = this.run.periodical(this.Delay,this);   
    },    
	
	initThumbButtons: function()
	{
		this.Thumbs = new Array(); 
		
		for(var i = 0; i < this.Images.length; i++)
		{
			thumb = this.Images[i].clone();
			thumb.setAttribute("rel",i); 
			thumb.addClass("thumbs" + ((i == 0)? ' first' : ''));

			thumb.addEvent('click', function()
			{
				slideshow.scrollTo(parseInt(this.getAttribute("rel")));
			});
			thumb.inject(this.Identifier); 
		}
	},

    scrollTo: function(pos)
    {
        clearInterval(this.timer);
		
		this.NewPos = this.WrapperWidth*pos*-1; 
        //this.NewPos = (this.NewPos < (this.ContainerWidth-this.WrapperWidth)*-1) ? 0 : this.NewPos; 
		

		
        slideshow.Container.morph({
            duration: this.Duration, 
            "left": this.NewPos
        });  
        this.timer = this.run.periodical(this.Delay,this);   
    },
	
	initButtons: function()
    {
		this.BtnLeft.addEvent('click', function() {
					
			slideshow.scrollBwd();
		});
		this.BtnRight.addEvent('click', function() {
					
			slideshow.scrollFwd();
		});		
		
    }       
}); 

window.addEvent("load", function(){
	new slideshow("slider");
}); 

