// JavaScript Document
/* ================================================================ 
This copyright notice must be untouched at all times.
Copyright (c) 2009 Stu Nicholls - stunicholls.com - all rights reserved.
=================================================================== */
onload = function() {

/* default variable settings */
timer=0;
curent = 1;
button = 1;

/* delay in miliiseconds */
showTime=4500;

/* automatic variable settings */
pictures = $('#buttons div img').length;
width = $('#p1 img').outerWidth();

/* initial set up */
$('#buttons div img').animate({opacity: 0.4});
$('#buttons #b1 img').animate({opacity: 1});
$('#buttons #b1 img').css("borderColor","#888");

$('#p1').animate({"left": "0px"}, 400, "swing");
$('#loading').css("display","none");

/* start the timer */
timer = setTimeout ("autoPlay()", showTime);


$("#next").click(function() {
		button = curent;
		curent++
	if (curent == (pictures+1) ) {curent = 1}
animateLeft(curent,button)
});

$("#previous").click(function() {
		button = curent;
		curent--
	if (curent == 0 ) {curent = pictures}
animateRight(curent,button)
});

$("#buttons div").click(function() {
	button=curent;
	clickButton = $(this).attr('id');
	curent = parseInt(clickButton.slice(1));
	if (curent > button) {animateLeft(curent,button)}
	if (curent < button) {animateRight(curent,button)}
});

function animateRight(curent,button) {
	$('#p'+curent).css("left",-width+"px");
	$('#p'+curent).animate({"left": "0px"}, 400, "swing");
	$('#p'+button).animate({"left": width+"px"}, 400, "swing");
	setbutton()
}

$("#wrap").mouseover(function() {clearTimeout(timer);});
$("#wrap").mouseout(function() {timer = setTimeout ("autoPlay()", showTime);});

}

function autoPlay () {
	button = curent;
	curent++
	if (curent == (pictures+1) ) {curent = 1}
	animateLeft(curent,button)

timer = setTimeout ("autoPlay()", showTime);
}

function animateLeft(curent,button) {
	$('#p'+curent).css("left",width +"px");
	$('#p'+curent).animate({"left": "0px"}, 400, "swing");
	$('#p'+button).animate({"left": -width+"px"}, 400, "swing");
	setbutton()
}

function setbutton () {
	$('#b'+button+' img').animate({opacity: 0.4})
						.css("borderColor","#444");
	$('#b'+curent+' img').animate({opacity: 1})
						.css("borderColor","#888");
}
