function bind(object, fnc) {
	var args = [];
	for (var i=2; i<arguments.length; ++i) {
		args.push(arguments[i]);
	}
	return function() {
		var args2 = [];
		for (var i = 0; i < arguments.length; i++) {
			args2.push(arguments[i]);
		}
		return fnc.apply(object, args.concat(args2));
	}
}

function bindAsEventListener(object, fnc) {
	return function(event) {
		return fnc.apply(object, [event || window.event]);
	}
}

function inArray(a, value) {
	var i;
	for (i=0; i < a.length; i++) {
		if (a[i] === value) {
			return true;
		}
	}
	return false;
}

function trace(v) {
	alert(v);
}

function $() {
	var elements = [];
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			element = document.getElementById(element);
		}
		if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}
	return elements;
}

ItemController = function() {
	this.view = {}
	this.id = "item-selector-";

	this.params = [];
	this.items = [];
	this.values = [];
}
ItemController.i18n = {
	"submit" : "Add to Cart"
};

ItemController.prototype.bind = function(container, form, submit) {
	while (container.hasChildNodes()) {
		container.removeChild(container.firstChild);
	}
	this.view.selects = [];
	for (var i=0; i < this.params.length; i++) {
		var name = this.params[i];

		var select = document.createElement("SELECT");
		select.id = this.id + name;
		var label = document.createElement("LABEL");
		label.setAttribute("for", this.id + name);
		label.appendChild(document.createTextNode(ItemController.i18n[name] ? ItemController.i18n[name] : name));
		this.view.selects[name] = select;
		select.onchange = bind(this, this.updateView, name);
		container.appendChild(this.createOptionPane(label, select));
	}
	this.view.form = form;
	if (submit) {
		this.view.submit = submit;
	} else {
		this.view.submit = document.createElement("BUTTON");
		this.view.submit.appendChild(document.createTextNode(ItemController.i18n["submit"]));
		container.appendChild(this.view.submit);
	}
	if (this.view.submit.nodeName.toLowerCase() == "input" && this.view.submit.type == "submit") {
	} else {
		this.view.submit.onclick = function(e) {
			if (this.disabled) {
				return;
			}
			form.submit();
		};
	}
	// if (count(this.items) == 1) :
	// @note since this.items.length won't return the count
	var c = 0;
	var item = null;
	for (var i in this.items) {
		c++;
		if (item == null) {
			item = this.items[i];
		}
	}
	if (c == 1) {
		this.populateAfter();
		this.updateSubmit(item.href);
	} else {
	// else
		for (var i in item.params) {
			var name = this.params[i];
			this.values[name] = item.params[i];
		}
		this.populateAfter();
		this.populateAfter(this.params[0]);
		var item = this.find(this.values);
		this.updateSubmit(item ? item.href : null);
	}
	// endif
}
ItemController.prototype.populateAfter = function(name) {
	var found = name == null;
	var values = [];
	for (var j=0; j < this.params.length; j++) {
		var _name = this.params[j];
		values[_name] = this.values[_name];
		if (found) {
			this.populate(j, values);
		} else {
			found = name == _name;
		}
	}
}
ItemController.prototype.populate = function(paramIndex, values) {
	if (!values) var values = [];
	var name = this.params[paramIndex];
	var select = this.view.selects[name];

	var value = select.value;
	var selValue = select.value;
	var options = select.options;
	while (options.length > 0) {
		select.removeChild(options[0]);
	}

	var keys = this.getUniqueKey(paramIndex);
	for (var i=0,kl=keys.length; i < kl; ++i) {
		var _value = keys[i];
		values[name] = _value;
		if (this.find(values)) {
			var option = document.createElement("OPTION");
			option.value = _value;
			option.appendChild(document.createTextNode(_value));
			select.appendChild(option);
			if (option.value == selValue) {
				select.selectedIndex = i;
				try {
					option.selected = true;
				} catch (ex) {}
			}
		}
	}
	if (select.options.length == 0) {
		this.values[name] = null;
	} else {
		if (select.selectedIndex == -1) {
			select.selectedIndex = 0;
			try {
				select.options[0].selected = true;
			} catch (ex) {}
		}
		var val = this.values[name];
		for (var i=0,a=select.options,l=a.length; i < l; ++i) {
			if (a[i].value == val) {
				select.selectedIndex = i;
				try {
					a[i].selected = true;
				} catch (ex) {}
			}
		}
		this.values[name] = select.options[select.selectedIndex].value;
	}
}
ItemController.prototype.createOptionPane = function(label, select) {
	var div = document.createElement("DIV");
	div.appendChild(label);
	div.appendChild(select);
	return div;
}
ItemController.prototype.updateView = function(name) {
	this.values[name] = this.view.selects[name].value;
	this.populateAfter(name);
	var item = this.find(this.values);
	this.updateSubmit(item ? item.href : null);
}
ItemController.prototype.updateSubmit = function(href) {
	if (!href) {
		this.view.submit.disabled = true;
	} else {
		this.view.submit.disabled = false;
		this.view.form.action = href;
	}
},
ItemController.prototype.find = function (values) {
	for (var i in this.items) {
		var item = this.items[i];
		if (typeof(item) != "function") {
			var match = true;
			for (var j=0; j < this.params.length; j++) {
				if (typeof(values[this.params[j]]) != "undefined" && item.params[j] != values[this.params[j]]) {
					var match = false;
				}
			}
			if (match) {
				return item;
			}
		}
	}
}
ItemController.prototype.getUniqueKey = function(index) {
	var keys = [];
	for (var i in this.items) {
		if (typeof(this.items[i]) != "function") {
			var value = this.items[i].params[index];
			if (!inArray(keys, value)) {
				keys.push(value);
			}
		}
	}
	return keys;
}



















/*


ItemController = {
	params : [],
	items : [],
	values : [],
	i18n : [],
	view : null,
	bind : function(container, form, submit) {
		this.view = {};
		while (container.hasChildNodes()) {
			container.removeChild(container.firstChild);
		}
		var creator = function(name) {
			return function(e) {
				ItemController.updateView(name);
			}
		}
		this.view.selects = Array();
		for (var i=0; i < this.params.length; i++) {
//			this.values[name] = null;
			var name = this.params[i];

			var select = document.createElement("SELECT");
			select.id = "item-selector-" + name;
			var label = document.createElement("LABEL");
			label.setAttribute("for", "item-selector-" + name);
			label.appendChild(document.createTextNode(ItemController.i18n[name] ? ItemController.i18n[name] : name));
			this.view.selects[name] = select;
			select.onchange = creator(name);
			container.appendChild(this.createOptionPane(label, select));

//			var keys = this.getUniqueKey(i);
//			for (var j=0; j < keys.length; j++) {
//				var option = document.createElement("OPTION");
//				option.value = keys[j];
//				option.appendChild(document.createTextNode(keys[j]));
//				select.appendChild(option);
//			}
		}
		this.view.form = form;
		if (submit) {
			this.view.submit = submit;
		} else {
			this.view.submit = document.createElement("BUTTON");
			this.view.submit.appendChild(document.createTextNode("add to cart"));
			container.appendChild(this.view.submit);
		}
		this.view.submit.onclick = function(e) {
			if (this.disabled) {
				return;
			}
			form.submit();
		};
		// if (count(this.items) == 1) :
		// @note since this.items.length won't return the count
		var c = 0;
		var item = null;
		for (var i in this.items) {
			c++;
			if (item == null) {
				item = this.items[i];
			}
		}
		if (c == 1) {
			this.updateSubmit(item.href);
		} else {
		// else
			for (var i in item.params) {
				var name = this.params[i];
				this.values[name] = item.params[i];
			}

//			for (var name in this.view.selects) {
//				this.values[name] = this.view.selects[name].value
//			}
			this.populateAfter();
			this.populateAfter(this.params[0]);
			var item = this.find(this.values);
			this.updateSubmit(item ? item.href : null);
		}
		// endif
	},
	populateAfter : function(name) {
		var found = name == null;
		var values = [];
		for (var j=0; j < this.params.length; j++) {
			var _name = this.params[j];
			values[_name] = this.values[_name];
			if (found) {
				this.populate(j, values);
			} else {
				found = name == _name;
			}
		}
	},
	populate : function(paramIndex, values) {
		if (!values) var values = [];
		var name = this.params[paramIndex];
		var select = this.view.selects[name];

		var value = select.value;
		var selValue = select.value;
		var options = select.options;
		while (options.length > 0) {
			select.removeChild(options[0]);
		}

		var keys = this.getUniqueKey(paramIndex);
		for (var i=0,kl=keys.length; i < kl; ++i) {
			var _value = keys[i];
			values[name] = _value;
//			alert(values.join(","));
			if (this.find(values)) {
				var option = document.createElement("OPTION");
				option.value = _value;
				option.appendChild(document.createTextNode(_value));
				select.appendChild(option);
				if (option.value == selValue) {
					select.selectedIndex = i;
				}
			}
		}
		if (select.options.length == 0) {
			this.values[name] = null;
		} else {
			if (select.selectedIndex == -1) {
				select.selectedIndex = 0;
			}
			this.values[name] = select.options[select.selectedIndex].value;
		}
	},
	createOptionPane : function(label, select) {
		var div = document.createElement("DIV");
		div.appendChild(label);
		div.appendChild(select);
		return div;
	},
	updateView : function(name) {
		try {
		this.values[name] = this.view.selects[name].value;
		this.populateAfter(name);
		var item = this.find(this.values);
		this.updateSubmit(item ? item.href : null);
		} catch (e) {
			alert(e.message);
		}
	},
	updateSubmit : function(href) {
		if (!href) {
			this.view.submit.disabled = true;
		} else {
			this.view.submit.disabled = false;
			this.view.form.action = href;
		}
	},
	find : function (values) {
//		var join = "";
//		for (var i in values) {
//			join += i+":"+values[i]+"\n";
//		}
//		alert(join);

		for (var i in this.items) {
			var item = this.items[i];
			if (typeof(item) != "function") {
				var match = true;
				for (var j=0; j < this.params.length; j++) {
					if (typeof(values[this.params[j]]) != "undefined" && item.params[j] != values[this.params[j]]) {
						var match = false;
					}
				}
				if (match) {
					return item;
				}
			}
		}
	},
	getUniqueKey : function(index) {
		var keys = [];
		for (var i in this.items) {
			if (typeof(this.items[i]) != "function") {
				var value = this.items[i].params[index];
				if (!this.arrayContains(keys, value)) {
					keys.push(value);
				}
			}
		}
		return keys;
	},
	arrayContains : function (a, value) {
		var i;
		for (i=0; i < a.length; i++) {
			if (a[i] === value) {
				return true;
			}
		}
		return false;
	}
};
*/
