﻿$(document).ready(function() {   
    //$('#cart').slideUp(1);
    product_id = 0;
    option_id = 0;
	
    $('#showCart').click(function()  {
        $.get('/shopping-cart/view', function(msg){
        	jQuery("#cart").html(msg);
        	$('#cartHead').html($('#total').html());
        	$('#cart').slideToggle('slow');
        	
        	start = $('#cartSlider li').length;
			jQuery('#cartSlider').jcarousel({
				vertical: true,
				scroll: 3,
				start: start,
				wrap: null,
				buttonNextHTML: '<span id="nextBtn"></span>',
				buttonPrevHTML: '<span id="prevBtn"></span>'
			});
        });
    });
	
	// Add item to cart
	$('#addToCart').click(function() {
		// Show cart and update
		getDetails();
		updateCart(product_id, option_id);
	});
});

function getDetails() {
	product_id = $('#productId').val();
	option_id =  $('#optionId').val();
}

function updateCart(product, option) {
	jQuery.ajax({
					url:	"/shopping-cart/add",
					type:	"POST",
					data:	{ id: product, option_id: option },
					dataType:	"html",
					success: function(msg) {
						jQuery("#cart").html(msg);
						$('#cartHead').html($('#total').html());
						if($('#cart').is(':hidden')) {
							$('#cart').slideDown('slow');
							setTimeout("$('#cart').slideUp('slow')", 3000);
						}
						start = $('#cartSlider li').length;
						jQuery('#cartSlider').jcarousel({
							vertical: true,
							scroll: 3,
							start: start,
							wrap: null,
							buttonNextHTML: '<span id="nextBtn"></span>',
							buttonPrevHTML: '<span id="prevBtn"></span>'
						});
					} // success
				}).responseText;
	
	
}

function removeItemFromCart(item_id) {
	jQuery.ajax({
				url:	"/shopping-cart/remove/"+item_id,
				type:	"GET",
				data:	{},
				dataType:	"html",
				success: function(msg)  {
					jQuery("#cart").html(msg);
					$('#cartHead').html($('#total').html());
					
					start = $('#cartSlider li').length;
					jQuery('#cartSlider').jcarousel({
						vertical: true,
						scroll: 3,
						start: start,
						wrap: null,
						buttonNextHTML: '<span id="nextBtn"></span>',
						buttonPrevHTML: '<span id="prevBtn"></span>'
					});
				
				} // success
	}).responseText;
}jQuery;
