function ProductList() {
	this.items = new Array();
	this.slots = new Array();
	this.discounts = new Array();
	this.validations = new Array();
	this.request = null;
	this.currentCategory = null;
	this.currentGroup = null;
	this.currentProduct = null;
	this.currentNumber = 0;
	this.onLoad = function() {};
	this.onFailure = function() {};
	
	ProductList.prototype.load = function(url) {
		Progress.show("Nahrávám nabídku...");
		this.request = new AjaxRequest();
		this.request.onSuccess = delegate(this, this.success);
		this.request.onFailure = delegate(this, this.failure);
		this.request.get(url);
	};

	ProductList.prototype.failure = function() {
		Progress.hide();
		Error.show("Nepodařilo se nahrát konfiguraci.\n" + this.request.responseText);
		this.onFailure();
	};
	
	ProductList.prototype.success = function() {
		Progress.hide();
		var document = this.request.responseDocument;
		for (var i = 0; i < document.childNodes.length; i++) {
			this.parse("", document.childNodes[i]);
		}
		this.onLoad();
	};

	ProductList.prototype.parse = function(path, node) {
		var attrs = node.attributes;
		switch (path + "/" + node.nodeName) {
		case "/nabidky":	
			this.type = AttrReader.asString(attrs, "charakterUctu");
			break;
		case "/nabidky/nabidka":	
			this.currentNumber = 0;
			this.currentCategory = new Category();
			this.currentCategory.price = AttrReader.asNumber(attrs, "cena");
			this.currentCategory.id = AttrReader.asNumber(attrs, "id");
			this.currentCategory.name = AttrReader.asString(attrs, "name");
			break;
		case "/nabidky/nabidka/pasma/pasmo":	
			var tariff = new Tariff();
			tariff.id = AttrReader.asNumber(attrs, "id");
			tariff.min = AttrReader.asNumber(attrs, "min");
			tariff.max = AttrReader.asNumber(attrs, "max");
			tariff.price = AttrReader.asNumber(attrs, "cena");
			this.currentCategory.tariffs.push(tariff);
			break;
		case "/nabidky/nabidka/klicovky/pasma/hodnoceni":	
			var rating = new Rating();
			rating.min = AttrReader.asNumber(attrs, "minimum");
			rating.points = AttrReader.asNumber(attrs, "body");
			this.currentCategory.ratings.push(rating);
			break;
		case "/nabidky/nabidka/klicovky/produkty/produkt":	
			var rating = new Rating();
			rating.productId = AttrReader.asNumber(attrs, "id");
			rating.points = AttrReader.asNumber(attrs, "body");
			this.currentCategory.ratings.push(rating);
			break;
		case "/nabidky/nabidka/skupiny/skupina":	
			this.currentNumber = 0;
			this.currentGroup = new Group();
			this.currentGroup.id = AttrReader.asNumber(attrs, "id");
			this.currentGroup.name = AttrReader.asString(attrs, "name");
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt":	
			this.currentProduct = new Product();
			this.currentProduct.category = this.currentCategory;
			this.currentProduct.group = this.currentGroup;
			this.currentProduct.id = AttrReader.asNumber(attrs, "id");
			this.currentProduct.name = AttrReader.asString(attrs, "name");
			this.currentProduct.fullName = AttrReader.asString(attrs, "full_name");
			if (this.currentProduct.fullName == null) {
				this.currentProduct.fullName = this.currentProduct.name;
			}
			this.currentProduct.multiple = AttrReader.asNumber(attrs, "pocet");
			this.currentProduct.price = AttrReader.asNumber(attrs, "cena");
			this.currentProduct.version = AttrReader.asString(attrs, "version");
			this.currentProduct.number = ++this.currentNumber;
			this.items.push(this.currentProduct);
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/charakteristika":	
			if (node.firstChild != null) {
				this.currentProduct.description = node.firstChild.nodeValue;
			}
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/benefity/benefit":	
			var benefit = new Benefit();
			benefit.id = AttrReader.asNumber(attrs, "id");
			benefit.priority = AttrReader.asNumber(attrs, "priorita");
			if (node.firstChild != null) {
				benefit.text = node.firstChild.nodeValue;
			}
			this.currentProduct.benefits.push(benefit);
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/argumenty/argument":	
			var argument = new Argument();
			argument.type = AttrReader.asNumber(attrs, "typ");
			if (node.firstChild != null) {
				argument.text = node.firstChild.nodeValue;
			}
			this.currentProduct.arguments.push(argument);
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/mimo/product":
			var rule = new Rule();
			rule.productId = AttrReader.asNumber(attrs, "id");
			this.currentProduct.cantBe.push(rule);
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/mimo/product/upozorneni":
			var currentRule = this.currentProduct.cantBe[this.currentProduct.cantBe.length - 1];
			if (node.firstChild != null) {
				// IE vs. Mozilla workaround
				if (node.childNodes.length == 1) currentRule.message = node.childNodes[0].nodeValue;
				if (node.childNodes.length > 1) currentRule.message = node.childNodes[1].nodeValue;
			}
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/soucasne/product":
			var rule = new Rule();
			rule.productId = AttrReader.asNumber(attrs, "id");
			this.currentProduct.mustBe.push(rule);
			break;
		case "/nabidky/nabidka/skupiny/skupina/produkty/produkt/soucasne/upozorneni":
			var currentRule = this.currentProduct.mustBe[this.currentProduct.mustBe.length - 1];
			if (node.firstChild != null) {
				// IE vs. Mozilla workaround
				if (node.childNodes.length == 1) currentRule.message = node.childNodes[0].nodeValue;
				if (node.childNodes.length > 1) currentRule.message = node.childNodes[1].nodeValue;
			}
			break;
		case "/nabidky/nabidka/skupiny/skupina/grupy/grupa":	
			var slot = new Slot();
			slot.id = AttrReader.asString(attrs, "id");
			slot.name = AttrReader.asString(attrs, "name");
			slot.tooltip1 = AttrReader.asString(attrs, "tooltip1");
			slot.tooltip2 = AttrReader.asString(attrs, "tooltip2");
			slot.title = AttrReader.asString(attrs, "title");
			//alert(slot.name);
			this.slots.push(slot);
			break;
		case "/nabidky/nabidka/skupiny/skupina/grupy/grupa/produkt":	
			var currentSlot = this.slots[this.slots.length - 1];
			var id = AttrReader.asString(attrs, "id");
			currentSlot.products.push(this.find(id));
			break;
		case "/slevy/sleva":	
			var discount = new Discount();
			discount.id = AttrReader.asNumber(attrs, "id");
			discount.amount = AttrReader.asNumber(attrs, "sleva");
			discount.transactions = AttrReader.asBoolean(attrs, "transakce");
			discount.income = AttrReader.asNumber(attrs, "obrat");
			discount.billance = AttrReader.asNumber(attrs, "bilance");
			this.discounts.push(discount);
			break;
		case "/validace/kontrola":	
			var validation = new Validation();
			validation.name = AttrReader.asString(attrs, "name");
			this.validations.push(validation);
			break;
		case "/validace/kontrola/typ":	
			var currentValidation = this.validations[this.validations.length - 1];
			if (node.firstChild != null) {
				// IE vs. Mozilla workaround
				if (node.childNodes.length == 1) currentValidation.type = node.childNodes[0].nodeValue;
				if (node.childNodes.length > 1) currentValidation.type = node.childNodes[1].nodeValue;
			}
			break;
		case "/validace/kontrola/upozorneni":	
			var currentValidation = this.validations[this.validations.length - 1];
			if (node.firstChild != null) {
				// IE vs. Mozilla workaround
				if (node.childNodes.length == 1) currentValidation.message = node.childNodes[0].nodeValue;
				if (node.childNodes.length > 1) currentValidation.message = node.childNodes[1].nodeValue;
			}
			break;
		case "/validace/kontrola/soucet_levy/produkt":	
			var currentValidation = this.validations[this.validations.length - 1];
			currentValidation.leftProducts.push(AttrReader.asNumber(attrs, "id"));
			break;
		case "/validace/kontrola/soucet_pravy/produkt":	
			var currentValidation = this.validations[this.validations.length - 1];
			currentValidation.rightProducts.push(AttrReader.asNumber(attrs, "id"));
			break;
		}
		for (var i = 0; i < node.childNodes.length; i++) {
			if (node.childNodes[i].nodeType == 1) {
				this.parse(path + "/" + node.nodeName, node.childNodes[i]);
			}
		}
	};

	ProductList.prototype.find = function(id) {
		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i].id == id) {
				return this.items[i];
			}
		}
		return null;
	};

	ProductList.prototype.findCategory = function(id) {
		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i].category.id == id) {
				return this.items[i].category;
			}
		}
		return null;
	};

	ProductList.prototype.getDiscount = function(transactions, income, billance) {
		var perIncome = 0;
		var perBillance = 0;
		// sleva za obrat a bilanci
		for (var i = 0; i < this.discounts.length; i++) {
			var discount = this.discounts[i];
			if (transactions == discount.transactions && income >= discount.income) {
				perIncome = discount.amount;
			}
			if (transactions == discount.transactions && billance >= discount.billance) {
				perBillance = discount.amount;
			}
		}
		// plati vyssi sleva
		if (perIncome > perBillance) {
			return perIncome;
		}
		else {
			return perBillance;
		}
	};

}

