ObjectManager.load("css/controls.css");

var MyOrder = {
	initialize: function() {
		MyOrder.node = Innecto.$("myorder");
		MyOrder.input = new Input(MyOrder);
		EventManager.add(
			window,
			"load",
			function() {
				MyOrder.input.initialize();
				Popup.initialize();
				MyOrder.input.eventify(
					"myorder-shipping-sameasbilling",
					"click",
					MyOrder.sameAsBilling);
				Sly.search(
					".toolbar-continue").forEach(
					function(el) {
						EventManager.add(
							el,
							"click",
							function(e) {
								if (MyOrder.input.validate()) MyOrder.commit(e);
								e.preventDefault();
							});
					});
				Sly.search(
					".toolbar-delete, .toolbar-update").forEach(
					function(el) {
						EventManager.add(
							el,
							"click",
							MyOrder.commit);
					});
				MyOrder.sameAsBilling();
			});
	},
	sameAsBilling: function() {
		var Checkbox = MyOrder.input.controls["myorder-shipping-sameasbilling"];
		if (Checkbox.checked) {
			MyOrder.input.controls["myorder-shipping-firstname"].disable();
			MyOrder.input.controls["myorder-shipping-lastname"].disable();
			MyOrder.input.controls["myorder-shipping-address1"].disable();
			MyOrder.input.controls["myorder-shipping-address2"].disable();
			MyOrder.input.controls["myorder-shipping-city"].disable();
			MyOrder.input.controls["myorder-shipping-stateprovince"].disable();
			MyOrder.input.controls["myorder-shipping-zippostalcode"].disable();
			MyOrder.input.controls["myorder-shipping-country"].disable();
		}
		else {
			MyOrder.input.controls["myorder-shipping-firstname"].enable();
			MyOrder.input.controls["myorder-shipping-lastname"].enable();
			MyOrder.input.controls["myorder-shipping-address1"].enable();
			MyOrder.input.controls["myorder-shipping-address2"].enable();
			MyOrder.input.controls["myorder-shipping-city"].enable();
			MyOrder.input.controls["myorder-shipping-stateprovince"].enable();
			MyOrder.input.controls["myorder-shipping-zippostalcode"].enable();
			MyOrder.input.controls["myorder-shipping-country"].enable();
		}
	},
	commit: function(e) {
		var selEl = e.target;
		switch (selEl.name) {
			case "continue":
				Popup.setContent("<p><img src=\"/images/popup_indicator.gif\"> Sending order data...</p>");
				Popup.show();
				window.setTimeout(
					function() {
						Ajax.asyncRequest(
							"POST",
							"commands/myorder.php",
							Ajax.serialize(
								"continue",
								"Continue to order confirmation") + "&" + MyOrder.input.serialize(),
							{
								success: function(Obj) {
									window.location = "http://" + window.location.hostname + "/confirmorder.php";
									Popup.hide();
								}
							});
					},
					1000);
				break;
			case "delete":
				Popup.setContent("<p><img src=\"/images/popup_indicator.gif\"> Deleting selected item(s) from your order...</p>");
				Popup.show();
				window.setTimeout(
					function() {
						Ajax.asyncRequest(
							"POST",
							"commands/myorder.php",
							Ajax.serialize(
								"delete",
								"Delete selected item(s) from my order") + "&" + MyOrder.input.serialize(),
							{
								success: function(Obj) {
									Sly.search(
										".control-checkbox.checked",
										Innecto.$("myorder-items")).forEach(
										function(el) {
											var TR = el.parentNode.parentNode;
											TR.parentNode.removeChild(TR);
										});
									var Response = new Ajax.parseRequestResponse(Obj.response);
									var subtotal = 0;
									for ( var property in Response.data) {
										if (Response.data.hasOwnProperty(property)) {
											var Obj = Response.data[property];
											subtotal += Obj["price"] * Obj["quantity"];
										}
									}
									Sly.search(".shoppingcart-items-price.subtotal")[0].innerHTML = subtotal.toCurrency();
									Cufon.refresh();
									window.setTimeout(
										Popup.hide,
										1);
								}
							});
					},
					1000);
				break;
			case "update":
				if (!MyOrder.input.hasErrors()) {
					Popup.setContent("<p><img src=\"/images/popup_indicator.gif\"> Updating your order...</p>");
					Popup.show();
					window.setTimeout(
						function() {
							Ajax.asyncRequest(
								"POST",
								"commands/myorder.php",
								Ajax.serialize(
									"update",
									"Update my order") + "&" + MyOrder.input.serialize(),
								{
									success: function(Obj) {
										var Response = new Ajax.parseRequestResponse(Obj.response);
										var subtotal = 0;
										var totals = Sly.search(".shoppingcart-items-price.total");
										var i = 0;
										for ( var property in Response.data) {
											if (Response.data.hasOwnProperty(property)) {
												var Obj = Response.data[property];
												var total = Obj["price"] * Obj["quantity"];
												subtotal += total;
												totals[i].innerHTML = total.toCurrency();
												i++;
											}
										}
										Sly.search(".shoppingcart-items-price.subtotal")[0].innerHTML = subtotal.toCurrency();
										Cufon.refresh();
										window.setTimeout(
											Popup.hide,
											1);
									}
								});
						},
						1000);
				}
				break;
		}
		e.preventDefault();
	}
};
