// JavaScript Document

var firstLoadImg = 0;

//this function will add the correct button to the image as it is loaded the first time or is changed
function getAddButton(image_id)
{
	$("#inCartValue").load('pages/galleries/scripts/cart/getCartValue.php', {image_id: image_id}, function(){ displayAddButton($("#inCartValue").text(), image_id); });
}

//this function will work out the id that needs to be checked for the image loaded on page load
function getAddButtonLoad()
{
	getAddButton(firstLoadImg);
}

//this function displays the correct button required
function displayAddButton(inCartText, image_id)
{
	if (image_id != '-1')
	{
		if (inCartText == "true")
		{
			$("#inCartButton")
				.html("Item already in cart")
				.attr('disabled', 'true')
				.unbind('click')
				.click(function(event) { addImgToCart(imageId); })
				.css('display', 'block');
		} else if (inCartText == "false")
		{
			$("#inCartButton")
				.html("Click here to add to your cart")
				.removeAttr('disabled')
				.unbind('click')
				.click(function(event) { addImgToCart(image_id);})
				.css('display', 'block');
		}
	}
}

//adds this image to the cart and creates animation of pic to cart
function addImgToCart(image_id)
{
	//change the button text
	$("#inCartButton")
		.attr('disabled', 'true')
		.html("Adding item to cart...");
	//update the cart
	$("#cartDiv")
		.html("Updating your cart...")
		.load('pages/galleries/scripts/cart/addItemCreateCart.php', {image_id: image_id}, function(){ displayAddButton('true', image_id); } );
	//animate the new item
	$("#mainImgTopDiv").css({'display':'block'})
	$("#mainImgTop")
		.animate({ width: 0, height: 0, top: -80, left: 800 }, 500, function(){ resetTopImg(); });
}

//this function resets the top image after animation
function resetTopImg()
{
	$("#mainImgTopDiv").css({'display':'none'})
	$("#mainImgTop").css({'width':'690px', 'height':'690px', 'left':'0', 'top':'0', 'position':'absolute'});
}
