$.fn.gallery = function(options) {
				
	var	defaults = {
		speed:1,
		auto:false,
		maxWidthSetting:false,	//receives integer value - if set, will resize images proportionately when greater than this setting
		forceWidth:false,		//true/false value - if true, will force the maxWidthSetting option regardless of original image size (scales up and down)
		dontForceGal:false		//true will not set a width to the gallery container (default/false will set to first image or maxWidthSetting width
	};
    var options = $.extend(defaults, options);
    
    return this.each(function() {
    	var $gal = $(this);
    	var $photos = $gal.children();
    	var totalphotos = $photos.children().get().length;
    	
		if(!options.dontForceGal){ $gal.css("width",((options.maxWidthSetting)?options.maxWidthSetting:$photos.children(".imgSlide:first").children("img").attr("width"))); }
    	
    	var $galtop = $(document.createElement("div"))
    				.addClass("galtop")
    				.html("<!-- -->");
    				
    	$gal.prepend($galtop);
    	
    	if(totalphotos > 1 && !options.auto){
    		var $galbar = $(document.createElement("div"))
    					.attr("id","galbar")
    					.addClass("gal_bar");
    					
    		$gal.append($galbar);
    		
    		var $prev = $(document.createElement("div"))
    					.addClass("gal_button_left")
    					.html("back");
    					
    		var $next = $(document.createElement("div"))
    					.addClass("gal_button_right")
    					.html("next");
    					
    		var $counter = $(document.createElement("div"))
    					.addClass("gal_counter");
    					
    		$galbar.append($prev).append($next).append($counter);
    		
    		var $counter1 = $(document.createElement("span"))
    					.html("Image&nbsp;");
    					
    		var $galcur = $(document.createElement("span"))
    					.attr("id","galcur")
    					.html("0");
    					
    		var $counter3 = $(document.createElement("span"))
    					.html("&nbsp;of&nbsp;");
    					
    		var $galtotal = $(document.createElement("span"))
    					.attr("id","galtotal")
    					.html(totalphotos);
    					
    		$counter.append($counter1).append($galcur).append($counter3).append($galtotal);
    	} else {
			if(options.forceWidth || $("img", this).attr("width")>options.maxWidthSetting){
				var	conversionRatio = options.maxWidthSetting/$("img", this).attr("width"),
					newHeight = $("img", this).attr("height")*conversionRatio;
				$("img", this)
					.attr("width",options.maxWidthSetting)
					.attr("height",newHeight);
			}		
		}
    	
    	var $caption = $(document.createElement("div"))
    				.addClass("gal_caption");

		//NOTE: changes made to utilize an image wrapper div, allowing for more CSS control of the display (centering, etc - changes made in common_vb.asp)
    	if($photos.children(".imgSlide:first").children("img").attr("alt") == ""){
    		$caption.css({"display":"none"});
    	}else{
    		$caption.html($photos.children(".imgSlide:first").children("img").attr("alt"));
    	}
    	
    	var $galbtm = $(document.createElement("div"))
    				.addClass("galbtm")
    				.html("<!-- -->");
    	
    	$gal.append($caption).append($galbtm);
    	
    	var to = 0;
    	if(options.auto){
    		to = 4000;
    	}
    	
    	var opts = {
    	    timeout:       to,  // milliseconds between slide transitions (0 to disable auto advance) 
    	    speed:         options.speed,  // speed of the transition (any valid fx speed value) 
    	    next:          $next,  // id of element to use as click trigger for next slide 
    	    prev:          $prev,  // id of element to use as click trigger for previous slide 
    	    before:        onBefore,  // transition callback (scope set to element to be shown) 
    	    after:         onAfter,  // transition callback (scope set to element that was shown) 
    	    height:       'auto', // container height 
    	    sync:          1,     // true if in/out transitions should occur simultaneously 
    	    fit:           1,     // force slides to fit container 
    	    pause:         0,     // true to enable "pause on hover" 
    	    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    	    slideExpr:     null  // expression for selecting slides (if something other than all children is required) 
    	};
    	$photos.cycle(opts);
    	
		//changes in functions below to utilize image wrapper div
    	function onBefore(){
			//edit: 	if forcing image width, or image is greater than a given max width setting, we
			//			figure out what the resizing ratio is and apply that to the height of the image
			if(options.forceWidth || $("img", this).attr("width")>options.maxWidthSetting){
				var	conversionRatio = options.maxWidthSetting/$("img", this).attr("width"),
					newHeight = $("img", this).attr("height")*conversionRatio;
				$("img", this)
					.attr("width",options.maxWidthSetting)
					.attr("height",newHeight);
			}

    	    $photos.css({"height":$(this).height()});
    	}
    	
    	function onAfter(){
    	    if($("img", this).attr("alt").length > 0){
    			$caption.html($("img", this).attr("alt")).css({"display":"block"});
    		}else{
    			$caption.html("").css({"display":"none"});
    		}
    	    
    	    if(totalphotos > 1 && !options.auto){
    	    	var ci = parseInt($galcur.html());
    	    	var ti = parseInt($galtotal.html());
    	    	if(ci == ti){
    	    		ci = 0;
    	   		}
    	    
    	    	$galcur.html(++ci);
    	    }

			//need to resize in case the new current image causes the page to become taller, extending outside the iframe (if no pdGlobal, we're on a custom)
			if(typeof(pdGlobal)=="undefined"){ willow.ReSizeIFRAME(); }
    	}
    });
};




