			(function(jq) {
				jq(document).ready(function(){
					
					/*
					** Carousel
					*/
					
					// Get number of items
					var a = jq("#carousel ol > li").size();
					jq(".totalImages").html(a);
					// Get width of each item
					var b = jq("#carousel ol > li").css("width");
					b = parseFloat(b, 10);
					// Get width of buttons
					var c = jq("#carousel .caButton").css("width");
					c = parseFloat(c, 10);				
					
					// Set #carousel to width of 6 items + 2 buttons
					jq("#carousel").css("width",(b * 6) + (c * 2));
					// Set #carousel list to width of all items
					// Move it left by width of button
					jq("#carousel ol").css("width",b * a).css("left", c);
					// Number li's
					jq("#carousel ol li").each(function(i) {
						jq(this).attr("id","item"+(i+1));
					});
					
					var animating = false;
					
					// Click Next
					jq("#caNext").click(function(){
						// Get left value of ul
						var left = jq("#carousel ol").css("left");
						left = parseFloat(left, 10);						
						// If there's more items to go:
						if(left > ((b * 7) - (b * a)) && animating == false){
							animating = true;
							// Subtract width of item... animate!
							jq("#carousel ol").animate({ left: left - b }, "slow", "swing", function(){ animating = false });
						}						
					});

					// Click Prev
					jq("#caPrev").click(function(){
						// Get left value of ul
						var left = jq("#carousel ol").css("left");
						left = parseFloat(left, 10);						
						// If there's more items to go:
						if(left < c && animating == false){
							animating = true;
							// Add width of item... animate!
							jq("#carousel ol").animate({ left: left + b }, "slow", "swing", function(){ animating = false });
						}
					});
					
					function init(){
						var src = jq(".mainImage img").attr("src");
						loadImage(src);
						updateCopyrightCaption();
					};
					
					var self = this;
					
					function loadImage(src){
						
						jq(".mainImage img").hide();
						jq('<p id="ldr">Loading...</p>').css({
							position: 'absolute',
							top: "10px",
							left: "10px",
							color: '#ffffff'
						}).appendTo(".mainImage");
						
						var image = new Image();
						image.onload = function() {
							jq('#ldr').remove();
							jq(".mainImage").css({
								width: image.width+10,
								height: image.height+10
							});
							jq(".mainImage img")
								.attr( "src", src )
								.attr( "width", image.width )
								.attr( "height", image.height ).fadeIn(800);
						};
						image.src = src;
					};
					
					/*
					** Gallery viewer
					*/
					// Carousel link is clicked
					jq("#carousel li a").click(function(){
						var src = jq(this).attr("href");
						jq("#carousel li a").removeClass("current");
						jq(this).addClass("current");
						var w = jq(".mainImage img").innerWidth();
						var h = jq(".mainImage img").innerHeight();
						jq(".mainImage").css({width: w+10,height: h+10});
						//jq(".mainImage img").attr("src",src);
						loadImage(src);
						updateCurrent();
						return false;
					});
					
					// Hide Next/Prev buttons, show on hover
					jq(".gaButton").hide();
					jq(".mainImage").hover(
						function () {
							jq(".gaButton").fadeIn("def");
						},
						function () {
							jq(".gaButton").fadeOut("def");						
						}
					)
					
					// Next image
					jq("#gaNext").click(function(){
						nextItem();
					});
					
					// Previous image
					jq("#gaPrev").click(function(){
						prevItem();
					});
					
					// Next item
					function nextItem(){
						n = jq("#carousel .current").parent().attr("id").substr(4);				
						if(n < a){
							lastItem = false;
							var src = jq("#carousel .current").parent().next().children("a").attr("href");
							jq("#carousel .current").removeClass("current").parent().next().children("a").addClass("current");
							// jq(".mainImage img").attr("src",src);
							var w = jq(".mainImage img").innerWidth();
							var h = jq(".mainImage img").innerHeight();
							jq(".mainImage").css({width: w+10,height: h+10});
							loadImage(src);
							updateCurrent();
						}else if(n == a){
							lastItem = true;	
							jq("#carousel .current").removeClass("current").parent().parent().children("li:first").children("a").addClass("current");	
							var src = jq("#carousel .current").parent().parent().children("li:first").children("a").attr("href");
							alert(alt);
							// jq(".mainImage img").attr("src",src);
							var w = jq(".mainImage img").innerWidth();
							var h = jq(".mainImage img").innerHeight();
							jq(".mainImage").css({width: w+10,height: h+10});
							loadImage(src);
							updateCurrent();
						}
					}
					
					// Previous item
					function prevItem(){
						n = jq("#carousel .current").parent().attr("id").substr(4);
						if(n > 1){
							var src = jq("#carousel .current").parent().prev().children("a").attr("href");
							jq("#carousel .current").removeClass("current").parent().prev().children("a").addClass("current");
							//jq(".mainImage img").attr("src",src);
							var w = jq(".mainImage img").innerWidth();
							var h = jq(".mainImage img").innerHeight();
							jq(".mainImage").css({width: w+10,height: h+10});
							loadImage(src);
							updateCurrent();
						}else if(n == 1){
							jq("#carousel .current").removeClass("current").parent().parent().children("li:last").children("a").addClass("current");
							var src = jq("#carousel .current").parent().parent().children("li:last").children("a").attr("href");
							//jq(".mainImage img").attr("src",src);
							var w = jq(".mainImage img").innerWidth();
							var h = jq(".mainImage img").innerHeight();
							jq(".mainImage").css({width: w+10,height: h+10});
							loadImage(src);
							updateCurrent();
						}
					}
					
					// Update the 'Image _ of _' text
					function updateCurrent(){
						n = jq("#carousel .current").parent().attr("id").substr(4);
						jq(".currentImage").html(n);
						jq(".totalImages").html(a);
						updateCopyrightCaption();
					}
					// Updates the Copyright and Caption
					function updateCopyrightCaption() {
						var caption = jq("#carousel .current").parent().children(".caption").clone();
						var copyright = jq("#carousel .current").parent().children(".copyright").clone();
						jq(".mainCaption p").html(caption);
						jq(".mainImage img").attr("alt",jq("#carousel .current").parent().children(".caption").text());
						jq(".galleryCopyright").html(copyright);
					}
					
					// Toggle Caption
					jq("#toggleCaptions").click(function(){
						var caption = jq(".mainCaption p");
						if(caption.is(":visible")){
							caption.slideUp();
							jq(this).text("Show Captions");
						}else{
							caption.slideDown();
							jq(this).text("Hide Captions");
						}
					});
					
					/*
					** Slideshow thingy
					*/
					// Slideshow, at first, isn't running
					var slideshow = false;
					// Toggle slideshow
					jq("#toggleSlideshow").click(function(){
						if(slideshow == false){
							slideshow = true; // We're running
							jq(this).text("Stop Slideshow"); // Update text
							runSlideshowOnce();
							startSlideshow(); // Run!
						}else{
							slideshow = false; // We're stopping
							jq(this).text("Stopping..."); // Update text
						}
					});
					
					lastItem = false;
					
					function startSlideshow(){
						if(slideshow == true){
						    setTimeout(function(){
						    
								runSlideshowOnce();
								
								startSlideshow();
							}, 8000);       
						}
						if(slideshow == false){
							jq("#toggleSlideshow").text("Start Slideshow"); // Update text
						}
					}	
					
					function runSlideshowOnce(){
						nextItem();
						// Get left value of ul
						var left = jq("#carousel ol").css("left");
						left = parseFloat(left, 10);						
						// If there's more items to go:
						if(left > ((b * 7) - (b * a)) && animating == false){
							animating = true;
							// Subtract width of item... animate!
							jq("#carousel ol").animate({ left: left - b }, "slow", "swing", function(){ animating = false });
						}
						else if(left < ((b * 7) - (b * a)) && animating == false && lastItem == true){
							animating == true;
							jq("#carousel ol").animate({ left: c }, "slow", "swing", function(){ animating = false });									
						}
					}	
					
										
					init();
					
				});
			})(jQuery)