var SelectControl = Class.create();
SelectControl.prototype = {
	initialize: function(leftid, rightid, buttonToLeft, buttonToRight, sourceListLeft, sourceListRight) {
		//this.baseuri = baseuri;
		this.leftid = leftid;
		this.rightid = rightid;
		this.buttonToLeftId = buttonToLeft;
		this.buttonToRightId = buttonToRight;
    this.left = sourceListLeft;
    this.right = sourceListRight;
    this.optionTemplate = new Template('<option value="#{value}">#{name}</option>');
    this.onloadDocument = this.loadDocumentHandler.bindAsEventListener(this);
	},

	loadDocumentHandler: function() {
		this.sleft = $(this.leftid);
  	this.sright = $(this.rightid);
    this.buttonToLeft = $(this.buttonToLeftId);
    this.buttonToRight = $(this.buttonToRightId);
	  Event.observe(this.buttonToLeft,'click', this.clickToLeftHandler.bindAsEventListener(this));
	  Event.observe(this.buttonToRight,'click', this.clickToRightHandler.bindAsEventListener(this));
    this.update();
	},

	clickToLeftHandler: function(e) {
		if (typeof console!='undefined') console.log('=> ',$F(this.sright));
		this.right = this.move(this.sright, this.right, this.left);
    this.update();
	},

	clickToRightHandler: function(e) {
		var tmp = $F(this.sleft);
		if (typeof console!='undefined') console.log('=> ',tmp);
		this.left = this.move(this.sleft, this.left, this.right);
    this.update();
	},

	move: function(sourceSelect, sourceList, targetList) {
    var values, length = sourceSelect.length;
    if (!length) return;
    var toRemove = [];
    for (var i = 0, values = []; i < length; i++) {
      var opt = sourceSelect.options[i];
      if (opt.selected) {
      	targetList[targetList.length] = {name: opt.text, id: opt.value};
      	toRemove[toRemove.length] = opt.value;
      }
    }
    return sourceList.findAll(function(o){
    	if(o) {//wegen IE Bug
    		return ! toRemove.include(o.id);
    	}
		}.bind(this));
	},

	selectAllLeft: function() {
		this.selectAll(this.sleft);
	},

	selectAllRight: function() {
		this.selectAll(this.sright);
	},

	selectAll: function(target) {
		if(target) {
		  var length = target.length;
		  if (!length) return;
		  for (var i = 0, values = []; i < length; i++) {
		    target.options[i].selected = true;
		  }
		}
	},

	update: function() {
		this.fillSelect(this.sleft, this.left);
		this.fillSelect(this.sright, this.right);
	},

	fillSelect: function(objTargetSelect, list) {
		Element.childElements(objTargetSelect).each(function(e){objTargetSelect.removeChild(e)}.bind(this));
		list.each(function(item) {
			if(item) {//wegen IE Bug
				this.createOpt(objTargetSelect, ''+item.id, item.name);
			}
		}.bind(this));
	},

	createOpt: function(objTargetSelect, strVal, strText) {
		var opt = document.createElement('option');
		opt.setAttribute('value', strVal);
    var text = document.createTextNode(strText);
    opt.appendChild(text);
    objTargetSelect.appendChild(opt);
	}

}
