$(document).ready(function() {

	visibleThumbs = 10;
	currentThumb = 0;
	totalThumbs = $('#thumb-scroll').children().size();

	var anchor = self.document.location.hash.substring(1);
	if (anchor == 'splash') {
		splashHeight = $('body').height();
		if ($(window).height() > $('body').height()) {
			splashHeight = $(window).height();
		}
		$('body').append('<div class="splash-bg" id="splash"><div class="inside"><div class="content"><img src="/images/logo.png" width="575" height="94" alt="Shadow Box Studios"/><p>Welcome to Shadow Box Studios. Creative Photography based in Edmonton, AB.</p><p><a href="/" onclick="$(\'#splash\').remove();return false;">View our Portfolio</a> | <a href="/blog">Read our Blog</a></p></div></div></div>');
		$('#splash').css('height', splashHeight + 'px');
		$('#splash .inside').css('height', splashHeight + 'px');
		$('#splash .inside .content').css('top', ($(window).height()/2-150) + 'px');
	}
	
	if ($('body').height() < $(window).height()) {
		$('.center').css('height', $(window).height() + 'px');
	}
	
});

function moveThumbs(direction) {
	if(direction == 'prev') {
		if(currentThumb > 0) {
			currentThumb -= 10;
			$('#thumb-scroll').animate({'left': '+=760'}, 800);
		}
	} else if(direction == 'next') {
		if(currentThumb + visibleThumbs < totalThumbs) {
			currentThumb += 10;
			$('#thumb-scroll').animate({'left': '-=760'}, 800);
		}
	}
	if(currentThumb == 0) {
		$('#arrow-left img').attr('src', '/images/thumb-arrow-left-blk.png');
	} else {
		$('#arrow-left img').attr('src', '/images/thumb-arrow-left.png');
	}
	if(currentThumb + visibleThumbs >= totalThumbs) {
		$('#arrow-right img').attr('src', '/images/thumb-arrow-right-blk.png');
	} else {
		$('#arrow-right img').attr('src', '/images/thumb-arrow-right.png');
	}
}

function loadPhoto(link, photo, offset) {
	shrinkPhoto();
	hideEnlarger();
	$('#thumb-scroll li a img').removeClass('selected');
	$(link).children().addClass('selected');
	$('#photo').stop().addClass('loading').css('top', '0px');
	$('#photo').children().remove();
	var img = new Image();
	$(img).load(function() {
		$(this).hide();
		$('#photo').removeClass('loading').css('top', (offset) + 'px').append(this).animate({'top': '-=15'}, 800);
		$(this).fadeIn();
		if(this.height >= 505) {
			showEnlarger(this, offset);
		}
	}).error(function () {
		// there was an error
	}).attr('src', '/images/' + photo).click(function(){
		toggleEnlarge(this, offset);
	});
}

function nextPhoto() {
	$('#thumb-scroll li a img.selected').parent().parent().next().children(':first-child').children(':first-child').click();
}

function prevPhoto() {
	$('#thumb-scroll li a img.selected').parent().parent().prev().children(':first-child').children(':first-child').click();
}

function toggleEnlarge(photo, offset) {
	if($(photo).hasClass('full')){
		$(photo).removeClass('full');
		$('#photo').animate({'top': '-=' + ((offset * -1) + 15)}, 800);
		$(photo).parent().parent().animate({'height': '-=' + (photo.height-495)}, 800, function(){
			if(photo.height >= 505) {
				showEnlarger(photo, offset);
			}
		});
	}else{
		$(photo).addClass('full');
		$('#photo').animate({'top': '0'}, 800);
		$(photo).parent().parent().animate({'height': '+=' + (photo.height-495)}, 800);
		hideEnlarger();
	}
}

function showEnlarger(photo, offset) {
	$('.photo').addClass('enlarge').append('<div class="enlarger"><img src="/images/zoom.png" height="25" width="25" title="Enlarge" alt="" /></div>');
	$('.enlarger').click(function(){
		toggleEnlarge(photo, offset);
	});
}

function hideEnlarger() {
	$('.enlarger').remove();
}

function shrinkPhoto() {
	$('img.full').removeClass('full');
	var oldheight = $('div.photo').css('height').slice(0, -2);
	if(oldheight > 495){
		$('div.photo').animate({'height': '-=' + (oldheight-495)}, 800);
	}
}
