/* =========================================================
// jquery.homepageGallery.js
//
// Date: 2010-12-14
// Author: Ian Read
// Mail: web@uea.ac.uk
// Web: http://www.uea.ac.uk
//
// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/ 
// and Ralf S. Engelschall http://trainofthoughts.org/ and Torsten Baldes http://medienfreunde.com
//

 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').homepageGallery({ 
 *	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
 *	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
 *	  timeout: Time between the fades in milliseconds (Default: '2000'), 
 *	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
 * 		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  runningclass: CSS-Class which the container get’s applied (Default: 'homepageGallery'),
 *	  children: optional children selector (Default: null)
 *  }); 
 *

// ========================================================= */
(function($) {
	var allImages = {};
	var elements = {};

	
	var currentSlide = 0;

	var timeout = null;
	var settings = {
		'animationtype':    'fade',
		'speed':            'normal',
		'type':             'sequence',
		'timeout':          2000,
		'containerheight':  'auto',
		'runningclass':     'innerfade',
		'children':         null,
		'state':		'paused'
        };
	
	// Primary homepageGallery initialization function that should be called on the thumbnail container.
	$.fn.homepageGallery = function(options)
	{
		//  Extend Gallery Object
		$.extend(this, {

			// Current state of the slideshow
			isSlideshowRunning: false,
			slideshowTimeout: undefined,
		
			getNextIndex: function()
			{
				return currentSlide == elements.length - 1 ? 0 : currentSlide + 1;
			},
			
			getPrevIndex: function()
			{
				return currentSlide == 0 ? elements.length - 1 : currentSlide - 1;
			},
			
			gotoNext: function( )
			{
				return this.next( this.getNextIndex(), currentSlide );
			},
			gotoPrev: function( )
			{
				return this.next( this.getPrevIndex(), currentSlide );
			},
			pause: function( )
			{
				// change the state		
				settings.state = 'paused';
				// remove any existing timeouts
				clearTimeout(timeout);
			},
			play: function()
			{
				$("#overlayMain").append("Hello");
			
				// clear existing timeout
				this.pause();
				settings.state = 'playing';
				var nextIndex = this.getNextIndex();
				var gallery = this;
				//alert("Pointing to slide " + nextIndex +" with state = " + settings.state);
				// set the time out to the next slide
				timeout = setTimeout((function()
							{
								gallery.next( nextIndex, currentSlide);
							}), settings.timeout);
			},
			next: function( current, last )
			{
				elements = $(this).children(settings.children);

				if (settings.animationtype == 'slide')
			        {
			            $(elements[last]).slideUp(settings.speed);
			            $(elements[current]).slideDown(settings.speed);
			        }
			        else if (settings.animationtype == 'fade')
			        {
			            $(elements[last]).fadeOut(settings.speed);
			            $(elements[current]).fadeIn(settings.speed);
			        }
			        else
			        {
			            alert('homepageGallery-animationtype must either be \'slide\' or \'fade\'');
				}
			
				currentSlide = current;
				
				// keep playing
				if( settings.state == 'playing' )
				{
					this.play();
			        }
    			}    			
		});

		if (options)
		{
			$.extend(settings, options);
		}
		if (settings.children === null)
		{
			elements = $(this).children();
		}
		else
		{
			elements = $(this).children(settings.children);
		}

		if (elements.length > 1)
		{
			$(this).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
			for (var i = 0; i < elements.length; i++)
			{
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
			};
			if (settings.type == "sequence")
			{
				setTimeout(function()
				{
					$(this).next(1, 0);
				}, settings.timeout);
				$(elements[0]).show();
			}
			else if (settings.type == "random")
			{
				var last = Math.floor ( Math.random () * ( elements.length ) );
				setTimeout(function()
				{
					do
					{ 
						current = Math.floor ( Math.random ( ) * ( elements.length ) );
					}
					while (last == current );
					$.homepageGallery.next(current, last);
				}, settings.timeout);
				$(elements[last]).show();
			}
			else if ( settings.type == 'random_start' )
			{
				settings.type = 'sequence';
				var current = Math.floor ( Math.random () * ( elements.length ) );
				setTimeout(function()
				{
					$.homepageGallery.next(elements, settings, (current + 1) %  elements.length, current);
				}, settings.timeout);
				$(elements[current]).show();
			}
			else
			{
				alert('homepageGallery-Type must either be \'sequence\', \'random\' or \'random_start\'');
			}
		}
	

		return this;
	};
})(jQuery);

