(function($){
	$.fn.extend({ 
		infiniteCarousel: function(options){
			var defaults = {
				transitionStyle : "fade",
				transitionSpeed : 1000,
				displayTime : 6000,
				textholderHeight : .25,
				textholderContent : 0,
				displayProgressBar : 0,
				displayThumbnails: 1,
				displayThumbnailNumbers: 1,
				displayThumbnailBackground: 1,
				thumbnailWidth: '10px',
				thumbnailHeight: '10px',	
			};
			var options = $.extend(defaults, options);
	
    		return this.each(function() {
    			var randID = Math.round(Math.random()*100000000);
				var o=options;
				var obj = $(this);
				var curr = 0;
				var numImages = $('img', obj).length; // Number of images
				var imgHeight = $('img:first', obj).height();
				var imgWidth = $('img:first', obj).width();
				var autopilot = 1;
				
				$('p', obj).hide();
				$(obj).width(imgWidth).height(imgHeight);
				
				if(o.displayProgressBar){
					$(obj).append('<div id="progress'+randID+'" style="position:absolute;bottom:0;background:#ddd;left:'+$(obj).css('paddingLeft')+'"></div>');
					$('#progress'+randID).width(imgWidth).height(5).css('opacity','.8');
				}	
				$(obj).css({'overflow':'hidden','position':'relative'});
				$('ul',obj).css({'list-style':'none','margin':'0','padding':'0','position':'relative'});
				$('li',obj).css({'position':'absolute','float':'left'});
				$('li',obj).hide();
				$('li:first',obj).show();
// Prev/next button(img) 
				html = '<div id="btn_rt'+randID+'" style="position:absolute;right:0;top:'+((imgHeight/2)-15)+'px"><a href="javascript:void(0);"><img style="border:none;margin-right:2px" src="./js/jquery/images/rt.png" /></a></div>';
				html += '<div id="btn_lt'+randID+'" style="position:absolute;left:0;top:'+((imgHeight/2)-15)+'px"><a href="javascript:void(0);"><img style="border:none;margin-left:2px" src="./js/jquery/images/lt.png" /></a></div>';
				$(obj).append(html);
// Pause/play button(img)	
				html = '<a href="javascript:void(0);"><img id="pause_btn'+randID+'" src="./js/jquery/images/pause.png" style="position:absolute;top:3px;right:3px;border:none" alt="Pause" /></a>';
				html += '<a href="javascript:void(0);"><img id="play_btn'+randID+'" src="./js/jquery/images/play.png" style="position:absolute;top:3px;right:3px;border:none;display:none;" alt="Play" /></a>';
				$(obj).append(html);
				$('#pause_btn'+randID).css('opacity','.5').hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.5'},250)});
				$('#pause_btn'+randID).click(function(){	// onclick pause
					autopilot = 0;
					$('#progress'+randID).stop().fadeOut();
					clearTimeout(clearInt);
					$('#pause_btn'+randID).fadeOut(250);
					$('#play_btn'+randID).fadeIn(250);
				});
				$('#play_btn'+randID).css('opacity','.5').hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.5'},250)});
				$('#play_btn'+randID).click(function(){	// onclick play
					autopilot = 1;
					anim("next",0);
					$('#play_btn'+randID).hide();
					clearInt=setInterval(function(){anim("next",0);},o.displayTime+o.transitionSpeed);
					setTimeout(function(){$('#pause_btn'+randID).show();$('#progress'+randID).fadeIn().width(imgWidth).height(5);},o.transitionSpeed);
				});
// Left and right arrow image button actions
				$('#btn_rt'+randID).css('opacity','.75').click(function(){ // onclick next
					$('#btn_rt'+randID).fadeOut(1);
					$('#btn_lt'+randID).fadeOut(1);
					$('#progress'+randID).stop().fadeOut();
					autopilot = 0;
					anim("next",0);
					setTimeout(function(){$('#play_btn'+randID).fadeIn(250);},o.transitionSpeed);
					clearTimeout(clearInt);
				}).hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.75'},250)});
				$('#btn_lt'+randID).css('opacity','.75').click(function(){ // onclick prev
					$('#btn_lt'+randID).fadeOut(1);
					$('#btn_rt'+randID).fadeOut(1);
					$('#progress'+randID).stop().fadeOut();
					autopilot = 0;
					anim("prev",0);
					setTimeout(function(){$('#play_btn'+randID).fadeIn(250);},o.transitionSpeed);
					clearTimeout(clearInt);
					}).hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.75'},250)});
// Build textholder div thats as wide as the carousel and 20%-25% of the height
			// titre et texte
				if(o.textholderContent){
					$(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;text-align:left;top:'+imgHeight+'px;left:0px;padding-left:10px;"></div>');
					$('#textholder'+randID).width(imgWidth).height(imgHeight*o.textholderHeight).css({'backgroundColor':'#000','opacity':'0.6'});
				}
				//					
// Build thumbnail viewer and thumbnail divs		
				if(o.displayThumbnails){
					$(obj).append('<div id="thumbs'+randID+'" style="position:absolute;top:340px;height:26px"></div>');
					$('#thumbs'+randID).width(1019);
					$('#thumbs'+randID).css({'padding-left':'5px','backgroundColor':'#000','opacity':'0.7'});
					for(i=0;i<numImages;i++){
						thumb = $('img:eq('+(i)+')', obj).attr('src');
						$('#thumbs'+randID).append('<div class="thumb" id="thumb'+randID+'_'+(i)+'" style="cursor:pointer;background-color:#000000;display:inline;float:left;width:'+o.thumbnailWidth+';height:'+o.thumbnailHeight+';padding:0;overflow:hidden;text-align:center;border:1px solid #888888;margin:7px 2px;"></div>');
					}
					thumb = $('img:first', obj).attr('src');
					$('#thumb'+randID+'_'+numImages).css({'background-color':'#000000'});
					$('#thumbs'+randID+' div.thumb:not(:first)').css({'opacity':'.65'});
					$('#thumbs'+randID+' div.thumb').hover(
						function(){$(this).animate({'opacity':.99},150)},
						function(){if(curr!=this.id.split('_')[1]) $(this).animate({'opacity':.65},250)}
					);
					$('#thumbs'+randID+' div').bind('click', thumbclick);
					if(!o.displayThumbnailNumbers) $('#thumbs'+randID+' div').text('');
					if(!o.displayThumbnailBackground) $('#thumbs'+randID+' div').css({'background-color':'#000000'});
					$('#thumb'+randID+'_'+(0)).css({'border':'2px solid #FF8800','margin':'6px 1px'});
				}
// Build textholder div thats as wide as the carousel and 20%-25% of the height
			// juste le titre
				if(!o.textholderContent){
					$(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;text-align:right;margin-bottom:-26px;left:'+(14*(numImages+1)+5)+'px;"></div>');
					$('#textholder'+randID).width(imgWidth-(27+(14*numImages+1))).height(26).css({'backgroundColor':'transparent'});
				}
				showtext($('li:first p', obj).html());
				
				function thumbclick(event){
					target_num = this.id.split('_');
					if(curr != target_num[1]){
						$('#thumb'+randID+'_'+curr).css({'border-color':'#888888'});
						$('#progress'+randID).stop().fadeOut();
						clearTimeout(clearInt);
						$('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click');// Unbind the thumbnail click event until the transition has ended
						autopilot = 0;
						borderpatrol($('#thumb'+randID+'_'+(target_num[1])));
						$('#btn_lt'+randID).fadeOut();
						$('#btn_rt'+randID).fadeOut();
						setTimeout(function(){$('#play_btn'+randID).fadeIn(250);},o.transitionSpeed);
						anim("thmb",parseInt(target_num[1]));
					}
				}
				function showtext(t){
					if(t != null){
						if(o.textholderContent){
							$('#textholder'+randID).html(t).animate({top:(imgHeight*(1-o.textholderHeight))+'px'},500);
						}else{
							$('#textholder'+randID).html(t).animate({marginBottom:'0px'},500);
						}
					}
				}
				function borderpatrol(elem){
					$('#thumbs'+randID+' div').css({'border':'1px solid #888888','margin':'7px 2px'}).animate({opacity: 0.65},500);
					setTimeout(function(){elem.css({'border':'2px solid #FF8800','margin':'6px 1px'}).animate({'opacity': 1},500);},o.transitionSpeed);
				}
				function anim(sens,nb){
							if (sens=="next"){
								next=(curr+1>=numImages) ? 0 : curr+1;
								delta=1;
							}
							if (sens=="prev"){
								next=((curr-1)<0) ? numImages-1 : curr-1;
								delta=-1;
							}
							if (sens=="thmb"){
								next=nb;
								delta=(next>curr)?1:-1;
							}
					if(o.textholderContent){
						$('#textholder'+randID).animate({top:imgHeight+'px'},500);	
					}else{
						$('#textholder'+randID).animate({marginBottom:'-26px'},500);
					}
											
					borderpatrol($('#thumb'+randID+'_'+(next)));
					if (o.transitionStyle=="fade"){	//fondu enchainé
						$('li:eq('+curr+')',obj).fadeOut(o.transitionSpeed);
						$('li:eq('+next+')',obj).fadeIn(o.transitionSpeed,function(){
							showtext($('li:eq('+next+') p', obj).html());
							$('#btn_rt'+randID).fadeIn(500);
							$('#btn_lt'+randID).fadeIn(500);
							if (autopilot){
								$('#progress'+randID).width(imgWidth).height(5);
								$('#progress'+randID).animate({'width':0},o.displayTime,function(){
									$('#pause_btn'+randID).fadeOut(50);
									$('#btn_lt'+randID).fadeOut(50);
									$('#btn_rt'+randID).fadeOut(50);
									setTimeout(function(){$('#pause_btn'+randID).fadeIn(250)},o.transitionSpeed)
								});
							}else{
								clearTimeout(clearInt);
								$('#pause_btn'+randID).fadeOut(50);
								$('#thumbs'+randID+' div').bind('click', thumbclick).css({'cursor':'pointer'});
							}
							curr=next;
						});
					}
				}
				var clearInt = setInterval(function(){anim("next",0);},o.displayTime+o.transitionSpeed);
				$('#progress'+randID).animate({'width':0},o.displayTime+o.transitionSpeed,function(){
					$('#pause_btn'+randID).fadeOut(100);
					$('#btn_lt'+randID).fadeOut(100);
					$('#btn_rt'+randID).fadeOut(100);
					setTimeout(function(){$('#pause_btn'+randID).fadeIn(250)},o.transitionSpeed)}
				);
  		});
    	}
	});
})(jQuery);
