var anim;
var anim2;

function addToBasket( productId, productName, qty ) {
	var validate = new formValidator();
	validate.checkNumeric( 'product_quantity', 'Quantity' );
	if ( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	
	var handleSuccess = function(o){
		window.scrollTo( 0, 0 );
		document.getElementById( 'minibasket' ).innerHTML = o.responseText;
		document.getElementById( 'minibasket' ).style.display = '';
		// show the "item added" bubble
		document.getElementById('itemAdded').style.display = '';
		document.getElementById('itemAddedName').innerHTML = qty + ' x ' + productName; 
		anim = new YAHOO.util.Anim('itemAdded', { opacity: { from: 0, to: 1 } }, 0.5, YAHOO.util.Easing.easeOut);
		anim.animate();
		setTimeout( 'fadeOutItemAdded()', 5000 )
	};
	var handleFailure = function(o){
		alert( 'error' );
	};
	var callback =
	{
		success:handleSuccess,
		failure:handleFailure
	};
	var formObject = document.getElementById( 'productForm' );
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/submit.php', callback);
	return false;
}

function hideItemAdded() {
	document.getElementById('itemAdded').style.display = 'none';
}

function fadeOutItemAdded() {
	anim2 = new YAHOO.util.Anim('itemAdded', { opacity: { to: 0 } }, 0.5, YAHOO.util.Easing.easeOut);
	anim2.animate();
	anim2.onComplete.subscribe(hideItemAdded);
}


var currentGalleryImage = 0;
var imageInterval = new Object();

function preloadImages( arrImages ) {
	for ( var i in arrImages ) {
		var image = new Image();
		image.src = arrImages[i];
	}
}
function fadeIn() {
	anim2.animate();
}
function fadeOut() {
	anim.animate();
}
function setGalleryImage() {
	document.getElementById( 'gallery' ).style.backgroundImage = 'url(' + images[ currentGalleryImage ] + ')';
	fadeIn();
}
function showGalleryImage( num, stopInterval ) {

	if ( stopInterval ) {
		clearInterval( imageInterval );
	}
	
	if ( num == '+' ) {
		currentGalleryImage++;
	} else if ( num == '-' ) {
		currentGalleryImage--;
	}
	
	if ( currentGalleryImage < 0 ) {
		currentGalleryImage = ( images.length - 1 );
	} else if ( currentGalleryImage > ( images.length - 1 ) ) {
		currentGalleryImage = 0;
	}

	if ( ( num != '+' ) && ( num != '-' ) ) {
		currentGalleryImage = num;
		setGalleryImage();
	} else {
		anim.onComplete.subscribe(setGalleryImage);
		fadeOut();
	}
}
function showNextGalleryImage() {
	showGalleryImage( '+', false );
}