

var browser;
var windowHeight;


function adjustDivContainerPosition()
{
	
    var window_height = $(window).height();
	var container_height = $("#divContainer").height();
	
	//alert("window_height: " + window_height + ", container_height: " + container_height);
	
	if (window_height<(container_height+40)) {
		var margin_top = 20;
	} else {
		var margin_top = (window_height-container_height)/2;
		
	}
	
	$("#divContainer").css("margin-top", margin_top);
}

function loadImage()
{	
	//Load image:
	var imageURL = imageFileName[imageToLoad];
	//$("#imgPortfolioImage").animate({opacity: 1.0}, 1000);
	$("#imgPortfolioImage").fadeOut('fast', function() {
		$("#imgPortfolioImage").attr("src", imageURL);
		//document.getElementById("imgPortfolioImage").src = imageURL;
		$("#imgPortfolioImage").load(function() {
			$("#imgPortfolioImage").fadeIn('fast');
		});
	});
	
	//$("#imgPortfolioImage").animate({opacity: 1.0}, 500);
	if (galleryCaption==''||galleryCaption==null) {
		document.getElementById("divImageCaption").innerHTML = imageCaption[imageToLoad];
	} else if (galleryCaption!=null) {
		document.getElementById("divImageCaption").innerHTML = galleryCaption;
	}
	
	//Previous and next button:
	
	
	if (imageToLoad==0)
	{
		var prevImage = imageFileName.length-1;
	}
	else
	{
		var prevImage = imageToLoad-1;
	}
	
	document.getElementById("aPrevImage").onclick = function(){
		pickImage(prevImage);
	}
	
	if (imageToLoad<(imageFileName.length-1))
	{
		var nextImage = 1+imageToLoad;	
	}
	else
	{
		var nextImage = 0;
	}
	document.getElementById("aNextImage").onclick = function(){
		pickImage(nextImage);
	}
	document.getElementById("aPortfolioImage").onclick = function(){
		pickImage(nextImage)
	}
	
	document.getElementById("aPrevImage").href = "#" + (imageToLoad);
	document.getElementById("aNextImage").href = "#" + (imageToLoad);
	document.getElementById("aPortfolioImage").href = "#" + (imageToLoad);
	
	//Preload images:
	if (document.images)
	{
		var prevImagePreload = new Image(884,560);
		prevImagePreload.src= "Images/" + galleryName + "/Pictures/" + imageFileName[prevImage];
		var nextImagePreload = new Image(884,560);
		nextImagePreload.src= "Images/" + galleryName + "/Pictures/" + imageFileName[nextImage];
	}
}
function pickImage(userChoice)
{
	imageToLoad = userChoice;
	
	loadImage();
}
function pageLoad()
{
	var urlHashNumber = window.location.hash.substring(1)
		
	if(urlHashNumber!="")
	{	
		imageToLoad = urlHashNumber;
	}
	else
	{
		imageToLoad = 0;
	}
	loadImage();
}
$(document.documentElement).keyup(function (event) {
	if (event.keyCode == 37)
	{
		if (imageToLoad==0)
		{
			imageToLoad = imageFileName.length-1;
		}
		else
		{
			imageToLoad--;
		}
		
		loadImage();
		
		window.location.hash = imageToLoad;
	}
	else if (event.keyCode == 39)
	{
		
		if (imageToLoad<(imageFileName.length-1))
		{
			imageToLoad++;
		}
		else
		{
			imageToLoad = 0;
		}
		
		loadImage();
		
		window.location.hash = imageToLoad;
	}
});

// JavaScript Document
$(document).ready(function() 
{
  	adjustDivContainerPosition();
 	$(window).resize(function() {
		adjustDivContainerPosition();
	});
	
	pageLoad();
	
  //{
	 // $("#imgPortfolioImage").hide('slow');
  //},function(){
	//  $("#imgPortfolioImage").show('fast');
  //});
});
