var item = {};
var itemDrawName = function () {
	$("#text .name").html(item.name);
}
var itemDrawItemNo = function () {
	$("#text .itemno").html("<em>Item No. " + item.itemno + "</em>");
}
var itemDrawDescription = function () {
	$("#text .description").html(item.description);
}
var itemDrawOptions = function () {
	for (var i = 0; i < item.options.length; i++) {
		$("#text .options")
			.append("<p></p>").children("p:last")
				.addClass("option")
				.html(item.options[i].name + ":")
				.append("<select></select>").children("select:last")
					.each(function () {
						for (var j = 0; j < item.options[i].options.length; j++) {
							$(this)
								.append("<option></option>").children("option:last")
									.attr("value", j)
									.attr("price", item.options[i].options[j].price)
									.html(item.options[i].options[j].name)
								.end()
							.end();
						}
					})
					.change(itemDrawPrice)
				.end()
			.end()
		.end();
	}
}
var itemDrawPrice = function () {
	var price = item.baseprice;
	$("#text p.option select")
		.each(function () {
			price += parseFloat($(this).children("option[value=" + $(this).val() + "]:first").attr("price"));
		})
	.end();
	item.price = price;
	var fmt = (""+price).split(".");
	if (fmt.length < 2) {
		fmt[1] = "00";
	} else if (fmt[1].length == 1) {
		fmt[1] = "0" + fmt[1];
	}
	var fmtp = fmt[0] + "." + fmt[1];
	$("#text .add")
		.html("Price: &nbsp; ")
		.append("<span></span>").children("span:last")
			.addClass("currency")
			.html("$")
		.end()
		.append("<span></span>").children("span:last")
			.addClass("price")
			.html(fmtp)
		.end()
		.append("<button></button>").children("button:last")
			.html("Add to Cart")
			.click(function () {
				cartAdd(itemAsCartObject());
			})
		.end()
	.end();
}
var itemAsCartObject = function () {
	var opt = [];
	$("#text p.option select").each(function (i) {
		opt[i] = $(this).children("option[value=" + $(this).val() + "]").html();
	});
	return {
		dbid: item.dbid,
		name: item.name,
		itemno: item.itemno,
		price: item.price,
		options: opt
	};
}
var itemDraw = function (data) {
	item = data;
	$("#text")
		.empty()
		.removeClass()
		.addClass("item")
		.append("<h2></h2>").children("h2:last")
			.addClass("name")
		.end()
		.append("<h5></h5>").children("h5:last")
			.addClass("itemno")
		.end()
		.append("<p></p>").children("p:last")
			.addClass("description")
		.end()
		.each(function () {
			if (data.category != 9) {
				$(this)
					.append("<div></div>").children("div:last")
						.addClass("options")
					.end()
					.append("<p></p>").children("p:last")
						.addClass("add")
					.end()
				.end();
			}
		})
		.show()
	.end();
	$("#display")
		.empty()
		.removeClass()
		.addClass("item")
		.append("<img/>").children("img:last")
			.attr("src", item.img)
		.end()
		.show()
	.end();
	itemDrawName();
	itemDrawItemNo();
	itemDrawDescription();
	itemDrawOptions();
	itemDrawPrice();
	triggerListener("pageLoad:item");
};
