function photoViewer(links) {
	var ie = false;
	var vers = 0;
	if(jQuery.browser.msie) {
		ie = true;
		vers = parseInt(jQuery.browser.version);
	}
	
	// LightBox thumbnail viewer style variables
	
	var thumbMaxSize = 60;	// In pixels. Sets the LightBox Thumbnails' max height/width
	var visibleThumbs = 8;	// How many thumbnails will be visible in each frame
	var sliderWidth = (ie === true && vers == 6 ? 712 : 712);	// In pixels. Set this to the width of the visible area of the thumbnail slider
	var lbWidth = 860;		// In pixels. Overrides the math for determining the width of the lightbox.
	
	// LightBox HTML elements
	
	var overlay = $(jQuery('<div id="photoviewer_overlay" style="display: none"></div>'));
	var lightbox = $(jQuery('<div id="photoviewer" style="display: none"></div>'));
	var closeBtn = $(jQuery('<a href="#close" class="photoviewer_close">&times; Close</a>'));
	var target = $(jQuery('<div id="photoviewer_target"></div>'));
	
	// Zoom-in/out
	
	// Slider
	var slider = $(jQuery('<div id="photoviewer_slider" />'));
	var sliderPrevNext = $(jQuery('<ul id="photoviewer_slider_nav"></ul>'));
	var sliderPrev = $(jQuery('<li class="prev">&laquo; Previous</li>'));
	var sliderNext = $(jQuery('<li class="next">Next &raquo;</li>'));
	var sliderInner = $(jQuery('<div id="photoviewer_slider_inner" />'));
	var sliderMask = $(jQuery('<div id="photoviewer_slider_mask" />'));
	var thumbList = $(jQuery('<ul id="photoviewer_slider_thumbs"></ul>'));
	
	// Slider Global variables
	var selectedThumb = null;
	var sliderCurrentView = 0;
	var clickedDir = 0;
	var slideCount = null;
	var slideIncrement = 1;
	var t = null;
	var resizeTimer = null;
	var mouseInside = false;
	var linkPos = null;
	var linkHeight = null;
	var linkWidth = null;
	var img = null;
	var imgOrigHeight = null;
	var imgOrigWidth = null;
	var browserWidth = null;
	var browserHeight = null;
	var url = null;
	var cssTop = null;
	var cssLeft = null;
	var targetPadding = new Array();
	var frameNum = null;
	
	// General: Cache background images in Internet Explorer 6
	// Used to prevent flickering background-images attached to links
		
	// Construct HTML
	$('body').append(overlay).append(lightbox);
	lightbox.append(closeBtn).append(target);
	if (links.size() > 1) {
		lightbox.append(slider);
		slider.append(sliderInner);
		sliderInner.append(sliderMask);
		sliderMask.append(thumbList);
	}
	
	lightbox.show().css({'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - lightbox.outerHeight()) / 2) + 'px', 'left': Math.round(($(window).width() - lightbox.outerWidth()) / 2) + 'px', 'margin-top': 0, 'margin-left': 0}).hide();
	
	// Lightbox: Set slider to 0 opacity to hide it
	// Used as alternative to "display:none;" so the element remains in the DOM
	
	slider.css('opacity', 0);
	if (ie=== false) lightbox.css('opacity', 0);
	
	// Lightbox: Detect if the cursor is the Lightbox
	// Used to determine if click is outside lightbox
	
	lightbox.hover(function(){ 
		mouseInside=true; 
	}, function(){ 
		mouseInside=false; 
	});
	
	// Lightbox: Handle the "Close" events
	
	// Close when ".close" button is pressed
	closeBtn.click(function(c) {
		c.preventDefault();
		LBClose();
	});
	
	// Close when click outside "#lightbox" is detected
	$('body').mouseup(function() {
		if (!mouseInside) {
			LBClose();
		}
	});
	
	// Lightbox: Perform the close event
	
	function LBClose() {
		if (lightbox.is(':visible')) {
			overlay.add(lightbox).add(closeBtn).fadeOut('normal');
			slider.css('opacity', 0);
			if (ie === true && vers != 8) lightbox.css('opacity', 0);
			selectedThumb.removeClass('selected');
			lightbox.css({'height':0 + 'px', 'width':0 + 'px', 'overflow':'hidden', 'border-width':'0px'});
			target.contents().remove();
			target.css('height','auto').css('width','auto');
			slider.css('display','block');
		}
	}

	// Function for looping through the links, attaching the event listener, and building the thumbnail list
	links.each(
		function(index) {
			var link = $(this);
			
			// Get the dimensions of the image belonging to "link" for resizing of LightBox thumbnails
			var thisThumb = $("body a[href$=" + link.attr('href') + "] img");
			var thumbSrc = thisThumb.attr('src');
			if (thisThumb.width() > thumbMaxSize || thisThumb.height() > thumbMaxSize) {			
				if (thisThumb.height() > thisThumb.width()) {
					var h = thumbMaxSize;
					var w = thumbMaxSize * (thisThumb.width() / thisThumb.height());
				} else {
					var w = thumbMaxSize;
					var h = thumbMaxSize * (thisThumb.height() / thisThumb.width());
				}
			}

			link.click(
				function(c) {
					if($(this).parent().hasClass('selected')) return false;
					frameNum = Math.ceil(slideCount / visibleThumbs);
					target.removeClass('zoomIn');
					closeBtn.fadeOut('normal');
					target.css('height','auto').css('width','auto');
					
					// Select the clicked anchor's corresponding thumbnail from the lightbox list, select it's parent LI
					selectedThumb = $('#photoviewer_slider_thumbs li a[href$=' + link.attr('href') + ']').parent();
					
					if (slideCount > visibleThumbs) {
					
						// Find the index of the selected LI
						var num = selectedThumb.parent().children().index(selectedThumb);
						//alert(' '+num);
						
						// Calculate which section of thumbs the LI is part of and slide to it is necessary
						if (num < slideCount) {
							slideIncrement = Math.floor((index) / (visibleThumbs));
							animateSlide(slideIncrement,frameNum);
						}
					}
					
					// Get the details about the clicked anchor's size and position
					// Used to set the lightbox position before open animation
					linkPos = link.children().offset();
					linkWidth = link.children().innerWidth();
					linkHeight = link.children().innerHeight();
					
					// Prevent redirect to linked image
					c.preventDefault();
					
					// Open the selected image in the lightbox
					open(link.attr('href'));
					
					// Remove selected state of last image thumbnail
					$("li.selected", thumbList).removeClass('selected');
					
					// Attach selected state to new thumbnail
					selectedThumb.addClass('selected');
				}
			);
			link.attr({'lb-position': index});
			var matching = $("a[href$=" + link.attr('href') + "]", thumbList);
			if(matching.size() < 1) {
				$("a[href$=" + link.attr('href') + "]:first:not(#photoviewer_slider_thumbs a)").clone(true).appendTo('<li />').parent().appendTo(thumbList);
				$('li a[href$=' + link.attr('href') + ']', thumbList).append('<span class="dart"></span>')
			}
			
			slideCount = thumbList.children().size();
			
			// Resize LightBox Thumbnails
			$("li a[href$=" + link.attr('href') + "] img", thumbList).css({ 'height': h + 'px', 'width': w + 'px', 'margin-top':(65 - h)/2 + 'px'});
			
			if (slideCount > visibleThumbs) {
				// Set the slider mask width to the combined width of all slides
				sliderMask.css('width', sliderWidth + 'px');
				
				// Insert the Slider Navigation into the DOM
				sliderInner.prepend(sliderPrevNext);
				sliderPrevNext.append(sliderPrev);
				sliderPrevNext.append(sliderNext);
			}
		}
	);
  
  	// Lightbox: Determine if lightbox should open or just swap the image
  	
	function open(url) {
		if (lightbox.is(':visible')) {
			target.contents().fadeOut('normal', function() {
				target.contents().remove();
				loadImage(url);
			});
		} else {
			target.children().remove();
			
			// Get current browser window dimensions
			browserWidth = $(window).width();
			browserHeight = $(window).height();
			
			// Calculate the difference between top top of the browser window and the top of the document
			var scrollTop = $(window).scrollTop();
			
			// Set the top and left position value
			if(ie===true && vers==6) {
				cssTop = parseInt((linkHeight / 2) - (lightbox.height() / 2) + linkPos.top);
			} else {
				cssTop = parseInt((linkHeight / 2) - (lightbox.height() / 2) - scrollTop + linkPos.top);
			}
			cssLeft = parseInt((linkWidth / 2) - (lightbox.width() / 2) + linkPos.left);
			
			// If a generated click event, center the lightbox for zooming
			if (linkPos.top == scrollTop && linkPos.left == 0) {
				cssTop = parseInt((browserHeight / 2) - (lightbox.height() / 2));
				cssLeft = parseInt((browserWidth / 2) - (lightbox.width() / 2));
			}
			
			// Position the lightbox to zoom from the clicked anchor
			lightbox.css('top', cssTop + 'px').css('left', cssLeft + 'px');
			
			overlay.css({'width':'100%', 'opacity':'0.8', 'height':$('body').height()});
			overlay.add(lightbox).fadeIn('normal',function(){
				loadImage(url);
			});
		}
	}
	
	// Lightbox: Load the image
	
	function loadImage(url) {	
		if (lightbox.is('.loading')) { return; }
		lightbox.addClass('loading');
		img = new Image();
		img.onload = function() {
			target.contents().css('display', 'none');
			
			// Store the current image and browser dimensions
			// Used for dynamic resizing of the lightbox when the browser is resized
			imgOrigHeight = img.height;
			imgOrigWidth = img.width;
			
			// Resize the image
			imageResize(url);			
		}
		img.src = url;
		$(img).attr('id', 'LBMainImg');
	}
	
	// Lightbox: Resize image
	
	function imageResize(url) {
		var newHeight = img.height;
		var newWidth = img.width;
		
		// Set the maximum image dimensions for the current broweser window size
		var maxWidth = (($(window).width() > window.innerWidth ? window.innerWidth : $(window).width()) - parseInt(lightbox.css('padding-left'),10) - parseInt(lightbox.css('padding-right'), 10) - parseInt(target.css('padding-left'), 10) - parseInt(target.css('padding-right'), 10)) - 100;
		
		var maxHeight = ((parseInt($(window).height() - slider.height()) > parseInt(window.innerHeight - slider.height()) ? parseInt(window.innerHeight - slider.height()) : parseInt($(window).height() - slider.height())) - parseInt(lightbox.css('padding-top'),10) - parseInt(lightbox.css('padding-bottom'), 10) - parseInt(target.css('padding-top'), 10) - parseInt(target.css('padding-bottom'), 10)) - 100;
		
		// Proportionally resize image to fit within useable window space
		if( imgOrigWidth > maxWidth || imgOrigHeight > maxHeight ) { // One of these is larger than the window
			var ratio = imgOrigWidth / imgOrigHeight;
			if(imgOrigHeight > imgOrigWidth) {
				if (imgOrigHeight >= maxHeight) {
					newHeight = maxHeight;
					newWidth = maxHeight * ratio;
				}
			} else {
				newWidth = maxWidth;
				newHeight = maxWidth / ratio;
			}
			if (newHeight >= maxHeight) {
				newHeight = maxHeight;
				newWidth = maxHeight * ratio;
			}
			if (newWidth >= maxWidth) {
				newWidth = maxWidth;
				newHeight = maxWidth / ratio;
			}
		}
		
		// If the current browser window dimensions are smaller than the stored window dimensions, resize the image now
		// Used to keep the image from resizing after the lightbox window resizes
		if (browserWidth > $(window).width() || browserHeight > $(window).height()) {
			img.height = newHeight;
			img.width = newWidth;
		}
				
		var height = parseInt(newHeight + slider.height() + parseInt(target.css('padding-top'), 10) + parseInt(target.css('padding-bottom'), 10));
		var top = parseInt((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - newHeight - slider.height() - parseInt(lightbox.css('padding-top'),10) - parseInt(lightbox.css('padding-bottom'),10) - parseInt(target.css('padding-top'), 10) - parseInt(target.css('padding-bottom'), 10)) / 2) + 'px';
		if(ie===true && vers==6) {
			top = $(window).scrollTop() + parseInt(top);
			alert(top);
		}
		var left = parseInt(($(window).width() - lbWidth - parseInt(lightbox.css('padding-left'),10) - parseInt(lightbox.css('padding-right'),10)) / 2) + 'px';
		
		// Set animation parameters 
		var LBAnimParams = {'width': lbWidth,'height': height, 'top': top, 'left': left, 'border-width':'10px', opacity: 1.0 };
		
		// override the pervious animation parameters for Internet Explorer 8
		if (ie === true) {
			var LBAnimParams = {'width': lbWidth,'height': height, 'top': top, 'left': left, 'border-width':'10px' };
		}
		
		lightbox.animate(LBAnimParams,'normal', function(){									
			lightbox.css('overflow', 'visible');
			
			// Resize image
			img.height = newHeight;
			img.width = newWidth;	
			
			// Verify that the lightbox is loading an image before appending any elements to the lightbox
			// Used to prevent re-appending the current image on browser window resize
			if (lightbox.is('.loading')) {
				// insert image into the lightbox
				if($('#LBMainImg')) $(img).remove();
				target.append($(img));
				
				target.contents().fadeIn('normal', function() {
					lightbox.removeClass('loading');
				});
				
				// Decide if slider should be full-width or sized to fit the available images
				if (visibleThumbs < slideCount) {
					
					// Resize the slide container to fit all slides
					thumbList.css('width',parseInt(($('li', thumbList).outerWidth(true))*slideCount) + 'px');
				} else {
					
					// Resize the slider inner-container to fit the slides
					sliderInner.css('width', parseInt($('li', thumbList).outerWidth(true)*slideCount) + 'px');
				}
				
				// Fade-in the slider
				slider.fadeTo('slow', 1.0);
				
				// reveal the close button
				closeBtn.css('display','block');
			} else {
				$(img).css('top','auto').css('left','auto').css('cursor','inherit').removeClass('ui-draggable');
				target.css('width','auto').css('height','auto');
				slider.add(closeBtn).add($(img)).fadeIn('normal');	
			}
			
			var LBMainPad = parseInt(target.css('padding-top'));
			if(LBMainPad < 0) LBMainPad = 0;
			$(img).css('padding-top', LBMainPad + 'px');

			// Store current browser window dimensions
			browserWidth = $(window).width();
			browserHeight = $(window).height();
		});
	}
	
	// Lightbox: Select the next set of slides and slide them into view
	
	function selectAndSlide() {		
		
		if (!clickedDir) clickedDir = $("#photoviewer_slider_nav li").index(this);
		
		var nextView = (sliderCurrentView - 1);
		if (clickedDir) {
			nextView = (sliderCurrentView + 1);
		}
		if (nextView <= frameNum - 1 && nextView >= 0) {
			// slide to the new panel
			animateSlide(nextView, frameNum);
		}
		clickedDir = 0;
	}
	
	// Lightbox: Perform the slide movement
	function animateSlide(n,f) {
		if (n == 0) {
			sliderPrev.removeClass('hover').css({'opacity':0.3, 'cursor':'default'});
		} else sliderPrev.css({'opacity':1.0, 'cursor':'pointer'});
		
		if (n == f - 1) {
			sliderNext.removeClass('hover').css({'opacity':0.3, 'cursor':'default'});
		} else sliderNext.css({'opacity':1.0, 'cursor':'pointer'});
		
		sliderCurrentView = n;
		thumbList.animate({left:"-"+ Math.round(n*sliderWidth)+"px"});
	}
	
	// LightBox: Listen for Window resize
	$(window).bind('resize', function(){
		if (lightbox.is(':visible')) {
			
			// Pause resize to help alleviate jerky animation when resizing
			if (resizeTimer) clearTimeout(resizeTimer);
			resizeTimer = setTimeout(imageResize, 500);
		}
	});
	
	// General: Adds "hover" class to elements
	// Used to simulate :hover in Internet Explorer
	function hoverClass() {
		if ($(this).css('opacity') < 1) return;
		$(this).toggleClass('hover');
	}
	
	$("#photoviewer_slider_nav .prev, #photoviewer_slider_nav .next").click(selectAndSlide);
	$("#photoviewer_slider_nav .prev, #photoviewer_slider_nav .next").hover(hoverClass,hoverClass);

}
