/**
 * @author cjoudrey
 */

var Selectors = {};

Selectors.selectors = [];

Selectors.add = function(id, field, callback)
{
	this.selectors.push(new selector(id, field, callback, this.selectors.length));
	return(this.selectors[this.selectors.length - 1]);
};

Selectors.itemMove = function(key, id)
{
	this.selectors[key].itemMove(id);
};

Selectors.itemClick = function(key, id)
{
	this.selectors[key].itemClick(id);
};

Selectors.addSelection = function(key)
{
	this.selectors[key].addSelection();
};

Selectors.removeSelection = function(key)
{
	this.selectors[key].removeSelection();
};

Selectors.itemMoveUp = function(key)
{
	this.selectors[key].itemMoveUpGroup();
};

Selectors.itemMoveDown = function(key)
{
	this.selectors[key].itemMoveDownGroup();
};

Selectors.size = function(key)
{
	this.selectors[key].size();
};

function selector(id, field, callback, key)
{
	this.id = id;
	this.realId = id;
	this.key = key;
	this.field = field;
	this.callback = callback;

	this.dataLeft = [];
	this.dataRight = [];
	
	this.selectedLeft = [];
	this.selectedRight = [];
	
	this.options = {'multiselect': true};
	
	this.set = function(name, value)
	{
		this.options[name] = value;
	};
	
	this.size = function()
	{
		return(this.dataRight.length);
	};
	
	this.arrayRemove = function(arr, id)
	{
		var newArr = [];
		
		if (arr.length > 0)
		{
			for(newArrId = 0; newArrId < arr.length; newArrId++)
			{
				if (arr[newArrId] != id)
				{
					newArr.push(arr[newArrId]);
				}
			}
		}
		return(newArr);
	};
	
	this.getKey = function(id)
	{
		if (this.dataRight.length > 0)
		{
			for(key = 0; key < this.dataRight.length; key++)
			{
				if (this.dataRight[key] == id)
				{
					return(key);
				}
			}
		}
		
		return(-1);
	};

	this.itemMoveUpGroup = function()
	{
		if (this.selectedRight.length > 0)
		{
			for(selectedId = 0; selectedId < this.selectedRight.length; selectedId++)
			{
				this.itemMoveUp(this.selectedRight[selectedId]);
			}
		}
	};
	
	this.itemMoveDownGroup = function()
	{
		if (this.selectedRight.length > 0)
		{
			for(selectedId = 0; selectedId < this.selectedRight.length; selectedId++)
			{
				this.itemMoveDown(this.selectedRight[selectedId]);
			}
		}
	};

	this.itemMoveDown = function(id)
	{
		var item = document.getElementById(this.id + '_' + id);
		
		if (item)
		{
			var itemparent = item.parentNode;
			
			if (itemparent.id == this.id + "_right")
			{
				key = this.getKey(id);
				
				if (key != -1 && key < (this.dataRight.length - 1))
				{
					var swapEl = document.getElementById(this.id + '_' + this.dataRight[key + 1]);
					itemparent.removeChild(swapEl);
					
					itemparent.insertBefore(swapEl, item);
					this.dataRight[key] = this.dataRight[key + 1];
					this.dataRight[key + 1] = id;
					
					if (this.callback && typeof(this.callback) == "function")
					{
						this.callback(this.realId, 'down', id);
					}
					
					if (this.field && typeof(this.field.value) != "undefined")
					{
						this.field.value = this.dataRight.join(",");
					}
				}
			}
		}
	};
	
	this.itemMoveUp = function(id)
	{
		var item = document.getElementById(this.id + '_' + id);
		
		if (item)
		{
			var itemparent = item.parentNode;
			
			if (itemparent.id == this.id + "_right")
			{
				key = this.getKey(id);
			
				if (key > 0)
				{
					var swapEl = document.getElementById(this.id + '_' + this.dataRight[key - 1]);
					itemparent.removeChild(item);
					itemparent.insertBefore(item, swapEl);
					this.dataRight[key] = this.dataRight[key - 1];
					this.dataRight[key - 1] = id;
					
					if (this.callback && typeof(this.callback) == "function")
					{
						this.callback(this.realId, 'up', id);
					}
					
					if (this.field && typeof(this.field.value) != "undefined")
					{
						this.field.value = this.dataRight.join(",");
					}
				}
			}
		}
	};
	

	this.itemMove = function(id)
	{
		var item = document.getElementById(this.id + '_' + id);
		
		if (item)
		{
			var itemparent = item.parentNode;
			
			itemparent.removeChild(item);
			
			if (itemparent.id == this.id + "_left")
			{
				this.divRight.appendChild(item);
				this.dataLeft = this.arrayRemove(this.dataLeft, id);
				this.selectedLeft = this.arrayRemove(this.selectedLeft, id);
				this.dataRight.push(id);
				
				if (this.callback && typeof(this.callback) == "function")
				{
					this.callback(this.realId, 'add', id);
				}
			}
			else
			{
				this.divLeft.appendChild(item);
				this.dataRight = this.arrayRemove(this.dataRight, id);
				this.selectedRight = this.arrayRemove(this.selectedRight, id);
				this.dataLeft.push(id);
				
				if (this.callback && typeof(this.callback) == "function")
				{
					this.callback(this.realId, 'remove', id);
				}
			}
			
			if (item.className == 'selectorItem selectorItemSelected')
			{
				this.itemClick(id);
			}
			
			if (this.field && typeof(this.field.value) != "undefined")
			{
				this.field.value = this.dataRight.join(",");
			}
		}
	};
	
	this.itemClick = function(id)
	{
		var item = document.getElementById(this.id + '_' + id);
		
		if (item)
		{				
			if (item.className == 'selectorItem')
			{
				
				if (item.parentNode.id == this.id + "_left")
				{
					if (this.options['multiselect'] || this.selectedLeft.length == 0)
					{
						item.className = 'selectorItem selectorItemSelected';
						this.selectedLeft.push(id);
					}
					else if (!this.options['multiselect'])
					{
						for(sId = 0; sId < this.selectedLeft.length; sId++)
						{
							this.itemClick(this.selectedLeft[sId]);
						}
						
						item.className = 'selectorItem selectorItemSelected';
						this.selectedLeft.push(id);
					}
				}
				else if(item.parentNode.id == this.id + "_right")
				{
					if (this.options['multiselect'] || this.selectedRight.length == 0)
					{
						item.className = 'selectorItem selectorItemSelected';
						this.selectedRight.push(id);
						this.selectedRight = this.selectedRight.sort();
					}
					else if (!this.options['multiselect'])
					{
						for(sId = 0; sId < this.selectedRight.length; sId++)
						{
							this.itemClick(this.selectedRight[sId]);
						}
						
						item.className = 'selectorItem selectorItemSelected';
						this.selectedRight.push(id);
					}
				}
			}
			else
			{
				item.className = 'selectorItem';
				if (item.parentNode.id == this.id + "_left")
				{
					this.selectedLeft = this.arrayRemove(this.selectedLeft, id);
				}
				else
				{
					this.selectedRight = this.arrayRemove(this.selectedRight, id);
					this.selectedRight = this.selectedRight.sort();
				}
			}
		}
		
		if (this.selectedLeft.length > 0)
		{
			this.enableButton('add');
		}
		else
		{
			this.disableButton('add');
		}
		
		if (this.selectedRight.length > 0)
		{
			this.enableButton('remove');
			this.enableButton('up');
			this.enableButton('down');
		}
		else
		{
			this.disableButton('remove');
			this.disableButton('up');
			this.disableButton('down');
		}
	};
	
	this.initiate = function()
	{
		this.div = document.getElementById(this.id);
		
		this.div.id = "selector_" + this.key;
		this.id = "selector_" + this.key;
		
		if(this.div)
		{
			
			if (typeof(this.div.hasAttribute) == 'undefined')
			{
				this.isIE = true;
			}
			
			var list = null;
			var selected = null;
			
			divs = this.div.getElementsByTagName('div');
			
			for(divId = 0; divId < divs.length; divId++)
			{
				if ((typeof(divs[divId].hasAttribute) == 'undefined' && divs[divId].getAttribute('list') != null) || (typeof(divs[divId].hasAttribute) != 'undefined' && divs[divId].getAttribute('list') != null))
				{
					list = divs[divId];
				}
				else if ((typeof(divs[divId].hasAttribute) == 'undefined' && divs[divId].getAttribute('selected') != null) || (typeof(divs[divId].hasAttribute) != 'undefined' && divs[divId].getAttribute('selected') != null))
				{
					selected = divs[divId];
				}
			}
			
			if (this.div.getAttribute('itemheight'))
			{
				this.itemheight = this.div.getAttribute('itemheight');
			}
			else
			{
				this.itemheight = '59px';
			}
			
			this.divLeft = document.createElement('div');
			this.divLeft.id = this.id + '_left';
			this.divLeft.className = 'selectorBox';
			
			this.divRight = document.createElement('div');
			this.divRight.id = this.id + '_right';
			this.divRight.className = 'selectorBox';
			
			if (list != null && selected != null)
			{
				var data = list.getElementsByTagName('div');
				
				if (data.length > 0)
				{
					for(dataId = 0; dataId < data.length; dataId++)
					{
						thisElement = document.createElement('div');
						thisElement.id = this.id + '_' + data[dataId].id;
						thisElement.className = 'selectorItem';
						thisElement.style.height = this.itemheight;
						thisElement.innerHTML = data[dataId].innerHTML;
						thisElement.setAttribute('onClick', 'Selectors.itemClick(' + this.key + ',\'' + data[dataId].id + '\');');
						thisElement.setAttribute('onDblClick', 'Selectors.itemMove(' + this.key + ',\'' + data[dataId].id + '\');');

//						DOM.addEvent(thisElement, 'click', function(a, b) { Selectors.itemClick(a, b); }.wrap(document, this.key, data[dataId].id));
//						DOM.addEvent(thisElement, 'dblclick', function(a, b, c) { log(a); log(b); log(c); Selectors.itemMove(this.key, b); }.wrap(this, dataId));
						
						if ((typeof(data[dataId].hasAttribute) == 'undefined' && data[dataId].getAttribute('value') != null) || (typeof(data[dataId].hasAttribute) != 'undefined' && data[dataId].getAttribute('value') != null))
						{
							if (data[dataId].getAttribute('value') == '1')
							{
								this.divRight.appendChild(thisElement);
								this.dataRight.push(data[dataId].id);
							}
							else
							{
								this.divLeft.appendChild(thisElement);
								this.dataLeft.push(data[dataId].id);
							}
						}
						else
						{
							this.divLeft.appendChild(thisElement);
							this.dataLeft.push(data[dataId].id);
						}
						
						if (this.isIE)
						{
							thisElement.parentNode.innerHTML = thisElement.parentNode.innerHTML;
						}
					}
				}
				
				var data = selected.getElementsByTagName('div');
				
				if (data.length > 0)
				{
					for(dataId = 0; dataId < data.length; dataId++)
					{
						this.dataRight.push(data[dataId].id);

						thisElement = document.createElement('div');
						thisElement.id = this.id + '_' + data[dataId].id;
						thisElement.innerHTML = data[dataId].innerHTML;
						thisElement.className = 'selectorItem';
						thisElement.style.height = this.itemheight;
						thisElement.setAttribute('onClick', 'Selectors.itemClick(' + this.key + ',' + data[dataId].id + ');');
						thisElement.setAttribute('onDblClick', 'Selectors.itemMove(' + this.key + ',' + data[dataId].id + ');');

//						DOM.addEvent(thisElement, 'click', 'Selectors.itemClick(' + this.key + ',' + data[dataId].id + ');');
//						DOM.addEvent(thisElement, 'dblclick', 'Selectors.itemMove(' + this.key + ',' + data[dataId].id + ');');
						
						this.divRight.appendChild(thisElement);
						
						if (this.isIE)
						{
							thisElement.parentNode.innerHTML = thisElement.parentNode.innerHTML;
						}
					}
				}
			}
			
			this.div.innerHTML = '';

			this.div.appendChild(this.divLeft);
			
			this.buttonsDiv = document.createElement('div');
			this.buttonsDiv.className = 'selectorButtons';
			
			this.buttonsDiv.innerHTML += '<img class="selectorButton" id="' + this.key + '_add" src="' + Images.basePath + '/selector_btn_add_off.gif"/>';
			this.buttonsDiv.innerHTML += '<img class="selectorButton" id="' + this.key + '_remove" src="' + Images.basePath + '/selector_btn_remove_off.gif"/>';
			
			if (!this.div.getAttribute('noorder'))
			{
				this.buttonsDiv.innerHTML += '<img class="selectorButton" id="' + this.key + '_up" src="' + Images.basePath + '/selector_btn_up_off.gif"/>';
				this.buttonsDiv.innerHTML += '<img class="selectorButton" id="' + this.key + '_down" src="' + Images.basePath + '/selector_btn_down_off.gif"/>';
			}
			
			this.div.appendChild(this.buttonsDiv);
			
			this.div.appendChild(this.divRight);
			
			if (typeof(this.field) == "string")
			{
				fieldName = this.field;
				this.field = document.createElement('input');
				this.field.type = "hidden";
				this.field.name = fieldName;
				this.div.appendChild(this.field);
			}
		}
		
		if (this.field && typeof(this.field.value) != "undefined")
		{
			this.field.value = this.dataRight.join(",");
		}
	};
	
	this.addSelection = function()
	{
		entries = this.selectedLeft;
		
		if (entries.length > 0)
		{
			for(selection = 0; selection < entries.length; selection++)
			{
				this.itemMove(entries[selection]);
			}
		}
	};
	
	this.removeSelection = function()
	{
		entries = this.selectedRight;
		
		if (entries.length > 0)
		{
			for(selection = 0; selection < entries.length; selection++)
			{
				this.itemMove(entries[selection]);
			}
		}		
	};
	
	this.enableButton = function(type)
	{
		var button = document.getElementById(this.key + '_' + type);
		
		if (button)
		{
			if (button.className != 'selectorButton selectorButtonEnabled')
			{
				button.className = 'selectorButton selectorButtonEnabled';
				button.src = button.src.replace("off.gif", "on.gif");
				
				switch(type)
				{
					case 'add':
						button.setAttribute('onClick', 'Selectors.addSelection(' + this.key + ');');
						break;
						
					case 'remove':
						button.setAttribute('onClick', 'Selectors.removeSelection(' + this.key + ');');
						break;
						
					case 'up':
						button.setAttribute('onClick', 'Selectors.itemMoveUp(' + this.key + ');');
						break;
						
					case 'down':
						button.setAttribute('onClick', 'Selectors.itemMoveDown(' + this.key + ');');
						break;
				}
			}
			
			if (this.isIE)
			{
				button.parentNode.innerHTML = button.parentNode.innerHTML;
			}
		}
	};
	
	this.disableButton = function(type)
	{
		var button = document.getElementById(this.key + '_' + type);
		
		if (button)
		{
			if (button.className != 'selectorButton')
			{
				button.className = 'selectorButton';
				button.src = button.src.replace("on.gif", "off.gif");
				button.removeAttribute('onClick');
			}
			
			if (this.isIE)
			{
				button.parentNode.innerHTML = button.parentNode.innerHTML;
			}
		}
	};

	this.initiate();
};

