// JavaScript Document

var hostname = window.location.hostname;
if(hostname=='localhost'){
	var hostname = 'pennyme.com.ar';
}

$(document).ready(function()
{

	$('.add_to_cart').click(function()
	{
		var producto_id = $(this).attr('id');
		add_to_cart(producto_id);
	});

	$('.delete_item').click(function()
	{
		var producto_id = $(this).attr('id');
		delete_item(producto_id);
		$(this).parent().parent().fadeOut();
	});

	$('.update_item').click(function()
	{
		
		var producto_id = $(this).attr('id');
		update_item(producto_id);
	});

});

function add_to_cart(producto_id)
{

	var data = {	action: 'add_item',
					nombre: $('#producto_'+producto_id+'_nombre').html(),
					producto_id: producto_id,
					cantidad: $('#producto_'+producto_id+'_cantidad').val(),
					precio: $('#producto_'+producto_id+'_precio').val(),
					talle_id: $('#talle_id').val(),
					color_id: $('.'+producto_id+':checked').val()
			   };

	$.ajax({
		url: "http://"+hostname+"/home/add_item",
		type: "POST",
		data: data,
		cache: false,
		dataType: 'json',

		//success  
		success: function (html) {
			//if process.php returned 1/true (send mail success)  
			 if (html.success == true)
			 {
				 $('#shopping_cart_total').html(html.total); 
				 $('#shopping_cart_items').html(html.total_items);
		 		$('.agregar_carrito').html(html.message);
			 }
			 else
			 {
		 		$('.agregar_carrito').html(html.message);
			 }
		}         
	});
}

function update_item(producto_id, qty)
{

	var data = {	action: 'update_item',
					row_id: producto_id,
					qty: qty
			   };

	$.ajax({
		url: "http://"+hostname+"/home/update_item",
		type: "POST",
		data: data,
		cache: false,
		dataType: 'json',
	
		//success  
		success: function (html) {
			//if process.php returned 1/true (send mail success)  
			 if (html.success == true)
			 {
				$('#shopping_cart_total').html(html.total); 
				$('#shopping_cart_items').html(html.total_items);
				$('.shopping_cart_total').html(html.total); 
				$('.shopping_cart_items').html(html.total_items);
				 return true;
				 // window.location.href='/kismehurlingham.com/mi-carrito';
			 }
			 else
			 {
				$('#shopping_cart_total').html(html.total); 
				$('#shopping_cart_items').html(html.total_items);
				$('.shopping_cart_total').html(html.total); 
				$('.shopping_cart_items').html(html.total_items);
				 return false;
			 }
		}         
	});
}

function delete_item(producto_id)
{

	var data = {	action: 'delete_item',
					row_id: producto_id,
					qty: 0
			   };

	$.ajax({
		url: "http://"+hostname+"/home/update_item",
		type: "POST",
		data: data,
		cache: false,
		dataType: 'json',
	
		//success  
		success: function (html) {
			//if process.php returned 1/true (send mail success)  
			 if (html.success == true)
			 {
				$('#shopping_cart_total').html(html.total); 
				$('#shopping_cart_items').html(html.total_items);
				$('.shopping_cart_total').html(html.total); 
				$('.shopping_cart_items').html(html.total_items);

				 return true;
				 // window.location.href='/kismehurlingham.com/mi-carrito';
			 }
			 else
			 {
				$('#shopping_cart_total').html(html.total); 
				$('#shopping_cart_items').html(html.total_items);
				$('.shopping_cart_total').html(html.total); 
				$('.shopping_cart_items').html(html.total_items);
				 return false;
			 }
		}         
	});
}


