var Etalage = Class.create({
	
	Version: "1.0",

	/**
	 * initialize the slideshow
	 * 
	 * @param id
	 */
	initialize: function(id, options) {
		this.Items = new Array();
		this.ActiveItemID = null;
		this.Executer = null;
	
		this.Id = id;
		this.setOptions(options);
		
		Event.observe(window, 'load', this.start.bind(this));
	},
	
	setOptions: function(options) {
	    this.options = {
	      speed: 	5,
	      duration: 1,
	      random: 	false,
	      runOnce: 	false,
	      showEffect: 	'APPEAR',
	      hideEffect: 	'FADE'
	    }
	    
	    Object.extend(this.options, options || {});
	  },
	  
	addItem: function(sId){
		this.Items.push(sId);
		this.AmountOfItems = this.Items.length;
	},
	
	/**
	 * 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() {
		
		if( this.options.runOnce && (this.ActiveItemID === 0 || this.ActiveItemID) )
		{
			var newID = this.ActiveItemID + 1;
			
			if(newID >= this.AmountOfItems){
				this.Executer.stop();
				return false;
			}
		}
		
		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.ActiveItemID - 1;

		//get the last picture if the current one is image 1
		if(ID == -1){
			ID = this.AmountOfItems - 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.ActiveItemID === 0 || this.ActiveItemID){
			
			if($('nav_'+this.Items[this.ActiveItemID]))
			{
				$('nav_'+this.Items[this.ActiveItemID]).removeClassName('active');
			}
			
			var effectName = this.options.hideEffect;
			
			switch(effectName.toUpperCase())
			{
				case "FADE":
					new Effect.Fade(this.Items[this.ActiveItemID], { duration: this.options.duration});
				break;
				case "BLINDUP":
					new Effect.BlindUp(this.Items[this.ActiveItemID]);
				break;
				case "FOLD":
					new Effect.Fold(this.Items[this.ActiveItemID]);
				break;
				case "SLIDEDOWN":
					new Effect.SlideDown(this.Items[this.ActiveItemID]);
				break;
				case "SHRINK":
					new Effect.Shrink(this.Items[this.ActiveItemID]);
				break;
				case "SWITCHOFF":
					new Effect.SwitchOff(this.Items[this.ActiveItemID]);
				break;
				case "PUFF":
					new Effect.Puff(this.Items[this.ActiveItemID]);
				break;
				case "DROPOUT":
					new Effect.DropOut(this.Items[this.ActiveItemID]);
				break;
			}
		}
	},
	
	/**
	 * show picture with a nice effect
	 */
	show: function(ID) {

		if(ID || ID === 0){
			this.ActiveItemID = ID;
		}else if(this.options.random){
			this.ActiveItemID = this.getRandomID();
		}else{
			this.ActiveItemID = this.getID();
		}

		if($('nav_'+this.Items[this.ActiveItemID]))
		{
			$('nav_'+this.Items[this.ActiveItemID]).addClassName('active');
		}
		
		var effectName = this.options.showEffect;
		
		switch(effectName.toUpperCase())
		{
			case "APPEAR":
				new Effect.Appear(this.Items[this.ActiveItemID], { duration: this.options.duration});
			break;
			case "SLIDEUP":
				new Effect.SlideUp(this.Items[this.ActiveItemID]);
			break;
			case "GROW":
				new Effect.Grow(this.Items[this.ActiveItemID], {direction: 'bottom-right'});
				
			default:	/* Thanh-2k11sep21: Added (project 101225) */
				if($('tab-container'))
				{
					$('tab-container').innerHTML = $(this.Items[this.ActiveItemID]).innerHTML;
				}
				else
				{
					new Effect.Grow(this.Items[this.ActiveItemID], {direction: 'bottom-right'});
				}
			break;
		}
		
	},
	

	/**
	 * return a random id of a picture to show
	 */
	getRandomID: function() {
		randomID = Math.round(Math.random()*this.AmountOfItems);
		
		//always a new unique image
		if($(this.Items[randomID]) && (randomID != this.ActiveItemID)){
			return randomID;
		}else{
			return this.getRandomID();
		}
	},
	
	/**
	 * return a id of a picture to show
	 */
	getID: function() {
		if(this.ActiveItemID === 0 || this.ActiveItemID ){
		
			var newID = this.ActiveItemID + 1;
			
			//if this.ActiveItemID is the last image, the newID is a non existing pic, so show the first one
			if(newID >= this.AmountOfItems){
				newID = 0;
			}
		}else{
			//begin with nr 0 if there is no active image ID
			var newID = 0;
		}
		
		return newID;
	},
	
	getItemKey: function(sId)
	{
		var iReturn;
		this.Items.each(function(sValue,sKey)
		{
			if(sValue == sId)
			{
				iReturn = sKey;
			}
		});

		return iReturn;
	}
});
