function requestCartState() {
	$.getJSON("/proxy.php?r=site/cartState", null, function(json) {
		showCartState(json.response.qty);
	});
}

function showCartState(qty) {
	if (qty > 0)
		$("#cart-qty").html("&nbsp;(&nbsp;" + qty + "&nbsp;)");
	else
		$("#cart-qty").html('');
}

function showAuthState() {
	$("#auth_area").load("/proxy.php?r=site/authstate");
}

$(document).ready(function() {
	$(this).ajaxStart(function() {
		$('body').append('<div id="ajax-loader"><img src="/images/loader.gif"></div>');
	});

	$(this).ajaxStop(function() {
		$("div#ajax-loader").remove();
	});

	showAuthState();
	
	$(".request a").live('click', function() {
		var obj = this;
		var pn = $(obj).html();
		var manufacturer = null;
		var package = null;
		
		if ($(obj).parents("div.product").length > 0) {
			var parent = $(this).parents("div.product");
			pn = $(".product_name a", parent).html();
			manufacturer = $('.info span[title="Manufacturer"] i', parent).html();
			package = $('.info span[title~="Case"] i', parent).html();
		}

		if ($(obj).parents("div.content_outer").children("table.parse_table_prod").length > 0) {
			var parent = $(this).parents("div.content_outer").children("table.parse_table_prod:eq(0)");
			pn = $("tr:eq(0) td:eq(1)", parent).html();
			manufacturer = $("tr:eq(1) td:eq(1)", parent).html();
			package = $('div.content_outer tr td[title~="Case"]').next().html();
		}

		manufacturer = (manufacturer) ? manufacturer : '';
		package = (package) ? package : '';

		$.post(obj.href, {pn: pn, package: package, manufacturer: manufacturer}, function(json) {
			if (json.response) {
				if (json.response['@'].status == 'success') {
					showCartState(json.response.qty);

					$("div#cart-request-added").css(
						{
							opacity: 0.0,
							left: $(window).width() / 2 + $(window).scrollLeft() + "px",
							top: $(window).height() / 2 + $(window).scrollTop() +  "px"
						}).show()
						.animate({opacity: 1.0}, {duration:500, queue: true})
						.animate({opacity: 0.0}, {duration:500, queue: true});

					$(obj).html(json.response.label);
				}
			}
		}, 'json');

		return false;
	});
	
	$("#wizard-login").live('click', function() {
		var data = $(this).parents("form#login-form").serializeArray();
		$("#login-errors").html('');
		$.post(this.href, data, function(json) {
			if (json.response) {
				if (json.response['@'].status == 'error') {
					if (json.response.auth) {
						var ul = $("<ul></ul>");
						for (var name in json.response.auth.errors) {
							ul.append("<li>" + json.response.auth.errors[name] + "</li>");
						}
						$("#login-errors").append(ul).show();
					} else {
						alert(json.response['@'].error);
					}
				} else {
					$("div.jqmWrap").remove();
					$("div.jqmOverlay").remove();
					$.modal.close();
					showAuthState();
					$("a#cart-request").click();
				}
			}
		}, 'json');

		return false;
	});

	$("#wizard-logout").live('click', function() {
		$.post(this.href, null, function(json) {
			if (json.response)
				if (json.response['@'].status == 'error') {
						alert(json.response['@'].error);
			}
			
			showAuthState();
		}, 'json');

		return false;
	});
	
	$(".cart-remove").live('click', function() {
		$(this).parents("tr:first").remove();
		return false;
	});

	$(".cart-add").live('click', function() {
		var proto = $(this).parents("tr.prototype-pn");
		var protoI = $(this).parents(".request-block_positions");
		var obj = proto.clone();
		obj.removeClass("prototype-pn").addClass("list-pn");
		$("a.cart-add", obj).remove();
		$("a.cart-remove", obj).show();

		var id = proto.siblings().length;
		$("input.request-partnumber", obj).attr("name", "pn[" + id + "][name]");
		$("input.request-manufacturer", obj).attr("name", "pn[" + id + "][brand]");
		$("input.request-case", obj).attr("name", "pn[" + id + "][case]");
		$("input.request-qty", obj).attr("name", "pn[" + id + "][qty]");
		$("input.request-notes", obj).attr("name", "pn[" + id + "][notes]");

		$("input.request-partnumber", proto).val("");
		$("input.request-manufacturer", proto).val("");
		$("input.request-case", proto).val("");
		$("input.request-qty", proto).val("");
		$("input.request-notes", proto).val("");
		
		$('.request-block_positions tbody').append(obj);
		$("tr.list-pn", protoI).remove();
	});

	$(".cart-show").live('click', function() {
		$.modal.close();
		$(".right_column_content").html('');
		$(".content_outer").html('');
		$("#api-body").html('').load("/proxy.php?r=site/cart").show();
		return false;
	});

	$("a#cart-request").live('click', function() {

		var data = $("#cart form").serializeArray();

		$("#cart-errors").html('');
		$("#cart table tr.errors").removeClass('errors');

		$.post(this.href, data, function(json) {
			if (json.response) {
				if (json.response['@'].status == 'error') {
					var error = json.response['@'].error;

					//must login?
					if (json.response['@'].code) {
						if (json.response['@'].code == 'resolve-user') {
							$(".wizard-signup").click();
							$(".regist .aut-form-reg-tr").show();
							//alert('23123');
							//$.modal.close();
							//$("#modal").load('/proxy.php?r=site/auth', null, function() {
							//	$("#modal").modal({autoResize:true});
							//	wizard.bind();
							//});
						} else {
							alert(error);
						}
					} else {
						if (json.response.request) {
							var ul = $("<ul></ul>");

							if (json.response.request.errors) {
								var id = parseInt(json.response.request.i);
								$("#cart form table tr.list-group:eq(" + id + ")").addClass('errors');
								for (var name in json.response.request.errors) {
									ul.append("<li>" + json.response.request.errors[name] + "</li>");
								}
							} else if (json.response.request.position.errors) {
								var id = parseInt(json.response.request.position.i);
								$("#cart form table tr.list-pn:eq(" + id + ")").addClass('errors');

								for (var name in json.response.request.position.errors) {
									ul.append("<li>" + json.response.request.position.errors[name] + "</li>");
								}
							}

							$("#cart-errors").append(ul).show();
						} else {
							alert(error);
						}
					} //else(response.code)
				} else {
					$("#cart").html('');
					$("#cart").load("/proxy.php?r=site/cartSuccess");
					requestCartState();
				}
			} //response
		}, 'json');

		return false;
	});
});

