// JavaScript Document

$(document).ready(function() {

	$('.addtocart').live('click', function(e){

		//
		// NOTE: image paths in this function MUST be updated if the cart add/remove images are changed
		//



			var theObject = $(this);
			var theObjectChild = $(this).find('img');

			if (theObjectChild.attr("src") == "/assets/images/fugue/icons-shadowless/overlay/drive--plus.png") {
				//alert("Action: adding");
				var action = "add";
			}
			else if (theObjectChild.attr("src") == "/assets/images/fugue/icons-shadowless/overlay/drive--minus.png") {
				//alert("Action: removing");
				var action = "remove_ajax";
			}
			else {
				// invalid image, bail out
				return false;
			}


			$.ajax({
				url: "/downloads.php",
				cache: false,
				data: "action="+action+"&files="+$(this).attr("rel"),
				success: function (data) {
					//alert(data);

					theObject.qtip({
						content: data,
						position: {
							corner: {
								tooltip: 'bottomMiddle',
								target: 'topMiddle'
							}
						},
						style: {
							tip: 'bottomMiddle',
							border: {
								width: 3,
								radius: 3,
								color: '#999999'
							},
							background: '#000',
							color: '#F7931E',
							tip: true
						},
						hide: {
							delay: 2000,
							effect: 'fade'
						},
						show: {
							delay: 0,
							ready: true
						},
						api: {
							onRender: function() {
								var self = this;

								setTimeout(function(){ self.hide(); }, 2000);
							},
							onHide: function(){
								this.destroy();

								// image refresh
								cart_item_refresh(theObject);
							}
						}
					});
					theObject.qtip("show");

				}
			});

			return false;
		//}

	});

	cart_item_refresh = function(theObject)
	{
		// theObject should be a reference to the anchor surrounding a cart image

		// get rel attr of link and check if the item to which it refers is already
		// in the user's cart, updating the child image accordingly

		var rel = theObject.attr("rel");
		var theObjectChild = theObject.find('img');

		$.ajax({
			url: "/downloads.php",
			cache: false,
			data: "action=incart_ajax&files="+rel,
			success: function (data) {
				if (data == "1") {
					// item in cart
					theObjectChild.attr({
						src: "/assets/images/fugue/icons-shadowless/overlay/drive--minus.png",
						alt: "Remove from briefcase",
						title: "Remove from briefcase"
					});
					theObject.attr({
						title: "Remove from briefcase"
					});
				}
				else if (data == "0") {
					// item not in cart
					theObjectChild.attr({
						src: "/assets/images/fugue/icons-shadowless/overlay/drive--plus.png",
						alt: "Add to briefcase",
						title: "Add to briefcase"
					});
					theObject.attr({
						title: "Add to briefcase"
					});
				}
				else {
					// error
					return false;
				}
			}
	   });
	};

	$(document).ready(function(){
	  // refresh all cart images in the DOM using the function below
	  $('.addtocart').each(function(i) { cart_item_refresh($(this)); });
	});

});
