
function ImageGallery() {
	
	//--------------------------------------------------------------------
	
	var images = null;
	var imageContainer = null;
	var bodyContainer = null;
	var img = null;
	var naviBox = null;
	var animation = false;
	var _self = this;
	var active = false;
	var activeImageId;
	var activeLinkColor;
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @param array a_images
	 * @return void
	 */
	this.__construct = function (a_imageContainer, a_images ) {
		imageContainer = a_imageContainer;
		images = a_images;
		activeImageId = 0;
		activeLinkColor = '#666666';
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setImg() {
		img = imageBg.getImg();
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @param integer id
	 * @return void
	 */
	function swapImage( id ) {
		if( animation === true ) return false;
		animation = true;
		if ($('galleryLink_' + activeImageId)) {
			$('galleryLink_' + activeImageId).style.color = '#FFFFFF';
			$('galleryLink_' + id).style.color = activeLinkColor;
		}
		activeImageId = id;
		
		var duration = 500;
		imageContainer.set('morph', {duration: duration, onComplete:function() {
			imageContainer.onload = function() {
				imageBg.adapt();
				imageContainer.set('morph', {duration: duration, onComplete:function(){animation=false;}});
				imageContainer.morph({opacity:1});
			}
			imageContainer.src = images[id];
		}});
		imageContainer.morph({opacity: 0});
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setElements() {
		if ( bodyContainer === null ) {
			bodyContainer = document.getElementsByTagName("body");
			bodyContainer = bodyContainer[0];
		}
		if(naviBox === null) {
			naviBox = document.createElement("div");
			naviBox.className = 'gallery_naviBox';
			bodyContainer.appendChild(naviBox);
			
			naviBox.innerHTML = '';
			var text = document.createTextNode('Seite ');
			
			var text = document.createElement('div');
			text.innerHTML = (LANG == 'de') ? 'Bilder' : 'Images';
			text.className = 'gallery_text';
			naviBox.appendChild(text);
			
			var btPrev = document.createElement('a');
			btPrev.innerHTML = '&laquo;';
			btPrev.href = '#';
			btPrev.className = 'gallery_step';
			btPrev.onclick = function() {
				_self.loadNext(false);
				return false;
			}
			naviBox.appendChild(btPrev);
			
			for( i=0;i<images.length;i++) {
				var a = document.createElement('a');
				a.id = 'galleryLink_'+i;
				a.innerHTML = i+1;
				a.href = '#';
				a.className = 'gallery_step';
				a.imageId = i;
				a.onclick = function() {
					swapImage(this.imageId);
					return false;
				}
				if( i === 0 ) { a.style.color = activeLinkColor; }
				naviBox.appendChild(a);
			}
			
			var btNext = document.createElement('a');
			btNext.innerHTML = '&raquo;';
			btNext.href = '#';
			btNext.className = 'gallery_step';
			btNext.onclick = function() {
				_self.loadNext(true);
				return false;
			}
			naviBox.appendChild(btNext);
			
			var x = document.createElement('div');
			x.className = 'gallery_x'
			x.onclick = function() {
				_self.hide();
			}
			naviBox.appendChild(x);
		}
		$(naviBox).style.height = '0px';
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @param boolean next
	 * @return void
	 */
	this.loadNext = function( next ) {
		next = ( next === true ) ? (activeImageId+1) : (activeImageId-1);
		if( next === -1 ) { swapImage(images.length-1); }
		else if( next === images.length ) { swapImage(0); }
		else { swapImage(next); }
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setNaviBox() {
		active = true;
		setElements();
		setKeyEvent();
		naviBox.set('morph', {duration: 400, transition: Fx.Transitions.Sine.easeOut});
		naviBox.setOpacity(0.001);
		naviBox.style.display = 'block';
		setNaviBoxPosition();
		naviBox.morph({height: 40, opacity:1});
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setKeyEvent() {
		document.onkeydown = function(event) {
			var code = null;
			if ( !event ) event = window.event;
			if ( event.which ) code = event.which;
			else if ( event.keyCode ) code = event.keyCode;
			switch(code) {
				case 37: _self.loadNext(false);  break;
				case 39: _self.loadNext(true);  break;
			}
		}
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function restoreKeyEvent() {
		document.onkeydown = function() {}
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setNaviBoxPosition() {
		if (naviBox === null) { return false; }
		naviBox.style.left = (window.getSize().x/2) - (naviBox.offsetWidth/2) + 'px';
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.setPosition = function() {
		setNaviBoxPosition();
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.show = function() {
		imageBg.freezeAnimation(true);
		page.hide();
		setTimeout(function(){swapImage(0);},500);
		setTimeout(function(){setNaviBox();},2000);
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.hide = function() {
		if( animation === true ) return false;
		restoreKeyEvent();
		animation = true;
		naviBox.set('morph', {duration: 400, transition: Fx.Transitions.Sine.easeOut});
		naviBox.morph({height: 0, opacity:0, onComplete:function(){
			setTimeout(function() {
				naviBox.style.display = 'none';
				active = false;
				page.show();
				imageBg.freezeAnimation(false);
				imageBg.startAnimation(true);
				animation = false;
			},500);
		}});
	}
	
	//--------------------------------------------------------------------
	/**
	 * 
	 * @return boolean
	 */
	this.isActive = function() {
		return active;
	}
}
