jQuery(document).ready(function(){
	if($('#total_articles').html() != "$ MXN "){
		$('#cart').slideDown(1000);
		$('#carrito').attr('id','carrito-over');
	}

	$('button[name=how_many]').live('click',function(){
		//getting the quantity
		value = $('#quantity').val();
		article_id = $('input[name=article_id]').val();	
        store = $(this).attr('rel');

		$.ajax({
			type: "POST",
			url: "/store/"+store+"/store/recalculate",
			data: "quantity="+value+"&article_id="+article_id,
			success: function(msg){
				var price = JSON.parse(msg);

		
				total = price.iva + price.price;
	
				$('#iva').html("$"+price.iva+".00");
				$('#subtotal').html("$"+price.price+".00");
				$('#total').html("$"+total+".00");
			}
		});
	});

	$('button[name=update_cart]').live('click',function(){
		values = $('#save_cart').serialize();
		store = $(this).attr('rel');
		
		$.ajax({
			type: "POST",
			url: "/store/"+store+"/store/save_cart/",
			data: values,
			success: function(msg){
				if(msg == "true"){

					//close the facebox
					jQuery(document).trigger('close.facebox');	

					//update the cart
					update_cart(store);	
				}
			}
		});
	});

	$(".quick_add").click(function(){
		article_id = $(this).attr('rel');
		article_id = article_id.split('/');
		store = article_id[1];
		article_id = article_id[3];

		$.ajax({
			type: "POST",
			data: "article_id="+article_id,
			url: "/store/"+store+"/store/save_cart/",
			success: function(msg){
				if(msg == "true"){
					//update the cart
					update_cart(store);
					$('#cart').slideDown(1000);
					$('#carrito').attr('id','carrito-over');
				}
			}
		});
		return false;
	});

	$('#update_cart_sidebar').live('submit',function(){
		values = $(this).serialize();
        store = $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: $(this).attr("action"),
			data: values,
			success: function(msg){
				update_cart(store);
			}
		});
		return false;
	});


	//enviar cotización
	$('#cotizacion').live('submit',function(){
		values = $(this).serialize();
		action= $(this).attr('action');
		$.ajax({
			type: "POST",
			url: action,
			data:  values,
			success: function(msg){
				$('#request').html(msg);
			}
		});
		return false;
	});;
});


update_cart = function(store){
	$.ajax({
		type: "POST",
		url: "/store/"+store+"/store/update_cart_ajax",
		success: function(msg){
		    $('#cart').html(msg);
		}
	});
}
