// JavaScript Document

$(function() {
	try {
		if (removeFromLightBox) removeFromLightBox = removeFromLightBox;
	} catch (e) {
		removeFromLightBox = false;
	}
		   
	$('.addToCart').each(function(index) {
		$(this).click(function() {
			var productID = $(this).attr('id');
			var clickedLink = $(this);
			clickedLink.html('<img src="images/loading.gif" border="0" width="16" height="16" />');
			jQuery.get('ajax/addToCart.php', {productID : productID, removeFromLightBox : removeFromLightBox}, function(data) {
				// show the popup message
				$('#onAddToCart').html('Cart now contains '+data+' items').fadeIn();
				$('#cart_amount').html(' ('+data+')');
				$('#cart_link').addClass('strong');
				
				setTimeout(function() {$('#onAddToCart').fadeOut(1000);},1500);
				
				if (removeFromLightBox) {
					$('#wishlist_'+productID).fadeOut();
					
					var wislistItems = $('#wishlist_amount').html();
					wislistItems = wislistItems.replace('(', '');
					wislistItems = wislistItems.replace(')', '');
					
					currentItems = parseInt(wislistItems) - 1;				
					
					if (currentItems <= 0) {
						$('#wishlist_amount').html('');
						$('#wishlist_link').removeClass('strong');
					} else					
						$('#wishlist_amount').html(' ('+currentItems+')');
				}
				
				// disable the add to cart icon
				clickedLink.parent().html('<img src="images/add_to_cart-d.png"   height="15" width="16"  border="0" alt="Sound already in Cart" title="Sound already in Cart"/>');
			});
						 	
			return false;
		})
	});
	
	
	
	$('.addToWishlist').each(function(index) {
		$(this).click(function() {
			var productID = $(this).attr('id');
			productID = productID.replace('wish_', '');
			
			var clickedLink = $(this);
			clickedLink.html('<img src="images/loading.gif" border="0" width="16" height="16" />');
			jQuery.get('ajax/addToWishlist.php', {productID : productID}, function(data) {															   
																				   
				if (data == 'login') {
					window.location.href = "login.html";
				} else {
																				   
					// show the popup message
					$('#onAddToCart').html('Wishlist now contains '+data+' items').fadeIn();
					$('#wishlist_amount').html(' ('+data+')');
					$('#wishlist_link').addClass('strong');
					
					setTimeout(function() {$('#onAddToCart').fadeOut(1000);},1500);
					
					// disable the add to cart icon
					clickedLink.parent().html('<img src="images/wishlist-d.png"   height="15" width="16"  border="0" alt="Sound already in your Wishlist" title="Sound already in your Wishlist"/>');
				}
			});
						 	
			return false;
		})
	});
});
