/**
 *
 * @author	Benoit Asselin <contact(at)ab-d(dot)fr>
 * @version	javascript.js, 2011/11/10
 *
 */


jQuery(document).ready(function($) {
	
	$.fn.bodySlide = function(options) {
		var settings = {
			images: [],	// Liste des images ( array )
			path: '',	// Chemin des images ( string )
			random: false,	// Images melangees ( boolean )
			loop: true,	// Animation en boucle ( boolean )
			buttons: false,	// Sans ou avec boutons de navigation ( 'selector' )
			animateMs: 600,	// Duree de l'animation ( ms )
			pauseMs: 5000,	// Duree du temps de pause ( ms )
			height: 0	// Hauteur de l'image ( px )
		};
		
		if(options) {
			$.extend(settings, options);
		}
		
		
		function shuffle(arr) {
			var tmp = [];
			while(arr.length) { tmp.push(arr.splice(Math.random() * arr.length, 1)); }
			while(tmp.length) { arr.push(tmp.pop()); }
			return arr;
		}
		
		return this.each(function() {
			var elt = this;
			var $area = $('<div class="slide-area"></div>');
			var images = (settings.random ? shuffle(settings.images) : settings.images);
			var pos = 0;
			var imgTime = false;
			
			var animationBreak = false;
			var animationRunning = false;
			
			
			
			// L'animation des images
			function imgGo() {
				animationRunning = true;
				
				$('<img src="' + settings.path + images[pos] + '" alt=""/>')
					.load(imgLoaded)
					.data('myPos', pos)
					.css({position:'absolute', left:'50%', top:'0', opacity:0})
					.appendTo($area);
			}
			
			function imgLoaded() {
				var $img = $(this);
				$img
					.css({marginLeft:-($img.width() / 2)})
					.animate({opacity:1}, settings.animateMs, imgAnimateComplete);

				if(!settings.height && $area.height() < $img.height()) {
					$area.css({height:$img.height()});
				}
			}
			
			function imgAnimateComplete() {
				var $img = $(this);
				imgTime = setTimeout(function() {
					pos++;
					if(pos >= images.length) {
						pos = 0;
						if(!settings.loop) { animationBreak = true; }
					}
					if(!animationBreak) {
						$img.animate({opacity:0}, settings.animateMs, function() {
							$(this).remove();
						});
						imgGo();
					} else {
						animationRunning = false;
					}
				}, settings.pauseMs);
			}
			
			
			
			
			// Les boutons
			function btnGo(p_inc) {
				var $children = $area.children('img');
				clearTimeout(imgTime);
				pos += p_inc;
				if($children.eq(0).data('myPos') == pos) { pos += p_inc; }
				if(pos >= images.length) { pos = 0; }
				if(pos < 0) { pos = images.length - 1; }
				
				$children.stop(true).animate({opacity:0}, settings.animateMs, function() {
					$(this).remove();
				});
				imgGo();
			}
			
			if(settings.buttons) {
				$(settings.buttons+'-prev').click(function() {
					btnGo(-1);
				});
				$(settings.buttons+'-pause').click(function() {
					animationBreak = true;
				});
				$(settings.buttons+'-play').click(function() {
					if(!animationRunning) {
						animationBreak = false;
						btnGo(0);
					}
				});
				$(settings.buttons+'-next').click(function() {
					btnGo(1);
				});
			}
			
			
			
			// Preparation de la zone
			$area
				.bind('mousedown', function() { return false; })
				.css({position:'absolute', left:0, top:0, width:'100%', height:settings.height, overflow:'hidden'})
				.prependTo(elt);
			
			// Demarrage apres le chargement complet de la page
			$(window).load(function() {
				if(!animationRunning) {
					imgGo();
				}
			});
			
			return this;
		});
	};
	
});



