-//--------------------------------------------------------------------
//Customisable Automated Slideshow function --------------------------
//--------------------------------------------------------------------

(function($){  
   $.fn.slideShow = function(options) {  
	   
	  var defaults = {  
			   slideShowType: "slide", 			  
			   showControl: true,   
			   showNavigation: true,
			   showPagination: true,
			   autoStart: true
			   };  
	 
	  var options = $.extend(defaults, options);
	   
	   return this.each(function() { 
		  
		   var obj = $(this);
		   var currentItem = 0;
		   var timer;
		   
		   
		   //Initiate ------------------------------------------------
		   
		   function initiate(){
			   
			   $(obj).wrap("<div class='slideshowWrapper' />") 
			   
			   $('.slideshowWrapper').append("<ul class='slideShowControl'><li id='play'><a href='#'><span>Play</span></a></li><li id='pause'><a href='#'><span>Pause</span></a></li></ul>");
			   $('.slideshowWrapper').append('<ul class="slideShowNavigation"><li id="next"><img src="/Assets/disableNav_tcm13-1592.gif" class="disableNavigation" /><a href="#"><span>Next</span></a></li><li id="prev"><img src="/Assets/disableNav_tcm13-1592.gif" class="disableNavigation" /><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
			   
			   var imageWidth = $(slideShowItems[0]).children('a').children('img').width(); // slideshow image width
			   var imageHeight = $(slideShowItems[0]).children('a').children('img').height(); // slideshow image height
			   
			   $(slideShowItems).each( function(i){
				   
				   $(this).addClass("item-" + i);
				   
				   $('.slideShowPagination').append("<li id='pagi-" + i + "'><a href='#'>" + (i + 1) + "<span> of " + slideShowItems.length + "</span></a></li>");
				   $('.slideShowPagination li:first').addClass('active');
			   })
			   
			 
				if(options.slideShowType == "slide"){ //If type is slide
				
					
					$(obj).addClass("slide");
					
					var sliderLength = imageWidth * slideShowItems.length;
				   
					$('ul.slideshow').width(sliderLength)					
				
					
				} else if(options.slideShowType == "fade"){ //If type is fade
				
					$('ul.slideshow').height(imageHeight);
					
					$(obj).addClass("fade");
					$(obj).children('li').hide();
					$(obj).children('li:first').show();
					
					
				}

			   //Paginate --------------------------------------------------------------			   
			   
			   function paginate(){
				   
				   //alert("paginate");
				   
				   $('.slideShowPagination li').removeClass('active');
				   $('.slideShowPagination li#pagi-' + currentItem).addClass('active');		      
				   
			   }
			   
			   
			   //Move(motion/transition) ------------------------------------------------			   
			   
			   function move(showType, trigger){
				   
				   if(showType == "slide"){
					   
					   var p = $('.slideshow').position();
					   
					   if(trigger == "next"){
							
							if(p.left == -(imageWidth * slideShowItems.length - (imageWidth + 20)) || p.left == -(imageWidth * slideShowItems.length - (imageWidth + 21))){
								$('.slideshow').animate({"left": "0px"}, "fast");

							} else {

								$('.slideshow').animate({"left": "-=" + imageWidth + "px"}, "slow");	
							}
							
							if(currentItem == slideShowItems.length -1){
						
								
								currentItem = 0;
							
							} else {
								
								currentItem++;								
								
							}						
							
							
							paginate();
							
							
						} else if(trigger == "prev"){
							
							if(	p.left == 20){
								
								$('.slideshow').animate({"left": -(imageWidth * slideShowItems.length - imageWidth) + "px"}, "fast");				
							
							} else {
								
								$('.slideshow').animate({"left": "+=" + imageWidth + "px"}, "slow");	
								
							}
							
							if(currentItem == 0){
								
								currentItem = slideShowItems.length - 1;
							
							} else {
								
								currentItem--;								
								
							}

							paginate();
							
						} else if(trigger == "slideShowPagination"){
							
							var activePag = $('.slideShowPagination li.active').attr('id');
							
							showItem = activePag.substring(activePag.length - 1);
							
							currentItem = showItem;
							
							//alert(showItem);

								$('.slideshow').animate({"left": "-" + (imageWidth * showItem) + "px"}, "slow");

								paginate();
						}			   
					   
				   } else if(showType == "fade"){
					   
					   if(trigger == "next"){

						   var visibleItem = $(obj).children('li:visible').attr('class');
						   var current = visibleItem.substring(visibleItem.length - 1)
						   var next = parseInt(current) + 1;
						   
						   if(next == slideShowItems.length){

							   next = 0;

						   }

						   $(obj).children('li').hide();
						   $(obj).children('li.item-' + next).fadeIn(3000);							   					   
						   
							if(currentItem == slideShowItems.length -1){
						
								
								currentItem = 0;
							
							} else {
								
								currentItem++;								
								
							}
							
							paginate();
							
						   
						} else if(trigger == "prev"){
					   
							   var visibleItem = $(obj).children('li:visible').attr('class');
							   var current = visibleItem.substring(visibleItem.length - 1)
							   var prev = parseInt(current) - 1;
							   
							   if(prev == -1){

								   prev = slideShowItems.length - 1;

							   }

							   $(obj).children('li').hide();
							   $(obj).children('li.item-' + prev).fadeIn(3000);	

						  
								if(currentItem == 0){
									
									currentItem = slideShowItems.length - 1;
								
								} else {
									
									currentItem--;								
									
								}
								
								paginate();
									
						   
						} else if(trigger == "slideShowPagination"){
							
							var activePag = $('.slideShowPagination li.active').attr('id');
							
							showItem = activePag.substring(activePag.length - 1);
							
							currentItem = showItem;
							
							$(obj).children('li').hide();
						    $(obj).children('li.item-' + showItem).fadeIn(3000);						
							
						    paginate();
						}	
					   
				   }
				   
			   }
			   
			   //Clicks -----------------------------------------------------------------
			   
			   $('.slideShowControl li a, .slideShowNavigation li a, .slideShowPagination li a').click( function(){
				   
				   if($(this).parent('li').parent('ul').attr('class') == "slideShowControl"){

					   if($(this).parent('li').attr('id') == "play"){
					   
						   if(currentItem != 0){
							   $('#next a').click();
						   }
						   
						   timer = setInterval("$('#next a').click()",8000);
						   
						   $('#play').hide();
						   $('#pause').show();
					   
					   } else {

						   clearInterval(timer);
						   $('#pause').hide();
						   $('#play').show();
						   
					   }
					   
				   } else if($(this).parent('li').parent('ul').attr('class') == "slideShowNavigation"){

					   move(options.slideShowType, $(this).parent('li').attr('id'));
					   
					   $('.disableNavigation').show();
					   setTimeout("$('.disableNavigation').hide()",2000);
					    
				   } else if($(this).parent('li').parent('ol').attr('class') == "slideShowPagination"){

					   $('ol.slideShowPagination li').removeClass('active');
					   $(this).parent('li').addClass('active');
					   
					   clearInterval(timer);
					   $('#pause').hide();
					   $('#play').show();					   
					   
					   move(options.slideShowType, "slideShowPagination");				   
					   
				   }
				   
				   return false;
				   
			   });
			   
			   
		   
		   }
		   
		   initiate(); 										//Initiate slideShow
		   if(options.autoStart == true){
			   $('.slideShowControl li#play a').click();	//Start automation
		   }   
	  });  
   };  
})(jQuery);  



