(function($){   
 $.fn.crodaGallery = function(settings) { 
 
 
if ((this).length == 0){ return; }

var defaults = {   
	thumbPercentage: 10,
	pause: 6000,
	allowThumbs: 1

}; 
  
var settings = $.extend(defaults, settings); 



var numImages = $("> img", this).size(); //count how many images there are
var imgCounter = 1;
var windowWidth = $(this).width(); //width of the gallery window
var windowHeight = $(this).height(); //height of the gallery window
var thumbWidth = windowWidth / settings.thumbPercentage;
var thumbHeight = windowHeight / settings.thumbPercentage;


$(this).css({"overflow" : "hidden", "position" : "relative"});//so the images can be positioned correctly. Can be done in css

$(this).prepend('<div id="thumbnailRack"></div>');//holds thumbnails

if(settings.allowThumbs == 1){
$("#thumbnailRack", this).css({"z-index" : numImages+10, "position":"absolute", "top": "0px"});

}

//$(this).after('<div id="progressbar"></div>');//will be removed
//$("#progressbar").progressbar({value: 0 });


$("> img", this).each(function(){
			var title = $(this).attr("rel");
			if (title == null){ title = imgCounter; }
			
			var url = $(this).attr("go");
			$(this).wrap('<a href="'+ url +'"></a>');
			
			
			$(this).css({"position" : "absolute", "border" : "none"});  
			$(this).attr("id", "galleryImage"+imgCounter);
			$(this).addClass("galleryImage");
			var imgSRC = $(this).attr("src"); //used to swap the image
			if(settings.allowThumbs == 1){					
			$("#thumbnailRack").append('<img id="'+imgCounter+'" class="galleryThumb" src="'+imgSRC+'" width="'+thumbWidth+'" height="'+thumbHeight+'"/>');
			}
			
			
			
			imgCounter++;					
							   
							   
							   
});

$('.galleryThumb').css({"cursor": "pointer", "margin" : "5px"})


$('.galleryImage').fadeTo(0, 0);
$('.galleryImage').css({"z-index": "0"})
$('#galleryImage1').fadeTo(0, 1); //initialize first images
$('#galleryImage1').css({"z-index": numImages})
imgCounter = 1; //reset image counter

var ticker = 0;

var pausedPlus = 1;






function spin(ticker){

	if(ticker < 100)
	{
		
		
	//$('#progressbar').progressbar('option', 'value', ticker);
	

	ticker = ticker + pausedPlus;
	
	setTimeout (function(){spin(ticker); }, settings.pause/100);
	
	
	} else {
		
		$('#galleryImage'+imgCounter).fadeTo(1000, 0);
		$('#galleryImage'+imgCounter).css({"z-index": "0"})
		imgCounter++;
		if (imgCounter > numImages){imgCounter = 1; }
		$('#galleryImage'+imgCounter).fadeTo(1000, 1);
		$('#galleryImage'+imgCounter).css({"z-index": numImages})
		
		ticker = 0;	
		spin(ticker);
	}
	
}





$(this).hover(
 function () {
       
		pausedPlus = 0;
		ticker = 0;
      }, 
      function () {
		
		
		pausedPlus = 1;
		ticker = 0;
      }
    );


$('.galleryThumb').click(function(){
								var meh = $(this).attr("id");
								  
								  $('#galleryImage'+imgCounter).fadeTo(700, 0);
								$('#galleryImage'+imgCounter).css({"z-index": "0"})
		imgCounter = meh;
		if (imgCounter > numImages){imgCounter = 1; }
		$('#galleryImage'+imgCounter).fadeTo(700, 1);
		$('#galleryImage'+imgCounter).css({"z-index": numImages})
								  });




spin(ticker);





 };   
})(jQuery);