﻿if (typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() {
		return this.replace( /^\s+|\s+$/g , '');
	};
}

function populateDropdown(dropdown, options) {
	dropdown.children("option[value!='']").remove();
	for (var index in options) {
		dropdown.append($("<option></option>").
			attr("value", options[index].value).
			text(options[index].name));
	}
}

function populateProducts(categoryNodeId, brandNodeId, callback) {
	$.getJSON("?ajax=products&brandNodeId=" + brandNodeId + "&categoryNodeId=" + categoryNodeId, function (products) {
		var productDropdown = $("#product-dropdown");

		productDropdown.removeAttr("disabled");
		productDropdown.prev(".dropdown-bg").removeClass("disabled");
		populateDropdown(productDropdown, products);
		updateDropdownText(productDropdown);

		productDropdown.change(function () {
			updateDropdownText(productDropdown);
		});

		if (callback) callback();
	});
}

function updateDropdownText(dropdown) {
	var newtext = dropdown.children("option:selected").text().trim();
	dropdown.prev(".dropdown-bg").text(newtext);
}

$(document).ready(function () {
	$.getJSON("?ajax=categories", function (categories) {
		var categoryDropdown = $("#category-dropdown");
		var brandDropdown = $("#brand-dropdown");
		var productDropdown = $("#product-dropdown");

		categoryDropdown.before("<div class='dropdown-bg'/>");
		brandDropdown.before("<div class='dropdown-bg'/>");
		productDropdown.before("<div class='dropdown-bg'/>");

		updateDropdownText(categoryDropdown);
		updateDropdownText(brandDropdown);
		updateDropdownText(productDropdown);

		brandDropdown.prev(".dropdown-bg").addClass("disabled");
		productDropdown.prev(".dropdown-bg").addClass("disabled");
		brandDropdown.attr("disabled", "disabled");
		productDropdown.attr("disabled", "disabled");

		populateDropdown(categoryDropdown, categories);

		categoryDropdown.change(function () {
			updateDropdownText(categoryDropdown);
			productDropdown.children("option[value!='']").remove();
			productDropdown.attr("disabled", "disabled");
			productDropdown.prev(".dropdown-bg").addClass("disabled");
			updateDropdownText(productDropdown);

			var categoryNodeId = categoryDropdown.children("option:selected").val();

			if (categoryNodeId == "") {
				brandDropdown.children("option[value!='']").remove();
				brandDropdown.attr("disabled", "disabled");
				brandDropdown.prev(".dropdown-bg").addClass("disabled");
				updateDropdownText(brandDropdown);
			}

			if ($("#brand-dropdown").length == 0) {
				// Brand is locked, go directly to products
				populateProducts(categoryNodeId, "");
			}
			else {
				populateBrands(brandDropdown, categoryNodeId, productDropdown);
			}
		});

		// For result page, set pre-selected options
		$.getJSON("?ajax=selected", function (selected) {
			if (selected == null) return;
			if (selected.category) {
				populateBrands(brandDropdown, selected.category, productDropdown, function () {
					$("#brand-dropdown option[value=" + selected.brand + "]").attr('selected', true);
					updateDropdownText(brandDropdown);
				});

				populateProducts(selected.category, selected.brand, function () {
					$("#product-dropdown option[value=" + selected.product + "]").attr('selected', true);
					updateDropdownText(productDropdown);
				});

				$("#category-dropdown option[value=" + selected.category + "]").attr('selected', true);
				updateDropdownText(categoryDropdown);
			}
		});
	});

	$("#xref-search-button").click(function () {
		var productDropdown = $("#product-dropdown");

		if (productDropdown.val() != "")
			window.location = productDropdown.val();

		return false;
	});
});

function populateBrands(brandDropdown, categoryNodeId, productDropdown, callback) {
	$.getJSON("?ajax=brands&categoryNodeId=" + categoryNodeId, function (brands) {
		brandDropdown.removeAttr("disabled");
		brandDropdown.prev(".dropdown-bg").removeClass("disabled");
		populateDropdown(brandDropdown, brands);
		updateDropdownText(brandDropdown);
		brandDropdown.change(function () {
			updateDropdownText(brandDropdown);
			var brandNodeId = brandDropdown.children("option:selected").val();

			if (brandNodeId == "") {
				productDropdown.children("option[value!='']").remove();
				productDropdown.attr("disabled", "disabled");
				productDropdown.prev(".dropdown-bg").addClass("disabled");
				updateDropdownText(productDropdown);
			}
			else {
				populateProducts(categoryNodeId, brandNodeId);
			}
		});
		if (callback) callback();
	});
}
