
function Gallery(settings) {
	var currentImage = 1;
	var inTransition = false;	
	var config = settings || {};
	if (config.leftarrow == undefined) {
		config.leftarrow = "<";
	} else {
		// put the url in an image tag
		config.leftarrow = "<img src='"+settings.leftarrow+"'/>";
	}
 
	if (config.rightarrow == undefined) {
		config.rightarrow = ">";
	} else {
		config.rightarrow = "<img src='"+settings.rightarrow+"' border='0' alt='Right'/>";
	}
	
	// get all of the images in the gallery div
	var imageIDs = $('#gallery').find('img').map(function(){
                       return this.id;
                   }).get();
	var totalImages = imageIDs.length;
	
	var largeImageIDs = $('#gallery-large').find('img').map(function(){
                       return this.id;
                   }).get();
                   
	$(imageIDs).each(function(i) {
    	// hide everything except the current image
    	if ((i+1) != currentImage)
    		$('#'+imageIDs[i]).css("display", "none");
    	// get all the large images on the page, 
    	// if there are any add the click event
    	if (largeImageIDs.length > 0) {
	    	$('#'+imageIDs[i]).unbind('click').bind('click', function() {
				var largeImage = $(this).attr("id")+"_large";
				
				if ($('#'+largeImage).length) {
						// add a div to the screen width and height, 
					$('body').append("<div id='overlay'></div>");
					$('body').append("<div id='closeButton'><img src='/images/close-button.gif' alt=''></div>");
					$('body').append("<div id='largeImage'><img src='"+$('#'+largeImage).attr('src')+"' alt=''></div>");
					// set the position of the image and close button
					$('#largeImage').css('top', 100);
					$('#closeButton').css('top', 100);
					
					$('#closeButton').css('left', 476);
					// set the opacity of the button and image to 1
					$('#closeButton').unbind('click').bind('click', function() {
						$('#overlay').remove();
						$('#closeButton').remove();
						$('#largeImage').remove();
					});
				}
			});
		}
  	});
  	var arrowPosTop = ($('#'+imageIDs[0]).height()/2)-30;
	var arrowPosLeft = $('#'+imageIDs[0]).width()+20;
	// minus half the arrow height
	
	function updateCurrentImage(direction, cb) {
		// direction = direction || 'F';
		var firstImg = $('#'+imageIDs[currentImage-1]);
		if (direction == 'F') currentImage++;
		if (direction == 'B') currentImage--;
		if (currentImage > totalImages)	currentImage = 1;
		if (currentImage <= 0)	currentImage = totalImages;
		// update the arrows
		//if (currentImage > 1 && currentImage < totalImages) {
		//	$('#galleryLeftArrow').css("display", "inline");
		//	$('#galleryRightArrow').css("display", "inline");
		//} else if(currentImage == 1) { 
		//	$('#galleryLeftArrow').css("display", "none");
		//	$('#galleryRightArrow').css("display", "inline");
		//} else {
		//	$('#galleryLeftArrow').css("display", "inline");
		//	$('#galleryRightArrow').css("display", "none");
		//}
		var secondImg = $('#'+imageIDs[currentImage-1]);
		secondImg.css("display", "block");
		secondImg.css("z-index", "100");
		firstImg.css("z-index", "101");
		// put the second image under the first 
		if (direction == 'F') { 
			firstImg.hide('slide', {direction: 'left'}, 1000);
			secondImg.show('slide', {direction: 'right'}, 1000, function() {
				inTransition = false;
			});
		}
		if (direction == 'B') {
			firstImg.hide('slide', {direction: 'right'}, 1000);
			secondImg.show('slide', {direction: 'left'}, 1000, function() {
				inTransition = false;
			});
		}
		if (cb != undefined) {
			cb();
		}
	}

	// Add the arrows
	$('#gallery').append("<div id='galleryArrowHolder'></div>");
	$('#galleryArrowHolder').css("position", "absolute");
	$('#galleryArrowHolder').css("z-index", "99");
	$('#galleryArrowHolder').css("color", "white");
	$('#galleryArrowHolder').css("top", arrowPosTop+"px");
	$('#galleryArrowHolder').css("left", arrowPosLeft+"px");
	$('#galleryArrowHolder').width(100);
	//$('#galleryArrowHolder').append("<a href='' id='galleryLeftArrow' style='text-decoration: none; display: inline;'>"+config.leftarrow+"</a> ");
	$('#galleryArrowHolder').append("<a href='' id='galleryRightArrow' style='text-decoration: none; display: inline;'>"+config.rightarrow+"</a>");
	//$('#galleryLeftArrow').css("display", "none");
	updateCurrentImage();
	//$('#galleryLeftArrow').click(function() {
	//	if (!inTransition) {
	//		inTransition = true;
  	//		updateCurrentImage('B', function(){
  	//			inTransition = false;	
  	//		});
  	//	}
  	//	return false;
	//});
	$('#galleryRightArrow').click(function() {
  		if (inTransition == false) {
			inTransition = true;
  			updateCurrentImage('F');
  		}
  		return false;
	});
}



