/**
 * awMediaSlideshow.js
 *
 * This file is part of the awMediaSlideshow package.
 *
 * (c) David Aymanns Wissensmanagement <contact@aymanns.com>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */
(function($){  

	$.fn.awMediaSlideshow = function(config) {	   

		config = config || {};
		var elem = this;
		var elemId = elem[0].id;
		var slideInterval = config.slideInterval || 5000;
		var timeoutFn = null;
		var autoplay = (typeof config.autoplay != 'undefined') ? config.autoplay : true;
		var mediaItems = config.mediaItems || {};
		var items = $("." + elemId + "Content li");
		var lastItem = (items.length-1);
		var images   = $("." + elemId + " .awMediaSlideshowImage");
		var currentSlide = null;
		var currentIndex = 0;
		
		var fading = false;
		var fadeInTime = config.fadeInTime || 1000;
		var fadeOutTime = config.fadeOutTime || 750;
		var firstElementChild = null;
		
		var playingVideo = false;
		
		items.each(function(i) {
			
			$(images[i]).mouseover(function() {
			   mouseOver = true;
			   clearTimeout(timeoutFn);
			});
			
			$(images[i]).mouseout(function() {
				mouseOver   = false;
				if(!playingVideo)
				timeoutFn = setTimeout(next, slideInterval);
			});
			
			if(mediaItems[i] && mediaItems[i].video){
				images[i].style.cursor = 'pointer';
				$(images[i]).click(function() {
					clearTimeout(timeoutFn);
					playingVideo = true;
					this.style.display = 'none';
					$("#" + elemId + "Video_" + i).html(mediaItems[i].video);
					$("#" + elemId + "Video_" + i)[0].style.display = 'inline';
				 });
			}
			
		});
				
		var slideshowPrev = $("#" + elemId + "Prev");
		slideshowPrev.click(function() {
			prev();
			return false;
		 });
		
		var slideshowNext = $("#" + elemId + "Next");
		slideshowNext.click(function() {
			next();
			return false;
		 });
		
		var prev = function(config){
			config = config || {};
			config.currentIndex = (currentIndex == 0) ? lastItem : (currentIndex - 1);
			next(config);
		};

		var nextConfig = null;
		var gotoNext = false;
		var next = function(config){
			config = config || {};
			nextConfig = null;
			if(playingVideo){
				$(images[currentIndex])[0].style.display = 'inline';
				$("#" + elemId + "Video_" + currentIndex)[0].style.display = 'none';				
				$("#" + elemId + "Video_" + currentIndex).html("");
				playingVideo = false;
			}
			clearTimeout(timeoutFn);
			if(!fading){
				if((typeof config.currentIndex) != 'undefined') currentIndex = config.currentIndex;
				else currentIndex = (currentIndex == lastItem) ? 0 : (currentIndex + 1);            	
				fadeSlide(config);
			}
			else{
				gotoNext = true;
				nextConfig = config;
			}
			
			if(autoplay){
	            if(items.length > 0) {
	                timeoutFn = setTimeout(next, slideInterval);
	            }				
			}
		};
		
		var fadeSlide = function(config){
			config = config || {};
			fading = true;
			
			if(currentSlide){
				currentSlide.fadeOut((config.fadeOutTime || fadeOutTime), function() {
					fadeIn(config);
				});				
			}
			else{
				fadeIn(config);
			}
		};
		
		var fadeIn = function(config){
			var cIndex = config.itemNr ? (config.itemNr - 1) : currentIndex;
			$(items[cIndex]).fadeIn((config.fadeInTime || fadeInTime), function() {
				currentSlide = $(items[cIndex]);
				fading = false;
				if(gotoNext){
					gotoNext = false;
					next(nextConfig);
				}
			});			
		};
		
		fadeSlide();
		
		if(autoplay && items.length > 0) {
            timeoutFn = setTimeout(next, slideInterval);
		}
	};  

})(jQuery); 

