$(function() {
	$('#thumbsWrapper').css('height', thumbsHeight + "px");
	$('#thumbsWrapper a:first').css('margin-top', '0px');
	$('#thumbsWrapper a:last').css('margin-bottom', '0px');
	disableRightClick();
});

var numberOfImagesToScroll = 5;
var imageScrollAmount = (numberOfImagesToScroll * 50) + (numberOfImagesToScroll * 10) - 10;
var timer = window.setInterval('nextImage()', 10000);

$('#prevBtn').click(function(event){
	event.preventDefault();
	
	var position = $('#thumbsWrapper').position();
	var maxTopPos = -imageScrollAmount;
	
	if(position.top <= maxTopPos){
		$('#thumbsWrapper').animate({
			top: '+='+imageScrollAmount
		}, 'fast');
	}else if(position.top <= maxTopPos + imageScrollAmount){
		$('#thumbsWrapper').animate({
			top: '0px'
		}, 'fast');
	}
});

$('#nextBtn').click(function(event){
	event.preventDefault();
	
	var position = $('#thumbsWrapper').position();
	var maxTopPos = $('#thumbs').height() - $('#thumbsWrapper').height();
	
	if(position.top >= maxTopPos + imageScrollAmount){
		$('#thumbsWrapper').animate({
			top: '-='+imageScrollAmount
		}, 'fast');
	}else if(position.top >= maxTopPos){
		$('#thumbsWrapper').animate({
			top: maxTopPos
		}, 'fast');
	}
});

$('#thumbsWrapper a').click(function(event){
	event.preventDefault();
	
	var imageClass = $(this).attr('class');
	
	$('#thumbsWrapper a img').removeClass('active');
	$(this).find('img').addClass('active');
	
	loadImage(imageClass);
});

function loadImage(imageClass) {
	if($('#gallery img').attr('id') != imageClass) {
		$('#gallery img').fadeOut('slow', function(){
			
			$(this).attr('id',imageClass);
			$(this).attr('src','photos/'+imageClass+'.jpg');
			
			$(this).load(function(){
				$(this).attr('style','display:none;');
				updateImage();
				$(this).fadeIn('slow');
	
				window.clearInterval(timer);
				timer = window.setInterval('nextImage()', 10000);
			});
			
		});
	}
}

$(window).resize(function() {
	updateImage();	
	var position = $('#thumbsWrapper').position();
	var maxTopPos = $('#thumbs').height() - $('#thumbsWrapper').height();
	//console.log('top='+position.top+', maxTopPos='+maxTopPos);
	if(position.top <= maxTopPos){
		$('#thumbsWrapper').css('top', maxTopPos);
		console.log($('#thumbs').height());
	}
});

function updateImage() {
	var contentWidth = $('#gallery').width();
	var contentHeight = $('#gallery').height();

	var imageWidth = $('#gallery img').width();
	var imageHeight = $('#gallery img').height();
	var aspect = imageWidth/imageHeight;
	/*
	console.log('contentWidth='+contentWidth);
	console.log('contentHeight='+contentHeight);
	console.log('imageWidth='+imageWidth);
	console.log('imageHeight='+imageHeight);
	console.log('aspect='+aspect);
	console.log('----------------');
	*/
	if(imageWidth>imageHeight){
		if(contentWidth/aspect > contentHeight){
			$('#gallery img').height(contentHeight);
			$('#gallery img').width(contentHeight*aspect);
		}else {
			$('#gallery img').width(contentWidth);
			$('#gallery img').height(contentWidth/aspect);
			$('#gallery img').css('top', '50%');
			$('#gallery img').css('left', '0');
			$('#gallery img').css('position', 'absolute');
			$('#gallery img').css('margin-top', -$('#gallery img').height()/2);
		}
	}else if(imageWidth<imageHeight){
		$('#gallery img').height(contentHeight);
		$('#gallery img').width(contentHeight*aspect);
	}
}


function disableRightClick(e)
{
  var message = "© copyright 2007-2011 mrcreativeimages.co.uk";
  
  if(!document.rightClickDisabled) // initialize
  {
	if(document.layers) 
	{
	  document.captureEvents(Event.MOUSEDOWN);
	  document.onmousedown = disableRightClick;
	}
	else document.oncontextmenu = disableRightClick;
	return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
	if (e.which==2||e.which==3)
	{
	  alert(message);
	  return false;
	}
  }
  else
  {
	alert(message);
	return false;
  }
}

function nextImage()
{
	var currentImage = $('#gallery img').attr('id');
	var activeImageIndex = currentImage.substr(6);
	var nextImage;
	
	if(activeImageIndex == $('#thumbsWrapper a').size()) {
		nextImage = 'photo_01';
	}else {
		
		nextImageIndex = (Number(activeImageIndex) + 1);
		
		if(nextImageIndex < 10){ 
			nextImageIndex = '0' + nextImageIndex;
		}
		
		console.log('parseInt(activeImageIndex)+1 padded= '+nextImageIndex);
		
		nextImage = 'photo_' + nextImageIndex;
	}
	
	loadImage(nextImage);
}

