function ShippingByWeightController(){
	var selects = [];

	this.onMethodChanged = function (select) {
		var selectWrapper = this.getSelectWrapper(select);
		if (this.aLotOfBooksFromSeller(selectWrapper)) {
			if (getWeightDepended(selectWrapper.getSelectedOption().value)) {
				if (this.confirmSetShipping()) {
					this.markOtherShippingForSeller(selectWrapper, true);
				} else {
					this.selectWeightDepended(selectWrapper, false);
				}
			}else {
				this.markOtherShippingForSeller(selectWrapper, false);
			}
		}
		select.form.submit();
	}

	this.aLotOfBooksFromSeller = function(selectWrapper) {
		for (var i = 0; i < selects.length; i++) {
			if (selects[i].select.id != selectWrapper.select.id
					&& selects[i].sellerId == selectWrapper.sellerId) {
				return true;
			}
		}
		return false;
	}

	this.confirmSetShipping = function() {
		return confirm("Setting 'Shipping by weight' for this method will set all methods for the books buying from the same seller. Continue?");
	}

	this.registerOption = function(select, methodId, weightDepended, sellerId) {
		var selectWrapper = this.createSelectWrapper(select);
		//alert(selectWrapper);
		selectWrapper.sellerId = sellerId;
		setWeightDepended(methodId, weightDepended);
	}

	this.getSelectWrapper = function (select) {
		for (var i = 0; i < selects.length; i++) {
			if (select.id == selects[i].select.id) {
				return selects[i];
			}
		}
	}

	this.createSelectWrapper = function (select) {
		for (var i = 0; i < selects.length; i++) {
			if (select.id == selects[i].select.id) {
				return selects[i];
			}
		}
		var result = new SelectWrapper(select);
		selects[selects.length] = result;
		return result;
	}

	this.markOtherShippingForSeller = function(selectWrapper, mark) {
		for (var i = 0; i < selects.length; i++) {
			if (selects[i].select.id != selectWrapper.select.id
					&& selects[i].sellerId == selectWrapper.sellerId) {
				this.selectWeightDepended(selects[i], mark);
			}
		}
	}

	this.selectWeightDepended = function(selectWrapper, mask) {
		if (!mask && !getWeightDepended(selectWrapper.getSelectedOption().value)){
			return;
		}
		for (var i = 0; i < selectWrapper.select.options.length; i++) {
			var option = selectWrapper.select.options[i];
			if (getWeightDepended(option.value) == mask) {
				selectWrapper.selectValue(option.value);
				return;
			}
		}
	}

	var weightDependedMap = {};

	function setWeightDepended(methodId, weightDepended){		
		eval("weightDependedMap.m" + methodId + "=" + weightDepended);
	}

	function getWeightDepended(methodId){
		return eval("weightDependedMap.m" + methodId);
	}

}
