var eshop = {

	products: [],
	
	add_cart: function(id)
	{		
		
		$.ajax({
			url: "/eshop/",
			dataType: "json",
			type: "POST",
			data: {
				action: 'add_cart',
				productid: id,
				order_count: 1
			},
			success: function(data){
				if (data.status == 'ok')
				{
				
					if (eshop.products[id] == undefined)
						eshop.products[id] = 0;
					eshop.products[id]++;
					
					var pos = $('.cart').position();
					var o = $('.product-item-photo-'+id).parent().parent().position();		
	
					var obj = $('.product-item-photo-'+id).clone().css({
						display: 'block',
						left:o.left,
						top:o.top,
						position: 'absolute',
						width: 272,
						height: 197,
						'z-index': 1000
					});
					
					obj.appendTo('.product-to-cart');
					
					$('.product-to-cart').css({display: 'block'});
					
					obj.animate({
									left: pos.left,
									top: pos.top,									
									opacity: 0.2
								}, 500, function(){
									$('.product-item-photo-'+id).css({display: 'none'});
									$('.product-to-cart').html('');
								});
				
					$('#cart-count').html(data.cart_count);
					$('#cart-price').html(data.cart_price);
					$('#cart-weight').html(data.cart_weight);
					$('#cart-calories').html(data.cart_calories);
				}
			}
		});

	},
		
	remove_cart: function(id)
	{
		$.ajax({
			url: "/eshop/",
			dataType: "json",
			type: "POST",
			data: {
				action: 'remove_product_cart',
				productid: id
			},
			success: function(data){
				if (data.status == 'ok')
				{
					if (eshop.products[id] >= 0)
					{
					
						var pos = $('.cart').position();
						var o = $('.product-item-photo-'+id).parent().parent().position();		
					
						var obj = $('.product-item-photo-'+id).clone().css({
							display: 'block',
							left:pos.left,
							top:pos.top,
							position: 'absolute',
							width: 272,
							height: 197,
							'z-index': 1000
						});
						
						obj.appendTo('.product-to-cart');
						
						$('.product-to-cart').css({display: 'block'});
						
						obj.animate({
										left: o.left,
										top: o.top,						
										opacity: 0.2
									}, 500, function(){
										$('.product-item-photo-'+id).css({display: 'none'});
										$('.product-to-cart').html('');
									});
						eshop.products[id]--;		
						if (eshop.products[id] == 0)
							eshop.products[id] = undefined;
						
					}	
					$('#cart-count').html(data.cart_count);
					$('#cart-price').html(data.cart_price);
					$('#cart-weight').html(data.cart_weight);
					$('#cart-calories').html(data.cart_calories);
				}
			}
		});
	},
	
	add_cart_small: function(id)
	{		
		$.ajax({
			url: "/eshop/",
			dataType: "json",
			type: "POST",
			data: {
				action: 'add_cart',
				productid: id,
				order_count: 1
			},
			success: function(data){
				if (data.status == 'ok')
				{
					$('#cart-count').html(data.cart_count);
					$('#cart-price').html(data.cart_price);
					$('#cart-weight').html(data.cart_weight);
					$('#cart-calories').html(data.cart_calories);
					
					if (eshop.products[id] == undefined)
						eshop.products[id] = 0;
					eshop.products[id]++;
					
					var pos = $('.cart').position();
					var o = $('.product-small-item-'+id+' .product-photo').position();		
						
					var obj = $('.product-small-item-'+id+' .product-photo').clone().css({
						display: 'block',
						left:o.left,
						top:o.top,
						position: 'absolute',
						width: 150,
						height: 120,
						'z-index': 1000
					});
					
					obj.appendTo($('.product-to-cart'));
					
					obj.animate({
						left: pos.left,
						top: pos.top,
						opacity: 0.2
					}, 500, function(){						
						$('.product-to-cart').html('');
					});				
				}
			}
		});

	},
	
	remove_cart_small: function(id)
	{
		$.ajax({
			url: "/eshop/",
			dataType: "json",
			type: "POST",
			data: {
				action: 'remove_product_cart',
				productid: id
			},
			success: function(data){
				if (data.status == 'ok')
				{
					if (eshop.products[id] >= 0)
					{
					
						var pos = $('.cart').position();
						var o = $('.product-small-item-'+id+' .product-photo').position();		
					
						var obj = $('.product-small-item-'+id+' .product-photo').clone().css({
							display: 'block',
							left:pos.left,
							top:pos.top,
							position: 'absolute',
							width: 150,
							height: 120,
							'z-index': 1000
						});
						
						obj.appendTo('.product-to-cart');
						
						$('.product-to-cart').css({display: 'block'});
						
						obj.animate({
										left: o.left,
										top: o.top,						
										opacity: 0.2
									}, 500, function(){
										$('.product-to-cart').html('');
									});
						eshop.products[id]--;		
						if (eshop.products[id] == 0)
							eshop.products[id] = undefined;
						
					}	
					$('#cart-count').html(data.cart_count);
					$('#cart-price').html(data.cart_price);
					$('#cart-weight').html(data.cart_weight);
					$('#cart-calories').html(data.cart_calories);
				}
			}
		});
	},
	
	more_announce: function(id, action)
	{
		if (action == "open")
		{
			$('.product-shadow').css({
				'height': $(document).height(),
				'display': 'block'				
			}).click(function(){
				$('.more-announce-'+id).slideUp('slow');
				$('.product-shadow').css({
					'display': 'none'
				}).unbind('click');
			});
			$('.more-announce-'+id).slideDown('slow');
		}
		else if (action == "close")
		{
			$('.more-announce-'+id).slideUp('slow');
		}
	}	
};

$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed:'normal',
		theme:'facebook',
		slideshow:3000,
		social_tools: '',
		overlay_gallery: false,
		deeplinking: false
	});
	
	var margin = 0;
	switch(screen.width)
	{
		case 1024:
			margin = 20;
			break;
		case 1280:
			margin = 1;
			break;
		case 1920:
			margin = 0;
			break;
	}
	$('.product-item').css({
		'margin-right': margin
	});
});
