var repeatButton;
var containersList;
var tElement;
var DUMMY = true;
var toolbox;
var imagebox;
var infosbubble;

function initInteractivity(editable)
{
	
	infosbubble = document.createElement('DIV');
		inherit(infosbubble, InfosBubble);
		
	var elements = document.getElementsByTagName("*");
	
	if (editable)
	{
		initElements(elements);
	}
	else
	{
		initMessages(elements);
	}
}

function initElements(elements, container)
{
	pasteboard = document.createElement('TEXTAREA');
		pasteboard.style.position = 'absolute';
		pasteboard.style.left = '-200px';
		
	document.body.appendChild(pasteboard);
	
	}


function initMessages(elements)
{
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		
		if (element.getAttribute('alt') || element.getAttribute('title'))
		{
			inherit(element, InputObject);
		}
	}
}

function makeEditable(element)
{
	if (!element.editablestate)
	{
		if (element.tagName == 'P' || element.tagName == 'H1' || element.tagName == 'H2' || element.tagName == 'A' || element.tagName == 'SPAN')
		{
			inherit(element, EditableText);
		}
		else if (element.tagName == 'UL')
		{
			inherit(element, EditableContainer);
		}
		
		else if (element.tagName == 'LI')
		{
			inherit(element, EditableList);
		}
		
		else if (element.className == 'handle')
		{
			inherit(element, Handle, firstParent);
		}
		
		else if (hasElementClass(element, 'image'))
		{
			inherit(element, EditableImage);
		}
		
		else
		{
			inherit(element, EditableObject);
		}
		
		if (element.onedition)
		{
			element.onedition();
		}
		
		element.editablestate = true;
	}
}


function makeChildrenEditable(container, parent)
{
	if (!parent)
	{
		parent = container;
	}
	
	for (var i = 0; i < parent.childNodes.length; i++)
	{
		var child = parent.childNodes[i];
		if (child.nodeType == 1)
		{
			if (child.getAttribute('editable'))
			{
				makeEditable(child);
				child.container = container;
			}
			if (child.childNodes && child.childNodes.length > 0)
			{
				makeChildrenEditable(container, child);
			}
		}
	}
}

function InfosBubble(){}
{
	InfosBubble.prototype.constructor = function ()
	{
		this.className = 'infosbubble';
		this.id = 'infosbubble';
		
		this.text 	= document.createElement('P');
		
		var content	= document.createElement('DIV');
			content.appendChild(this.text);
			
		var top		= document.createElement('DIV');
			top.className = 'top';
			top.appendChild(content);
		
		var bottom	= document.createElement('DIV');
			bottom.className = 'bottom';
			bottom.appendChild(document.createElement('DIV'));
			
		this.appendChild(top);
		this.appendChild(bottom);
		
		document.body.appendChild(this);
	}
	
	InfosBubble.prototype.updateShow = function ()
	{
		
	}
	
	InfosBubble.prototype.freeze = function ()
	{
		this.frozen = true;
		setTimeout('infosbubble.unfreeze();', 3000);
	}
	
	InfosBubble.prototype.unfreeze = function ()
	{
		this.frozen = false;
	}
	
	InfosBubble.prototype.show = function (object, text)
	{
		if (this.frozen)
			return;
			
		while (this.text.firstChild)
			this.text.removeChild(this.text.firstChild);
		
		var lines = text.split('|');
			
		for (var i = 0; i < lines.length; i++)
		{
			if (i > 0)
				this.text.appendChild(document.createElement('BR'));
			this.text.appendChild(document.createTextNode(lines[i]));
		}
		
		
		this.style.display = 'block';
		var bubblebounds = getBounds(this.text);
		var bounds 	= getBounds(object);
		bounds.height = '';
		bounds.width = '';
		bounds.left += 5;
		bounds.top -= (bubblebounds.height + 28);
		
		setBounds(this, bounds);
		this.animation = animate(this, 'style.top', bounds.top - 8, bounds.top , 0.5, EaseInAndOut, RepeatPeriod);
	}
	
	InfosBubble.prototype.hide = function ()
	{
		if (this.frozen)
			return;
			
		clearTween(this.animation);
		this.style.display = 'none';
	}
}


function Tool(){}
{
	Tool.prototype.set = function (parent, className, callback, description)
	{
		this.parent 		= parent;
		this.callback 		= callback;
		this.className 		= className;
		this.description	= description;
		/*this.innerHTML = description;*/
	}
	
	Tool.prototype.onclick = function (e)
	{
		if (!e) e = window.event;
		
		this.callback.call(this.parent.element);
	}
	
	Tool.prototype.show = function ()
	{
		this.style.display = 'block';
	}
	
	Tool.prototype.onmouseover = function ()
	{
		this.parent.showInfos(this, this.description);
	}
	
	Tool.prototype.onmouseout = function ()
	{
		this.parent.hideInfos();
	}
	
	Tool.prototype.onmousedown = function ()
	{
		return false;
	}
	
	Tool.prototype.ondragstart = function ()
	{
		return false;
	}
	
	Tool.prototype.hide = function ()
	{
		this.style.display = 'none';
	}
}

function Toolbox(){}
{
	Toolbox.prototype.constructor = function ()
	{
		this.className = 'toolbox';
		this.tools = new Array();
		this.list = new Array();
		
		this.infos = infosbubble;
		
		this.left = document.createElement('DIV');
			this.left.className = 'left';
		
		this.right = document.createElement('DIV');
			this.right.className = 'right';
		
		this.appendChild(this.left);
		this.appendChild(this.right);
		
		/*this.move = this.addTool('move', 'move', 'left', function()
		{
			
		}, 'Move');
		*/
		this.style.display = 'none';
		document.body.appendChild(this);
	}
	
	Toolbox.prototype.addTool = function (name, className, alignement, callback, description)
	{
		var tool  = document.createElement('A');
			inherit(tool, Tool);
			tool.set(this, className, callback, description);
			
		if (alignement == 'right')
			this.right.appendChild(tool);else
			this.left.appendChild(tool);
		
		var pos = this.tools.length;		
		this.tools[pos] = tool;
		this.list[name] = pos;
		return tool;
	}
	
	Toolbox.prototype.onmouseover = function ()
	{
		this.show();
	}
	
	Toolbox.prototype.onmouseout = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		
		this.timer = setTimeout('imagebox.hide();', 1500);
	}
	
	Toolbox.prototype.show = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		
		this.style.display = 'block';
	}
	
	Toolbox.prototype.hide = function ()
	{
		this.style.display = 'none';
	}
	
	Toolbox.prototype.showInfos = function (tool, description)
	{
		this.infos.show(tool, description);
	}
	
	Toolbox.prototype.hideInfos = function ()
	{
		this.infos.hide();
	}
	
	Toolbox.prototype.hideAll = function ()
	{
		for (var i = 0; i < this.tools.length; i++)
		{
			this.tools[i].hide();
		}
	}
	
	Toolbox.prototype.showAll = function ()
	{
		for (var i = 0; i < this.tools.length; i++)
		{
			this.tools[i].show();
		}
	}
	
	Toolbox.prototype.hideTool = function (name)
	{
		if (this.list[name])
			this.tools[this.list[name]].hide();
	}
	
	Toolbox.prototype.showTool = function (name)
	{
		if (this.list[name])
			this.tools[this.list[name]].show();
	}
	
	Toolbox.prototype.adaptToElement = function (element, offsetX, offsetY)
	{
		if (this.element)
		{
			this.hide();
		}
		
		var frame;
		
		if (element.paragraph)
			frame = element.paragraph;else
			frame = element;
		
		var bounds = getBounds(frame);
			bounds.height = '';
			bounds.top += offsetY;
		
		/*if (hasElementClass(element, 'right'))
			bounds.left += 220;*/
		
		bounds.left += offsetX;
		
		setBounds(this, bounds);
		
		this.element = element;
		element.toolbox = this;
		this.show();
	}
}

function Imagebox(){}
{
	Imagebox.prototype = new Toolbox();
	
	
	Imagebox.prototype.constructor = function ()
	{
		Toolbox.prototype.constructor.call(this);
		
		//this.move.description = 'Repositionner';
		/*this.move.callback = function ()
		{
			this.startDrag();
		}*/
		
		this.selectimage = this.addTool('selectimage', 'addimage', 'left', function()
		{
			explorer.openFolderWindow(imagebox.folder, imagebox, 'selectimages');
		}, 'Chose a picture in gallery');
		
		this.addimage = this.addTool('addimage', 'uploadimage', 'left', function(){}, 'Select a picture you have on your computer');
		
		this.addimage.onmouseover = function()
		{
			Tool.prototype.onmouseover.call(this);
			setBounds(this.parent.imagebutton, getBounds(this));
		}
		
		this.addimage.onmouseout = function (){}
		
		
		this.removeimage = this.addTool('removeimage', 'removeimage', 'left', function()
		{
			if (this.parentNode.update)
				this.parentNode.update(false, 0);
			
			this.style.backgroundImage = 'url()';
			
			var container = this.parentNode.parentNode.parentNode;
			if (container.image)
				article.removeimage(container);
			
			imagebox.hide();
		}, 'Remove picture');
	}
	
	
	Imagebox.prototype.hideInfos = function ()
	{
		Toolbox.prototype.hideInfos.call(this);
		
		this.imagebutton.style.left = '-100px';
		this.imagebutton.style.top = '-100px';
	}
	
	Imagebox.prototype.set = function (folder)
	{
		this.folder = folder;
		
		this.imageupload = new Fileupload(this.folder, this,
			{size: '10MB', types: '*.jpg;*.png;*.gif', description: 'Images'},
			{data: 'item.'+this.folder+'.container', value : 'Image'}, 
			'Image&dummy=true', true);
		
		
		this.imageupload.settings.button_cursor = SWFUpload.CURSOR.HAND;
		
		this.imagebutton = document.getElementById('filesbutton.' + folder);
		//Image upload
	}
	
	
	Imagebox.prototype.onmouseover = function ()
	{
		this.show();
	}
	
	Imagebox.prototype.onmouseout = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		
		this.timer = setTimeout('imagebox.hide();', 2000);
	}
	
	Imagebox.prototype.show = function ()
	{
		if (hasElementClass(this.element.parentNode, 'hidden'))
		{
			return;
		}
		
		if (this.timer)
			clearTimeout(this.timer);
		
		var bounds = getBounds(this.element);
		
		this.style.display = 'block';
		
		this.imagebutton.style.zIndex = '500';
		this.imagebutton.style.display = 'block';
		this.imagebutton.style.visibility = 'visible';
		
		
		this.imageupload.load();
	}
	
	Imagebox.prototype.hide = function ()
	{
		this.style.display = 'none';
		
		/*if (this.element)
		{
			if (this.element.backupHeight)
				this.element.style.height = this.element.backupHeight;
			
			if (!this.element.parent.value)
			{
				this.element.style.background = '';
			}
		}*/
		//this.imagebutton.style.visibility = 'hidden';
	}
	
	Imagebox.prototype.adaptToElement = function (element)
	{
		if (this.uploading)
			return;
		
		if (this.element && this.element != element)
		{
			this.hide();
		}
		
		this.element = element;
		this.test = 'TEST';
		var bounds = getBounds(this.element);
			bounds.top += bounds.height - 30;
			bounds.left += (bounds.width - 150) / 2;
			bounds.width = '150';
			bounds.height = '';
		
		setBounds(this, bounds);
		
		/*if (this.element.value != 0)
			this.imageupload.swfu.setPostParams({PHPSESSID : 'a903f180493acbefb371771df63c1483', data: 'image.'+this.element.value+'.upload'});else*/
		
		this.show();
	}
	
	//File Upload
	
	Imagebox.prototype.initUpload = function (fileupload)
	{
		this.uploading = true;
		this.askReplace = false;
	}
	
	Imagebox.prototype.uploadEnd = function ()
	{
		removeElementClass(this.element, 'uploading');
		this.uploading = false;
		this.hide();
	}
	
	Imagebox.prototype.fileQueued = function (fileupload, file, params)
	{
		this.uploading = true;
		if (!this.askReplace && this.element.value)
		{
			
			this.askReplace = true;
		}
		fileupload.setNextFile(file);
	}
	
	Imagebox.prototype.uploadStart = function (progressbar, file)
	{
		this.hide();
		addElementClass(this.element, 'uploading');
		this.progressbar = progressbar;
		this.element.appendChild(this.progressbar);
	}
	
	Imagebox.prototype.uploadSuccess = function (imageupload, file, xhr)
	{
		this.element.removeChild(this.progressbar);
		
		imagebox.imagebutton.style.visibility = 'hidden';
		//alert('response = ' + serverData);
		
		//try
		if (xhr)
		{
			var element = xhr.responseXML;
			var id = element.getElementsByTagName('li')[0].getAttribute('objectid');
			
			this.element.parent.update(false, id);
			this.hide();
		}
		else
		//catch(ex)
		{
			
		}
		this.uploadEnd();
	}
	
	Imagebox.prototype.getSelectedItems = function (selected)
	{
		if (selected[0])
		{
			var id = selected[0].getAttribute('objectid');
			this.element.parent.update(false, id);
		}
		return true;
	}
}


//CLASS EditableText
function Handle(){}
{
	//Constructor
	Handle.prototype.constructor = function (element)
	{
		this.element 				= element;
		this.element.addClass 		= objectAddClass;
		this.element.removeClass 	= objectRemoveClass;
	}
	
	Handle.prototype.onclick = function (e)
	{
		return false;
	}
	
	Handle.prototype.onmousedown = function (e)
	{
		DragHandler._dragBegin(this.element, e, this.element.parentNode);
		//this.element.onmousedown(e);
	}
	
	Handle.prototype.ondragstart = function (e)
	{
		return false;
	}
}

function InputObject(){}
{
	InputObject.prototype.constructor = function ()
	{
		{
			this.updateMessages();
			var obj = this;
			
			addEventListener(obj, 'change', function (e)
			{
				/*if (this.getTagName() == 'SELECT')
				{
					if (this.selectedIndex != -1)
						this.setValue(this.options[this.selectedIndex].value);else
						this.setValue('');
					
					this.checkValue();
				}
				
				if (this.getTagName() == 'INPUT')
				{
					this.checkValue();
				}*/
			}, true);
			
			addEventListener(this, 'mousedown', function (e)
			{
				if (this.getTagName() == 'SELECT')
				{
					if (hasElementClass(this, 'defaultvalue'))
						this.checkValue(true);
				}
			}, true);
			
			addEventListener(this, 'mouseover', function (e)
			{
				if (this.description)
				{
					infosbubble.show(this, this.description);
				}
			}, true);
			
			addEventListener(this, 'mouseout', function (e)
			{
				if (this.description)
				{
					infosbubble.hide();
				}
			}, true);
			
			addEventListener(this, 'focus', function (e)
			{
				if (this.is_empty)
				{
					this.is_empty = false;
					this.setValue('');
					removeElementClass(this, 'defaultvalue');
				}
			}, true);
			
			addEventListener(this, 'blur', function (e)
			{
				if (this.checkValue)
					this.checkValue();
			}, true);
			
			this.checkValue();
			
			this.constructed = true;
		}
	}
	
	InputObject.prototype.getTagName = function()
	{
		if (this.tagName && !this.tag)
		{
			this.tag = this.tagName.toUpperCase();
		}
		
		if (this.tag)
			return this.tag;
		
	}
	
	InputObject.prototype.updateMessages = function()
	{
		if (this.getAttribute('alt'))
			this.defaultText = this.getAttribute('alt');else
			this.defaultText = '';
		
		if (this.getAttribute('title'))
			this.description = this.getAttribute('title');else
			this.description = '';
	}
	
	InputObject.prototype.getValue = function ()
	{
		if (this.tagName.toUpperCase() == 'SELECT')
		{
			if (this.selectedIndex != -1)
				return this.options[this.selectedIndex].value;
		}
		else if (this.tagName.toUpperCase() == 'INPUT' || this.tagName.toUpperCase() == 'TEXTAREA')
		{
			return this.value;
		}
		else
		{
			return this.innerHTML;
		}
		return false;
	}
	
	InputObject.prototype.setValue = function (value, tagName)
	{
		this.save_value = value;
		
		if (this.getTagName() == 'SELECT')
		{
			if (this.selectedIndex < 1 && this.defaultText && (value == this.defaultText || value == ''))
			{
				this.options[0].text = value;
				this.selectedIndex = 0;
			}
		}
		
		else if (this.getTagName() == 'INPUT' || this.getTagName() == 'TEXTAREA')
		{
			this.value = value;
			this.save_value = value;
		}
		else
		{
			while (this.firstChild)
				this.removeChild(this.firstChild);
			
			/*if (this.childNodes)
				this.appendChild(document.createTextNode(value));*/
		}
	}
	
	InputObject.prototype.checkValue = function (clear)
	{
		var value;
		
		if (!this.defaultText)
			return;
		//alert(this + ' => ' + this.getValue());
		
		if (!clear && (this.getValue() == '' || this.selectedIndex < 1 || this.getValue() == '0'))
		{
			
			this.is_empty = true;
			addElementClass(this, 'defaultvalue');
			this.setValue(this.defaultText);
		}
		else
		{
			removeElementClass(this, 'defaultvalue');
			
			if (this.getTagName() == 'SELECT' && this.defaultText)
				this.setValue('');
			
			else if (this.getTagName() == 'INPUT')
				this.setValue(this.value);
				
			this.is_empty = false;
		}
	}
}

function EditableObject(){}
{
	EditableObject.prototype = new InputObject();
	
	//Constructor
	EditableObject.prototype.constructor = function ()
	{
		if (!this.constructed)
		{
			this.constructed = true;
		}
		
		InputObject.prototype.constructor.call(this);
		
		addEventListener(this, 'change', function (e)
		{
			if (this.tag == 'SELECT')
			{
				if (this.selectedIndex != -1)
					this.setValue(this.options[this.selectedIndex].value);
			}
		});
		
	}
	
	
	EditableObject.prototype.onblur = function (e)
	{
		this.update();
	}
	
	EditableObject.prototype.update = function (xhr)
	{
		if (!xhr)
		{
			//this.innerHTML = stripTags(this.innerHTML);
			var data = this.id;
			
			this.setValue(this.getValue());
		
			if (this.getAttribute('type') && (this.getAttribute('type').toUpperCase() == 'CHECKBOX' || this.getAttribute('type').toUpperCase() == 'RADIO'))
			{
				if (this.checked)
					value = 1;else
					value = 0;
			}
			else
				value = this.save_value;
				
			sendAjaxRequest('?section=update&action=update&edit=true', 'data=' + data + '&value=' + encodeURIComponent(value), this, 'update');
		}
		else
		{
			if (xhr.responseText)
			{
				//alert(xhr.responseText);
				if (xhr.responseText.substring(0, 11) == 'javascript:')
					eval(xhr.responseText.substring(11, xhr.responseText.length));
			}
		}
	}
}


function EditableContainer(){}
{
	EditableContainer.prototype = new EditableObject();
	
	//Constructor
	EditableContainer.prototype.constructor = function ()
	{
		if (!this.constructed)
		{
			if (this.childNodes && this.childNodes.length > 0)
				makeChildrenEditable(this);
		}
		
		this.setEditable(true);
		
		if (this.getAttribute('order') == 'false')
			this.order = false;else
			this.order = true;
		
		EditableObject.prototype.constructor.call(this);
	}
	
	EditableContainer.prototype.setEditable = function (editable)
	{
		this.editable = editable;
	}
	
	EditableContainer.prototype.generateElements = function(xml, tagname)
	{
		var temp = generateElements(xml, tagname, this);
		var elements = new Array();
		
		for (var i = 0; i < temp.length; i++)
			if (temp[i].nodeType == 1)
				elements[elements.length] = temp[i];
		
		return elements;
	}
	
	EditableContainer.prototype.clearElements = function()
	{
		while (this.firstChild)
		{
			var element = this.firstChild;
			this.removeChild(element);
			element = null;
		}
	}
	
	
	EditableContainer.prototype.appendElements = function(xml, tagname, xhr)
	{
		var elements = this.generateElements(xml, tagname);
		
		for (var i = 0; i < elements.length; i++)
		{
			var element = elements[i];
			
			if (element.nodeType == 1)
			{
				this.appendChild(element);
				
				if (xhr && xhr.callback && xhr.callback.onElementAdded)
				{
					xhr.callback.onElementAdded(element, xhr.data);
				}
			}
		}
	}
	
	EditableContainer.prototype.add = function (xhr, params, callback, data)
	{
		if (!xhr)
		{
			if (params || callback)
			{
				xhr = sendAjaxRequest('?section=update&action=add&edition=true', 'data='+this.id+'&value='+ params, this, 'add');
				xhr.callback = callback;
				xhr.data = data;
			}
			else
			{
				return sendAjaxRequest('?section=update&action=add&edition=true', 'data='+this.id, this, 'add');
			}
		}
		else
		{
			if (xhr.responseXML)
			{
				this.appendElements(xhr.responseXML, false, xhr);
			}
			else
			{
				//alert(xhr.responseText);
				alert('A system error happened.nPlease inform an administrator.');
			}
		}
	}
	
	EditableContainer.prototype.update = function (xhr)
	{
		if (!xhr)
		{
			var pos = 0;
			for (var i = 0; i < this.childNodes.length; i++)
			{
				var child = this.childNodes[i];
				
				if (child.nodeType == 1)
				{
					
					if (child.style.display == 'none')
					{
						this.removeChild(child);
						child = null;
						this.update();
						return;
					}
					else
					{
						if (this.order)
						{
							var parts = child.id.split('.');
							var data = parts[0]+'.'+parts[1]+'.pos';
							var value = pos;
							sendAjaxRequest('?section=update&action=update&edit=true', 'data=' + data + '&value=' + value, this, 'update');
							pos++;
						}
					}
				}
			}
		}
		else
		{
			
		}
	}
	
	//File Upload
	
	EditableContainer.prototype.initUpload = function (fileupload)
	{
		this.elements = new Array(); 
	}
	
	EditableContainer.prototype.fileQueued = function (fileupload, file, params)
	{
		this.add(false, params, this, {fileupload:fileupload, file:file});
	}
	
	EditableContainer.prototype.onElementAdded = function (element, data)
	{
		var file 			= data.file;
		var fileupload 		= data.fileupload;
		
		
		var p = element.getElementsByTagName('P')[0];
		if (p)
		{
			p.innerHTML = '';
			p.appendChild(document.createTextNode(file.name));
		}
		
		addElementClass(element, 'cut');
		this.elements[file.id] = element;
		
		fileupload.setNextFile(file);
	}
	
	EditableContainer.prototype.uploadStart = function (progressbar, file)
	{
		this.elements[file.id].getElementsByTagName('A')[0].appendChild(progressbar);
		removeElementClass(this.elements[file.id], 'cut');
		addElementClass(this.elements[file.id], 'highlight');
		//document.scrollTop = document.scrollHeight - document.clientHeight;
	}
	
	EditableContainer.prototype.uploadSuccess = function (imageupload, file, xhr)
	{
		removeElementClass(this.elements[file.id], 'highlight');
		var element = this.generateElements(xhr.responseXML)[0];
		this.insertBefore(element, this.elements[file.id]);
		this.removeChild(this.elements[file.id]);
	}
}
//CLASS EditableText
function EditableList(){}
{
	EditableList.prototype = new EditableObject();
	
	
	//Constructor
	EditableList.prototype.constructor = function ()
	{
		if (!this.constructed)
		{
			this.addClass 		= objectAddClass;
			this.removeClass 	= objectRemoveClass;
			
			addElementClass(this, 'editable');
			
			//this.createToolbox();
			
			
			addEventListener(this, 'mouseover', function (e)
			{
				this.container.overItem = this;
			});
			
			addEventListener(this, 'mouseout', function (e)
			{
				
				this.container.overItem = false;
			});
			
		}
		
		EditableObject.prototype.constructor.call(this);
	}
	
	EditableList.prototype.setRepeatButton = function(button)
	{
		if (!repeatButton.style.display)
		{
			var dim = getDimensions(button);
			
			repeatButton.button = button;
			repeatButton.style.left = (dim.left - 3) + 'px';
			repeatButton.style.top = (dim.top - 3) + 'px';
			repeatButton.style.display = 'block';
			
			repeatButton.onclick = function() 
			{
				this.button.onclick();
			};
			
			repeatButton.onmouseout = function()
			{
				this.style.display = '';
			};
		}
	}
	
	EditableList.prototype.displayContainersList = function(xhr)
	{
		if (!xhr)
		{
			sendAjaxRequest('?update=true&action=displaycontainers&edit=true', 'data='+this.id, this, 'add');
		}
		else
		{
			
		}
	}
	
	EditableList.prototype.setToolbox = function()
	{
		
	}
	
	EditableList.prototype.update = function ()
	{
		this.parentNode.update();
		//this.toolbox.style.display = 'none';
	}
	
	EditableList.prototype.getPreviousItem = function ()
	{
		if (this.previousSibling)
		{
			var node = this.previousSibling;
			while (node && node.previousSibling && node.nodeType == 3 )
			{
				node = node.previousSibling;
			}
			
			if (node != this && node.nodeType == 1)
			{
				return node;
			}
		}
		return false;
	}
	
	EditableList.prototype.getNextItem = function ()
	{
		if (this.nextSibling)
		{
			var node = this.nextSibling;
			while (node && node.nextSibling && node.nodeType == 3)
			{
				node = node.nextSibling;
			}
			if (node != this && node.nodeType == 1 && node.nextSibling)
			{
				return node.nextSibling;
			}
		}
		return false;
	}
	
	EditableList.prototype.moveup = function (el)
	{
		if (this.getPreviousItem())
		{
			this.parentNode.insertBefore(this, this.getPreviousItem());
		}
		
		this.update();
	}
	
	EditableList.prototype.movedown = function ()
	{
		if (this.getNextItem())
		{
			this.parentNode.insertBefore(this, this.getNextItem());
		}
		else
		{
			this.parentNode.appendChild(this);
		}
		this.update();
	}
	
	
	EditableList.prototype.setContainer = function (xhr, id)
	{
		if (!xhr)
		{
			addElementClass(this, 'cut');
			sendAjaxRequest('?section=update&action=update&edit=true', 'data='+this.id+'&value='+id, this, 'setContainer');
		}
		else
		{
			if (!xhr.responseText)
			{
				this.style.display = 'none';
				this.container.update();
			}
		}
	}
	
	
	EditableList.prototype.setInstance = function (xhr, id)
	{
		if (!xhr)
		{
			addElementClass(this, 'highlighted');
			sendAjaxRequest('?section=update&action=copy', 'data='+this.id+'&value='+id, this, 'setInstance');
		}
		else
		{
			removeElementClass(this, 'highlighted');
		}
	}
	
	EditableList.prototype.displayContainersList = function (xhr)
	{
		if (!xhr)
		{
			var dim = getDimensions(this.toolbox.changeContainer);
			containersList.style.left = (dim.left) + 'px';
			containersList.style.top = (dim.top + 20) + 'px';
			containersList.style.display = 'block';
			
			containersList.innerHTML = '<label>Veuillez patienter...</label>';
			
			
			var data = this.id;
			
			sendAjaxRequest('?section=update&action=display&content=containers&edit=true', 'data=' + data , this, 'displayContainersList');
		}
		else
		{
			containersList.innerHTML = xhr.responseText;
			containersList.onmouseover();
		}
	}
	
	EditableList.prototype.remove = function (xhr)
	{
		if (!xhr)
		{
			addElementClass(this, 'cut');
			
			{
				var data = this.id;
				xhr = sendAjaxRequest('?section=update&action=remove&edition=true', 'data=' + data , this, 'remove');
				xhr.update = true;
				return xhr;
			}
		}
		else
		{
			if (xhr.responseText == '')
			{
				this.style.display = 'none';
				
				if (xhr.update)
					this.update(xhr);
			}
		}
	}
}

//CLASS EditableText
function EditableText(){}
{
	
	EditableText.prototype = new EditableObject();
	
	//Constructor
	EditableText.prototype.constructor = function ()
	{
		//if (!this.constructed)
		{
			addElementClass(this, 'transparentborders');
			this.focus = false;
			this.column = null;
			this.draggable = false;
			this.sliceTools = new Array();
			this.clearNode = document.createElement('span');
				this.clearNode.className = 'clear';
				this.clearNode.appendChild(document.createTextNode(' '));
			
			var child = this.parentNode.firstChild;
			while (child)
			{
				if (child.nodeType == 1 && child.tagName == 'UL')
				{
					this.column = child;
					break;
				}
				child = child.nextSibling;
			}
			
			if (this.getAttribute('linebreak'))
				this.linebreaks = true;
				
			if (this.linebreaks)
			{
				this.checkLineBreaks();
			}
			
			//this.appendChild(this.clearNode);
			
			addElementClass(this, 'editable');
		}
		
		this.setContentEditable(true);
		
		EditableObject.prototype.constructor.call(this);
	}
	
	EditableText.prototype.setContentEditable = function (editable)
	{
		if (editable)
		{
			this.contentEditable = true;
			//addElementClass(this, 'editable');
		}
		/*else if (!this.linebreaks)
		{
			this.contentEditable = false;
			//removeElementClass(this, 'editable');
		}*/
	}
	
	EditableText.prototype.onmousedown = function (e)
	{
		e = e | window.event;
		
		if (e.preventDefault)
				e.preventDefault();	
	}
	
	EditableText.prototype.onmouseover = function (e)
	{
		this.over = true;
		this.setContentEditable(true);
		//this.appendChild(this.clearNode);
	}
	
	EditableText.prototype.onmouseout = function (e)
	{
		this.over = false;
		if (!this.focus)
			this.setContentEditable(false);
	}
	
	EditableText.prototype.oncontextmenu = function (e)
	{
		return false;
	}
	
	EditableText.prototype.onfocus = function (e)
	{
		this.setContentEditable(true);
		this.focus = true;
	}
	
	EditableText.prototype.onblur = function (e)
	{
		this.focus = false;
		this.setContentEditable(false);
		this.update();
	}
	
	EditableText.prototype.joinParagraphs = function ()
	{
		var parent = this.parentNode;
		if (parent.previousSibling)
		{
			
			var previous = parent.previousSibling;
			while(previous && previous.nodeType != 1 )
			{
				previous = previous.previousSibling;
			}
			
			if (previous && previous.childNodes)
			{
				var child = previous.firstChild;
				var p;
				
				while (child)
				{
					if (child.nodeType == 1 && child.tagName == 'P'){
						p = child;break;
					}
					child = child.nextSibling
				}
				
				if (p)
				{
					var br = document.createElement('BR');
					p.appendChild(br);
					
					if (this.childNodes)
					for (var i = 0; i < this.childNodes.length; i++)
					{
						p.appendChild(this.childNodes[i]);
					}
					p.update();
				}
				/*if (p.column && this.column && this.column.childNodes)
				{
					for (var i = 0; i < this.column.childNodes.length; i++)
					{
						p.column.appendChild(this.column.childNodes[i]);
					}
				}*/
			}
			
			this.remove();
		}
		
	}
	
	EditableText.prototype.newBlock = function ()
	{
		var p = document.createElement('P');
			p.setAttribute('editable', 'true');
			p.setAttribute('linebreak', 'true');
		
		var li = document.createElement('LI');
			li.setAttribute('editable', 'true');
			li.appendChild(p);
		
		li.p = p;
		
		return li;
	}
	
	EditableText.prototype.onElementAdded = function (element, data)
	{
		var paragraph = false;
		hideModal();
		
		for (var i = 0; i < element.childNodes.length; i++)
		if (element.childNodes[i].nodeType == 1 && element.childNodes[i].tagName == 'P')
		{
			paragraph = element.childNodes[i];
			break;
		}
		
		if (data == 'slice')
		{
			if (this.sliceNode.nextSibling)
				var node = this.sliceNode.nextSibling;
			
			this.container.insertBefore(element, this.parentNode.nextSibling);
			
			var next = false;
			
			while (node)
			{
				if (node.nextSibling)
					next = node.nextSibling;else
					next = false;
				
				paragraph.appendChild(node);
				node = next;
			}
			
			paragraph.update();
			this.update();
			
			this.sliceNode.parentNode.removeChild(this.sliceNode);
			this.sliceNode = false;
		}
		
		if (updateParagraphs)
			updateParagraphs(element);
			
		this.container.update();
			
	}
	
	EditableText.prototype.addParagraph = function (xhr)
	{
		this.container.add(false, '', this);
	}
	
	EditableText.prototype.sliceAt = function (node)
	{
		//this.block = this.newBlock();
		if (this.sliceNode)
			return;
			
		this.sliceNode = node;
		this.container.add(false, '', this, 'slice');
	}
	
	EditableText.prototype.removeSliceTools = function ()
	{
		if (this.sliceTools)
		{
			for (var i = 0; i < this.sliceTools.length; i++)
			{
				document.body.removeChild(this.sliceTools[i]);
				this.sliceTools[i] = null;
			}
			this.sliceTools = null;
			this.sliceTools = new Array();
		}
	}
	
	EditableText.prototype.getLeftPos = function ()
	{
		var bounds = getBounds(this);
		return bounds.left;
	}
	
	EditableText.prototype.initStaticTools = function ()
	{
		if (this.parentNode.tagName.toUpperCase() == 'LI')
		{
			if (!this.staticToolbox)
			{
				this.staticToolbox = document.createElement('DIV');
				inherit(this.staticToolbox, Toolbox);
			}
			this.setBreakTool();
			this.showStaticTools();
		}
	}
	
	
	EditableText.prototype.removeJoinTool = function ()
	{
		if (this.joinTool)
		{
			document.body.removeChild(this.joinTool);
			this.joinTool = null;
		}
	}
	
	EditableText.prototype.addSliceTool = function (atNode)
	{
		var tool = this.createTool('slice', 'slice', '', function(param)
		{
			this.sliceAt(param);
		}, atNode);
		
		var bounds = getBounds(atNode);
			bounds.left = this.getLeftPos() - 30;
		
		setBounds(tool, bounds);
		document.body.appendChild(tool);
		
		this.sliceTools[this.sliceTools.length] = tool;
	}
	
	EditableText.prototype.showStaticTools = function ()
	{
		
		var bounds = getBounds(this);
			bounds.left	-= 30;
			bounds.top 	-= 20;
			bounds.height = 20;
		
		setBounds(this.staticToolbox, bounds);
		
		this.staticToolbox.hideAll();
		
		if (this.parentNode.getPreviousItem())
			this.breakTool.show();
		
		this.staticToolbox.show();
	}
	
	
	EditableText.prototype.setBreakTool = function ()
	{
		if (!this.breakTool)
		{
			this.breakTool = this.staticToolbox.addTool('breaktool', 'breakpage', 'left', function(param)
			{
				this.togglePageBreak();
			}, 'Insérer un saut de page à cet endroit');
			
			this.breakTool.togglePageBreak = function ()
			{
				this.breakpage = !this.breakpage;
				
				if (!this.breakpage)
				{
					this.tool.description = 'Insérer un saut de page à cet endroit';
					this.tool.className = 'breakpage';
				}
				else
				{
					this.tool.description 	= 'Supprimer le saut de page';
					this.tool.className 	= 'removepage';
				}
			}
			
			this.breakTool.onmouseover = function (e)
			{
				Tool.prototype.onmouseover.call(this, e);
				addElementClass(this.parent, 'linebreak');
			}
			
			this.breakTool.onmouseout = function (e)
			{
				Tool.prototype.onmouseout.call(this, e);
				addElementClass(this.parent, 'linebreak');
			}
		}
	}
	
	EditableText.prototype.setJoinTool = function ()
	{
		var previousParagraph = false;
		
		if (!this.joinTool)
		{
			this.joinTool = this.createTool('join', 'join', 'Fusionner les paragraphes', function(param)
			{
				this.joinParagraphs();
			});
			document.body.appendChild(this.joinTool);
		}
		
		if (this.parentNode.previousSibling)
		{
			var previous = this.parentNode.previousSibling;
			while (previous.previousSibling && previous.nodeType != 1)
			{
				previous = previous.previousSibling;
			}
			//Il y a un paragraphe avant celui là
			if (previous.nodeType == 1)
			{
				previousParagraph = true;
			}
		}
		
		if (previousParagraph)
		{
			var bounds = getBounds(this);
				bounds.left	-= 30;
				bounds.top 	-= 20;
				bounds.height = 20;
			
			setBounds(this.joinTool, bounds);
			this.joinTool.style.display = 'block';
		}
		else if (!previousParagraph && this.joinTool)
		{
			this.joinTool.style.display = 'none';
		}
	}
	
	EditableText.prototype.checkLineBreaks = function ()
	{
		var prevNode = '';
		if (this.parentNode.tagName == 'LI')
		{
			this.removeSliceTools();
			//this.setJoinTool();
			
			for (var i = 0; i < this.childNodes.length - 1; i++)
			{
				var node = this.childNodes[i];
				if (node.nodeType == 1 && node.tagName == 'BR')
				{
					if (prevNode == 1)
					{
						//this.addSliceTool(node);
					}
					prevNode ++;
				}
				else 
				if (node.nodeType == 1  || node.length > 0)
				{
					prevNode = 0
				}
			}
		}
	}
	
	EditableText.prototype.insertHtmlAtCursor = function (html) {
		var range, node;
		var length = this.childNodes.length;
		if (window.getSelection && window.getSelection().getRangeAt) {
			range = window.getSelection().getRangeAt(0);
			node = range.createContextualFragment(html);
			range.insertNode(node);
			
		} else if (document.selection && document.selection.createRange) {
			document.selection.createRange().pasteHTML(html);
		}
	}
	
	EditableText.prototype.insertNodeAtCursor = function (node) {
		var range, html;
		if (window.getSelection && window.getSelection().getRangeAt) {
			range = window.getSelection().getRangeAt(0);
			range.insertNode(node);
			
			if (node.nextSibling)
				range.setStart(node.nextSibling, 0);
			
		} else if (document.selection && document.selection.createRange) {
			range = document.selection.createRange();
			html = (node.nodeType == 3) ? node.data : node.outerHTML;
			range.pasteHTML(html);
		}
	}
	
	EditableText.prototype.caretPos = function getCaretPos()
	{
		var cursorPos;
		if (window.getSelection) {
			var selObj = window.getSelection();
			var selRange = selObj.getRangeAt(0);
			cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
			/* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
		}
		else if (document.selection) {
			var range= window.getSelection().getRangeAt(0);
			alert('Current position: '+range.startOffset+' inside '+range.startContainer);
		}
	}
	
	
	EditableText.prototype.getHtml = function (parent)
	{
		var value = '';
		if (!parent)
			parent = this;
		
		if (this.clearNode.nodeParent)
			this.removeChild(this.clearNode);
		
		if (parent.childNodes)
		for (var i = 0; i < parent.childNodes.length; i++)
		{
			var child = parent.childNodes[i];
			if (child.nodeType == 1)
			{
				if (child.tagName.toUpperCase() == 'BR' && this.linebreaks)
					value += '<br />';else
					value += this.getHtml (child);
			}
			else
				value += child.nodeValue.replace('&','%26');
		}
		
		//this.appendChild(this.clearNode);
		
		return value;
	}
	
	EditableText.prototype.update = function (xhr)
	{
		if (!xhr)
		{
			//this.innerHTML = stripTags(this.innerHTML);
			var data = this.id;
			var value = this.getHtml();
			
			if (this.is_empty)
				value = '';
				
			sendAjaxRequest('?section=update&action=update&edit=true', 'data=' + data + '&value=' + encodeURIComponent(value), this, 'update');
		}
		else
		{
			if (this.updateTimer)
			{
				clearTimeout(this.updateTimer);
			}
			this.updateTimer = false;
			
			if (xhr.responseText)
			{
				if (xhr.responseText.substring(0, 11) == 'javascript:')
					eval(xhr.responseText.substring(11, xhr.responseText.length));
			}
		}
	}
	
	EditableText.prototype.remove = function ()
	{		
		this.removeSliceTools();
		this.removeJoinTool();
			
		if (this.parentNode.remove)
		{
			this.parentNode.style.display = 'none';
			this.parentNode.remove();
		}
	}
	
	EditableText.prototype.checkBackspace = function ()
	{
		if (!this.linebreaks || !this.parentNode.getPreviousItem())
			return false;
		
		var nocontent = true;
		
		if (this.childNodes)
		for (var i = 0; i < this.childNodes.length; i++)
		{
			if (this.childNodes[i].nodeType == 3)
			{
				var text = this.childNodes[i].nodeValue;
				
				for (var k = 0; k < text.length; k++)
				if (text.charAt(k) != ' ' && text.charAt(k) != '\n')
				{
					nocontent = false;
					break;
				}
			}
		}
		
		if (nocontent)
		{
			this.remove();
		}
	}
	
	EditableText.prototype.paste = function ()
	{
		/*if (pasteboard.value.length > 2000)
		{
			if (!confirm('Votre texte fait plus de 2000 characters. Couper le texte avant de le coller?'))
				return;
		}*/
		
		var paragraphs = pasteboard.value.split('\n');
		
		for (var i = 0; i < paragraphs.length; i++)
		{
			if (i > 0)
				this.insertBefore(document.createElement('BR'), this.pastelocation);

			this.insertBefore(document.createTextNode(paragraphs[i]), this.pastelocation);
		}
		this.removeChild(this.pastelocation);
		this.pastelocation = null;
		this.update();
	}
	
	EditableText.prototype.onkeydown = function (e)
	{
		
		e = e || window.event;
		var charCode = (e.keyCode) ? e.keyCode : e.charCode;
		
		if (this.clearNode.parentNode)
			this.clearNode.parentNode.removeChild(this.clearNode);
		
		if (!this.updateTimer && this.id)
		{
			this.updateTimer = setTimeout('document.getElementById(\''+this.id+'\').update();', 5000);
		}
		
		/*if (!e.ctrlKey)
		{
			var selectedContent;
			var text;
			
			if (window.getSelection)
			{
				selectedContent = window.getSelection();
				text = selectedContent.toString();
			}
			else if (document.selection)
			{
				selectedContent = document.selection.createRange()
				text = selectedContent.text;
			}
			
			if (text.length > 1)
			{
				if (selectedContent.clear)
					selectedContent.clear();else
				if (window.getSelection)
					window.getSelection().removeAllRanges();
			}
		}*/
		
		if (charCode == 13)
		{
			if (e.preventDefault)
				e.preventDefault();
			 
			if (this.linebreaks)
			{
				//this.checkParagraphs();
				
				/*if (this.newParagraphElement)
				{
					this.newParagraph();
				}*/
			}
			else
			{
				//this.blur();
			}
			return false;
		}
		
		
		if (e.ctrlKey && charCode == 86) 
		{
			this.pastelocation = document.createElement('SPAN');
			this.insertNodeAtCursor(this.pastelocation);
			
			pasteboard.value ='';
			pasteboard.contentdiv = this;
			pasteboard.focus();
			
			setTimeout('pasteboard.contentdiv.paste();', 1);
		}
		
		if (charCode == 8 || (e.ctrlKey && charCode == 88))
		{
			tElement = this;
			setTimeout('tElement.checkBackspace();', 1);
		}
		
		
		return true;
		/*if (charCode == 8 || charCode == 32 || charCode == 13 || charCode == 46 || (charCode >= 37 && charCode <= 40))
		{
			return true;
		}*/
	}
	
	EditableText.prototype.onkeypress = function (e)
	{
		e = e || window.event;
		
		var charCode = (e.keyCode) ? e.keyCode : e.charCode;
		
		if (charCode == 13)
		{
			//this.checkParagraphs();
			if (this.linebreaks)
			{
				this.insertNodeAtCursor(document.createElement('BR'));
				//this.appendChild(this.clearNode);
			}
			else
			{
				this.blur();
			}
				
			return false;
		}
		return true;
	}
	
	EditableText.prototype.onkeyup = function (e)
	{
		this.checkLineBreaks();
		return true;
	}
}

//CLASS EditableImage
function EditableImage(){}
{
	//Constructor
	EditableImage.prototype.constructor = function ()
	{
		addElementClass(this, 'editable');
		
		this.tempimage = new Image();
		if (this.getAttribute('imageid'))
		{
			this.tempimage.src = '?section=images&id='+this.getAttribute('imageid')+'&type='+this.getAttribute('imagetype')+'&nocrop=true';
		}
		
		this.image = this.getElementsByTagName('DIV')[0];
		this.image.parent = this;
		this.value = this.getAttribute('value');
		
		this.image.onmousedown = function ()
		{
			this.parent.startdrag();
		}
		
		this.image.ondragstart = function ()
		{
			return false;
		}
		
		this.image.onmouseover = function ()
		{
			//if (this.parent.value == 0)
				imagebox.adaptToElement(this);
		}
		
		this.image.onclick = function ()
		{
			/*if (this.parent.value != 0)
				imagebox.adaptToElement(this);*/
		}
	}
	
	EditableImage.prototype.startdrag = function ()
	{
		if (!EditableImage.draggedImage && this.tempimage)
		{	
			EditableImage.draggedImage = this;
			
			var bounds = getBounds(this);
			
			if (!this.fullimage)
			{
				this.imageframe = document.createElement('div');
				this.imageframe.style.overflow = 'hidden';
				setBounds(this.imageframe, bounds);
				
				this.fullimage = document.createElement('img');
				this.fullimage.src = this.tempimage.src;
				this.fullimage.zIndex = this.zIndex + 1;
				this.fullimage.parent = this;
				this.fullimage.style.position = 'relative';
				this.fullimage.style.left = '0px';
				this.fullimage.style.top = '0px';
				this.fullimage.style.cursor = 'move';
				this.fullimage.style.top 	= - ((this.fullimage.height - bounds.height) / 2) + 'px';
				this.fullimage.style.left 	= - ((this.fullimage.width - bounds.width) / 2) + 'px';
				
				this.fullimage.onmousedown = function ()
				{
					this.parent.startdrag();
					return false;
				}
				
				this.fullimage.ondragstart = function ()
				{
					return false;
				}
				
				this.imageframe.appendChild(this.fullimage);
				document.body.appendChild(this.imageframe);
			}
			
			this.fullimage.style.display = 'block';
			
			this.mouseoffset = {
				x:bounds.left - mouse.x + parseInt(this.fullimage.style.left), 
				y:bounds.top - mouse.y  + parseInt(this.fullimage.style.top)
			};
		}
	}
	
	EditableImage.prototype.dragging = function ()
	{
		var bounds = getBounds(this);
		var left 	= mouse.x + this.mouseoffset.x - bounds.left;
		var top 	= mouse.y + this.mouseoffset.y - bounds.top;
		var width	= this.fullimage.width;
		var height	= this.fullimage.height;
		
		if (left > 0) 	left 	= 0;
		if (top > 0)	top 	= 0;
		if (left + width < bounds.width)	left = bounds.width - width;
		if (top + height < bounds.height)	top = bounds.height - height;
		
		this.fullimage.style.left = left + 'px';
		this.fullimage.style.top = top + 'px';
	}
	
	EditableImage.prototype.stopDrag = function (xhr)
	{
		if (!xhr)
		{
			var bounds = getBounds(this);
			
			var left 	= - Math.round(parseInt(this.fullimage.style.left) + ((this.fullimage.width - bounds.width) / 2));
			var top 	= - Math.round(parseInt(this.fullimage.style.top)  + ((this.fullimage.height - bounds.height) / 2));
			
			sendAjaxRequest('?section=update&action=query', 'data=image.'+this.getAttribute('imageid')+'.customresize&value='+this.getAttribute('imagetype')+'&left=' + left + '&top='+top, this, 'stopDrag');
		}
		else
		{
			//alert(xhr.responseText);
		}
	}
	
	EditableImage.mouseup = function (event)
	{
		if (EditableImage.draggedImage)
		{
			EditableImage.draggedImage.stopDrag();
		}
		EditableImage.draggedImage = false;
	}
	
	EditableImage.mousemove = function (event)
	{
		image = EditableImage.draggedImage;
		if (image)
		{
			image.dragging();
		}
	}
	
	EditableImage.prototype.update = function(xhr, id)
	{
		if (!xhr)
		{
			if (id != 0)
				this.value = id;
			
			xhr = sendAjaxRequest('?section=update&action=update', 'data='+this.id+'&value='+id, this, 'update');
		}
		else
		{
			if (xhr.responseXML)
			{
				//try
				{
					var response = xhr.responseXML.getElementsByTagName('response')[0];
					this.image.style.backgroundImage = 'url('+response.getAttribute('path')+')';
					this.tempimage.src = response.getAttribute('path');
					this.image.style.height = response.getAttribute('height') + 'px';
					
					if (this.imageframe)
					{
						this.imageframe.parentNode.removeChild(this.imageframe);
						this.imageframe = null;
						this.fullimage = null;
					}
				}
				//catch(ex)
				{
					
				}
				removeElementClass(this.image, 'empty');
				removeElementClass(this.image, 'uploading');
			}
			else
			{
				
			}
			this.uploading = false;
		}
	}
	
	
	EditableImage.prototype.isEmpty = function ()
	{
		if (this.image.className == 'empty')
			return true;else
			return false;
	}
}
