// 	This script has been modified from its original state
//	(www.gcmingati.net/wordpress/wp-content/lab/jquery/imagestrip/imageslide-plugin.html)
//
// Instructions:
//	This script allows you to have 2 images in one slide. The image with a class of "FirstSlide" is what the user first see's,
// seconds later, the first slide fades out, and reveals the second slide. This can be used to make the effect of fading text.
// all adjustments should be editable in the settings bellow.


jQuery(function(){
   jQuery("div.stripViewer").prepend("<div class='ldrgif'>Loading...</div>"); 
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
	 settings = jQuery.extend({
		 // --- SETTINGS --- //
	     easeFunc: "easeInOutExpo",
	     easeTime: 750,
	     toolTip: false,
	     slideChangeTime: 8000,
	     slideFadeTime: 2000,
	     //# of times slideshow plays before pausing.
	     cycle: 3
  }, settings);
	return this.each(function(){
		var container = jQuery(this);
		container.find(".ldrgif").remove(); // removes the preloader
		var pictWidth = container.find("img").width();
		var pictHeight = container.find("img").height();
		var pictEls = container.find("li").size();
		var stripViewerWidth = pictWidth*pictEls;
		container.find("ul").css("width" , stripViewerWidth); //assegnamo la larghezza alla lista UL
		container.css("width" , pictWidth);
		container.css("height" , pictHeight);
		
		
		// find how many list items there are in order to
		// determin the amount of times the slideshow needs to cycle
		listTotal = 0;
		container.find("li").each( function() {
			listTotal++;
		});
		
		container.each(function(i) {
			
			//adds nav
			jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
			jQuery(this).find("li").each(function(n) {
					jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");												
			});
			
			//clicking nav
			jQuery("div#stripTransmitter" + j + " a").each(function(z) {
				jQuery(this).bind("click", function(){
								
					//this clears the auto rotate on click
					clearInterval(rotate);
					//stops first slide from fading back in on click
					clearTimeout(fadeInSlide1);
					//hides all first slides, so you see full text
					jQuery(".FirstSlide").fadeOut();
					
					//gives clicked item class of current, and moves 
					jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current");
					var cnt = -(pictWidth*z);
					container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
					return false;
				
				});	
			});
			
			// n counts the number of slides that have been animated
			// so that we stop at the last slide, after the chosen amount of
			// cycles.
			n = 0;
			// z represents the # slide you are on or clicking on.
			z = 0;
			fadeInSlide2(n, settings);
			// checks to see if we have cycled through each slide the number of times that has
			// been set.
			rotate = setInterval( function(){ 
				if (n < (listTotal * settings.cycle) - 1) {
					if ($(".stripTransmitter a.current").parent('li').next('li').length != 0) {
						z++;
						n++;
						var firstSlideButton = jQuery("div.stripTransmitter a.current").parent().next().find("a");
						changeSlide(firstSlideButton, pictWidth, z, settings);
						fadeInSlide2(z, settings);
					} else {
						$('#art ul li:last').prependTo('#art ul').addClass('clone');
						$('#art ul').css('left','0');
						clearTimeout(fadeInSlide1);
						z = 1;
						n++;
						var firstSlideButton = jQuery("div.stripTransmitter a:first");
						$('.FirstSlide').fadeIn();
						fadeInSlide2(z, settings);
						changeSlide(firstSlideButton, pictWidth, z, settings, function () {
							$("li.clone").appendTo('#art ul').removeClass('clone');							
							$('#art ul').css('left','0');
							z = 0;
						});
					}
				} 
				else
				{
					clearTimeout(rotate);
					clearTimeout(fadeInSlide1);
					clearTimeout(fadeInSlide2);
					$('.FirstSlide').hide();
				}
			}, settings.slideChangeTime);
						
			//jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
			jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
			if(settings.toolTip){
			container.next(".stripTransmitter ul").find("a").Tooltip({
				track: true,
				delay: 0,
				showURL: false,
				showBody: false
				});
			}
			
		});
	j++;
  });	
};


function changeSlide(myThis, pictWidth, z, settings, callBack) {
	jQuery(myThis).addClass("current").parent().parent().find("a").not(jQuery(myThis)).removeClass("current"); // wow!
	var cnt = - (pictWidth*z);
	jQuery(myThis).parent().parent().parent().prev().find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc, callBack);
	return false;
}


function fadeInSlide2(z, settings) {
	fadeInslide2 = setTimeout( function(){
		$('.FirstSlide').fadeOut();
	}, settings.slideFadeTime);

	if (n < ((listTotal + 1) * settings.cycle)) {
		fadeInSlide1 = setTimeout( function(){
			$('.FirstSlide').fadeIn();
		}, settings.slideChangeTime);
	}
}

