var updateCartTotal = null; 
(function($) {
	updateCartTotal = function() {
		$.ajax({
			url : '/ajaxcart/index/count',
			type : 'GET',
			success : function(response) {
				$('.cart_count a').html(response);
			}
		});
	}

	$(document).ready(function() {

		$('body').bind("ajaxSend", function() {
			$('#preview_loader').show();
		});

		$('.remove-btn-popup').live('click', function() {
			id = $(this).attr('data-cartid');
			data = "cartid=" + id;
			$.ajax({
				url : '/cartpreview/index/remove',
				type : 'GET',
				data : data,
				success : function(response) {

					updatePreview();

					if( typeof IS_FISHEYE_AJAXCART != "undefined") {
						updateCartTotal();
					}

				}
			});

		});

		$('.infohead').qtip({

			content : {
				text : $('#preview_loader_default').html(), // The text to use whilst the AJAX request is loading
				ajax : {
					url : '/cartpreview', // URL to the local file
					type : 'GET', // POST or GET
					data : {}, // Data to pass along with your request
					success : function(data, status) {
						this.set('content.text', data);
						$('#preview_loader').hide();
					}
				}
			},
			position : {
				at : "bottom right",
				my : "top right"
			},

			hide : {
				effect : function(offset) {
					$(this).hide();
					// "this" refers to the tooltip
				},
				fixed : true,
				delay : 200
			},

			show : {
				effect : function(offset) {
					$(this).show();
					// "this" refers to the tooltip
				},
				fixed : true
			}

		});

		$('.addqty').live('click', function() {
			id = $(this).attr('data-id');
			inc = $(this).attr('data-increments');
			data = "cartid=" + id + "&math=add&inc=" + inc;
			$.ajax({
				url : '/cartpreview/index/qty',
				type : 'GET',
				data : data,
				success : function(response) {

					updatePreview(response);

					if( typeof IS_FISHEYE_AJAXCART != "undefined") {
						updateCartTotal();
					}

				}
			});
		});

		$('.minusqty').live('click', function() {
			id = $(this).attr('data-id');
			inc = $(this).attr('data-increments');
			data = "cartid=" + id + "&math=minus&inc=" + inc;
			$.ajax({
				url : '/cartpreview/index/qty',
				type : 'GET',
				data : data,
				success : function(response) {

					updatePreview(response);

					if( typeof IS_FISHEYE_AJAXCART != "undefined") {
						updateCartTotal();
					}
				}
			});

		});
		showCartPreview = function() {
			updatePreview();
			$('.infohead').qtip('toggle', true);
		}
		hideCartPreview = function() {
			$('.infohead').qtip('toggle', false);
		}
		updatePreview = function(item_count) {
			
			$('.infohead').qtip('toggle', true);
			obj = $('.infohead').qtip();
			$('#preview_loader').show();

			$.ajax({
				url : '/cartpreview',
				type : 'GET',
				success : function(response) {
					obj.set('content.text', response);
					$('#preview_loader').hide();
				}
			});
		
			updateCartTotal();

		}
	});
})(jQuery);

