com.ydso.modules = {};
com.ydso.modules.slideShow = function( moduleId )
{
	var busy 			= false;
	var _self			= this;													// Self reference for timer callback

	var galleryImages	= new Array();
	var currentImage	= 0;
	var countImages		= 0;
	var tweenPhoto;
	var container		= document.getElementById( "moduleSlideShow" + moduleId );
	var tweenEffect;
	var tweenEffectTime;
	var tweenAnimation;   //<---- wordt nog niet gebruikt
	var tweenDescription;
	var slideshowDescription;
	var timeout;
	
	var begin;
	var finish;
	
	var primary;				// contains the primary image container
	var secondary;
	
	var imageA;
	var imageB;

	this.init = function( xmldoc )
	{
		/**
		 * Load configuration from XML file
		 */
		var xml					= xmldoc;
		var tweenWidth			= parseInt( xml.getElementsByTagName("width")[0].childNodes[0].nodeValue );
		var tweenHeight			= parseInt( xml.getElementsByTagName("height")[0].childNodes[0].nodeValue );
		var tweenControls		= xml.getElementsByTagName("controls")[0].childNodes[0].nodeValue;
		var tweenAutoscroll		= xml.getElementsByTagName("autoscroll")[0].childNodes[0].nodeValue;
		tweenDescription		= parseInt( xml.getElementsByTagName("description")[0].childNodes[0].nodeValue );
		tweenEffect				= xml.getElementsByTagName("effect")[0].childNodes[0].nodeValue;
		tweenEffectTime			= parseInt( xml.getElementsByTagName("effectTime")[0].childNodes[0].nodeValue );
		tweenAnimation			= xml.getElementsByTagName("animation")[0].childNodes[0].nodeValue; //<---- wordt nog niet gebruikt
		timeout					= xml.getElementsByTagName("timeout")[0].childNodes[0].nodeValue;
		var tweenImages			= xml.getElementsByTagName("image");
		
		
		/**
		 * Get all images from the xml and put all data in an array
		 */
		countImages				= tweenImages.length;
		
		for ( var i = 0; i < countImages; i++ )
		{
			galleryImages[i] = {};
			galleryImages[i].url = tweenImages[i].getElementsByTagName("url")[0].childNodes[0].nodeValue;
			galleryImages[i].txt = tweenImages[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
			galleryImages[i].img = new Image();
		}
		
		container.className				= "slideshow";
		container.style.position		= "relative";
		

		/**
		 * Create a mask for the images so they don't overlap with the controls or other elements
		 */
		imageMask					= document.createElement( "div" );
		imageMask.className			= "slideshowImageContainer";
		imageMask.style.position 	= "absolute";
		imageMask.style.overflow	= "hidden";
		imageMask.style.height		= tweenHeight + "px";
		imageMask.style.width		= tweenWidth + "px";
		
		/**
		 * Create two images, one for the current image, one for the next
		 */
		imageA 						= document.createElement( "img" );
		imageA.className			= "slideshowImage";
		imageA.style.position 		= "absolute";
		imageA.style.height			= tweenHeight + "px";
		imageA.style.width			= tweenWidth + "px";
		
		imageB 						= document.createElement( "img" );
		imageB.className			= "slideshowImage";
		imageB.style.position 		= "absolute";
		imageB.style.height			= tweenHeight + "px";
		imageB.style.width			= tweenWidth + "px";
		
		/**
		 * Append all components to the container
		 */
		container.appendChild( imageMask );
		imageMask.appendChild( imageA );
		imageMask.appendChild( imageB );
		
		/**
		 * Set the default values
		 */
		imageA.src				= galleryImages[0].url;
		primary					= imageA;
		secondary				= imageB;
				
		/**
		 * Set tween start and end points
		 */
		switch ( tweenEffect )
		{
			case 'x':
				begin	= 0;
				finish	= tweenWidth;
				break;
			
			case 'y':
				begin	= 0;
				finish	= tweenHeight;
				break;
			
			case 'alpha':
				begin	= 1;
				finish	= 0;
				break;
		}
		
		new com.ydso.transitions.Tween( secondary, tweenEffect, com.ydso.easing.outBackCubic, begin, finish, 50 );
		
		if ( tweenDescription == 1 )
		{
			slideshowDescription				= document.createElement( "div" );
			slideshowDescription.className		= "slideshowDescription";
			slideshowDescription.style.position	= "absolute";
			slideshowDescription.innerHTML		= galleryImages[0].txt;
			
			container.appendChild( slideshowDescription );
		}
		
		if ( tweenAutoscroll == 1 )
		{
			setInterval ( function() { _self.btnNextClick(); }, timeout );
		}

		if ( tweenControls == 1 )
		{
			var controlContainer 			= document.createElement( "div" );
			controlContainer.className		= "slideshowControls";
			controlContainer.style.position	= "absolute";
			controlContainer.style.height	= tweenHeight + "px";
			controlContainer.style.width	= tweenWidth + "px";
		
			var btnNext 					= document.createElement( "div" );
			btnNext.style.position			= "absolute";
			btnNext.style.cursor			= "pointer";
			btnNext.className 				= 'slideshowNextSlide';
			btnNext.onclick 				= function() { _self.btnNextClick(); };
			
			var btnBack 					= document.createElement( "div" );
			btnBack.style.position			= "absolute";
			btnBack.style.cursor			= "pointer";
			btnBack.className 				= 'slideshowPreviousSlide';
			btnBack.onclick 				= function() { _self.btnBackClick(); };
			
			container.appendChild( controlContainer );
			controlContainer.appendChild( btnNext );
			controlContainer.appendChild( btnBack );
			
			/**
			 * Fade in/out when you move over the buttons
			 *
			 * tween 							= new com.ydso.transitions.Tween( slideshowControls, 'alpha', com.ydso.easing.linear, 1, 0.4, 1000 );
			 * slideshowControls.onmouseover	= function() { tween = new com.ydso.transitions.Tween( slideshowControls, 'alpha', com.ydso.easing.linear, 0.4, 1, 100); };
			 * slideshowControls.onmouseout		= function() { tween = new com.ydso.transitions.Tween( slideshowControls, 'alpha', com.ydso.easing.linear, 1, 0.4, 100); };
			*/
		}
	}
	
	/**
	 * Next button click eventHandler
	 */
	this.btnNextClick = function ()
	{
		if ( !busy )
		{
			busy = true;
			
			currentImage++;
		
			if ( currentImage > countImages - 1 )
			{
				currentImage = 0;
			}
			
			galleryImages[currentImage].img.src		= galleryImages[currentImage].url;
			galleryImages[currentImage].img.onload	= function()
			{
				secondary.src = galleryImages[currentImage].img.src;

				new com.ydso.transitions.Tween( primary, tweenEffect, com.ydso.easing.outBackCubic, begin, finish, tweenEffectTime );
				new com.ydso.transitions.Tween( secondary, tweenEffect, com.ydso.easing.outBackCubic, -finish, begin, tweenEffectTime, _self.tweenOutComplete );
				
				if ( tweenDescription )
				{
					new com.ydso.transitions.Tween( slideshowDescription, 'alpha', com.ydso.easing.outBackCubic, 1, 0, tweenEffectTime / 2, _self.changeDescription );
				}
			};
		}
	}
	
	/**
	 * Back button click eventHandler
	 */
	this.btnBackClick = function ()
	{
		if ( !busy )
		{
			busy = true;
			
			currentImage--;
			
			if (currentImage < 0 )
			{
				currentImage = countImages - 1;
			}
			
			galleryImages[currentImage].img.src		= galleryImages[currentImage].url;
			galleryImages[currentImage].img.onload	= function()
			{
				new com.ydso.transitions.Tween( primary, tweenEffect, com.ydso.easing.outBackCubic, begin, -finish, tweenEffectTime );
				new com.ydso.transitions.Tween( secondary, tweenEffect, com.ydso.easing.outBackCubic, finish, begin, tweenEffectTime, _self.tweenOutComplete );
				
				if ( tweenDescription )
				{
					new com.ydso.transitions.Tween( slideshowDescription, 'alpha', com.ydso.easing.outBackCubic, 1, 0, tweenEffectTime / 2, _self.changeDescription );
				}
			};
		}
	}

	this.tweenOutComplete = function ()
	{
		if ( primary == imageA )
		{
			primary = imageB;
			secondary = imageA;
		}
		else
		{
			primary = imageA;
			secondary = imageB;	
		}
		
		busy = false;
	}
	
	this.changeDescription = function ()
	{
		slideshowDescription.innerHTML = galleryImages[currentImage].txt;
		new com.ydso.transitions.Tween( slideshowDescription, 'alpha', com.ydso.easing.outBackCubic, 0, 1, tweenEffectTime / 2 );
	}
	
	var request = new com.ydso.net.HTTPRequest();
	request.url			= "/var/cache/" + moduleId + ".xml";
	request.callback	= _self.init
	request.mode		= "XML";
	request.send();
};
