/* <![CDATA[ */

ownPreload=new Class({
	Extends: Fx,
	options: {
		itemsClass:'',
		onReadyLoad:$empty
     },
     initialize: function(element, options){
    	 this.parent(options);
    	 this.elementClass=$$(this.options.itemsClass);
    	 this.ImagePreloadArray=new Array();
    	 this.ImagePreloadTitleArray=new Array();
    	 this.elementFromClass={};
    	 this.elementClass.each(function(elem,i){
    		 this.elementFromClass[i]=$(elem);
    		 this.ImagePreloadArray.push(elem.get('href'));
    		 this.ImagePreloadTitleArray.push(elem.get('title'));
 			
 		}.bind(this));
    	 this.preloadPics();
     },
     preloadPics:function(){

 		var images=this.ImagePreloadArray;
 		
 		var loader = new Asset.images(images, {
 			onProgress: function(counter,index) {
 			 
 			}.bind(this),
 			onComplete: function() {
 				images.each(function(elem,i){
 					this.elementFromClass[i].set('html','');
 					loadDescr=new Element('span',{'html':this.ImagePreloadTitleArray[i]}).inject(this.elementFromClass[i]);
 					loadImg=new Element('img', { 'src': elem ,'class':'thumb'}).inject(this.elementFromClass[i]);
 					
 				}.bind(this));
 				
 				this.onReadyLoad=this.fireEvent('onReadyLoad',this.options.onReadyLoad,1000);
 				
 			}.bind(this)
 		});
 		
 	}
});


ownSlide = new Class({

	Extends: Fx,

	options: {
		SlideMode:'galerie',
		loop:false,
		processing:false,
		offset: {'x': 0, 'y': 0},
		wheelStops: true,
		wheelScroll: true,
		scrollDir:'horizontal',
		elementWidth:100,
		anzeigeBreite:300,
		itemsClass:'',
		itemsAnimationClass:'',
		itemsOutputClass:'',
		showDescription:false,
		addButtons:{'previousLink':null,'nextLink':null,'stopLink':null,'playLink':null,'loopLink':null},
		
		SlideAnimation:{},
	    SlideAnimationOut:{},
		animation:{},
	    animationOut:{},
	    SlideIn: 700,
	    SlideOut:1100,
		StayAlive: 3000,
		fxTransition:Fx.Transitions.Sine.easeOut
		
	},

	initialize: function(element,slideOutput, options){
		//ScrollElement
		this.element = this.subject = $(element);
		this.slideOutput=null;
		if(slideOutput!=''){
			this.slideOutput=$(slideOutput);
		}
		this.parent(options);
		this.elementClass=$$(this.options.itemsClass);
		this.elementFromClass={};
		this.itemsOutputClass=null;
		this.itemsFromOutputClass=null;
		
		
		this.elementFxFromClass={};
		this.ImagePreloadArray=new Array();
		this.processing=this.options.processing;
		this.elementWidth=this.options.elementWidth;
		this.iAnzahl=this.elementClass.length;
		this.anzeigeBreite=this.options.anzeigeBreite;
		this.buttonsLoad=this.options.addButtons;
		this.counter=0;
		this.currentPage=0;
		this.currentslide = 1;
		this.NowPos=0;
		this.oldPos=0;
		
		this.SlideIn=this.options.SlideIn;
		this.SlideOut=this.options.SlideOut;
		this.StayAlive=this.options.StayAlive;
		this.fxTransition=this.options.fxTransition;
		this.loop=this.options.loop;
		this.animation=this.options.animation;
		this.animationOut=this.options.animationOut;
		this.SlideAnimation=this.options.SlideAnimation;
		this.SlideAnimationOut=this.options.SlideAnimationOut;
		
		if(this.options.itemsOutputClass!=''){
			this.itemsOutputClass=$$(this.options.itemsOutputClass);
			this.itemsFromOutputClass={};
			this.elementOutputFxInFromClass={};
			this.elementOutputFxOutFromClass={};
			this.itemsOutputClass.each(function(elem,i){
				
				this.itemsFromOutputClass[i]=$(elem);
				this.elementOutputFxInFromClass[i] = new Fx.Morph(elem,{duration: this.SlideIn, transition: this.fxTransition});
				this.elementOutputFxOutFromClass[i] = new Fx.Morph(elem,{duration: this.SlideOut, transition: this.fxTransition});

			}.bind(this));
			
		}
		
		this.maxPages=Math.round((this.iAnzahl*this.elementWidth)/this.anzeigeBreite)+1;
		if(this.slideOutput!=null){
			this.fxIn = new Fx.Morph(this.slideOutput,{duration: this.SlideIn, transition: this.fxTransition});
			this.fxOut = new Fx.Morph(this.slideOutput,{duration: this.SlideOut, transition: this.fxTransition});
		}
		//this.ControlFx=new Fx.Morph('controlMenu',{duration: '1000', transition: this.fxTransition});


		var cancel = this.cancel.bind(this, false);

		if ($type(this.element) != 'element') this.element = $(this.element.getDocument().body);
        
		
		
		this.elementClass.each(function(elem,i){
			
			this.elementFromClass[i]=$(elem);
			
			if(this.options.itemsAnimationClass==''){
				
				this.elementFxFromClass[i]=new Fx.Morph(elem, {duration:this.SlideIn,transition: this.fxTransition, wait: false});
			}
		  
		}.bind(this));
		
		if(this.options.itemsAnimationClass!=''){
			this.elementAnimationClass=$$(this.options.itemsAnimationClass);
			
			this.elementAnimationClass.each(function(elem,i){
				this.elementFxFromClass[i]=new Fx.Morph(elem, {duration:this.SlideIn,transition: this.fxTransition, wait: false});
			}.bind(this));
		}
		
		
		//this.interActiveMenu();
		this.caputereItemEvents();
		
		if(this.buttonsLoad['previousLink']!=null && this.buttonsLoad['nextLink']!=null){
			this.LoadPrevNextLinkEvent();
		}
		if(this.buttonsLoad['startLink']!=null && this.buttonsLoad['stopLink']!=null){
			this.LoadStartStopLinkEvent();
		}
		
		if (this.options.wheelScroll){
			 this.LoadMouseWheelActions();
		 }
        
		

		  
	},
	
	interActiveMenu:function(){
		
		$('showControl').addEvent('click',function(e){
			e = new Event(e).stop();
			
			this.ControlFx.start({
				'display':'block',
				'height':[0,40]				
					}).wait(3000).chain(function(){
						this.ControlFx.start({'display':'none'});
			}.bind(this));
		}.bind(this));
		
	},
	caputereItemEvents:function(){
	
      
		this.elementClass.each(function(el,i){
			
			el.addEvent('click', function(e) { 
				e = new Event(e).stop();
				this.processing=false;
				
				this.elementFxFromClass[this.oldPos].start(this.animationOut);
				
				if(this.slideOutput==null){
                    this.elementOutputFxInFromClass[this.oldPos].start(this.SlideAnimationOut);
				}
				
				this.oldPos=i;
				this.elementFxFromClass[i].start(this.animation);
				
				if(this.slideOutput!=null){
						
						this.fxOut.start(this.SlideAnimationOut).chain(function(){
							this.actionSlideOutput(i);
							this.fxOut.start(this.SlideAnimation);	
						}.bind(this));
						
					
				}
				
				if(this.slideOutput==null){
                   	
						this.elementOutputFxOutFromClass[i].start(this.SlideAnimation).chain(function(){
							this.actionSlideOutput(i);
							//this.elementOutputFxOutFromClass[i].start(this.SlideAnimation);	
						}.bind(this));
						
					
				}
			}.bind(this));
		
		}.bind(this));
		
		
	},
	
	showSlideOutput:function(IndexIt,MODE){
		
		this.oldPos=IndexIt;
		if(this.slideOutput!=null){
			if(MODE=='in'){
				this.fxOut.start(this.SlideAnimation);
				this.actionSlideOutput(IndexIt);
			}
			if(MODE=='out'){
				this.fxIn.start(this.SlideAnimationOut);
			}
			if(MODE=='start'){
				//this.fxIn.start(this.SlideAnimation);
				this.actionSlideOutput(IndexIt);
			}
		}
		if(this.slideOutput==null){
			if(MODE=='in'){  
				this.elementOutputFxInFromClass[IndexIt].start(this.SlideAnimation);
				this.actionSlideOutput(IndexIt);
			}
			if(MODE=='out'){
				this.elementOutputFxOutFromClass[IndexIt].start(this.SlideAnimationOut);
			}
		}
	},
	actionSlideOutput:function(IndexIt){
		
		if(this.options.SlideMode=='galerie' && this.slideOutput!=null){
			this.slideOutput.empty();	
			if(this.options.showDescription){
				var description =this.elementFromClass[IndexIt].getElement('span');
				if(description)	{				   
					
					var des = new Element('p').set('text', description.get('text')).inject(this.slideOutput);
				}
			}
			var largeImage = new Element('img', { 'src': this.elementFromClass[IndexIt].href }); // create large image
					largeImage.inject(this.slideOutput);
		}
		if(this.options.SlideMode=='elements'){
			
			
			this.itemsFromOutputClass[IndexIt].setStyles({'z-index':(IndexIt+1)});
			
		}
		
	},
	startAnimManuellFunction:function(IndexIt){
       
        
		this.showSlideOutput(IndexIt,'start');
		
			 
		this.elementFxFromClass[IndexIt].start(this.animation).wait(this.StayAlive).chain(function(){
			  
			  if(this.processing){
				  this.showSlideOutput(IndexIt,'out');
				  this.getNextAnim((IndexIt+1));
  		   }

		}.bind(this));
    
	},
	startAnim:function(IndexIt){
       
        
		this.showSlideOutput(IndexIt,'in');
		
			 
		this.elementFxFromClass[IndexIt].start(this.animation).wait(this.StayAlive).chain(function(){
			  
			  if(this.processing){
				  this.showSlideOutput(IndexIt,'out');
				  this.getNextAnim((IndexIt+1));
  		   }

		}.bind(this));
    
	},
	getNextAnim:function(IndexIt){
	
		
		if(IndexIt==this.iAnzahl){
 			
 			if(this.loop){
  			  this.elementFxFromClass[(IndexIt-1)].start(this.animationOut).chain(function(){
  				  this.ResetStartPage();
  				  this.startAnim(0);
					
				}.bind(this));
			}else{
				
				this.processing=false;
			}
 			
		}else{
            if(this.processing){
            	//Page Calculating
            	this.setCurrentPage(IndexIt);
            	
            	
            	this.elementFxFromClass[(IndexIt-1)].start(this.animationOut).chain(function(){
            		
            		this.showSlideOutput(IndexIt,'in');
            		
					this.elementFxFromClass[IndexIt].start(this.animation).wait(this.StayAlive).chain(function(){
						
						if(!this.loop && this.iAnzahl<IndexIt){
							this.showSlideOutput(IndexIt,'out');
						}
						if(this.loop){
							this.showSlideOutput(IndexIt,'out');
						}
						
						this.getNextAnim1((IndexIt+1));
						
					}.bind(this));

				}.bind(this));
		  }
		}

	},
	getNextAnim1:function(IndexIt){
		
		if(IndexIt==this.iAnzahl){
			
			if(this.loop){
				
				this.elementFxFromClass[(IndexIt-1)].start(this.animationOut).chain(function(){
					this.ResetStartPage();
					this.startAnim(0);
				}.bind(this));
		}else{
			
			this.processing=false;
			
		}
			
			
		}else{
			if(this.processing){
				this.setCurrentPage(IndexIt);
				this.elementFxFromClass[(IndexIt-1)].start(this.animationOut).chain(function(){
					this.showSlideOutput(IndexIt,'in');
					this.elementFxFromClass[IndexIt].start(this.animation).wait(this.StayAlive).chain(function(){
						
						if(!this.loop && this.iAnzahl<IndexIt){
							this.showSlideOutput(IndexIt,'out');
						}
						if(this.loop){
							this.showSlideOutput(IndexIt,'out');
						}
						
						this.getNextAnim((IndexIt+1));
						
					}.bind(this));

				}.bind(this));
		    }
		}

	},
	ResetStartPage:function(){
		this.currentslide=1;
		this.currentPage= 0;	
		this.start(this.currentPage);
	},
	setCurrentPage:function(INDEXIT){
		var calc=((INDEXIT+1)*this.elementWidth);
		
		if(calc>(this.anzeigeBreite*this.currentslide)){
    		var TempCal=Math.round(calc/this.anzeigeBreite)+1;
    		if(this.currentslide!=TempCal){
    			this.currentslide=TempCal;
    		
    		this.currentPage += this.anzeigeBreite;	
    		this.start(this.currentPage);
    		}
    	}
	},
	startAnimManuell:function(NOWPOS){
		
		this.startAnimManuellFunction(NOWPOS);
		
		
	},
	LoadStartStopLinkEvent:function(){
		//init
		if(this.loop){
      		$(this.buttonsLoad['loopLink']).set('html','noloop');
      	}else{
      		$(this.buttonsLoad['loopLink']).set('html','Loop');
      	}
		
		$(this.buttonsLoad['loopLink']).addEvent('click',function(event){
	      	  event = new Event(event).stop();
	      	if(this.loop){
	      		this.loop=false;
	      		$(this.buttonsLoad['loopLink']).set('html','noLoop');
	      	}else{
	      		this.loop=true;
	      		$(this.buttonsLoad['loopLink']).set('html','Loop');
	      	}
	      	
	      	
	      	
		}.bind(this));
		
		$(this.buttonsLoad['startLink']).addEvent('click',function(event){
	      	  event = new Event(event).stop();
	      	this.processing=true;
	      	
	      	this.startAnimManuell(this.oldPos);
	      	
		}.bind(this));
		
		$(this.buttonsLoad['stopLink']).addEvent('click',function(event){
	      	  event = new Event(event).stop();
	      	this.processing=false;
	      	
		}.bind(this));
	},
	LoadPrevNextLinkEvent:function(){
		
		$(this.buttonsLoad['previousLink']).addEvent('click',function(event){
      	  event = new Event(event).stop();
      	 
      	  this.processing=false;
      	  
    		 if(this.currentslide == 1) return;
    		 this.currentslide--;				
    		 this.currentPage += -(this.anzeigeBreite);				
    		 this.start(this.currentPage);
		      

        }.bind(this));
		
		$(this.buttonsLoad['nextLink']).addEvent('click',function(event){
      	  event = new Event(event).stop();
      	  this.processing=false;
      	  
      	  if(this.currentslide >= this.maxPages) return;
    		this.currentslide++;
    		this.currentPage += this.anzeigeBreite;	
    		this.start(this.currentPage);
    		

        }.bind(this));
	},
	LoadMouseWheelActions:function(){
		
		this.element.addEvent('mousewheel', function(event){
			 
			event = new Event(event);
			this.processing=false;
           
			if (event.wheel > 0) {
				
				 if(this.options.scrollDir=='vertical'){
					
					 if(this.counter>0){
						 this.set(false,this.elementWidth*(this.counter-1));
                         this.counter--;
						
					 }
					 
			        }else{
			        	if(this.counter<this.iAnzahl){
			        		this.set(this.elementWidth*this.counter, false);
			        		this.counter++;
			        	
			        		 
			        	}
			        }


			}
			else if (event.wheel < 0) {

				 if(this.options.scrollDir=='vertical'){
						
							this.counter++;
							this.set(false,this.elementWidth*this.counter);
						
			    	
			        }else{
			        	if(this.counter>0){
			        		this.counter--;
			        		this.set(this.elementWidth*(this.counter-1), false);
			        	}
			        }
				 
			}

		  }.bind(this));
		
		
	},
	
	set: function(){
		var now = Array.flatten(arguments);
		this.element.scrollTo(now[0], now[1]);
	},

	compute: function(from, to, delta){
		var now = [];
		var x = 2;
		x.times(function(i){
			now.push(Fx.compute(from[i], to[i], delta));
		});
		return now;
	},

	start: function(x, y){
		if (!this.check(arguments.callee, x, y)) return this;
		var offsetSize = this.element.getSize(), scrollSize = this.element.getScrollSize();
		var scroll = this.element.getScroll(), values = {x: x, y: y};
		for (var z in values){
			var max = scrollSize[z] - offsetSize[z];
			if ($chk(values[z])) values[z] = ($type(values[z]) == 'number') ? values[z].limit(0, max) : max;
			else values[z] = scroll[z];
			values[z] += this.options.offset[z];
		}
		return this.parent([scroll.x, scroll.y], [values.x, values.y]);
	},


	toTop: function(){
		return this.start(false, 0);
	},

	toLeft: function(){
		return this.start(0, false);
	},

	toRight: function(){
		return this.start('right', false);
	},

	toBottom: function(){
		return this.start(false, 'bottom');
	},
  
	toElement: function(el){
		var position = $(el).getPosition(this.element);
		return this.start(position.x, position.y);
	}


});



/* ]]> */
