//--------------------------------------------------------------------
//Customisable Automated Slideshow function --------------------------
//--------------------------------------------------------------------

(function($){  
   $.fn.slideShow = function(options) {
	   
	  var defaults = {  
			   slideshowType: "fade",
			   showControl: true,   
			   showNavigation: true,
			   showPagination: true,
			   createThumbnails: true,
			   autoStart: true,
			   autoDelay: 5000,//milliseconds between slides/fades
			   speed: 1000//speed of slide/fade in milliseconds
			   };  
	 
	  var options = $.extend(defaults, options);
	  
	  return this.each(function() { 
		  
		   var obj = $(this);
		   var count = 1;
		   var imageWidth;
		   var imageHeight;
		   var sequence;
		   
	   //Initiate ------------------------------------------------
	   
	   function initiate(){

		   $(obj).wrap("<div class='slideshowWrapper' />");
		   $('.slideshowWrapper').append("<ul class='slideshowControl'><li class='play'><a href='#'><span>Play</span></a></li><li class='pause'><a href='#'><span>Pause</span></a></li></ul>");
		   $('.slideshowWrapper').append("<ul class='slideshowNavigation'><li class='next'><a href='#'><span>Next</span></a></li><li class='prev'><a href='#'><span>Previous</span></a></li></ul>");
		   $('.slideshowWrapper').append("<ol class='slideshowPagination'></ol>");
		   

		   if(options.showControl == false){$('.slideshowWrapper .slideshowControl').hide()};
		   
		   if(options.showNavigation == false){$('.slideshowWrapper .slideshowNavigation').hide()};
		   
		   if(options.showPagination == false){$('.slideshowWrapper .slideshowPagination').hide()};	   
		   
		
		   var slideshowItems = $(obj).children('li'); //How many slide-show items are there

       if(options.slideshowType == "slide"){

       imageHeight = $(obj).children('li').children('img').height();

       imageWidth = $(obj).children('li').children('img').width();
		  			
		    }			
		  			   
		   $(slideshowItems).each( function(i){
			   
			   //alert(imageHeight);
			   
			   $(this).height(imageHeight);
			   
			   $(this).addClass("item-" + (i + 1));
			   $('.slideshowPagination').append("<li class='pagi-" + (i + 1) + "'><a href='#' title='" + (i + 1) + " of " + slideshowItems.length + "'><span class='overlay'></span><span class='number'>" + (i + 1) + " of " + slideshowItems.length + "</span></a></li>");
			   $('.slideshowPagination li:first').addClass('active');
			   $('.slideshowPagination li. span.overlay').show();
			   $('.slideshowPagination li.active span.overlay').hide();
			  
			   if(options.createThumbnails == true){ // create thumbnails
			   
				   var thumb = $('<img />');
				   $(thumb).attr('src', $(this).children('a').children('img').attr('src'));
				   $(thumb).width("95px");
				   $(thumb).height("36px");
				   $('.slideshowPagination li.pagi-' + (i + 1)).children('a').append(thumb);
			   
			   }  
		   })
		  
		   
		 
			if(options.slideshowType == "slide"){ //If type is slide
				
				$(obj).addClass("slide");
				$('ul.slideshow').width(imageWidth * slideshowItems.length);
				
			} else if(options.slideshowType == "fade"){ //If type is fade

				$(obj).addClass("fade");
				$(obj).children('li').hide();
				$(obj).children('li:first').show();
				
			}
		   

		   //Navigate -------------------------------------------------------------------------------------------------------------------------		   
		   
		   function navigate(direction){
			   
			 	 clearInterval(sequence);
				 $('.slideshowControl li.pause').hide();				 
				 $('.slideshowControl li.play').show();
			   
				if(options.slideshowType == "slide"){ //If type is slide
						
					switch(direction){
					
					case "next":
						if(count >=  slideshowItems.length){
							count = 1;
							$('ul.slideshow').animate({"left": "0px"}, options.speed);
						} else {
							count = parseInt(count) + 1;
							$('ul.slideshow').animate({"left": "-=" + imageWidth + "px"}, options.speed);
						}
						break;
					case "prev":
						if(count ==  1){
							count = slideshowItems.length;
							$('ul.slideshow').animate({"left": "-=" + (imageWidth * (slideshowItems.length - 1)) + "px"}, options.speed);
						} else {	
							count = parseInt(count) - 1;
							$('ul.slideshow').animate({"left": "+=" + imageWidth + "px"}, options.speed);					
						}
					break;
					
					}
			
				 $('.slideshowPagination li').removeClass('active');
				 $('.slideshowPagination li.pagi-' + count).addClass('active');
				   $('.slideshowPagination li. span.overlay').show();
				 $('.slideshowPagination li.active span.overlay').hide();
					
				} else if(options.slideshowType == "fade"){ //If type is fade
					
					
					switch(direction){
					
					case "next":
					   count = count + 1;
					   var visibleItem = $(obj).children('li:visible').attr('class');
					   var current = visibleItem.substring(visibleItem.length - 1)
					   var next = parseInt(current) + 1;
					   if(next > slideshowItems.length){
						   count = 1;
						   next = 1;
					   }
				   break;
					case "prev":
					   count = count - 1;
					   var visibleItem = $(obj).children('li:visible').attr('class');
					   var current = visibleItem.substring(visibleItem.length - 1)
					   var next = parseInt(current) - 1;
					   if(next < 1){
						   count = slideshowItems.length;
						   next = slideshowItems.length;
					   }
				   break;
				   }

				   $(obj).children('li').hide();
				   $(obj).children('li.item-' + next).fadeIn(options.speed);
				   
				   $('.slideshowPagination li').removeClass('active');
				   $('.slideshowPagination li.pagi-' + count).addClass('active');
				   $('.slideshowPagination li. span.overlay').show();
				   $('.slideshowPagination li.active span.overlay').hide();
				   
				   
				}				   
		   }	   
		   
		 $('.slideshowNavigation .next a, .slideshowNavigation .prev a').click( function(){
			 
			 navigate($(this).parent().attr('class'));
			 
			 return false;
			 
		 })
			 

		   //Paginate -------------------------------------------------------------------------------------------------------------------------		   
		   
		   function paginate(direction,currentPagination){
			 
			 	 clearInterval(sequence);
				 $('.slideshowControl li.pause').hide();				 
				 $('.slideshowControl li.play').show();
				 
				if(options.slideshowType == "slide"){ //If type is slide
				
					 var step = currentPagination - count;
					 var distance = step * imageWidth;
						
					switch(direction){
					
					case "next":
						if(count >=  slideshowItems.length){
							count = 1;
							$('ul.slideshow').animate({"left": "0px"}, options.speed);
						} else {
							count = currentPagination;
							$('ul.slideshow').animate({"left": "-=" + distance + "px"}, options.speed);
						}
						break;
					case "prev":
						if(count ==  1){
							count = slideshowItems.length;
							$('ul.slideshow').animate({"left": "-=" + (imageWidth * (slideshowItems.length - 1)) + "px"}, options.speed);
						} else {	
							count = currentPagination;
							$('ul.slideshow').animate({"left": "-=" + distance + "px"}, options.speed);	
						}
					break;
					
					}
									
				} else if(options.slideshowType == "fade"){ //If type is fade
					
					count = currentPagination;
		
					//alert(count + " ~ " + currentPagination);
					
					$('ul.slideshow li').hide();
					$('ul.slideshow li.item-' + currentPagination).fadeIn(options.speed);
					
				}				   
		   }	   
		 
		 $('.slideshowPagination a').click( function(){
			 
			 $('.slideshowPagination li').removeClass('active');
			 
			 var currentPagination = parseInt($(this).parent('li').attr('class').substring($(this).parent('li').attr('class').length - 1));
			 
			 $(this).parent('li').addClass('active');
			 $('.slideshowPagination li. span.overlay').show();
			 $('.slideshowPagination li.active span.overlay').hide();
			 
			 if(currentPagination > count){
			 
				 paginate("next",currentPagination);
			 
			 } else if(currentPagination < count){
				 
				 paginate("prev",currentPagination);				 
				 
			 }
			 
			 
			 return false;
			 
		 })			 
			 
		   //Automate -------------------------------------------------------------------------------------------------------------------------		   
		   
		   function automate(){
			 
			 $('.slideshowControl li.play').hide();
			 $('.slideshowControl li.pause').show();
			 sequence = setInterval(function(){

				 if(options.slideshowType == "slide"){
					 
					 if(count >=  slideshowItems.length){
						 
						count = 1;
						 $('ul.slideshow').animate({"left": "0px"}, options.speed);
						 $('.slideshowPagination li').removeClass('active');
						 $('.slideshowPagination li.pagi-1').addClass('active');
						 $('.slideshowPagination li. span.overlay').show();
						 $('.slideshowPagination li.active span.overlay').hide();
						 
					} else {
						
						count = parseInt(count) + 1;
						 $('ul.slideshow').animate({"left": "-=" + imageWidth + "px"}, options.speed);
						 $('.slideshowPagination li').removeClass('active');
						 $('.slideshowPagination li.pagi-' + count).addClass('active');
						 $('.slideshowPagination li. span.overlay').show();
						 $('.slideshowPagination li.active span.overlay').hide();
					}
					 
			        } else {

					 if(count >=  slideshowItems.length){
						 
							 count = 1;
							 $('ul.slideshow li').hide();
							 $('ul.slideshow li.item-1').fadeIn(options.speed);	
							 $('.slideshowPagination li').removeClass('active');
							 $('.slideshowPagination li.pagi-1').addClass('active');
							 $('.slideshowPagination li. span.overlay').show();
							 $('.slideshowPagination li.active span.overlay').hide();
							 
						} else {
							
							 count = parseInt(count) + 1;
							 $('ul.slideshow li').hide();
							 $('ul.slideshow li.item-' + count).fadeIn(options.speed);	
							 $('.slideshowPagination li').removeClass('active');
							 $('.slideshowPagination li.pagi-' + count).addClass('active');
							 $('.slideshowPagination li. span.overlay').show();
							 $('.slideshowPagination li.active span.overlay').hide();
						}					 	
				 		
				 }
				 
				 
			 },options.autoDelay);  
			 			 
		   }	   
		   
		 
		 $('.slideshowControl li a').click( function(){
			 
			 if($(this).parent('li').attr('class') == "pause"){
				 
				 clearInterval(sequence);
				 
				 $('.slideshowControl li.pause').hide();				 
				 $('.slideshowControl li.play').show();
				 
			 } else {

				 automate();
				 
			 }
			 
			 return false;
			 
		 })
		 
		 if(options.autoStart == true){
			 
				automate();
		 
		 }		 
			 
	   
	   } //End initiate

		
	var slideshowImages = $('.slideshow li img').attr('src');
	   /*
					preLoadImages(slideshowImages,function(){
		   
		   			imageWidth = $('ul.slideshow li img:first').width();
		   			imageHeight = $('ul.slideshow li img:first').height();
		   			
		   		    initiate(); 					//Initiate slideShow
		   			
			     }
			);	   
            */
		   initiate();
	  });
	      
   };  
})(jQuery);






















