$(document).ready(function () {
	var ajaxstart = false;
    //ajax add from flypage
	$('#add_to_cart').removeAttr('onclick');

	$('#add_to_cart:not(.disabled)').click(function () {
		var collaterlas = '';
		$('input[name="addcart"]').each(function(index, el){
			if (el.checked == true ){
				collaterlas += el.value+'|';
			}
			
		});
		
		$('input[name="product_collaterals"]').val(collaterlas);
	
		$.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();
				});		
			}
		});
	});

   //ajax add from other pages
    // GAB 2011.05.05
    $('.list_addtocart .button:not(.button-disabled)').live('click',function (event) {
		$('#temp').remove();
        $('.list_addtocart .button').removeAttr('onclick');
        event.preventDefault();
        var eventParent = $(event.target).parents('td');
		if (eventParent.length < 1){
			eventParent = $(event.target).parents('li');
		}
        var picture = eventParent.find('.list_picture img');
        var inputs = eventParent.find('.list_actions :input');
		if (ajaxstart==false){
        $.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: inputs,
			beforesend: function(){
				ajaxstart=true;
			},
			success: function (html) {
				$('#module_cart .middle').html(html);
			},
			complete: function () {
				var image = picture.offset();
				var cart  = $('#module_cart').offset();

				picture.before('<img src="' + picture.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 () {
					ajaxstart = false;
					$('#temp').remove();
				});
			}
		});
		}
    });
});
