var totalDesigns;
var currentDesign = 1;
var item = 1;
var total;
var subtotal;
var shippingfee = 0;

$.fn.delay = function(time, callback){
	jQuery.fx.step.delay = function(){};
	return this.animate({delay:1}, time, callback);
}
function resetform(){
 	document.preorderform.reset();
}
function nextdesign(){
	if (currentDesign >= totalDesigns){
		currentDesign = 0;
		$("#designs").animate({
			left:"760px"
		},1);
	}
	var nextPos = (currentDesign*-762)+"px";
	$("#designs").animate({
		left:nextPos
	},400)
	$(".design").animate({
		opacity:0
	},200).animate({
		opacity:1
	},400);
	currentDesign += 1;
}
function prevdesign(){
	if (currentDesign <= 1){
		currentDesign = totalDesigns+1;
		var pos = (totalDesigns+1)*-762;
		$("#designs").animate({
			left:pos
		},1);
	}
	var prevPos = ((currentDesign-2)*-762)+"px";
	$("#designs").animate({
		left:prevPos
	},400)
	$(".design").animate({
		opacity:0
	},200).animate({
		opacity:1
	},400);
	currentDesign -= 1;
}
function updateTotal(){
	subtotal = item*16;
	$("#subtotal_value").html("$"+subtotal);
	shippingfee = eval($("#shipping_method").val());
	$("#total_value").html("$"+eval(subtotal+shippingfee));
}
$(document).ready(function(){
	totalDesigns = $(".design").length;
	
	$(":input").change(function () {
		updateTotal();
	});
	$(".item").hide();
	$(".item:first").show();
	$(".button_add").click(function(){
		if (item < 5){
			item += 1;
			$(".button_add").hide();
			$(".button_add").eq(item-1).show();
			$(".button_remove").hide();
			$(".button_remove").eq(item-2).show();
			$("#item_"+item).show();
			updateTotal();
		}
	});
	$(".button_remove").click(function(){
		if (item > 1){
			item -= 1;
			$(".button_remove").hide();
			$(".button_remove").eq(item-2).show();
			$(".button_add").eq(item-1).show()
			$("#item_"+eval(item+1)).hide();
			updateTotal();
		}
	});
	$("#submitbutton").click(function(){
		$(".msg").animate({
			opacity:0
		},200);
		setTimeout(function(){
			var name = $("#name").val();
			var email = $("#email").val();
			var d1 = $("#design_1").val();
			var d2 = $("#design_2").val();
			var d3 = $("#design_3").val();		
			var d4 = $("#design_4").val();
			var d5 = $("#design_5").val();		
			var s1 = $("#size_1").val();
			var s2 = $("#size_2").val();
			var s3 = $("#size_3").val();
			var s4 = $("#size_4").val();
			var s5 = $("#size_5").val();
			var shipping = $("#shipping_method").val();
			var remark = $("#remark").val();
			var subtotal = $("#subtotal_value").html().substr(1);
			
			$.ajax({
				type: "POST",
				url: "preorder.php",
				data: "action=preorder&name="+name+"&email="+email+"&d1="+d1+"&d2="+d2+"&d3="+d3+"&d4="+d4+"&d5="+d5+"&s1="+s1+"&s2="+s2+"&s3="+s3+"&s4="+s4+"&s5="+s5+"&shipping="+shipping+"&remark="+remark+"&item="+item,
				success:function(msg){
					result = msg.split('|');
	            	if (result[0] == 0){
	            		$("#message0").html(result[1]).animate({
	            			opacity:1
	            		},300);
	            		resetform();
	            		window.location = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=ajnichols%40Mac%2ecom&lc=US&item_name=Take%20a%20Tee%20Order&amount="+subtotal+"%2e00&currency_code=USD&button_subtype=products&shipping="+shipping+"%2e00&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHosted";
	            	}else{
	            		$("#message"+result[0]).html(result[1]).animate({
	            			opacity:1
	            		},300);
	            	}
	            }
			});
		},200);
		return false;
	});
});

