/* ================================================================ 
This copyright notice must be untouched at all times.
Copyright (c) 2008 Stu Nicholls - stunicholls.com - all rights reserved.
=================================================================== */
/* <![CDATA[ */
gallery = function() {

/* show first image and information */
showTime=7000;
timer=0;
absLeft=0;
picCount=0;
total = $('.thumb img').length;
$totalImgs = $('.thumb img');
$('#imageNumber').html('Image 1 of ' + total + ' images' );

thumbWidth = 0;
for (x=0; x<total; x++) {
thumbWidth = thumbWidth + $($totalImgs[x]).outerWidth();
}

$('#innerContainer')	.css({ width: thumbWidth + 'px'});
$('#innerContainer img').css({ display: 'inline'});

firstStr = $('.thumb');
showStr = firstStr.html();
showDiv(showStr)


$('#slideLeft')			.hover(function(e){
$('#innerContainer')	.stop();
$('#innerContainer')	.animate({"left": "0px"}, 2500 , "swing")
},function(){
$('#innerContainer')	.stop();
});

$('#slideRight')			.hover(function(e){
posLeft = $('#thumbnails')	.offset().left;
absLeft = e.pageX - posLeft;
hiddenBit = thumbWidth - 500;
$('#innerContainer')		.stop();
$('#innerContainer')		.animate({"left": -hiddenBit + "px"}, 2500 , "swing")
},function(){
$('#innerContainer')		.stop();
});



/* monitor mouse clicks */
$('.thumb').click(function(){
htmlStr = $(this).html();
currentLarge = $(this).attr('class').split(' ').slice(-1);
if ("p"+(total) == currentLarge) {
$('#clickRight').css("backgroundImage", "url()")
				.attr("alt", "")
				.attr("title", "");
}
else {
$('#clickRight').css("backgroundImage", "url(next.jpg)")
				.attr("alt", "Next")
				.attr("title", "Next");
}
if ("p1" == currentLarge) {
$('#clickLeft')	.css("backgroundImage", "url(blank.jpg)")
				.attr("alt", "")
				.attr("title", "");
}
else {
$('#clickLeft').css("backgroundImage", "url(prev.jpg)")
				.attr("alt", "Previous")
				.attr("title", "Prevous");
}

/* swap images and information */
	$('#fullSize')	.fadeOut(600, function(){showDiv(htmlStr)});
});

/* show image and information */
function showDiv (htmlStr) {
	$('#fullSize')		.html(htmlStr)
						.fadeIn(600);
	currentLarge = $('#fullSize img').attr('class');
	currentLarge = currentLarge.substr(1,3);
	$('#imageNumber').html('Image ' + currentLarge + ' of ' + total + ' images' );
}


$('#clickRight').click(function(){
currentLarge = $('#fullSize img').attr('class');
if ("p"+(total-1) == currentLarge) {
$('#clickRight')	.css("backgroundImage", "url(blank.jpg)")
					.attr("alt", "")
					.attr("title", "");
}
if ("p" + total != currentLarge) {
$('#clickLeft')	.css("backgroundImage", "url(prev.jpg)")
				.attr("alt", "Previous")
				.attr("title", "Previous");
htmlStr = $('#innerContainer li.'+currentLarge).next('li').html();
$('#fullSize')	.fadeOut(600, function(){showDiv(htmlStr)});
}
});

$('#clickLeft')	.click(function(){
currentLarge = $('#fullSize img').attr('class');
if ("p2" == currentLarge) {
$('#clickLeft')	.css("backgroundImage", "url(blank.jpg)")
				.attr("alt", "")
				.attr("title", "");
}
if ("p1" != currentLarge) {
$('#clickRight')	.css("backgroundImage", "url(next.jpg)")
					.attr("alt", "Next")
					.attr("title", "Next");
htmlStr = $('#innerContainer li.'+currentLarge).prev('li').html();
$('#fullSize')	.fadeOut(600, function(){showDiv(htmlStr)});
}
});


$('#autoPlay')	.toggle(function(){
$('#autoPlay')	.css("backgroundImage", "url(stop.jpg)")
				.attr("alt", "Stop")
				.attr("title", "Stop");
autoPlay()
},function(){
clearTimeout(timer);
$('#autoPlay')	.css("backgroundImage", "url(play.jpg)")
				.attr("alt", "Play")
				.attr("title", "Play");
 });


}

function autoPlay () {
currentLarge = $('#fullSize img').attr('class');
if ("p"+(total-1) == currentLarge) {
$('#clickRight')	.css("backgroundImage", "url(blank.jpg)")
					.attr("alt", "")
					.attr("title", "");
}
if ("p" + total != currentLarge) {
$('#clickLeft')		.css("backgroundImage", "url(prev.jpg)")
					.attr("alt", "Previous")
					.attr("title", "Previous");
htmlStr = $('#innerContainer li.'+currentLarge).next('li').html();
$('#fullSize')		.fadeOut(600, function(){playDiv(htmlStr)});
}
if ("p"+(total) == currentLarge) {
$('#clickLeft')		.css("backgroundImage", "url(blank.jpg)")
					.attr("alt", "")
					.attr("title", "");
$('#clickRight')	.css("backgroundImage", "url(next.jpg)")
					.attr("alt", "Next")
					.attr("title", "Next");
htmlStr = $('#innerContainer li.p1').html();
$('#fullSize')		.fadeOut(600, function(){playDiv(htmlStr)});
}
timer = setTimeout ("autoPlay()", showTime);
}


function playDiv (htmlStr) {
$('#fullSize')		.html(htmlStr)
					.fadeIn(600);
currentLarge = $('#fullSize img').attr('class');
currentLarge = currentLarge.substr(1,3);
$('#imageNumber').html('Image ' + currentLarge + ' of ' + total + ' images' );
}

/* ]]> */