/**
 * EZ File Frontend
 * Slideshow
 * 
 * (c) EZdesign.de
 * 
 * Author:   Timo Besenreuther
 * Created:  2009-10-09
 * Modified: 2010-05-02
 */


(function($) {
	
	// default options
	var defaults = {
		position: 0
	};
	
	$.fn.ezFileFrontends_Slideshow = function(action, options) {
		
		var el;
		
		var topupImages = [];
		var toptions = {
			'layout': 'dashboard',
			'shaded': 1,
			'overlayClose': 1,
			'resizable': 0,
			'effect': 'transform',
			'closeEffect': 'fade',
			'title': 'Bild {current} von {total}',
			'ondisplay': function() {
				el.ezFileFrontends_Slideshow('Pause');
			},'onclose':function() {
				el.ezFileFrontends_Slideshow('Play');
			}
		};
		
		if (typeof(action) == 'object') {
      		options = action;
      		action = 'Initialize';
    	}
		
		this.each(function() {
			el = $(this);
			mergeOptions();
			
			switch(action) {
			case 'Initialize':
				initialize();
				break;
			case 'NextImage':
				if (!options.lock) {
					increasePosition();
					transition();
				}
				break;
			case 'PreviousImage':
				if (!options.lock) {
					decreasePosition();
					transition();
				}
				break;
			case 'Pause':
				clearTimeout();
				break;
			case 'Play':
				if (options.images.length > 1) {
					setTimeout(1);
				}
				break;
			default:
				alert('Unknown Action "'+action+'"');
			}
			
			saveOptions();
		});
		
		function mergeOptions() {
			options = $.extend({}, defaults, el.data('EzFileFrontends_SlideshowOptions'), options);
		};
		
		function saveOptions() {
			el.data('EzFileFrontends_SlideshowOptions', options);
		}
		
		
		/**
		 * initialize
		 */
		
		function initialize() {
			// prepage images array for topup
			for (var i = 0; i < options.images.length; i++) {
				topupImages.push(options.images[i].href);
			}
			// image click listener
			$('a', el.find('div.Image')).live('click', function() {
				toptions.topUp = this;
				TopUp.display(topupImages, toptions, el.data('SlideshowIndex'));
				return false;
			});
			
			if (options.images.length <= 1) {
				toptions.title = '';
				return false;
			}
			
			// activate controls
			el.find('a.Prev').click(function() {
				$(this).blur();
				el.ezFileFrontends_Slideshow('PreviousImage');
				return false;
			});
			el.find('a.Next').click(function() {
				$(this).blur();
				el.ezFileFrontends_Slideshow('NextImage');
				return false;
			});
			// start timeout
			setTimeout();
		}
		
		
		/**
		 * timeout
		 */
		
		function setTimeout(length) {
			if (!length) {
				length = options.timeoutLength * 1000; 
			}
			clearTimeout();
			el.data('EzFileFrontends_SlideshowTimeout', window.setTimeout(function() {
				el.ezFileFrontends_Slideshow('NextImage');
			}, length));
		}
		
		function clearTimeout() {
			window.clearTimeout(el.data('EzFileFrontends_SlideshowTimeout'));
		}
		
		
		/**
		 * current image pointer
		 */
		
		function increasePosition() {
			if (options.position + 1 >= options.images.length) {
				options.position = 0;
			} else {
				options.position++;
			}
			el.data('SlideshowIndex', options.position);
		}
		
		function decreasePosition() {
			if (options.position == 0) {
				options.position = options.images.length - 1;
			} else {
				options.position--;
			}
			el.data('SlideshowIndex', options.position);
		}
		
		
		/**
		 * transition
		 */
		
		function transition() {
			options.lock = true;
			saveOptions();
			clearTimeout();
			// load image
			showLoading();
			var img = new Image();
			img.onload = function() {
				hideLoading();
				showImage(this);
			};
			img.src = options.images[options.position].fileName;
		}
		
		function showImage(newImageObject) {
			var img = el.find('.Image img');
			var callback = function() {
				var href = options.images[options.position].href;
				if (href) {
					el.find('.Image a').attr('href', href);
				} 
				showImage2(img, newImageObject);
			};
			var imgContainer = el.find('.Image');
			imgContainer.css('height', imgContainer.height()+'px');
			// hide text
			el.find('.Title').hide();
			el.find('.Description').hide();
			img.fadeOut(options.fadeLength*1000, function() {
				// container height
				imgContainer.animate({
					height: (newImageObject.height+parseInt(img.css('marginTop'), 10)+parseInt(img.css('marginBottom'), 10))+'px'
				}, options.fadeLength*1000, callback);
			});
		}
		
		function showImage2(img, newImageObject) {
			// status
			el.find('.Current').html(options.position+1);
			// image
			img.attr('src', newImageObject.src).fadeIn(options.fadeLength*1000, function() {
				// title
				if (options.images[options.position].title != '') {
					el.find('.Title').html(options.images[options.position].title).fadeIn(options.fadeLength*700);
				}
				// description
				if (options.images[options.position].description != '') {
					el.find('.Description').html(options.images[options.position].description).fadeIn(options.fadeLength*700);
				}
				options.lock = false;
				saveOptions();
			});
			setTimeout();
		}
		
		
		/**
		 * loading
		 */
		
		function showLoading() {
			el.addClass('Loading');
		}
		
		function hideLoading() {
			el.removeClass('Loading');
		}
		
		
		return this;
	};
	
})(jQuery);