function viewGallery(name,startPosition){

	// AJAX

	//Browser Support Code
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				// Something went wrong
				alert("Your browser is too old to view the images as a slideshow.");
				return false;
			}
		}
	}
	// gets gallery from server and adds it to ajaxDiv
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 1 || ajaxRequest.readyState == 2 || ajaxRequest.readyState == 3){
			$('#loading_overlay').show();
			$('#gallery_container').hide();
			$('#portfolio').hide();
		}
		if(ajaxRequest.readyState == 4){
			
			document.getElementById('ajaxDiv').innerHTML = ajaxRequest.responseText;

			$('#slide_controller_' + name).show();
			$('.slideshow_container_' + name).css('overflow', 'hidden');
			$('.slideshow_container_' + name).show();
			$('.slide_' + name).show();
			$('#loading_overlay').hide();
			
			// define variables for slideshow navigation
			slideWidth = $(".slide_" + name).width();
			if(startPosition != 'undefined'){
				 currentPosition = ((1 - parseInt(startPosition)) * (slideWidth));
			}else{
				currentPosition = 0;
			}
			numberOfSlides = $(".slide_" + name).length;
			
			// fits all slides onto one horizontal line
			$('.slide_inner').css('margin-left', currentPosition + 'px');
			$('.slide_inner').css('width', slideWidth * numberOfSlides);
			$('.slideshow_container_' + name).css('width', slideWidth);
			
		
		}
	}
	// Now get the value from user and pass it to server script.
	var queryString = "?gallery=" + name ;
	if(name == "norwegiancountryside"){
		ajaxRequest.open("GET", "../../../gallery_slideshow.php" + queryString, true);
	}else{
		ajaxRequest.open("GET", "../gallery_slideshow.php" + queryString, true);
	}
	ajaxRequest.send(null);
}

function closeSlideshow(name){
	$('#slide_controller_' + name).hide();
	$('#gallery_container').show();
	$('#portfolio').show();
	$('.slideshow_container_' + name).hide();
}

function nextSlide(){
	if (currentPosition <= 0-(slideWidth*(numberOfSlides-1))){
		currentPosition=0;
	}
	else {
		currentPosition-=slideWidth;
	}
	$('.slide_inner').animate({marginLeft : currentPosition});
}

function previousSlide(){
	if (currentPosition >= 0){
		currentPosition=(0-(slideWidth*(numberOfSlides-1)));
	}
	else{
		currentPosition+=slideWidth;
	}
	$('.slide_inner').animate({marginLeft : currentPosition});
}

$(document).keydown(function(e){
	if (e.keyCode == 37 || e.keyCode == 38) {
    	if (currentPosition >= 0){
    		currentPosition=(0-(slideWidth*(numberOfSlides-1)));
    	}
    	else{
    		currentPosition+=slideWidth;
    	}
    	$('.slide_inner').animate({marginLeft : currentPosition});
    }
	if (e.keyCode == 39 || e.keyCode == 40) {
		if (currentPosition <= 0-(slideWidth*(numberOfSlides-1))){
			currentPosition=0;
		}
		else {
			currentPosition-=slideWidth;
		}
		$('.slide_inner').animate({marginLeft : currentPosition});
	}
	return false;
});
