$(document).ready(function () {
	$('#add_to_cart').removeAttr('onclick');

	$('#add_to_cart').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#module_cart .middle').html(html);
			},
			complete: function () {
				var image = $('#image').offset();
				var cart  = $('#module_cart').offset();

				$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),
					heigth : $('#module_cart').height()
				};

				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});
			}
		});
	});

	// add to cart for small plus icons
	$('.button_add_small').removeAttr('onclick');
	$('.button_add_small').click(function (e) {
		var buttonElement = $(this);
		e.preventDefault(); // stop the normal link
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			//dataType: 'json',
			data: ({product_id : getUrlVars($(this).attr('href'))['product_id'], quantity : 1}),
			success: function (html) {
				$('#module_cart .middle').html(html);
			},
			complete: function () {
				var imageElement = buttonElement.parent().children().children().first();
				var image = imageElement.offset();
				var cart  = $('#module_cart').offset();

				imageElement.before('<img src="' + imageElement.attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),
					heigth : $('#module_cart').height()
				};

				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});
			}
		});
	});

});

function getUrlVars($url)
{
    var vars = [], hash;
    var hashes = $url.slice($url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
