/**
 *	Cart functions
 *
 *	Developed for Greenflame
 *	October 2011
 *
 *	Version 1.0
 */
 
//-------------------------------------------------------------------
 
 
/**
 *	Add to cart
 */ 
function addToCart(values) {
	$.ajax({
		url: "/cart/add",
		async: true,
		cache: false,
		type: 'post',
		data: values,
		statusCode: {
	    	404: function() {
				$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
	    	}
	  	},
		error:function (xhr, ajaxOptions, thrownError) {
			$.jGrowl(xhr.status);
			$.jGrowl(thrownError);
		},
		success: function(data) {
			if(data == 'success') {
				$.jGrowl('Varen er lagt i kurven');
				updateCartView();
				showCart();
			} else if(data == 'negative_stock') {
				$.jGrowl('Så mange er der ikke på lager!', { header: 'UPS!' , life: 6000 });
			} else if(data == 'no_size') {
				$.jGrowl('Du har glemt at vælge en størrelse!', { header: 'UPS!' , life: 6000 });
			} else if(data == 'missing_required_value') {
				$.jGrowl('Der gik noget galt! Varen blev derfor IKKE lagt i kurven.', { header: 'UPS!' , life: 6000 });
			} else {
				$.jGrowl('Der gik noget galt! Varen blev derfor IKKE lagt i kurven.', { header: 'UPS!' , life: 6000 });
			};
		}
	});
}


/**
 *	Remove from cart
 */ 
function removeFromCart(row) {
	$.ajax({
		url: "/cart/remove",
		async: true,
		cache: false,
		type: 'post',
		data: {
			row: row
		},
		statusCode: {
	    	404: function() {
				$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
	    	}
	  	},
		error:function (xhr, ajaxOptions, thrownError) {
			$.jGrowl(xhr.status);
			$.jGrowl(thrownError);
		},
		success: function(data) {
			$.jGrowl('Varen er fjernet fra kurven');
			updateCartView();
		}
	});
}


/**
 *	Update item in cart
 */ 
function updateItemInCart(row,qty) {
	$.ajax({
		url: "/cart/update",
		async: true,
		cache: false,
		type: 'post',
		data: {
			row: row,
			qty: qty
		},
		statusCode: {
	    	404: function() {
				$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
	    	}
	  	},
		error:function (xhr, ajaxOptions, thrownError) {
			$.jGrowl(xhr.status);
			$.jGrowl(thrownError);
		},
		success: function(data) {
			$.jGrowl('Antal er rettet');
			updateCartView();
		}
	});
}


/**
 *	Update cart view
 */ 
function updateCartView() {
	// Side cart
	$.ajax({
		url: "/cart/update_cart_view",
		async: true,
		cache: false,
		type: 'post',
		statusCode: {
	    	404: function() {
				$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
	    	}
	  	},
		error:function (xhr, ajaxOptions, thrownError) {
			$.jGrowl(xhr.status);
			$.jGrowl(thrownError);
		},
		success: function(data) {
			//$.jGrowl(data);
			$('.sideBar .kurv').html(data);
		}
	});
	
	// Top cart
	$.ajax({
		url: "/cart/update_top_cart_view",
		async: true,
		cache: false,
		type: 'post',
		statusCode: {
	    	404: function() {
				$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
	    	}
	  	},
		error:function (xhr, ajaxOptions, thrownError) {
			$.jGrowl(xhr.status);
			$.jGrowl(thrownError);
		},
		success: function(data) {
			//$.jGrowl(data);
			$('.miniMenu .kurv').html(data);
		}
	});
}


/**
 *	Show cart
 */
function showCart() {
	
}


/**
 *	Hide cart
 */
function hideCart() {
	
}

