var images = [];
var pauseTime = 5000; //time between slides
var fadeTime = 400; //length of the fade between slides

var currentIndex = 0;
var animating = false;
var nextAnimation = false;
var loaded = [];


function nextImage(slideShow){
	if(!animating){
		var next = currentIndex + 1;
		if(next == images.length) next = 0;
		animate(next);
	}else{
		nextAnimation = function(){
			nextImage();
		};
	}
}

function prevImage(){
	if(!animating){
		var next = currentIndex - 1;
		if(next == -1) next = images.length - 1;;
		animate(next);
	}else{
		nextAnimation = function(){
			prevImage();
		};
	}
}

function jumpTo(index){
	if(!animating){
                animate(index);
        }else{
                nextAnimation = function(){
                        jumpTo(index);
                };
        }
}

function animate(nextIndex){
	nextIndex = parseInt(nextIndex);
	
	//console.log("going from " + currentIndex + " to " + nextIndex);
	
	$("#big_image_wrapper").css({background: "url(" + images[nextIndex].src + ")"});
	currentIndex = nextIndex;
	animating = true;
	
	
	$("#big_image").animate({
		opacity: 0
	}, fadeTime, function(){
		indexLabel = currentIndex + 1;
		$("#gallery_count").html(indexLabel + " of " + images.length);
		
		$("#big_image").attr('src', images[currentIndex].src);
		$("#big_image").css({opacity: 1.0});
	
		animating = false;
		if(nextAnimation){
			nextAnimation();
			nextAnimation = false;
		}
	});	
	
}

$(function () {
	var index = 0;

	if(images){
		$.each(images, function(){
			$("<img src='" + this.src + "'>");    
			index++;
		});		
	}

	$('.scrollpane').jScrollPane({scrollbarWidth: 5});

	var coupon_error = $('#coupon_error_message');
	if(coupon_error && coupon_error.html() != '') coupon_error.show();
	
});




