function GroupList(){}

{
	GroupList.prototype.setId= function(id)
	{
		this.id = id;
		this.original = 0;
	}
	
	GroupList.prototype.setOriginal = function(original)
	{
		this.original = original;
	}
	
	GroupList.prototype.setFolder = function(folder)
	{
		this.folder = folder;
	}
	
	GroupList.prototype.addTranslationDialog = function()
	{
		if (!this.window)
		{
			this.window = document.createElement('div');
				this.window.className = 'dialog';
			
			var label = document.createElement('label');
				label.appendChild(document.createTextNode(':'));
			
			var select = document.createElement('select');
			var option;
				option = document.createElement('option');
				option.innerHTML = 'Français';
				option.value = '1';
				select.appendChild(option);
				option = document.createElement('option');
				option.innerHTML = 'English';
				option.value = '2';
				select.appendChild(option);
				option = document.createElement('option');
				option.innerHTML = 'Deutsh';
				option.value = '3';
				select.appendChild(option);
				option = document.createElement('option');
				option.innerHTML = 'Italiano';
				option.value = '4';
				select.appendChild(option);
						
			var buttons = document.createElement('div');
			
			var ok 		= document.createElement('input');
				ok.type = 'button';
				ok.className = 'button';
				ok.value = 'Ok';
				ok.select = select;
				ok.groupList = this;
				
				ok.onclick = function(e)
				{
					var language = this.select.options[this.select.selectedIndex].value;
					this.groupList.createTranslation(language);
					
					hideModal();
				}
				
			var cancel	= document.createElement('input');
				cancel.type = 'button';
				cancel.className = 'button';
				cancel.value = 'Annuler';
				cancel.onclick = function(e)
				{
					hideModal();
				}
			
			buttons.appendChild(cancel);
			buttons.appendChild(ok);
			
			this.window.appendChild(label);
			this.window.appendChild(select);
			this.window.appendChild(buttons);
		}
		
		displayModal(this.window);
	}
	
	GroupList.prototype.getSelected = function(element)
	{
		var parent = element.parentNode;
		var values = new Array();
		
		for (var i = 0; i < parent.childNodes.length; i++)
		{
			if (parent.childNodes[i].nodeType == 1)
			{
				if (parent.childNodes[i].getValue && parent.childNodes[i].tagName.toUpperCase() == 'SELECT' && parent.childNodes[i].getValue())
					values[values.length] = parent.childNodes[i].getValue();//else
					//alert(parent.childNodes[i]);
			}
		}
		return '|' + values.join('|') + '|';
	}
	
	GroupList.prototype.select = function(xhr, element)
	{
		if (!xhr)
		{
			var value = this.getSelected(element);
			xhr = sendAjaxRequest('?section=update&action=update', 'data='+ element.parentNode.id +'&value=' + value, this, 'select');
				element.options[element.selectedIndex].text = 'Veuillez patienter...';
			xhr.container = element.parentNode;
		}
		else
		{
			var xml = xhr.responseXML;
			var container = xhr.container;
				while (container.firstChild)
				container.removeChild(container.firstChild);
					
			var elements = generateElements(xml, 'response', container);
			for (var i = 0; i < elements.length; i++)
				if (elements[i].nodeType == 1)
					container.appendChild(elements[i]);
		}
	}
	
	GroupList.prototype.clickItem = function(element, element_id)
	{
		var select 	= document.getElementById(element_id);
		var link 	= element;
		
		addElementClass(link, 'hidden');
		removeElementClass(select, 'hidden');
		
		for (var i = element.parentNode.childNodes.length - 1; i >= 0; i --)
			if (element.parentNode.childNodes[i].nodeType == 1)
			{
				addElementClass(element.parentNode.childNodes[i], 'hidden');
				break;
			}
	}
	
	GroupList.prototype.createTranslation = function (language)
	{
		this.selectTranslation(false, language);
	}
	
	GroupList.prototype.selectTranslation = function (xhr, language, translation_id, inheritPictures)
	{
		if (!xhr)
		{
			var id = this.id;
			
			if (this.original)
				id = this.original;
				
			this.li = document.createElement('LI');
				this.li.appendChild(document.createTextNode('Veuillez patienter...'));
				document.getElementById('translations').appendChild(this.li);
			
			if (!translation_id)
			{
				this.action = 'create';
				sendAjaxRequest('?section=update&action=query&folder='+this.folder, 'data=groupList.'+ id +'.createTranslation&value=' + language, this, 'selectTranslation');
			} 
			else
			{
				this.action = 'select';
				sendAjaxRequest('?section=update&action=query&folder='+this.folder, 'data=groupList.'+ translation_id +'.setTranslationOf&value=' + id +'&language=' + language +'&inheritpictures=' + inheritPictures, this, 'selectTranslation');
			}
			
			
		}
		else
		{
			if (xhr.responseXML)
			{
				var xml = xhr.responseXML;
				document.getElementById('notranslations').style.display = 'none';
				
				var groupList = xml.getElementsByTagName('response')[0];
				
				var a = document.createElement('A');
				a.setAttribute('href', groupList.getAttribute('link'));
				a.appendChild(document.createTextNode(groupList.getAttribute('title')));
				
				this.li.removeChild(this.li.firstChild);
				this.li.appendChild(a);
				
				if (this.action == 'create')
				{
					window.location = groupList.getAttribute('link');
				}
			}
			else
			{
				document.getElementById('translations').removeChild(this.li);
			}
		}
	}
}


var list = new GroupList();