function addToCart() {
	$('.cart_add').unbind('click');  //Clears all Add to Cart

	$('.cart_add').click(function() {
		var id = $(this).attr('id');
		var id_split = id.split('_');
		var name = $(this).attr('title');
		addlist(id_split[0], id_split[1], name);
	});
}

function addlist(type, id, name) {
	$.ajax({
		type: "POST",
		url: themeurl + "/cart.php",
		data: 'type=' + encodeURIComponent(type) + '&id=' + encodeURIComponent(id) + '&name=' + encodeURIComponent(name),
		dataType: 'json',
		async: false,
		beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } },
		success: function(msg, status){
			$.gritter.add({ title: 'Added to cart', text: msg.name, sticky: false, time: 1000, });
		}
	});
}

function emptyCart() {
	$.ajax({
		type: "POST",
		url: themeurl + "/cart.php",
		data: 'emptycart=' + encodeURIComponent('1'),
		dataType: 'json',
		async: false,
		beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } },
		success: function(msg, status){
			$('#tablecart tbody').html('<tr><td width="80"></td><td>Your Cart is Empty</td><Td></td></tr>');
			$('#cart_buttons').hide();
			$('#cart_form').hide();
		}
	});
}

function updateCart(cartObj) {
	$.ajax({
		type: "POST",
		url: themeurl + "/cart.php",
		data: { updatecart: cartObj },
		dataType: 'json',
		async: false,
		success: function(msg, status){
			
		}
	});
}

$(document).ready(function() {
	addToCart();
	$('.tablesorter th').click(function() { setTimeout ('addToCart()', 200); });
	$('.pager img').click(function() { addToCart(); });
	$('.pager select').change(function() { addToCart(); });
	$('#empty_cart').click(function() { emptyCart(); });


	$('.req').keyup(function() {
		var process = new Array();
		$('.req').each(function() { if($(this).val() === '') { process.push(false); } });
		if(process.length == 0) { $('#submit_cart').attr('disabled', ''); } //enable form submit
	});


	$('.qtyi').keyup(function() { $(this).replaceAttr(); });


	$('.removerow').click(function() {
		rows = $(this).parents('table').find('tbody tr');

		$(this).parents('tr').remove(); //Removes current row
		$('#tablecart').trigger("applyWidgets"); //Apply Zebra to rows
		$('#update_cart').click(); //updats Session Cart

		if(rows.length == 1) {
			$('#tablecart tbody').html('<tr><td width="80"></td><td>Your Cart is Empty</td><Td></td></tr>');
			$('#cart_buttons').hide();
			$('#cart_form').hide();
		}
	});


	$('#update_cart').click(function() {
		var table = $(this).parents('#content').find('#tablecart td[id]');
		var cart = new Array();

		for(var i = 0; i < table.length; i++) {
			var id = $($(table).get(i)).attr('id');
			var id_split = id.split('_');
			var qty = $($($(table).get(i)).find('input').get(0)).val();
			var item = {};

			item['id'] = id_split[0]; //ID
			item['type'] = id_split[1]; //Type
			item['qty'] = qty; //Quanity
			cart.push(item);
		}

		updateCart(cart);
	});
});


jQuery.fn.replaceAttr = function() { 
	var new_value = $(this).val().replace(/[^0-9]/g, ""); 
	if(new_value == '') { new_value = 1;  }
	$(this).val(new_value); 
	return parseFloat(new_value); 
};

