jQuery(function(){
   jQuery("div.svw").prepend("<img src='http://www.gcmingati.net/wordpress/wp-content/uploads/svwloader.gif' class='ldrgif' alt='loading...'/ >"); 
});
var j = 0;
jQuery.fn.slideView = function(settings) {
	  settings = jQuery.extend({
     easeFunc: "easeInOutExpo", /* <-- easing function names changed in jquery.easing.1.2.js */
     easeTime: 750,
     toolTip: false,
     automate: false, //MBH - add settings to automatically switch between slides
     autodelay: 5000  //MBH - default to a 5 second delay
  }, settings);
	return this.each(function(){
		var container = jQuery(this);
		container.find("img.ldrgif").remove(); // removes the preloader gif
		container.removeClass("svw").addClass("stripViewer");		
		var pictWidth = container.find("li").find("img").width();
		var pictHeight = container.find("li").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);
		container.wrap("<div class='stripContainer' id='stripContainer" + j + "'></div>");  //MBH - add outer container div around viewer and transmitter
		container.each(function(i) {
			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='#'>"+jQuery(this).find("img").attr("alt")+"<\/a><\/li>");
						settings.slideCount=n; //MBH - count the slides
				});
			jQuery("div#stripTransmitter" + j + " a").each(function(z) {
				jQuery(this).bind("click", function(){
				jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
				var cnt = - (pictWidth*z);
				jQuery(this).parent().parent().parent().prev().find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				return false;
				   });
				});
			jQuery("div#stripTransmitter" + j).css("width" , "600px");//set width because ie6 did not like the pictWidth with our lay out.  I'm sure this could be coded dynamically... will do if I have time.
			jQuery("div#stripTransmitter" + j + " a:eq(0)").addClass("current");
			if(settings.toolTip){
				container.next(".stripTransmitter ul").find("a").Tooltip({
					track: true,
					delay: 0,
					showURL: false,
					showBody: false
				});
			}
		});
		if(settings.automate != false) {  //MBH - add automation
			var rnd = function(){
				return (Math.random() >= 0.3)?((Math.random() >= 0.5)?1:-1):((Math.random() >= 0.5)?1:-1)*2;
			};
			container.each(function(i){
				var func = function(){
					var root = jQuery(this);
					var tid = root.find('.stripTransmitter').attr('id');
					root.toggleClass("auto");
			        if(root.hasClass("auto")) {
			        	// make the viewer move by itself
			        	settings.timerId = setInterval( function() {
			        			// select the next active item from the transmitter strip
			        			var next, offset, current;
		        				current = (settings.current != undefined)?settings.current:0;
			        			if(settings.automate == "random") {
			        				offset = rnd(); // randomly chosen from ( -2, -1, 1, 2 )	        				
			        			} else if(settings.automate == "linear") {
			        				offset = 1;
			        			} else if(settings.automate == "rebound") {
			        				offset = (settings.offset != undefined)?settings.offset:-1;
			        				repeat = (settings.repeat != undefined)?settings.repeat:false;
			        				if (repeat==true) {
			        					settings.repeat = false;
			        					offset = -offset;
			        				}
			        				settings.offset = offset;
			        				if ((current == 0 || current == settings.slideCount)&& repeat==false) {
			        					settings.repeat = true;
			        					offset = 0;
			        				}
			        			}
		        				offset = (offset+current > settings.slideCount)?offset+current-settings.slideCount-1:offset+current; //wrap if new offset greater than count
		        				offset = (offset < 0)?settings.slideCount+offset:offset; //wrap if new offset less than 0
		        				next = jQuery("div#"+tid).find('a').eq(offset);
			        			// activate it
			        			next.addClass("current").parent().parent().find("a").not(next).removeClass("current");
			    				var cnt = - (pictWidth*offset);
			    				next.parent().parent().parent().prev().find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
//			        			alert('current:'+current+'  next:'+offset+'  of:'+settings.slideCount);
			        			settings.current = offset;
				        	},
				        	settings.autodelay
			        	);
			        	
			        } else {
			        	if(settings.timerId != undefined){
			        		clearInterval(settings.timerId);
			        	}
			        }
			    };  // end of func();
			    var root = jQuery(this).parent();
				var tid = root.find('.stripTransmitter').attr('id');
				root.bind("mouseover", func);
				root.bind("mouseout", func);
				root.toggleClass("auto");
		        if(jQuery(this).parent().hasClass("auto")) {
		        	// make the viewer move by itself
		        	settings.timerId = setInterval( function() {
		        			// select the next active item from the transmitter strip
		        			var next, offset, current;
	        				current = (settings.current != undefined)?settings.current:0;
		        			if(settings.automate == "random") {
		        				offset = rnd(); // randomly chosen from ( -2, -1, 1, 2 )	        				
		        			} else if(settings.automate == "linear") {
		        				offset = 1;
		        			} else if(settings.automate == "rebound") {
		        				offset = (settings.offset != undefined)?settings.offset:-1;
		        				repeat = (settings.repeat != undefined)?settings.repeat:false;
		        				if (repeat==true) {
		        					settings.repeat = false;
		        					offset = -offset;
		        				}
		        				settings.offset = offset;
		        				if ((current == 0 || current == settings.slideCount)&& repeat==false) {
		        					settings.repeat = true;
		        					offset = 0;
		        				}
		        			}
	        				offset = (offset+current > settings.slideCount)?offset+current-settings.slideCount:offset+current; //wrap if new offset greater than count
	        				offset = (offset < 0)?settings.slideCount+offset:offset; //wrap if new offset less than 0
	        				next = jQuery("div#"+tid).find('a').eq(offset);
		        			// activate it
		        			next.addClass("current").parent().parent().find("a").not(next).removeClass("current");
		    				var cnt = - (pictWidth*offset);
		    				next.parent().parent().parent().prev().find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
		        			settings.current = offset;
			        	},
			        	settings.autodelay
		        	);	
		        }
			});
		}
		j++;
  });	
};
