$(document).ready(function () {
	var ajaxstart = false;
	//ajax add from flypage
	$('#add_to_cart').removeAttr('onclick');
	$('#add_to_cart').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() / 2,
					heigth: $('#module_cart').height()
				};
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});
			}
		});
	});

	//ajax add from other pages
	// GAB 2011.05.05
	$('.list_addtocart .button').live('click',function (event) {
		$('#temp').remove();
		$('.list_addtocart .button').removeAttr('onclick');
		event.preventDefault();
		var eventParent = $(event.target).parents('td');
		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() / 2,
						heigth : $('#module_cart').height()
					};

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

	//ajax add from other pages
	// Vince 2011.09.09
	$('.ptable .add_to_cart_button').removeAttr('onclick').live('click',function (event) {
		$('#temp').remove();
		event.preventDefault();
		$('.ptable .add_to_cart_button');
		var row = $(event.target).parents('tr');
		var inputs = row.find(':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 cart  = $('#module_cart').offset();
					$('body').append('<table id="temp" class="ptable"><tbody><tr class="ptable_tr">'+row.html()+'</tr></tbody></table>');
					$('#temp').css({
						position:'absolute',
						top: row.offset().top + 'px',
						left: row.offset().left + 'px',
						width: row.width(),
						heigh: row.height()
					});
					params = {
						top : cart.top + 'px',
						left : cart.left + 'px',
						opacity : 0.0,
						width : $('#module_cart').width() / 2,
						heigth : $('#module_cart').height()
					};
					$('#temp').animate(params, 'slow', false, function () {
						ajaxstart = false;
						$('#temp').remove();
					});
				}
			});
		}
	});
});

