var Slideshow = Class.create();
Slideshow.prototype = {
	
	Version: "1.0",
	Id: null,
	AmountOfPics: null,
	ActiveImage: null,
	Executer: null,

	/**
	 * initialize the slideshow
	 * 
	 * @param id
	 */
	initialize: function(id, options) {
		this.Id = id;
		this.AmountOfPics = $(id).childNodes.length;
		
		this.setOptions(options);
	},
	
	setOptions: function(options) {
	    this.options = {
	      speed: 	5,
	      random: 	false,
	      showEffect: 	'GROW',
	      hideEffect: 	'DROPOUT'
	    }
	    
	    Object.extend(this.options, options || {});
	  },
	  
	  
	/**
	 * start the slideshow
	 */
	start: function() {
		
		this.toggle();

		//start executer to toggle the pics
		this.initExecuter();
		
	},
	
	initExecuter: function(){	
		this.Executer = new PeriodicalExecuter(this.toggle.bind(this), this.options.speed);
	},
	
	
	/**
	 * stop the execution script
	 */	
	pause: function(){
		this.Executer.stop();
	},
	
	/**
	 * hide the old picture and show a new one
	 */
	toggle: function() {

		this.hide();
		this.show();	
	},
	
	prev: function ()	{
		//first stop the executer
		this.pause();
		
		//hide the current image
		this.hide();
		
		//get the new one
		var ID = this.ActiveImageID - 1;
		//get the last picture if the current one is image 1
		if(ID == 0){
			ID = this.AmountOfPics - 1;
		}
		this.show(ID);
		
		//and start the executer again
		this.initExecuter();
	},
	
	next: function ()	{
		//first stop the execute
		this.pause();
		
		//hide and show a new one
		this.toggle();
		
		//and start again
		this.initExecuter();
	},
	
	to: function (ID)	{

		//first stop the execute
		this.pause();
		
		//hide the current image
		this.hide();
		
		//hide and show a new one
		this.show(ID);
		
		//and start again
		this.initExecuter();

	},
	
	/**
	 * hide picture with a nice effect
	 */	
	hide: function() {
		if(this.ActiveImageID){
			$('navigation_to_'+this.ActiveImageID).removeClassName('active');
			
			var effectName = this.options.hideEffect;
			
			switch(effectName.toUpperCase())
			{
				case "FADE":
					new Effect.Fade('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "BLINDUP":
					new Effect.BlindUp('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "FOLD":
					new Effect.Fold('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "SLIDEDOWN":
					new Effect.SlideDown('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "SHRINK":
					new Effect.Shrink('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "SWITCHOFF":
					new Effect.SwitchOff('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "PUFF":
					new Effect.Puff('image_'+this.Id+'_'+this.ActiveImageID);
				break;
				case "DROPOUT":
					new Effect.DropOut('image_'+this.Id+'_'+this.ActiveImageID);
				break;
			}
		}
	},
	
	/**
	 * show picture with a nice effect
	 */
	show: function(ID) {
		if(ID){
			this.ActiveImageID = ID;
		}else if(this.options.random){
			this.ActiveImageID = this.getRandomID();
		}else{
			this.ActiveImageID = this.getID();
		}

		$('navigation_to_'+this.ActiveImageID).addClassName('active');
		
		var effectName = this.options.showEffect;
		
		switch(effectName.toUpperCase())
		{
			case "APPEAR":
				new Effect.Appear('image_'+this.Id+'_'+this.ActiveImageID);
			break;
			case "SLIDEUP":
				new Effect.SlideUp('image_'+this.Id+'_'+this.ActiveImageID);
			break;
			case "GROW":
				new Effect.Grow('image_'+this.Id+'_'+this.ActiveImageID, {direction: 'bottom-right'});
			break;
		}
		
	},
	

	/**
	 * return a random id of a picture to show
	 */
	getRandomID: function() {
		randomID = Math.round(Math.random()*this.AmountOfPics);

		//always a new unique image
		if($('image_'+this.Id+'_'+randomID) && (randomID != this.ActiveImageID)){
			return randomID;
		}else{
			return this.getRandomID();
		}
	},
	
	/**
	 * return a id of a picture to show
	 */
	getID: function() {
		if(this.ActiveImageID){

			var newID = this.ActiveImageID + 1;
			
			//if this.ActiveImageID is the last image, the newID is a non existing pic, so show the first one
			if(newID >= this.AmountOfPics){
				newID = 1;
			}
		}else{
			//begin with nr 1 if there is no active image ID
			var newID = 1;
		}
		
		return newID;
	}
};
