/**
 * jQuery Diaporama 
 * 
 * Sylvain Gougouzian // acti
 * 
 * Usual :
 * 
 * 		<div id="marche">
 *			<div class="phototeque">
 *         		<div class="legende">
 *			    	<div class="puce">
 *           			<a class="left" href="#"><img src="images/puce-violette-left.gif" alt="pr&eacute;c&eacute;dente" /></a>
 *						<a class="right" href="#"><img src="images/puce-violette-right.gif" alt="suivante" /></a>    
 *						<h2></h2>          
 *					</div>
 *				</div>
 *			</div>
 *			<ul>
 *	    		<li><a href="link" rel="link_img">text</a></li>
 *	    	</ul>
 *		</div>
 * 
 * 
 * 		jQuery("#marche").diaporama();
 * 
 */


(function ($) {
	var length = 0;
	var current = 0;
	
	var diapos = new Array();
	var $this;
	
	$.fn.diaporama = function(){
		return this.each(function(){
			$this = $(this);
			$('ul', this).fadeOut();
			length = $('ul li', this).length;
			lis = $('ul li', this);
			if (length == 0) {
        $this.hide();
      }
      if (length <= 1) {
				$('a.left, a.right', this).hide();
			}
			else {
				$('a.left', this).click(function() {
					current--;
					if (current < 0) {
						current = length - 1;
					}
					lis.each(function (i) {
						if (i == current) {
							var my_a = $('a', this);
							if (length > 1) {
								if (current%2 == 0)
									$('div.first_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
								else 
									$('div.last_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
								if (current%2 == 0) {
									$('div.first_phototeque').fadeIn();
									$('div.last_phototeque').fadeOut();
								}
								else {
									$('div.first_phototeque').fadeOut();
									$('div.last_phototeque').fadeIn();
								}
							}
							else {
								$('div.phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');	
							}
							if (my_a.attr('href') != "") {
								$('h2', $this).html('<a href="' + my_a.attr('href') + '" class="' + my_a.attr('class') +'">' + my_a.text() + '</a>');
							}
							else {
								$('h2', $this).html(my_a.text());
							}
								
						}
					});
				});
				$('a.right', this).click(function() {
					current++;
					if (current >= length) {
						current = 0;
					}
					lis.each(function (i) {
						if (i == current) {
							var my_a = $('a', this);
							if (length > 1) {
								if (current%2 == 0)
									$('div.first_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
								else 
									$('div.last_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
								if (current%2 == 0) {
									$('div.first_phototeque').fadeIn();
									$('div.last_phototeque').fadeOut();
								}
								else {
									$('div.first_phototeque').fadeOut();
									$('div.last_phototeque').fadeIn();
								}
							}
							else {
								$('div.phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');	
							}
							if (my_a.attr('href') != "") {
								$('h2', $this).html('<a href="' + my_a.attr('href') + '" class="' + my_a.attr('class') +'">' + my_a.text() + '</a>');
							}
							else {
								$('h2', $this).html(my_a.text());
							}
						}
					});
				});
			}
			
			
			if (length > 1) {
				$("div.phototeque").append('<div class="first_phototeque"></div><div class="last_phototeque"></div>');
				$("div.first_phototeque, div.last_phototeque", $("div.phototeque")).css({
					'position' : 'absolute',
					'top' : '0px',
					'z-index' : '1',
					'width' : $("div.phototeque").width(),
					'height' : $("div.phototeque").height()
				});
				$('div.last_phototeque').hide();
			}
			var firstLi = $('ul li:first a', this);
			if (length > 1) {
				$('div.first_phototeque', this).css('background', 'url("' + firstLi.attr('rel') + '") no-repeat');
			}
			else {
				$('div.phototeque', this).css('background', 'url("' + firstLi.attr('rel') + '") no-repeat');				
			}
			if (firstLi.attr('href') != "") {
				$('h2', $this).html('<a href="' + firstLi.attr('href') + '" class="' + firstLi.attr('class') +'">' + firstLi.text() + '</a>');
			}
			else {
				$('h2', $this).html(firstLi.text());
			}
			setInterval(function(){
				current++;
				if (current >= length) {
					current = 0;
				}
				lis.each(function (i) {
					if (i == current) {
						var my_a = $('a', this);
						if (length > 1) {
							if (current%2 == 0)
								$('div.first_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
							else 
								$('div.last_phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');
							if (current%2 == 0) {
								$('div.first_phototeque').fadeIn();
								$('div.last_phototeque').fadeOut();
							}
							else {
								$('div.first_phototeque').fadeOut();
								$('div.last_phototeque').fadeIn();
							}
						}
						else {
							$('div.phototeque', $this).css('background', 'url("' + my_a.attr('rel') + '") no-repeat');	
						}
						
						var target = "#";
						if (my_a.attr('href') != "") {
							$('h2', $this).html('<a href="' + my_a.attr('href') + '" class="' + my_a.attr('class') +'">' + my_a.text() + '</a>');
						}
						else {
							$('h2', $this).html(my_a.text());
						}
					}
				}); 
            //}, 10000);
            }, 2500);

		});
	};
	
})(jQuery);
