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) {
	itemstmp=[];
	for (var i in this.items) {
		item=this.items[i];
		if (item.params!=undefined) itemstmp.push(item);
	}
	this.items=itemstmp;
	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();
		};
	}
	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 {
		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);
	}
}

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" && typeof(values[this.params[j]])!="function" && 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;
}

