function loadGallery() {
	
	//change_url();
	
	var visible = 6;
	var start = 0;
	var scroll = 5;
	var request;

	var bigImgWidth = 660;
	var bigImgQuality = 80;
	var pageNum = 1;
	var pageNum;
	var hash = true;
	var totalImages = parseInt($("#gallery_thumbs").find('li').length);
	var firstload = true;

	//grab the current page number.						
	pageNum = parseInt(window.location.hash.substr(2));
	
	if (isNaN(pageNum)) {
		fullUrl = window.location.toString().replace("#content", '');
		page = fullUrl.match(/\/([0-9]*$)/gi);

		if (page != null) {
			pageNum = parseInt(page.toString().replace("/", ""));
		}
		hash = false;
	}

	if (isNaN(pageNum)) {
		pageNum = parseInt(1);
	}

	if (pageNum > 0) {
		imgObj = $("#imglink_" + pageNum);
		updateImages(imgObj);	
	}
	

	if (!$('#gallery_nav_wrap').hasClass('no_display')) {
		$('#gallery_nav_wrap').show();
	}

	//load in the next and previous buttons.
	nextButton = $(".main_image_wrapper .large .main_next").length;
	if (nextButton != 1) {
		$(".main_image_wrapper").append("<div class='large'><a class='main_next' href='#'>next &raquo;<span></span></a></div>");
	}
	prevButton = $(".main_image_wrapper .large .main_prev").length;
	if (prevButton != 1) {
		$(".main_image_wrapper").append("<div class='large'><a class='main_prev' href='#'>&laquo; previous<span></span></a></div>");
	}

	nextButton = $(".manual_nav .main_next").length;
	if (nextButton != 1) {
		$(".manual_nav").append("<a class='main_next' href='#'>next &raquo;<span></span></a>&nbsp;");
	}
	prevButton = $(".manual_nav .main_prev").length;
	if (prevButton != 1) {
		$(".manual_nav").prepend("&nbsp;<a class='main_prev' href='#'>&laquo; previous<span></span></a>");
	}
	
	if($('.extras').length != 1) {
		//<a class='hide_captions' href='#'>Hide Captions<span></span></a>
		
		$(".over_image").before("<div class='extras'></div>");		
		if($('#gallery_nav_wrap li').length > 2) {
			$(".extras").append("<a class='start_slideshow' href='#'>Start Slideshow<span></span></a>");
		}
		$(".extras").append("<a class='hide_captions' href='#'>Hide Captions<span></span></a>");
		
	}
	//check if we need them!
	//$("#curr_page").text(pageNum);
	checkNextPrev(pageNum);

	$(".restart").click(function() {
		stop_slideshow();
		$("#whats_next").hide();
		nextPage = 1;
		if (nextPage <= totalImages) {
			imgObj = $("#imglink_" + nextPage);
			updateImages(imgObj);
			$("#curr_page").click();
		}

		return false;
	});	
	
	$(".close_whats_next").click(function() {
		_gaq.push([ "_trackEvent", "gallery", 'whatsNext', 'close button' ]);
		stop_slideshow();
		$("#whats_next").hide();
		return false;
	});	
	
	$(".hide_captions").click(function() {
		if($("#image_caption").is(':visible')) {
			$(".hide_captions").text("Show Captions");
			_gaq.push([ "_trackEvent", "gallery", 'captions', 'hide captions' ]);
			$("#image_caption").slideUp();
			$(".title_bar").slideUp();
			$("#image_credit").css({'text-align':'left'});
			$(".over_image").css({'padding':'10px 10px 8px'});	
		} else {
			show_captions();
		}	
		return false;
	});
	
	function show_captions() {
		$(".hide_captions").text("Hide Captions");
		$("#image_caption").slideDown();
		$(".title_bar").slideDown();
		$(".over_image").css({'padding':'10px 10px 20px'});
		$("#image_credit").css({'text-align':'right'});			
	}
	
	
	var t;
	$(".start_slideshow").click(function() {
		if(t) {
			$(this).text("Start Slideshow");
			_gaq.push([ "_trackEvent", "gallery", 'slideShow', 'stop' ]);
			t = clearInterval(t);
		} else{
			$(this).text("Stop Slideshow");
			_gaq.push([ "_trackEvent", "gallery", 'slideShow', 'start' ]);
			t = setInterval(function(){	
				nextPage = parseInt(pageNum) + 1;
				if($('.large .main_next').is(':visible') && (parseInt(pageNum) < totalImages)) {
					imgObj = $("#imglink_" + nextPage);
					updateImages(imgObj);
					$("#curr_page").click();
				} else {
					$("#curr_page").text('1');
					imgObj = $("#imglink_" + 1);
					updateImages(imgObj);
					$("#whats_next").hide();
					$("#curr_page").click();
				}
			}			
			,3500);
		}	
		return false;
	});
	
	function stop_slideshow() {
		if(t) {
			_gaq.push([ "_trackEvent", "gallery", 'slideShow', 'stopped other button' ]);
		}
		t = clearInterval(t);
		$(".start_slideshow").text("Start Slideshow");
	}
	
	
	$(".main_next").click(function() {
		stop_slideshow();
		$("#whats_next").hide();
		nextPage = parseInt(pageNum) + 1;
		if (nextPage <= totalImages) {
			imgObj = $("#imglink_" + nextPage);
			updateImages(imgObj);
			$("#curr_page").click();
		} else if (parseInt(pageNum) == totalImages) {
			//google analytics - see who has activated the popup!
			_gaq.push([ "_trackEvent", "gallery", 'whatsNext', 'activated' ]);
			$("#whats_next").fadeIn();
			$(".main_next").hide();
		}
		return false;
	});
	$(".main_prev").click(function() {
		stop_slideshow();
		prevPage = parseInt(pageNum) - 1;

		if ($("#whats_next").is(":visible")) {
			$("#whats_next").hide();
			prevPage = parseInt(pageNum);
		}

		if (prevPage > 0) {
			imgObj = $("#imglink_" + prevPage);
			updateImages(imgObj);
			$("#curr_page").click();
		}
		return false;
	});

	function checkNextPrev(index) {
		if (index > 0) {
			$('a.prev', cont).removeClass('disabled');
			$('a.overlay.prev', cont).show();
		}
		if (index < total - 1) {
			$('a.next', cont).removeClass('disabled');
			$('a.overlay.next', cont).show();
		}
	}

	$("#gallery_nav_wrap a").click(function() {
		stop_slideshow();
		$("#whats_next").hide();
		updateImages($(this));
		return false;
	});

	$("#image_caption").click(function() {
		$('html,body').animate({
			scrollTop : $('#gallery_body').offset().top
		}, 'slow');
		return false;
	});
	
	function makeRequest(linkImage) {
			
		var request = $.ajax({
					url : linkImage.attr('href'),
					cache : false,
					async : false,
					success : function(html) {
						$("#gallery_body_content").replaceWith(html);
					},
					error : function() {
						$("#gallery_body_content").replaceWith('<div id="gallery_body_content">Opps, I can\'t find the content</div>');
						_gaq.push([ "_trackEvent", "gallery", 'error', 'no loaded ajax content' ]);
					}
				});
		
		setTimeout(function() {
			if (request)
				request.abort();
		}, 5000);

	}

	function updateImages(linkImage) {
		$("img.main_image").stop();
		show_captions();
		if($("#gallery_body").length) {
			$("#gallery_body").stop();
			makeRequest(linkImage);
		}		

		$('#gallery_thumbs').find('li').each(function() {
			$(this).removeClass('selected');
		});

		$(linkImage).parent().addClass('selected');

		var newBigSrc = linkImage.find("img").attr("src").replace(
				/(.*)-[0-9]{1,4}-([0-9]{1,3}).([a-zA-Z]{3,4}$)/gi,
				"$1-" + bigImgWidth + "-" + bigImgQuality + ".$3");
		$(".main_image_wrapper img").attr("src", newBigSrc);

		page = linkImage.attr("href").match(/\/([0-9]*$)/gi);
		
		if (page != null) {
			pageNum = page.toString().replace("/", "");
		} else {
			pageNum = 1;
		}

		if ($("#curr_page").hasClass('intro dec')) {
			thisPage = totalImages - (pageNum - 1);
			if (thisPage == totalImages) {
				thisPage = 'intro';
			}
			$(".curr_page.intro.dec").text(thisPage);

		} else if ($("#curr_page").hasClass('intro')) {
			thisPage = pageNum - 1;
			if (thisPage == '0') {
				thisPage = 'intro';
			}
			$(".curr_page.intro").text(thisPage);

		} else if ($("#curr_page").hasClass('dec')) {
			thisPage = totalImages - (pageNum) +1;
			$(".curr_page.dec").text(thisPage);

		} else if ($("#curr_page").hasClass('alpha')) { 		
			alpha = parseInt(pageNum) + 64;	
			$(".curr_page.alpha").html('&#' + alpha + ';');
		}

		//update what image we are on
		$("#curr_page").text(pageNum);

		$("#image_caption").removeClass('active');
		$("#image_credit").removeClass('active');
		$("#image_title").text(linkImage.find('span.title').text());

		if (linkImage.find("img").attr("title").length > 0) {
			$("#image_caption").addClass('active');
			$("#image_caption").text(linkImage.find("img").attr("title"));
		} else {
			$("#image_caption").text('');
		}

		page = '';
		if (pageNum !== 1) {
			page = '| Page ' + pageNum;
		}

		document.title = $("#article_wrapper h2").text() + ' | ' + linkImage.find('span.title').text() + ' | MusicRadar.com';
		copy = linkImage.find("img").attr("alt").match(/\(\(.*\)\)/gi);

		$('#image_credit').html('');
		if (copy != null && copy != '(())') {
			$("#image_credit").addClass('active');
			copy = copy.toString().replace(/\(\(/, "").replace(/\)\)/, "");
			copy = '&copy; ' + copy;
			$('#image_credit').html(copy);
		}

		change_url(pageNum);
		checkNextPrev(pageNum);
		resizeImage(linkImage);

		var mpu = $('iframe.mpu'),sky = $('iframe.sky'),top_banner = $('#header_ad'), mpuSrc = mpu.attr('src'),skySrc = sky.attr('src'), bannerSrc = top_banner.attr('src');
	
		if(!firstload) {
			$(document).ready(function() {
				sitestat(ssurl);
				
				// Refresh the MPU and banner
				$('#mpu_wrapper').html(mpu);
				mpu.attr('src', mpuSrc);
				var mpuCss = { 'width' : '300px','height' : '250px' };
				mpu.css(mpuCss);		
				sky.attr('src', skySrc);		
				top_banner.attr('src', top_banner.attr('src'));
				
			});
		}
		firstload = false;
	}

	function resizeImage(linkImage) {
		if (linkImage.find("img").attr('height') < 100) {
			$("img.main_image").attr('style', '');
		}		
		$('img.main_image').stop();
		$('img.main_image').load(function() {
			if ($('img.main_image').attr('height') > 500) {
				$('img.main_image').attr('height',495);
			//	$("img.main_image").animate({ height:'495', width:'auto'},100);
				
			}
		});
		
		
	}

	function checkNextPrev(index) {
		if (index <= 1) {
			$(".main_prev").hide();
		} else {
			$(".main_prev").show();
		}

		if (index > totalImages) {
			$(".main_next").hide();
		} else {
			$(".main_next").show();
		}
	}

	//this function uses the hash or rewites the url for new browsers
	function change_url(setPage) {
		
		fullUrl = window.location.pathname.toString().replace(window.location.hash, '');
		page = fullUrl.match(/\/([0-9]+$)/gi);

		fullUrl = fullUrl.replace(new RegExp("[/]+$", "g"), "");
		
				
		if (page != null && setPage == null) {
			pageNum = page.toString().replace("/", "");
		}
				

		if (history.replaceState) {

			if (window.location.hash) {
				//replace the hashbang if browser supports replaceState
				fullUrl = fullUrl.replace(window.location.hash, '');
				
				if(isNaN(pageNum)) {
					pageNum = window.location.hash.replace('#!', '');		
					pageNum = parseInt(pageNum);
					fullUrl = fullUrl.replace(/\/$/,"");
					window.location = fullUrl + '/' + pageNum;
				}
				
			}

			if (setPage > 0) {
				pageNum = setPage;
			}

			if (page == null && pageNum > 0) {
				url = fullUrl + "/" + parseInt(pageNum);
			} else {
				if (pageNum == 1) {
					url = fullUrl.toString().replace(page, "");
				} else {
					url = fullUrl.toString().replace(/\/([0-9]*$)/gi,
							"/" + parseInt(pageNum))
				}
			}

			window.history.replaceState(null, document.title, url);

		} else {
			if (pageNum > 1 && setPage == null) {
				window.location = fullUrl.toString().replace(page,
						"#!" + parseInt(pageNum));
			} else if (setPage > 0) {
				window.location.hash = '#!' + parseInt(setPage);
			}
		}

	}

	// Create a carousel on the article
	go = $('#gallery_nav_wrap');
	go.find('li').each(function() {		
		if ($(this).find('img').height() != 75) {
			$(this).find('img').height(75);
		}
	});

	if (go.length == 1) {
		go.prepend('<a class="btn btnPrev disabled" href="#"><span></span>Previous</a>').append('<a class="btn btnNext" href="#"><span></span>Next</a>');
		go.jCarouselLite({
			visible : visible,
			start : start,
			scroll : scroll,
			toGo : '#curr_page',
			circular : false,
			btnNext : '#gallery_nav_wrap .btnNext',
			btnPrev : '#gallery_nav_wrap .btnPrev'
		});
		$("#curr_page").click();
	}

}

$(document).ready(function() {
	if($("#article_wrapper.gallery_wrapper").length) {
		loadGallery();
	}
});

