var cart 			= new Object();
cart.url 			= 'cfc/frontend/io/jquery/cart.cfc';
cart.successCode 	= '';



cart.updateCart = function() {
	cart.updateCartProduct();
	cart.updateCartPrice();
	cart.updateCartMissing();
	cart.updateCartTotal();
};


cart.getCount = function() {
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartCount'},
	  success:function(response){
	  	//console.log(response)
	  }
	});

};

cart.checkCode = function(code) {
	
	$.ajax({
	  url: cart.url,
	  data:{method:'checkCode',code:code},
	  success:function(response){
	  	if(response == 1){
	  		cart.successCode = code;
	  		cart.updateCart();
	  	}else{
	  		if($('input.code').hasClass('valid')){
	  			$('input.code').val(cart.successCode);
	  		}else{
	  			$('input.code').addClass('invalid');
	  		}		
	  	}
	  	
	  }
	});
};

cart.updateCartBox = function() {
	
	showLoading($("#cart"));
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartBoxHtml'},
	  success:function(response){
	  	$("#cart").html(response);
	  }
	});

};

cart.updateCartProduct = function() {
	
	showLoading($(".products"));
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartProductHtml'},
	  success:function(response){
	  	$(".products").html(response);
	  }
	});

};

cart.updateCartPrice = function() {

	showLoading($(".details"));
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartPriceHtml'},
	  success:function(response){
	  	$(".details").html(response);
	  }
	});

};

cart.updateCartMissing = function() {
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartMissingHtml'},
	  success:function(response){
	  	$(".missing").html(response);
	  }
	});

};

cart.updateCartTotal = function() {
	
	showLoading($(".total"));
	
	$.ajax({
	  url: cart.url,
	  data:{method:'getCartTotalHtml'},
	  success:function(response){
	  	$(".total").html(response);
	  }
	});

};


cart.clear = function() {

	$.ajax({
	  url: cart.url,
	  data:{method:'clear'},
	  success:function(){
	  	cart.updateCart();
	  }
	});
};

cart.addProduct = function(id,count) {

	$.ajax({
	  url: cart.url,
	  data:{method:'addProduct',id:id,count:count},
	  success:function(){
	  	cart.updateCartBox();
	  	
		$( "#cart-message" ).dialog({
			height: 150,
			resizable:false,
			draggable:false,
			modal: true,
			buttons:[
			    {
			        text: "Zum Warenkorb",
			        click: function() { window.location = "?page=Warenkorb" }
			    },{
			        text: "Ok",
			        click: function() { $(this).dialog("close") }
			    }
			]
		});
		$( ".selector" ).dialog( "option", "buttons", { "Ok": function() { $(this).dialog("close") } } );
		
	  }
	});

};

cart.increaseCount = function(id) {

	$.ajax({
	  url: cart.url,
	  data:{method:'increaseCount',id:id},
	  success:function(){
	  	cart.updateCart();
	  }
	});

};

cart.decreaseCount = function(id) {

	$.ajax({
	  url: cart.url,
	  data:{method:'decreaseCount',id:id},
	  success:function(){
	  	cart.updateCart();
	  }
	});

};

cart.setCount = function(id,count) {

	$.ajax({
	  url: cart.url,
	  data:{method:'setCount',id:id,count:count},
	  success:function(){
	  	cart.updateCart();
	  }
	});

};

cart.setCountEvent = function(id,count,event) {
	
	if(event[0].keyCode == 13) {
		$.ajax({
			url: cart.url,
			data:{method:'setCount',id:id,count:count},
			success:function(){
				cart.updateCart();
			}
		});
	}

};


