/***************************************************************
*  Copyright notice
*
*  (c) 2009 Stefan Aebischer <aebischer@pixtron.ch>
*  All rights reserved
*
*  You can redistribute this script and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

var PxComboBox = new Class({
	comboboxes: new Hash({}),

	initialize: function(form) {
		if (this.form = $(form)) {
			this.form.getElements("*[class*=pxComboBoxParent]").each(function(select) {
				select.addEvent('change', this.optionChanged.pass(select, this));
				this.comboboxes[select.id] = [];
				
				var i = 0;
				select.getElements('option').each(function(option){
					option.store('itemId', i);
					this.comboboxes[select.id][i] = {
						'option' : option,
						'hiddenBy' : []
					};
					i++;
				},this);
			}, this);
			
			this.comboboxes.each(function(config, id) {
				var select = $(id);
				select.getSelected().each(function(option){
					this.hideOthers(option, select.id);
				}, this);
			}, this);
		}
	},
	
	optionChanged: function(select) {
		this.comboboxes.each(function(config, selectId){
			if(selectId != select.id) {
				this.rePopulateSelect(selectId, select.id);
			}
		}, this);	
	
		select.getSelected().each(function(option){
			this.hideOthers(option, select.id);
		}, this);
	},
		
	rePopulateSelect: function(id, hidingSelectId) {
		var select = $(id);
		var selectedItems = [];
		select.getSelected().each(function(option){
			this.push(option.value);
		}, selectedItems);
		
		select.getElements('option').each(function(option){
			option.dispose();
		});
		
		
		this.comboboxes[id].each(function(item, itemId){
			this.comboboxes[id][itemId].hiddenBy.erase(hidingSelectId);
			item.option.injectInside(select);
		}, this);
		
		select.getElements('option').each(function(option){
			option.selected = selectedItems.contains(option.value);
		});
	},
	
	hideOthers: function(hidingOption, hidingSelectId) {
		var matchRes = hidingOption.getProperty("class").match(/pxComboBox([\w\[\]\-,\*']*)/);
		if(matchRes) {
			eval(matchRes[1]).each(function(trigger){
				var match2Res = trigger.match(/^(.+)\[(.*)\]$/);
				if(match2Res) {
					this.hideOptions(match2Res[1], match2Res[2].split(','), hidingSelectId);
				}
			}, this);
		}
	},
	
	hideOptions: function(id, allowedValues, hidingSelectId) {
		var select = $(id);
		
		select.getElements('option').each(function(option){
			var itemId = option.retrieve('itemId');	
				
			if(!(allowedValues.contains(option.value) || option.value=='' || allowedValues.contains('*'))) {
				this.comboboxes[id][itemId].hiddenBy.push(hidingSelectId);
				
			}

			if(this.comboboxes[id][itemId].hiddenBy.length != 0) {
				option.selected = false;
				option.dispose();
			}
		}, this);
	}
});

window.addEvent("domready", function() {
	var comboBox = new PxComboBox('lawyersearch', {});
});
