$(document).ready(function() {
													 
	// Variant Drop Down
	$('#vid').change(function() {
		$.ajax({
			url: "/inc/ajax/prod_var_price.php",
			global: false,
			type: "POST",
			dataType: "html",
			data: "pid=" + $('#pid').val() + "&vid=" + $('#vid').val(),
			success: function(msg){
				$('#prodprice').html(msg); // Show New price
				// Query max QTY
				$.ajax({
					url: "/inc/ajax/prod_var_instock.php",
					global: false,
					type: "POST",
					dataType: "html",
					data: "pid=" + $('#pid').val() + "&vid=" + $('#vid').val(),
					success: function(msg){
						// If variant not in stock
						if (msg == 0) {
							$('#qtybuttons').css('display','none');
							$('#addtobasket').css('display','none');
						} else {
							$('#qtybuttons').css('display','block');
							$('#addtobasket').css('display','block');
						}
					}
				});
			}
		});

	});
	
	// Add to basket button
	$('a#addtobasket').click(function() {
		if ($('#qty').val() > 0) {
			$.ajax({
				url: "/inc/ajax/cart_add.php",
				global: false,
				type: "POST",
				dataType: "html",
				data: "pid=" + $('#pid').val() + "&vid=" + $('#vid').val() + "&qty=" + $('#qty').val(),
				success: function(msg){
					$.ajax({
						url: "/inc/ajax/cart_update_mini.php",
						global: false,
						type: "POST",
						dataType: "html",
						success: function(msg){
							$('#cartmini').html(msg);	
						}
					});
					cartAdded('Shopping bag updated',msg); // Show Dialog box
				}
			});
		}
		return false;
	});
	
	// Discount code button
	$('a#usediscount').click(function() {
		discountCode(); return false;
	});
	$('#dcodefrm').submit(function() {
		discountCode(); return false;
	});
	$('a#changediscount').click(function() {
		changeDiscount();
	});
	
	
})

function proceedPayment(type) {
	$.ajax({
		url: "/inc/ajax/cart_proceed_payment.php?type=" + type,
		global: false,
		type: "POST",
		dataType: "html",
		success: function(msg){
			$('#' + type).html(msg);
			//if (confirm("Proceeed?")) {
				$('#' + type + ' form').submit();
			//}
		}
	});
}

function changeDiscount() {
	$('#currentcode').hide();
	$('#dcode').show();
	$('#usediscount').show();
}

function discountCode() {
	$.ajax({
		url: "/inc/ajax/cart_dcode.php",
		global: false,
		type: "POST",
		dataType: "html",
		data: "dcode=" + $('#dcode').val(),
		success: function(msg) {
			if (msg == 0) {
				okBox('Discount Code','The discount code you have entered was invalid, please try again'); // Show Dialog box						
			} else {
				$('#currentcode').html("<span>" + $('#dcode').val() + "</span> (<a id=\"changediscount\" onclick=\"changeDiscount();\">Change</a>)");
				$('#currentcode').show();
				$('#dcode').hide();
				$('#usediscount').hide();				
				cartRender();
			}
		}
	});		
}

function qtyChange(option,cartid,cart) {
	var currentqty = cleanNum($('#qty'+cartid).val());
	var newqty = currentqty;	
	if (option == "add") {
		newqty = newqty + 1;
	} else if (newqty > 1) {
		newqty -= 1;
	}
	if (newqty == 1) {
		$('#qty'+cartid+'minus').addClass('disabled');
	} else {
		$('#qty'+cartid+'minus').removeClass('disabled');
	}
	$('#qty'+cartid).val(newqty);
	if (cart) updateCartTotals(cartid);
}

function qtyEnter(qty,cartid,cart) {
	cleanqty = cleanNum(qty,1);
	$('#qty'+cartid).val(cleanqty);
	if (cleanqty == 1) {
		$('#qty'+cartid+'minus').addClass('disabled');
	} else {
		$('#qty'+cartid+'minus').removeClass('disabled');
	}
	if (cart) updateCartTotals(cartid);
}

function updateCartTotals(cartid) {
	// Update actual QTY first
	$.ajax({
		url: "/inc/ajax/cart_update.php",
		global: false,
		type: "POST",
		dataType: "html",
		data: "cid=" + cartid + "&qty=" + $('#qty'+cartid).val(),
		success: function(msg) {
			cartRender();
		}
	});
}

function cartRender() {
	//!TODO Update item total/sub total
	cartheight = $('#cartholder').height();
	$('#cartholder').addClass('loading');
	$('#cartholder').html('');
	$('#cartholder').css('height',cartheight);
	$.ajax({
		url: "/inc/ajax/cart_render.php",
		global: false,
		type: "POST",
		dataType: "html",
		success: function(msg) {
			$('#cartholder').css("height", 'auto');
			$('#cartholder').removeClass('loading');
			$('#cartholder').html(msg);
		}
	});
}

function cartDelete(cid) {
	$('#cart'+cid).hide();
	$.ajax({
		url: "/inc/ajax/cart_delete.php",
		global: false,
		type: "POST",
		dataType: "html",
		data: "cid=" + cid,
		success: function(msg) {
			cartRender();
		}
	});
}

function cartAdded(title,subtitle) {
	$.ui.dialog.defaults.bgiframe = true;
	$("#dialog").html(subtitle);
	$("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		width: 350,
		height: 160,
		modal: true,
		title: title,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			},
			'View Shopping Bag': function() {
				document.location = '/bag';
			}
		}
	});
	$("#dialog").dialog('open');
}
