if (window.TP == null) {
	var TP = {};
}

TP.TreeList = {
	init: function() {
		$('div.treelistElement').each( function() {
			var selectBoxes = $(this).find('select');
			var buttons = $(this).find('div.movebuttons input');

			TP.TreeList.setupDropdowns(selectBoxes[0], selectBoxes[1]);
			TP.TreeList.setupMoveButtons(buttons[1], buttons[0], selectBoxes[0], selectBoxes[1]);
			TP.TreeList.hookToForm(selectBoxes[1]);
		});
	},
	
	setupDropdowns: function(availableBox, selectedBox) {
        $(availableBox).dblclick( function() {TP.TreeList.moveSelectedItems(availableBox, selectedBox);} );
		$(selectedBox).dblclick( function() {TP.TreeList.moveSelectedItems(selectedBox, availableBox);} );
	},

	setupMoveButtons: function(toLeftButton, toRightButton, availableBox, selectedBox) {
		$(toRightButton).click(function() {
			TP.TreeList.moveSelectedItems(availableBox, selectedBox);
		});
		
		$(toLeftButton).click(function() {
			TP.TreeList.moveSelectedItems(selectedBox, availableBox);
		});
	},
	
	hookToForm: function(selectedBox) {
	  $(selectedBox.form).submit(function() {
	    $(selectedBox).find('option').each(function() {
            this.selected = "selected";
	    });
	  });
	},
	
	moveSelectedItems: function(fromSelectbox, toSelectbox) {
            // fix for FF 3.0 and older
            if ($(toSelectbox).find('option').length == 0) {
                emptyOption = document.createElement('option');
                $(toSelectbox).append(emptyOption);
            }

            $(fromSelectbox).find('option:selected').each(function () {
                    TP.TreeList.createOptgroupElemIfNeeded(toSelectbox, this.parentNode.label);
                    TP.TreeList.addToOptgroupElem(toSelectbox, this.parentNode.label, this);
            });
            TP.TreeList.clearEmptyOptGroups(fromSelectbox);

            if (typeof(emptyOption) != 'undefined' && emptyOption) {
                $(emptyOption).remove();
            }

            $(toSelectbox).trigger("change");
            $(fromSelectbox).trigger("change");
	},
	
	getOptgroupElemForOptionElem: function(optionElement) {
		return $(optionElement.parentNode)[0];
	},
	
	addToOptgroupElem: function(selectBox, optGroupLabel, option) {
		var targetOptGroup = this.getOptgroupElem(selectBox, optGroupLabel);
		targetOptGroup.append(option);
	},
	
	getOptgroupElem: function(selectBox, optGroupLabel) {
		var targetOptGroup = $(selectBox).find('optgroup[label=' + optGroupLabel + ']');
		if (targetOptGroup.length == 0) {
			targetOptGroup = document.createElement('optgroup');
			targetOptGroup.label = optGroupLabel;
			$(selectBox).append(targetOptGroup);
			targetOptGroup = $(targetOptGroup);
		}
		return targetOptGroup;
	},

    createOptgroupElemIfNeeded: function(selectBox, optGroupLabel) {
		var targetOptGroup = TP.TreeList.getOptgroupElem(selectBox, optGroupLabel);
		if (targetOptGroup.length == 0) {
			targetOptGroup = document.createElement('optgroup');
			targetOptGroup.label = optGroupLabel;
			$(selectBox).append(targetOptGroup);
			targetOptGroup = $(targetOptGroup);
		}
		return targetOptGroup;
	},

	clearEmptyOptGroups: function(selectBox) {
		$(selectBox).find('optgroup').each( function() {
			if ($(this).children().length == 0) {
				$(this).remove();
			}
		});
	}
}

$(document).ready(function() {TP.TreeList.init();});
