$().ready(function() {

	/* ajax add / remove to cart */

	$('a[rel=lightbox]').lightbox();
	
	$('#enquirylink a.cartadd').click(function() { //add to cart click
		var $addedid = $(this).attr('rel'); // assign productid to click
	
		if ($(".cart").length > 0) { // if cart exists				
			$.get("/enquiry_add.php", {id : $addedid , ajax : 'true'}, function($status) { // add item to session					
				$.get("/_cartitem.php", {id : $addedid }, function($html){ //get product details							
					$("#newitem").append($html).fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400); //add product details to cart and flash							
				});											
			});				
		} else { // cart does not exist
			$.get("/enquiry_add.php", {id : $addedid , ajax : 'true'}, function($status) { // add item to session
				$.get("/_cart.php", {id : $addedid }, function($html){ //get cart
			
					$("#cartwrapper").hide().append($html).fadeIn(400).fadeOut(400).fadeIn(400);
			
				});
			});
		}
		
		// replace add button with remove
		$("p[rel=add]").fadeOut("slow", function() {
			$("p[rel=remove]").fadeIn("slow");
		});
		
		return false; // kill default behavior
	});

	$('#enquirylink a.cartremove').click(function() { //remove from cart click			
		var $removedid = $(this).attr('rel'); //assign productid to variable							
		$.get("/enquiry_remove.php", {id : $removedid , ajax : 'true'}, function($status) { //remove item from session
			$("div[rel="+ $removedid + "]").fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400, function() { //flash item
				$(this).remove(); //remove item from vart
				if ($(".cart h4").length < 1) { //if cart empty				
					$(".cart").fadeOut("slow",function() { //fade out cart
						$(this).remove(); // remove cart
					});
				}
			});
		});
		
		// replace remove button with add
		$("p[rel=remove]").fadeOut("slow", function() {
			$("p[rel=add]").fadeIn("slow");
		});
		
		return false; // kill default behavior
	});

});

