/**
 * DualSelectBox
 * attach a second select box to an already existing one
 * allow shifting of entries between both
 * 
 * @version 0.4.0 2008-06-29
 * 
 * @param {object} existing select box, becomes destination box
 */

if(!this.vxJS) { throw new Error("widget.dualSelectBox: vxJS core missing."); }

vxJS.widget.dualSelectBox = function(select) {
	if(!select) { throw new Error ("widget.dualSelectBox: missing element."); }

	var container, srcBox, buttonBar, destBox, name, form;
	
	function handleClick(e) {
		var te	= e.target || e.srcElement, key;
		if (e.className && (key = te.className.match(/^vxJS_dualSelect_option __key__(.*)$/))) {

		}
	}
	
	function createContainer() {
		var c = "div".create();
		c.style.clear = "left";

		container = "div".setProp("class", "vxJS_dualSelect_container").create([
			srcBox,
			buttonBar,
			destBox,
			c]);
		select.parentNode.insertBefore(container, select);
		
		vxJS.event.addListener(c, "click", handleClick);
	}

	function createBoxes() {
		var i, o, d;

		srcBox = "div".setProp("class", "vxJS_dualSelect_src").create();
		destBox = "div".setProp("class", "vxJS_dualSelect_dest").create();
		
		o = select.options;
		for(i = 0; o[i];) {
			d = "div".setProp("class", "vxJS_dualSelect_option __key__"+o[i].value).create(o[i].text);
			(o[i++].selected ? destBox : srcBox).appendChild(d);
		}

		if(!(form = select.form)) { return; }

		vxJS.event.addListener( form, "submit", primeSubmit);
	}

	function createButtonBar() {
		var alladd	= "input".setProp([["type", "button"], ["value", "\u00BB"]]).create();
		var allsub	= "input".setProp([["type", "button"], ["value", "\u00AB"]]).create();
		var add		= "input".setProp([["type", "button"], ["value", "\u203a"]]).create();
		var sub		= "input".setProp([["type", "button"], ["value", "\u2039"]]).create();
		buttonBar	= "div".setProp("class", "vxJS_dualSelect_buttons").create([alladd, add, sub, allsub]);

		vxJS.event.addListener(add, "click", function()	{ shiftSelectedOptions(srcBox, destBox); });
		vxJS.event.addListener(sub, "click", function()	{ shiftSelectedOptions(destBox, srcBox); });
		vxJS.event.addListener(alladd, "click", function(){ shiftAllOptions(srcBox, destBox); });
		vxJS.event.addListener(allsub, "click", function(){ shiftAllOptions(destBox, srcBox); });
	}

	function primeSubmit() {
		var	i,
			n = destBox.childNodes,
			h = "input".setProp([["name", name], ["type", "hidden"]]).create();

		for(i = n.length; i--;) {
			h.value = n[i].firstChild.value;
			form.appendChild(h.cloneNode);
		}
		alert("inspection time...");
	}

	function shiftSelectedOptions(src, dest) {
		var i = 0, n = src.childNodes;
		
	 	while(n[i]) {
			if (n[i].firstChild.checked) {
				n[i].firstChild.checked = false;
				dest.appendChild(n[i]);
			}
			else {
				i++;
			}
	 	}
	}

	function shiftAllOptions(src, dest) {
		var n = src.childNodes;

	 	while(n[0]) {
			n[0].firstChild.checked = false;
			dest.appendChild(n[0]);
	 	}
	}

	select.style.display = "none";
	createBoxes();
	createButtonBar();
	createContainer();
};