var DiaShow = Class.create();

Object.extend(

	DiaShow.prototype, {
		
		initialize: function(obj, id, pages, width, height) {
			this._obj = $(obj);
			this._id = id;
			this._pages = pages;
			this._width = width;
			this._height = height;
			this._currentSlideId = 0;

			this._nextButton = this._obj.up().getElementsBySelector('.next-button')[0];
			this._nextButton.setOpacity(0.5);
			
			this._eL1 = this.clickButton.bindAsEventListener(this);
			this._eL2 = this.overButton.bindAsEventListener(this);
			this._eL3 = this.outButton.bindAsEventListener(this);
	
			this._nextButton.onclick = function() { this.blur(); return false; };
				
			Event.observe(this._nextButton, 'click', this._eL1);			
			Event.observe(this._nextButton, 'mouseover', this._eL2);			
			Event.observe(this._nextButton, 'mouseout', this._eL3);			
			
			this.loadSlide();
			this.startSlideShow();

		},
		
		startSlideShow: function() {
			if (navigator.appVersion.indexOf("Mac")!=-1) {
				var wait = 13500;
			} else {
				var wait = 10000;
			};
			this._timedNext = setInterval(function() { this.nextImage() }.bind(this) , wait);
		},
		
		clickButton: function() {
			try {
				window.clearInterval(this._timedNext);
				window.clearTimeout(this._restartEvent);
			} catch(E) {
			}
			this._restartEvent = window.setTimeout(function() {this.startSlideShow()}.bind(this), 10000);
			this.nextImage();
		},
		
		overButton: function() {
			this._nextButton.setOpacity(1);
		},
		
		outButton: function() {
			this._nextButton.setOpacity(0.5);
		},
		nextImage: function() {

			this._currentSlideId = (++this._currentSlideId) % this._pages;
			
			this.loadSlide();
		},
		
		loadSlide: function() {
			
			new Ajax.Updater(this._obj, 'index.php?eID=diashow', {
					evalScripts: true,
					parameters: { 
						slideId: this._currentSlideId, 
						diashowId: this._id,
						width : this._width,
						height : this._height
					},
					insertion: 'bottom',
					onComplete: function() {
						new Effect.Appear(this._obj.childElements().last(), { afterFinish: function() { this.showNextSlide() }.bind(this) } );
					}.bind(this)
				}
			);
			
		},

		showNextSlide: function() {

			tmp = this._obj.childElements();
			if (tmp.length > 1) {
				tmp.first().remove();
			}

			try {			
				var overlay = tmp.last().down().next();
							
				var overlayBackground = tmp.last().down().next().down();
				overlayBackground.setOpacity(0.35);
	
				window.setTimeout(function() { new Effect.Morph(overlay,{ style:{ width:'300px' }, duration: 4.0 } ) }, 2000);
			} catch(E) {
				
			}
			
						
		}
	}
);


