/******************************************************************************
* pgClientRuntime.js
*******************************************************************************

*******************************************************************************
*                                                                             *
* Copyright 2006									                          *
*                                                                             *
******************************************************************************/
var isoProfiler;
function pgInitWindowOptions()
{
	var options = document.getElementById("windowOptions").value;
	if(options != null) {
		options = options.split(",");
		for(var i=0;i < options.length;i++) {
			var option = options[i];
			var pos = option.indexOf("=");
			var key = option.substr(0, pos);
			var value = option.substr(pos+1);
			switch(key) {
			case "width":
				value = parseInt(value, 0);
				if(!isNaN(value)) document.getElementById("WIDTH").value = value;
				break;
			case "height":
				value = parseInt(value, 0);
				if(!isNaN(value)) document.getElementById("HEIGHT").value = value;
				break;
			case "top":
				value = parseInt(value, 0);
				if(!isNaN(value)) document.getElementById("TOP").value = value;
				break;
			case "left":
				value = parseInt(value, 0);
				if(!isNaN(value)) document.getElementById("LEFT").value = value;
				break;
			case "scrollbars":
				value = pgMakeBoolean(value);
				document.getElementById("SCROLLBARS").checked = value;
			case "toolbar":
			case "status":
				value = pgMakeBoolean(value);
				document.getElementById("MENUS").checked = value;
			}
		}
	}
}
function pgMakeBoolean(str)
{
	return str == "1" || str == "yes" || str == "true";
}
function pgGenerateOption(name)
{
	var input = document.getElementById(name.toUpperCase());
	if(input == null || input.value == "") return "";
	return name + "=" + input.value + ",";
}

function pgCommitWindowOptions(form)
{
	var options = "";
	options += pgGenerateOption("width");
	options += pgGenerateOption("height");
	options += pgGenerateOption("top");
	options += pgGenerateOption("left");
	var scrollbars = document.getElementById("SCROLLBARS").checked ? "1": "0";
	var menus = document.getElementById("MENUS").checked ? "1": "0";
	if(options == "" && scrollbars == "0" && menus == "0") {
	} else {
		options += "resizable=1,";
		options += "scrollbars=" + scrollbars;
		options += ",menubar=" + menus +",location" + menus + ",status=" + menus + ",toolbar=" + menus;
	}
	document.getElementById("windowOptions").value = options;
}


function pgInsertElement(select, pageVersionOid, zone)
{
	var model = select.value;
	if(model == "") return;
	select.selectedIndex = 0;
	pgCommandManager.createElement(model, zone, null);
}

function pgUpdateElementHTML(oid, htmlElement)
{
	pgCommandManager.updateElementHTML(oid, htmlElement);
}

function PgDragAndDropManager()
{
	this.init();
}

PgDragAndDropManager.prototype.init = function()
{
	//this.enableDrag(true);
}

PgDragAndDropManager.prototype.enableDrag = function(status)
{
	if(status) {
		//this.addEvent(pgCommandManager._bodyDiv, "mousedown", pgDragStart);
		this.addEvent(pgCommandManager._elementToolBar, "mousedown", pgDragStart);
	} else {
		//this.removeEvent(pgCommandManager._bodyDiv, "mousedown", pgDragStart);
		this.removeEvent(pgCommandManager._elementToolBar, "mousedown", pgDragStart);
	}
}
PgDragAndDropManager.prototype.addEvent = function(element, evname, func)
{
	if (element.attachEvent) { // IE
		element.attachEvent("on" + evname, func);
	} else if (element.addEventListener) { // Gecko / W3C
		element.addEventListener(evname, func, true);
	} else {
		element["on" + evname] = func;
	}
}

PgDragAndDropManager.prototype.removeEvent = function(element, evname, func)
{
	if (element.detachEvent) { // IE
		element.detachEvent("on" + evname, func);
	} else if (element.removeEventListener) { // Gecko / W3C
		element.removeEventListener(evname, func, true);
	} else {
		element["on" + evname] = null;
	}
}

PgDragAndDropManager.prototype.getAbsolutePos = function(element)
{
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(element.tagName);
	if (is_div && element.scrollLeft)
		SL = element.scrollLeft;
	if (is_div && element.scrollTop)
		ST = element.scrollTop;
	var r = { x: element.offsetLeft - SL, y: element.offsetTop - ST, w: element.offsetWidth, h:element.offsetHeight};
	if (element.offsetParent) {
		var tmp = this.getAbsolutePos(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}
PgDragAndDropManager.prototype.dragStart = function(event, contentDiv)
{
	var dragElement = pgCommandManager.getDragElement();
	var element = pgGetElement(event);
	var pivot = element;
	while(pivot && pivot.nodeType == 1) {
		var dragType = pivot.getAttribute("dragType");
		if(dragType != null) break;
		pivot = pivot.parentNode;
	}
	if(dragType == null) return;
	switch(dragType) {
	case "webPart":
		var readOnly = pgCommandManager.isReadOnly();
		if(readOnly) return;
		var target = this.getTarget(element);
		if(target == null) return; 
		this.dragType = dragType;
		var id = target.id.substr(1);
		this.draggedObjectOid = parseInt(id);
		var label = target.getAttribute("elementLabel");
		dragElement.innerHTML = label;
		dragElement.style.zIndex = 10;
		contentDiv = document;
		this.addEvent(contentDiv, "mousemove", pgDragIt);
		this.addEvent(contentDiv, "mouseover", pgStopEvent);
		this.addEvent(contentDiv, "mouseup", pgDragEnd);
		break;
	case "newWebPart":
		var id = pivot.id.substr(1);
		var elementModel = pgCommandManager._elementModels[id];
		if(elementModel == null) return;
		this.dragType = dragType;
		this.draggedElementModel = elementModel;
		dragElement.innerHTML = elementModel.label;
		dragElement.style.zIndex = 10;
		contentDiv = document;
		this.addEvent(contentDiv, "mousemove", pgDragIt);
		this.addEvent(contentDiv, "mouseover", pgStopEvent);
		this.addEvent(contentDiv, "mouseup", pgDragEnd);
		break;
	case "elementToolBar":
		this.dragType = dragType;
		if(document.all) {
			this.dragOffsetX = parseInt(pgCommandManager._elementToolBar.style.left) - event.clientX;
			this.dragOffsetY = parseInt(pgCommandManager._elementToolBar.style.top) - event.clientY;
		} else {
			this.dragOffsetX = parseInt(pgCommandManager._elementToolBar.style.left) - event.pageX;
			this.dragOffsetY = parseInt(pgCommandManager._elementToolBar.style.top) - event.pageY;
		}
		contentDiv = document;
		this.addEvent(contentDiv, "mousemove", pgDragIt);
		this.addEvent(contentDiv, "mouseover", pgStopEvent);
		this.addEvent(contentDiv, "mouseup", pgDragEnd);
	}
}

PgDragAndDropManager.prototype.dragEnd = function()
{
	var contentDiv = pgCommandManager._bodyDiv;
	var dragElement = pgCommandManager.getDragElement();
	dragElement.className = "pgDragElement";
	switch(this.dragType) {
	case "webPart":
		var doCommit = this.highlightedTarget != null;
		if(doCommit) {
			var descr = this.highlightedTarget.getAttribute("droptarget");
			var targetDescr = descr.split(":");
			var targetOid = this.draggedObjectOid;
		}
		dragElement.innerHTML = "";
		dragElement.className = "pgDragElement";
		this.draggedObjectOid = null;
		contentDiv = document;
		this.removeEvent(contentDiv, "mousemove", pgDragIt);
		this.removeEvent(contentDiv, "mouseover", pgStopEvent);
		this.removeEvent(contentDiv, "mouseup", pgDragEnd);
		this.highlightTarget(null);
		if(doCommit) {
			pgCommandManager.moveElement(targetOid, targetDescr[1], targetDescr[2]);
		}
		break;
	case "newWebPart":
		var doCommit = this.highlightedTarget != null;
		if(doCommit) {
			var targetDescr = this.highlightedTarget.getAttribute("droptarget").split(":");
		}
		dragElement.innerHTML = "";
		dragElement.className = "pgDragElement";
		elementModel = this.draggedElementModel;
		this.draggedElementModel = null;
		this.draggedObjectOid = null;
		if(doCommit) {
			pgCommandManager.createElement(elementModel.name, targetDescr[1], targetDescr[2]);
		}
		break;
	case "elementToolBar":
		contentDiv = document;
		this.removeEvent(contentDiv, "mousemove", pgDragIt);
		this.removeEvent(contentDiv, "mouseover", pgStopEvent);
		this.removeEvent(contentDiv, "mouseup", pgDragEnd);
		this.highlightTarget(null);
		break;		
	}
	this.dragType = null;
	dragElement.className = "pgDragElement";

}

PgDragAndDropManager.prototype.getTarget = function(element)
{
	while(element && element.nodeType == 1) {
		var droptarget = element.getAttribute("droptarget");
		if(droptarget != null) return element;
		element = element.parentNode;
	}
}
PgDragAndDropManager.prototype.dragIt = function(event, element, posX, posY)
{
	if(this.dragType == null) return;
	if(this.dragType == "elementToolBar") {
		var toolBar = pgCommandManager._elementToolBar
		if(document.all) {
			toolBar.style.left = this.dragOffsetX + event.clientX;
			toolBar.style.top = this.dragOffsetY + event.clientY;
		} else {
			toolBar.style.left = this.dragOffsetX + event.pageX;
			toolBar.style.top = this.dragOffsetY + event.pageY;
		}
		this.stopEvent(event);
		return;
	}
	var target = this.getTarget(element);
	var rank = parseInt(element.getAttribute("rank"), 10);
	var type = element.id;
	var posX;
	var posY;
	if (document.all != null) {
		posY = window.event.clientY;
		posX = window.event.clientX;
	} else {
		posX = event.pageX;
		posY = event.pageY;
	}
	//posY -= pgCommandManager._bodyTopOffset;
	//posY += pgCommandManager._bodyDiv.scrollTop;
	var dragElement = pgCommandManager.getDragElement();
	dragElement.className = "pgDragElementActive";
	dragElement.style.left = posX + 10;
	dragElement.style.top = posY + 10;
	if(target != null) {
		var targetDescr = target.getAttribute("droptarget").split(":");
		if(targetDescr[0] == "o") {
			var height = target.offsetHeight;
			cm.target = target;
			var targetPosition = this.getAbsolutePos(target);
			if(posY < targetPosition.y + height/2) {
				var sibling = target.previousSibling;
				while(sibling != null && (sibling.nodeType != 1 || sibling.getAttribute("droptarget") == null)) {
					sibling = sibling.previousSibling;
				}
			} else {
				var sibling = target.nextSibling;
				while(sibling != null && (sibling.nodeType != 1 || sibling.getAttribute("droptarget") == null)) {
					sibling = sibling.nextSibling;
				}			
			}
			target = sibling;
		} else if(targetDescr[0] == "c") {
			var sibling = target.lastChild;
			while(sibling != null && (sibling.nodeType != 1 || sibling.getAttribute("droptarget") == null)) {
				sibling = sibling.previousSibling;
			}
			target = sibling;
		}
	}
	this.highlightTarget(target);
	this.stopEvent(event);

}

PgDragAndDropManager.prototype.highlightTarget = function(target) 
{
	if(this.highlightedTarget != null) {
		var isLast = this.highlightedTarget.className.indexOf("Last") > 0;
		this.highlightedTarget.className =  isLast ? "pgLastDropTarget": "pgDropTarget";
		this.highlightedTarget = null;
	}
	if(target != null) {
		var isLast = target.className.indexOf("Last") > 0;
		this.highlightedTarget = target;
		target.className = isLast ? "pgLastDropTargetActive" : "pgDropTargetActive";
	}
}


PgDragAndDropManager.prototype.stopEvent = function(ev) 
{
	ev || (ev = window.event);
	if (document.all != null) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
}

function pgDragStart(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	var obj = pgCommandManager.getObjectFromElement(element);
	if(obj != null && obj._objectType == "PgToolBarButton") return pgButtonClick(evt);
	dragAndDropManager.dragStart(evt, element);
}
function pgDragIt(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	dragAndDropManager.dragIt(evt, element);
}

function pgDragEnd(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	dragAndDropManager.dragEnd(evt, element);
}


function pgStopEvent(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	dragAndDropManager.stopEvent(evt, element);
}


function pgNavigationButtonClick(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	pgCommandManager.navigationButtonClick(evt, element);
}


function pgGetElement(evt) 
{
	if (document.all != null) {
		return window.event.srcElement;
	} else {
		return evt.currentTarget;
	}
}

function PgNavigationButton(definition, label, visibility, link)
{
	this.definition = definition;
	this.label = label;
	this.visibility = visibility;
	this.link = link;
}

PgNavigationButton.prototype.highlight = function()
{
	if(this.img == null) return;
	this.img.src = "./iso_icons/pg_navbar_big_selected.png";
}

PgNavigationButton.prototype.unhighlight = function()
{
	if(this.img == null) return;
	this.img.src = "./iso_icons/pg_navbar_big_active.png";
}

PgNavigationButton.prototype.registerEvent = function(func, obj)
{
	this.eventFunc = func;
	this.eventparam = obj;
}

PgNavigationButton.prototype.click = function()
{
	var func = this.eventFunc
	if(typeof(func) == "function") {
		func(this.eventparam);
	}
}

function PgElementModel(name, label, description, icon)
{
	this.name = name;
	this.label = label;
	this.description = description;
	this.icon = icon;
}

var dragAndDropManager;
var pgAllObject = {};

function PgCommandManager()
{
	this._started = false;
	addLoadAction(pgOnload);
	addResizeActions(pgResize);
	this._flows = {};
	this._editMode = false;
	this._debug = false;
	this._consoleVisible = false;
	this._delayedCommand = "";
	this._elementModels = {};
	this._webParts = {};
	this._objectIdCounter = 1;
	this._objectId = 0;
	pgAllObject[0] = this;
	this._toolBarButtons = new Array();
	this._objectType = "PgToolBPgCommandManagerarButton";
	this._active = false;
	this._navigationButtons = new Array();
	this._navigationButtonByDefinition = new Object();
	this._mode = "pg:workshop";
	this._isEdit = false;
	this._rights = {};
	this._clientId = Math.floor(Math.random() * 100000) + "_";
	this.jobId = 0;
	this._jobPostProcessors = new Object();
}

PgCommandManager.prototype.acknowledgePageStatusChange = function(pageOid, newStatus, newVersion, newPageVersionOid)
{
	consoleDump("change status " + pageOid + " " + newStatus + " " + newPageVersionOid);
	if(this.pageStatusChangeProcessor) {
		this.pageStatusChangeProcessor.pageStatusChange(pageOid, newStatus, newVersion, newPageVersionOid)
	}
	if(pageOid == this.pageOid) {
		if(newVersion == null) newVersion = this.lastPublishedVersion;
		if(newPageVersionOid == null) newPageVersionOid = this.pageVersionOid;
		this.setPageOid(pageOid, newStatus, newVersion, newPageVersionOid)
	}
}

PgCommandManager.prototype.acknowledgePageVersionClone = function(pageOid, oldPageVersionOid, newPageVersionOid, newMode, needEditPage)
{
	consoleDump("acknowledgePageVersionClone " + pageOid +" : " +  oldPageVersionOid + " -> " + newPageVersionOid);
	if(this._mode == "pg:siteMap") {
		if(this.pageStatusChangeProcessor) {
			this.pageStatusChangeProcessor.pageStatusChange(pageOid, 0, this.lastPublishedVersion, newPageVersionOid)
		}
		if(pageOid == this.pageOid) this.setPageOid(pageOid, 0, this.lastPublishedVersion, newPageVersionOid)
	} else {
		this.reloadWithMode(newMode, needEditPage);
	}
}

PgCommandManager.prototype.setActive = function(isEdit, mode, login, userName, status, lastPageUrl)
{
	this._active = true;
	this._isEdit = isEdit;
	this._mode = mode;
	this._login = login;
	this._userName = userName;
	this._status = status;
	this._lastPageUrl = lastPageUrl;
}
PgCommandManager.prototype.setPublishedVersions = function(versionMap)
{
	this._versionMap = versionMap;
}
PgCommandManager.prototype.registerObject = function(obj)
{
	var id = obj._objectId = this._objectIdCounter++;
	pgAllObject[id] = obj;
}
PgCommandManager.prototype.unregisterObject = function(obj)
{
	var id = obj._objectId;
	if(pgAllObject[id] != obj) return;
	pgAllObject[id] = null;
}

PgCommandManager.prototype.getObject = function(id)
{
	return pgAllObject[id];
}
PgCommandManager.prototype.getElementModel = function(name)
{
	return this._elementModels[name];
}
PgCommandManager.prototype.getObjectFromElement = function(element)
{
	while(element && element.nodeType == 1) {
		var obj= pgAllObject[element.getAttribute("objectId")];
		if(obj != null) return obj;
		element = element.parentElement;
	}
	return null;
}

PgCommandManager.prototype.start = function()
{
	if(this._started) return;
	this._started = true;
	if(this._active) {
		dragAndDropManager = new PgDragAndDropManager();
		this.installLayout();
		if(this._needEditPage == true) {
			this._needEditPage = false;
			if(this._status != 2) this.editPage();
		}
	}
}
PgCommandManager.prototype.addElementModel = function(name, label, description, icon)
{
	this._elementModels[name] = new PgElementModel(name, label, description, icon);
}
PgCommandManager.prototype.setCommandProcessorUrl = function(commandProcessorUrl)
{
	this.commandProcessorUrl = commandProcessorUrl;
}
PgCommandManager.prototype.setElementEditorUrl = function(elementEditorUrl)
{
	this.elementEditorUrl = elementEditorUrl;
}
PgCommandManager.prototype.setPageEditorUrl = function(pageEditorUrl)
{
	this.pageEditorUrl = pageEditorUrl;
}
PgCommandManager.prototype.setRights = function(name, state)
{
	this._rights[name] = state;
}

PgCommandManager.prototype.setPageVersionOid = function(pageVersionOid)
{
	this.pageVersionOid = pageVersionOid;
}


PgCommandManager.prototype.setEditionVersionOid = function(editionVersionOid)
{
	this.editionVersionOid = editionVersionOid;
}

PgCommandManager.prototype.setLastPublishedVersion = function(lastPublishedVersion)
{
	this.lastPublishedVersion = lastPublishedVersion;
}
PgCommandManager.prototype.registerEditPageProcessor = function(processor)
{
	this.editPageProcessor = processor;
}

PgCommandManager.prototype.registerPageStatusChangeProcessor = function(processor)
{
	this.pageStatusChangeProcessor = processor;
}

PgCommandManager.prototype.needEditPage = function()
{
	this._needEditPage = true;
}


PgCommandManager.prototype.setPageOid = function(pageOid, status, lastPublishedVersion, pageVersionOid)
{
	this.pageOid = pageOid;
	this._status = status;
	this.lastPublishedVersion = lastPublishedVersion;
	this.pageVersionOid = pageVersionOid;
	if(this.redactionButton != null) {
		var redactionButton = this.redactionButton;
		var validationButton = this.validationButton;
		var publishedButton = this.publishedButton;
		var label = "";
		if(this.lastPublishedVersion != null) {
			label = "Publié (version " + this.formatVersion(this.lastPublishedVersion) + ")";
		} else {
			label = "Publié";
		}
		publishedButton.setLabel(label);
		switch(this._status) {
		case 0:  // en redaction
			redactionButton.setState(true);
			validationButton.setState(false);
			publishedButton.setState(false);
			redactionButton.setActive(false);
			validationButton.setActive(true);
			if(!this._rights["validation"]) {
				publishedButton.setActive(false);
			} else {
				publishedButton.setActive(true);
			}
			break;
		case 1:  // en validation
			redactionButton.setState(false);
			validationButton.setState(true);
			publishedButton.setState(false);
			redactionButton.setActive(true);
			validationButton.setActive(false);
			if(!this._rights["validation"]) {
				publishedButton.setActive(false);
			} else {
				publishedButton.setActive(true);
			}
			break;
		default: // publie
			redactionButton.setState(false);
			validationButton.setState(false);
			publishedButton.setState(true);
			redactionButton.setActive(true);
			validationButton.setActive(true);
			publishedButton.setActive(false);
		}
		redactionButton.setVisible(true);
		validationButton.setVisible(true);
		publishedButton.setVisible(true);
	}

}

PgCommandManager.prototype.resize = function()
{
	if(!this._bodyDiv) return;
	this._reservedHeight = 25;
	if(this._consoleVisible) this._reservedHeight += 100;
	this._bodyTopOffset = this._reservedHeight
	if(this._isEdit && this._hasNavigationBar) this._reservedHeight += 50;
	var bodyClientSize = getBodyClientSize();
	var height = bodyClientSize.height - this._reservedHeight;
	var width = bodyClientSize.width;
	if(height > 10) {
		this._bodyDiv.style.height = height + "px";
	}
	if(this._consoleInput != null && width - 150 > 10) this._consoleInput.style.width = (width - 150) + "px";
	if(this._mode == "pg:siteMap") {
		var treeRoot = document.getElementById("pgTreeRoot");
		var height = bodyClientSize.height - this._reservedHeight - 180;
		if(treeRoot && height > 10) {
			treeRoot.style.height = height + "px";
		}
	}
}

PgCommandManager.prototype.addToolBarButton = function(button)
{
	this._toolBarButtons[this._toolBarButtons.length] = button;
}

PgCommandManager.prototype.getToolBarDiv = function()
{
	return this._toolBarDiv;
}
PgCommandManager.prototype.dObject = function(o)
{
	if(o == null) return "null";
	switch(typeof(o)) {
	case "function": return "[function]";
	case "boolean":
	case "number":
	case "string":
		return o.toString().replace(/</g, "&lt;").replace(/\n/g, "<br>").replace(/\t/g, "\240\240\240\240");
	case "object":
		return "[object]";
	}	
}
PgCommandManager.prototype.dumpObject = function(o)
{
	switch(typeof(o)) {
	case "function":
	case "boolean":
	case "number":
	case "string":
		return o.toString().replace(/</g, "&lt;").replace(/\n/g, "<br>").replace(/\t/g, "\240\240\240\240");
	case "object":
		if(o == null) return "null";
		if(typeof(o.nodeType) == "number") {
			switch(o.nodeType) {
			case 1:
				if(o.namespaceURI != null) { // XML node
					var res = "&lt;<FONT COLOR='red'>" + o.nodeName + "</FONT>";
					var i;
					for(i=0;i < o.attributes.length; i++) {
						var a = o.attributes.item(i);
						res += " <FONT COLOR='GREEN'>" + a.name + "</FONT>=<FONT COLOR='blue'>" + a.value + "</FONT>";
					}
					res += "&gt;";
				} else {
					var res = "&lt;<FONT COLOR='red'>" + o.nodeName + "</FONT>";
					var name;
					for(name in o) {
						res += " <FONT COLOR='GREEN'>" + name + "</FONT>=<FONT COLOR='blue'>" + this.dObject(o[name]) + "</FONT>";
					}
					res += "&gt;";
				}
				break;
			}
		} else {
			var res = "[object]<BR>";
			var sub;
			try {
				for (sub in o) {
					res += "&nbsp;&nbsp;&nbsp;&nbsp;" + sub + "=" + this.dObject(o[sub]); 
					res += "<BR>";
				}
			} catch(e) {
				if(typeof(o.length) == "number") {
					for(var i=0;i<o.length;i++) {
						res += "&nbsp;&nbsp;&nbsp;&nbsp;" + i + "=" + this.dObject(o.item(i)); 
						res += "<BR>";
					}
				}
			}
		}
		return res;
		
	}	
}

PgCommandManager.prototype.consoleDump = function(obj, title)
{
	if(this._consoleForm == null) return;
	if(this._consoleVisible == null) return;
	var str = "";
	if(title != null) str = title + " = ";
	str += this.dumpObject(obj);
	str = "" + str;
	var div = document.createElement("div");
	div.innerHTML = str;
	this._consoleFlow.appendChild(div);
	var parent = this._consoleFlow;//.parentElement;
	var height = parent.scrollHeight - 73;
	if(height > 0) parent.scrollTop = height + 20;

}
PgCommandManager.prototype.buildConsole = function()
{
	var consoleForm = this._consoleForm = document.createElement("form");
	consoleForm.className = "pgConsole";
	consoleForm.style.display = "none";
	var consoleFlow = this._consoleFlow = document.createElement("div");
	consoleFlow.className = "pgConsoleFlow";
	consoleForm.appendChild(consoleFlow);
	var consoleInput = this._consoleInput = document.createElement("input");
	consoleInput.className = "pgConsoleInput";
	consoleForm.appendChild(consoleInput);

	var code = "<input type=\'submit\'>";
	var evalButton = this._evalButton = document.createElement("input");
	evalButton.type = "submit";
	evalButton.className = "pgConsoleButton";
	evalButton.value = "Ok";
	consoleForm.appendChild(evalButton);
	
	var consoleClearButton = this._consoleClearButton = document.createElement("input");//"<input type='button'>");
	consoleClearButton.type = "button";
	consoleClearButton.className = "pgConsoleButton";
	consoleClearButton.value = "clear";
	consoleClearButton.onclick = pgClearConsole;
	evalButton.onclick = pgEvalConsole;
	consoleForm.appendChild(consoleClearButton);
	consoleForm.onsubmit = pgEvalConsole;
}



PgCommandManager.prototype.buildCommandIframe = function()
{
	var commandIframe = this._commandIframe = document.createElement("iframe");
	commandIframe.className = "pgCommandIframe";
	commandIframe.src = this.commandProcessorUrl;
	commandIframe.name = "pgCommandProcessor";
	commandIframe.id = "pgCommandProcessor";
}

PgCommandManager.prototype.createNavigationButton = function(row, button, position, rank)
{
	var td = document.createElement("td");
	td.width = 100;
	td.className = "PgNavigationbar" + position + "Cell";
	td.vAlign = "top";
	row.appendChild(td);
	// taking initial  button label
	var label = button.label;
	var alt = button.alt;
	if(alt == null || alt == "") alt = label;
	var a = document.createElement("a");
	a.button = button
	a.href = "#";
	td.appendChild(a);
	a.onclick = pgNavigationButtonClick;
	var img = document.createElement("img");
	img.alt = alt;
	img.src = "./iso_icons/pg_navbar_big_active.png";
	img.className = "format_png";
	img.border = "0px";
	button.img = img;
	a.appendChild(img);
	var br = document.createElement("br");
	a.appendChild(br);
	var span = document.createElement("span");
	a.appendChild(span);
	span.innerHTML = label.replace(/&/g, "&amp;").replace(/</g, "&lt;");
}
PgCommandManager.prototype.buildElementToolBar = function()
{
	var elementToolBar = this._elementToolBar = document.createElement("div");
	elementToolBar.className = "pgElementToolBarDiv pgProtect";
	elementToolBar.style.display = "none";
	elementToolBar.style.top = "25px";
	elementToolBar.style.left = "0px";
	var titleBar = document.createElement("div");
	titleBar.setAttribute("dragType", "elementToolBar");
	titleBar.innerHTML = "Palette";
	titleBar.className = "pgPopupTitle";
	elementToolBar.appendChild(titleBar);
	var closeButton = document.createElement("a");
	closeButton.className = "pgTitleCloseButton";
	closeButton.innerText = "x";
	closeButton.href = "#";
	closeButton.onclick = pgCloseButtonClick;
	titleBar.insertBefore(closeButton, titleBar.firstChild);
	var ul = document.createElement("ul");
	elementToolBar.appendChild(ul);
	var n = 0;
	for(var name in this._elementModels) {
		n++;
		var elementModel = this._elementModels[name];
		var modelDiv = document.createElement("li");
		modelDiv.className = "elementModel";
		modelDiv.id = "M" + name;
		modelDiv.setAttribute("dragType", "newWebPart");
		ul.appendChild(modelDiv);
		var img = document.createElement("img");
		img.src = "./imageProvider.asp?private_image=" + elementModel.icon;
		img.className = "format_png";
		img.width = 16;
		img.height = 16;
		img.align = "absbottom";
		img.border = 0;
		modelDiv.appendChild(img);
		var span = document.createElement("span");
		span.innerHTML = elementModel.label;
		modelDiv.appendChild(span);
	}
	if(n > 20) {
		n = 20;
		var height = n * 25;
		ul.style.height = height + "px"; 
	}
}

PgCommandManager.prototype.hideElementToolBar = function()
{
	if(this._elementToolBar == null) return;
	this._elementToolBar.style.display = "none";
	this.elementToolBarButton.setState(false);
}


PgCommandManager.prototype.toggleElementToolbar = function(evt, state)
{
	if(this._elementToolBar == null) return;
	if(state) {
		this._elementToolBar.style.display = "block";
	} else {
		this._elementToolBar.style.display = "none";
	}
}



PgCommandManager.prototype.buildNavigationBar = function()
{
	var navigationBarDiv = this._navigationBarDiv = document.createElement("div");
	navigationBarDiv.className = "pgNavigationBarDiv pgProtect";
	var table = document.createElement("table");
	table.cellSpacing = 0;
	table.cellPadding = 0;
	navigationBarDiv.appendChild(table);
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);
	var tr = document.createElement("tr");
	tbody.appendChild(tr);
	var buttons = new Array();
	for(var i = 0;i< this._navigationButtons.length;i++) {
		var button = this._navigationButtons[i];
		if(this._reducedSize && button.definition != "pg:workshop" && button.definition != "pg:preview") continue;
		buttons[buttons.length] = button;
	}
	for(var i = 0;i< buttons.length;i++) {
		var button = buttons[i];
		var position = "Middle";
		if(i==0) {
			position = "First";
		} else if(i == buttons.length - 1) {
			position = "Last";
		}
		this.createNavigationButton(tr, button, position, i);
	}
	if(this._status == 2 && this._mode == "pg:workshop") {
		this._mode = "pg:preview";
	}
	this._editMode = this._mode == "pg:workshop";
	switch(this._mode) {
	default:
		var button = this._navigationButtonByDefinition[this._mode];
		if(button) button.highlight();
	}
}

PgCommandManager.prototype.navigationButtonClick = function(evt, element)
{
	var button = null;
	while(element && element.nodeType == 1) {
		button = element.button;
		if(button != null) break;
		element = element.parentNode;
	}
	if(button == null) {
		return false;
	}
	button.click();
	switch(this._mode) {
	case "pg:services":
	case "pg:configuration":
		switch(button.definition) {
		case "pg:workshop":
		case "pg:preview":
			if(this._lastPageUrl != null) {
				var url = this._lastPageUrl + "&pgMode=" + button.definition;
				document.location.assign(url);
				return;
			}
		}
	}
	switch(button.definition) {
	case "pg:workshop":
		if(!this.toEdit(null, null, "pg:workshop")) return;
		this.highlightNavigationButton(button);
		this._mode = button.definition;
		this._editMode = true;
		this.updateEditMode();
		this.postCommand("mo " + this._mode);
		if(this.elementToolBarButton) {
			if(this.isReadOnly()) {
				this.hideElementToolBar();
				this.elementToolBarButton.setVisible(false);
				this.elementToolBarButton.setState(false);
			} else {
				this.elementToolBarButton.setVisible(true);
				if(this._reducedSize) {
					this.toggleElementToolbar(null, false);
					this.elementToolBarButton.setState(false);
				} else {
					this.toggleElementToolbar(null, true);
					this.elementToolBarButton.setState(true);
				}
			}
		}
		break;
	case "pg:preview":
		this.highlightNavigationButton(button);
		this._mode = button.definition;
		this._editMode = false;
		this.updateEditMode();
		this.postCommand("mo " + this._mode);
		if(this.elementToolBarButton) {
			this.hideElementToolBar();
			this.elementToolBarButton.setVisible(false);
			this.elementToolBarButton.setState(false);
		}
		break;
	default:
		var url = button.link + "&page=" + this.pageOid;
		document.location.assign(url);
		break;
	}
}
PgCommandManager.prototype.highlightNavigationButton = function(button)
{
	for(var i=0; i < this._navigationButtons.length; i++) {
		var b = this._navigationButtons[i];
		if(b == button) {
			b.highlight();
		} else {
			b.unhighlight();
		}
	}
}
PgCommandManager.prototype.patchInvisibleWebPart = function()
{
	this.postCommand("patchInvisibleWebPart");
}

PgCommandManager.prototype.twoDigits = function(n)
{
	n = "" + n;
	if(n.length == 1) n = "0" + n;
	return n;
}
PgCommandManager.prototype.formatDate = function(date, needHours)
{
	var hours = "";
	if(needHours) {
		hours = " " + this.twoDigits(date.getHours()) + ":" + this.twoDigits(date.getMinutes())
	}
	if(lang.substr(0,2) == "_en") {
		return this.twoDigits(date.getMonth() + 1) + "/" + this.twoDigits(date.getDate()) + "/" + date.getFullYear() + hours;
	} else {
		return this.twoDigits(date.getDate()) + "/" + this.twoDigits(date.getMonth() + 1) + "/" + date.getFullYear() + hours;
	}
}
PgCommandManager.prototype.getDragElement = function()
{
	return this._dragElement;
}

PgCommandManager.prototype.addCssConsoleButton = function()
{
	if(!this._debug) return;
	var button = new PgToolBarButton("iso_icons/pgCssConsole.gif", "Afficher la console CSS", true, "toggleCssConsole", this._objectId, true, false);
	this.addToolBarButton(button);
	pgCssInspector.setButton(button);
}

PgCommandManager.prototype.installLayout = function()
{
	this._hasNavigationBar = true;
	this._reducedSize = typeof(pageElementType) == "string" && pageElementType == "pg:genericPopup";
	var dragDiv = this._dragElement = document.createElement("div");
	dragDiv.className = "pgDragElement";
	dragDiv.id = "pgDragElement";
	var bodyDiv = this._bodyDiv = document.createElement("div");
	bodyDiv.className = "pgBodyDiv";
	bodyDiv.id = "bodyDiv";
	var toolBarDiv = this._toolBarDiv = document.createElement("div");
	toolBarDiv.className = "pgToolBar pgProtect";
	if(this._debug) this.buildConsole();
	this.buildCommandIframe();
	if(this._isEdit) {
		if(this._hasNavigationBar) {
			this.buildNavigationBar();
		} else {
			if(this._status == 2 && this._mode == "pg:workshop") {
				this._mode = "pg:preview";
			}
			this._editMode = this._mode == "pg:workshop";
		}
		this.buildElementToolBar();
	}
	this._reservedHeight = 25;
	var body = document.body;
	body.appendChild(bodyDiv);
	var inputs = document.getElementsByTagName("input");
	var checkedInputs = new Array();
	if(inputs != null) {
		for(var i=0;i<inputs.length;i++) {
			var input = inputs[i];
			if(input.type == "checkbox" && input.checked) checkedInputs[checkedInputs.length] = input;
		}
	}
	while(body.firstChild != bodyDiv) {
		var firstChild = body.firstChild;
		body.removeChild(firstChild);
		bodyDiv.appendChild(firstChild);
	}
	body.insertBefore(toolBarDiv, bodyDiv);
	body.appendChild(this._commandIframe);
	if(this._consoleForm != null) body.appendChild(this._consoleForm);
	if(this._isEdit && this._hasNavigationBar) {
		body.appendChild(this._navigationBarDiv);
		body.appendChild(this._elementToolBar);
	}
	for(var i=0;i<checkedInputs.length;i++) {
		var input = checkedInputs[i];
		input.checked = true;
	}
	// pour que cela fonctionne partout ("W3C compliant mode" = avec DOCTYPE ou ancien IE mode)
	// on regle le scroll sur <body> et <html>
	body.style.width = "100%";
	body.style.height = "100%";
	body.style.overflow = "hidden";
	body.style.overflowX = "hidden";
	body.style.overflowY = "hidden";
	body.parentNode.style.width = "100%";
	body.parentNode.style.height = "100%";
	body.parentNode.style.overflow = "hidden";
	body.parentNode.style.overflowX = "hidden";
	body.parentNode.style.overflowY = "hidden";
	body.style.margin = "0px";
	if(this._debug) {
		this.addToolBarButton(new PgToolBarButton("iso_icons/pgConsole.gif", "Afficher la console", true, "toggleConsole", this._objectId, true, false));
		if(isoProfiler != null && isoProfiler.data != null) this.addToolBarButton(new PgToolBarButton("iso_icons/pgProfiler.gif", "Afficher les résultats du profiler", false, "showProfile", this._objectId, true, false));
	}
	this.addCssConsoleButton();
	switch(this._mode) {
	case "pg:siteMap":
		this.installSitemapToolBar(pgHierarchyEditor);
		break;
	}
	if(!this._isEdit) {
		this.addToolBarButton(new PgToolBarButton("iso_icons/pgEdit.gif", "Éditer le site", true, "startEdit", this._objectId, false, false));
	} else {
		this.addToolBarButton(new PgToolBarButton("iso_icons/pgBackToSite.gif", "Retour au site en ligne", true, "stopEdit", this._objectId, false, false));
	}
	if(this._isEdit) {
		if(this._status == 3) {
			this.addToolBarButton(new PgToolBarButton("iso_icons/pgRollback.gif", "Éditer cette version", false, "rollback", this._objectId, false, !this._reducedSize));
		} else {
			this.addToolBarButton(new PgToolBarButton("iso_icons/pgEdit.gif", "Éditer la page", false, "editPage", this._objectId, false, false));
		}
		switch(this._mode) {
		case "pg:workshop":
		case "pg:preview":
			this.addToolBarButton(this.elementToolBarButton = new PgToolBarButton("iso_icons/pgElementToolBarButton.gif", "Palette d'éléments", true, "toggleElementToolbar", this._objectId, false, false));
			if(this._mode == "pg:workshop") {
				if(!this._reducedSize) {
					this.toggleElementToolbar(null, true);
					this.elementToolBarButton.setState(true);
				} else {
					this.elementToolBarButton.setState(false);
				}
				this.elementToolBarButton.setVisible(true);
			} else {
				this.elementToolBarButton.setVisible(false);
				this.elementToolBarButton.setState(false);
			}
			if(this._status != 3) {
				this.installStateButtons(true, this);
			}
			this.installVersionSelect();
			if(this._status < 2) {
				this.addToolBarButton(new PgToolBarButton("iso_icons/pgRollback.gif", "Abandonner l'édition en cours et revenir à la dernière version publiée", false, "dropEditionVersion", this._objectId, false, false));
			}
			break;
		case "pg:siteMap":
			this.installStateButtons(false, pgHierarchyEditor);
			break;
		}

	}
	var toolBarEndDiv = this._toolBarEndDiv = document.createElement("div");
	toolBarEndDiv.className = "pgToolBarEnd";
	var text = this._login;
	if(this._userName != "") text = this._userName + " (" + text + ")";
	var now = new Date();
	text += " - " + this.formatDate(now, true)
	if(this._reducedSize) text = "";
	toolBarEndDiv.innerHTML = text;
	toolBarDiv.appendChild(toolBarEndDiv);
	body.appendChild(dragDiv);
	this.updateEditMode();
	this.resize();
	//document.body.ondblclick = function() { cm.div = event.srcElement; alert(event.srcElement.outerHTML);};
}

PgCommandManager.prototype.installVersionSelect = function()
{
	if(this._versionMap == null) return;
	for(var k in this._versionMap) {
		break;
	}
	if(k == null) return;
	this._versionSelect = new PgVersionSelect(this._versionMap, this.pageVersionOid, this._status, this.editionVersionOid, this._reducedSize);
}
PgCommandManager.prototype.installStateButtons = function(fixed, owner)
{
	var redactionButton, validationButton, publishedButton;
	this.addToolBarButton(redactionButton = this.redactionButton = new PgToolBarButton("iso_icons/pgRedactionState.gif", "En rédaction", true, "toEdit", owner._objectId, false, !this._reducedSize));
	this.addToolBarButton(validationButton = this.validationButton = new PgToolBarButton("iso_icons/pgValidationState.gif", "En validation", true, "toValidate", owner._objectId, false, !this._reducedSize));
	var label = "";
	if(fixed) {
		if(this.lastPublishedVersion != null) {
			label = "Publié (version " + this.formatVersion(this.lastPublishedVersion) + ")";
		} else {
			label = "Publié";
		}
	}
	this.addToolBarButton(publishedButton = this.publishedButton = new PgToolBarButton("iso_icons/pgPublishedState.gif", label, true, "publish", owner._objectId, false, !this._reducedSize));
	if(fixed) {
		switch(this._status) {
		case 0:  // en redaction
			redactionButton.setState(true);
			redactionButton.setActive(false);
			if(!this._rights["validation"]) {
				publishedButton.setActive(false);
			}
			break;
		case 1:  // en validation
			validationButton.setState(true);
			validationButton.setActive(false);
			if(!this._rights["validation"]) {
				publishedButton.setActive(false);
			}
			break;
		default: // publie
			publishedButton.setState(true);
			publishedButton.setActive(false);
		}
	} else {
		if(this.pageOid != null) {
			this.setPageOid(this.pageOid, this._status, this.lastPublishedVersion, this.pageVersionOid);
		} else {
			redactionButton.setVisible(false);
			validationButton.setVisible(false);
			publishedButton.setVisible(false);
		}
	}
}


PgCommandManager.prototype.installSitemapToolBar = function(hierarchyEditor)
{
	this.registerObject(hierarchyEditor);
	var objectId = hierarchyEditor._objectId;
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgMoveleft.gif", "Moins profond dans la hiérarchie", false, "moveLeft", objectId, false, false));	
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgMovedown.gif", "Avancer", false, "moveDown", objectId, false, false));	
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgMoveup.gif", "Reculer", false, "moveUp", objectId, false, false));	
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgMoveright.gif", "Plus profond dans la hiérarchie", false, "moveRight", objectId, false, false));	
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgRemove.gif", "Supprimer", false, "deleteFolder", objectId, false, false));	
}

PgCommandManager.prototype.declareNavigationButton = function(definition, label, visibility, link)
{
	var button = new PgNavigationButton(definition, label, visibility, link);
	this._navigationButtons[this._navigationButtons.length] = button;
	this._navigationButtonByDefinition[button.definition] = button;
}

PgCommandManager.prototype.registerNavigationButtonEvent = function(definition, func, obj)
{
	var button = this._navigationButtonByDefinition[definition];
	if(button == null) return;
	button.registerEvent(func, obj);
}

PgCommandManager.prototype.setDebug = function(status)
{
	this._debug = status;
}
PgCommandManager.prototype.addEvent = function(element, evname, func)
{
	if (element.attachEvent) { // IE
		element.attachEvent("on" + evname, func);
	} else if (element.addEventListener) { // Gecko / W3C
		element.addEventListener(evname, func, true);
	} else {
		element["on" + evname] = func;
	}
}

PgCommandManager.prototype.removeEvent = function(element, evname, func)
{
	if (element.detachEvent) { // IE
		element.detachEvent("on" + evname, func);
	} else if (element.removeEventListener) { // Gecko / W3C
		element.removeEventListener(evname, func, true);
	} else {
		element["on" + evname] = null;
	}
}

PgCommandManager.prototype.stopEvent = function(ev) 
{
	ev || (ev = window.event);
	if (document.all != null) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
}
PgCommandManager.prototype.registerFlow = function(oid, zone)
{
	var id = oid + "_" + zone;
	this._flows[id] = new PgFlowManager(oid, zone);
}
PgCommandManager.prototype.registerWebPart = function(webPart)
{
	this._webParts[webPart._oid] = webPart;
}

PgCommandManager.prototype.unregisterWebPart = function(webPart)
{
	if(this._webParts[webPart._oid] != webPart) return;
	this._webParts[webPart._oid] = null;
}

PgCommandManager.prototype.toggleEditMode = function(evt)
{
	this._editMode = !this._editMode;
	this.updateEditMode();
}

PgCommandManager.prototype.isReadOnly = function()
{
	return this._status >= 2;
}

PgCommandManager.prototype.updateEditMode = function()
{
	for(var oid in this._flows) {
		this._flows[oid].setEditMode(this._editMode, this._status == 3);
	}
	if(this._editMode) {
		this.addEvent(this._bodyDiv, "mousedown", pgDragStart);
		if(this._elementToolBar) this.addEvent(this._elementToolBar, "mousedown", pgDragStart);
	} else {
		this.removeEvent(this._bodyDiv, "mousedown", pgDragStart);
		if(this._elementToolBar) this.removeEvent(this._elementToolBar, "mousedown", pgDragStart);
	}
}

PgCommandManager.prototype.editPage = function()
{
	if(this._status == 2) {
		this.clonePageVersion(0, null, new ToEditPagePostProcessor(), true, null);
		return;
	}
	this.editPageWork();
}

PgCommandManager.prototype.editPageWork = function()
{	
	if(this.editPageProcessor) {
		return this.editPageProcessor.editPage();
	}
	var width = "800";
	var height = "600";
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	var options = 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	var url = this.pageEditorUrl;
	url += "?page=" + this.pageOid;
	window.pgPageEditorRequirer = this;
	//showModalWindow(url, "pageEditor", width, height);
	var win = window.open(url, "pageEditor", options);
	if (win==null)
		alert(objThesaurus.translate("odfCheckPopupBlocker"));
	else
		win.focus();

}
PgCommandManager.prototype.pageHasBeenEdited = function(descr)
{
	this.reload();
}

PgCommandManager.prototype.getCurrentPageUrl = function()
{
	var href = document.location.href;
	var pos = href.indexOf("#");
	if(pos >= 0) {
		anchor = href.substr(pos);
		href = href.substr(0, pos);
	}
	var pos = href.indexOf("?");
	if(pos > 0) href = href.substr(0, pos);
	return href;
}


PgCommandManager.prototype.startEdit = function(evt)
{
	var href = this.getCurrentPageUrl();
	href += "?pgEdit=true";
	if(this.pageOid != null) {
		href += "&page=" + this.pageOid;
	}
	document.location.assign(href);
}

PgCommandManager.prototype.stopEdit = function(evt)
{
	var href = this.getCurrentPageUrl();
	href += "?pgEdit=false";
	if(this.pageOid != null) {
		href += "&page=" + this.pageOid;
	}
	document.location.assign(href);
}

PgCommandManager.prototype.reload = function()
{
	var href = this.getCurrentPageUrl();
	if(this.pageOid != null) {
		href += "?page=" + this.pageOid;
	}
	document.location.assign(href);
}

PgCommandManager.prototype.reloadWithMode = function(mode, needEditPage)
{
	var href = this.getCurrentPageUrl();
	var extension = "";
	if(this.pageOid != null) {
		extension += "page=" + this.pageOid;
	}
	if(mode != null) {
		if(extension != "") extension += "&";
		extension += "pgMode=" + mode;
	}
	if(needEditPage) {
		extension += "&needEditPage=true";
	}
	document.location.assign(href + "?" + extension);
}

PgCommandManager.prototype.setStatus = function(status, version)
{
	this._status = status;
	var command = "ss " + this.pageOid +" " + this.pageVersionOid + " " + status;
	if(status == 2) command += " " + version;
	var job = null;
	if(status == 2) {
		job = new ReloadPostProcessor();
	}
	this.postCommand(command, job);
}

PgCommandManager.prototype.formatVersion = function(version)
{
	var major = Math.floor(version);
	var minor = Math.round((version - major) * 1000) + "";
	return major + "." + ("000").substr(minor.length) + minor;
}

PgCommandManager.prototype.clonePageVersion = function(newStatus, newMode, job, needEditPage, srcOid)
{
	if(srcOid == null && !confirm("La page courante est actuellement publiée.\nVoulez-vous la passer en édition?")) return false;
	if(srcOid == null) srcOid = pgCommandManager.pageVersionOid;
	var command = "cl " + pgCommandManager.pageOid +" " + srcOid + " " + newStatus;
	if(newMode != null) command += " " + newMode;
	else command += " null";
	if(srcOid != null) command += " " + srcOid;
	else command += " null";
	command += " " + (needEditPage == true);
	this.postCommand(command, job);
	return true;
}

PgCommandManager.prototype.rollback = function(event, state)
{
	if(!confirm("Voulez-vous éditer à partir de cette version de la page?")) return;
	this.clonePageVersion(0, null, new ToEditPagePostProcessor(), true, this.pageVersionOid);
}


PgCommandManager.prototype.dropEditionVersion = function(event, state)
{
	if(!confirm("Êtes-vous sûr d'abandonner l'édition en cours et de revenir à la dernière version publiée?")) return;
	var command = "de " + this.pageOid + " " + this.pageVersionOid;
	this.postCommand(command, new ReloadPostProcessor());
}
PgCommandManager.prototype.toEdit = function(event, state, mode)
{
	if(this._status == 3) {
		return true;
	}
	if(this._status == 2) {
		if(this.redactionButton != null) this.redactionButton.setState(false);
		this.clonePageVersion(0, mode, null, false, null);
		return false;
	}
	return this.toEditWork();
}

PgCommandManager.prototype.toEditWork = function()
{
	this.setStatus(0);
	if(this.redactionButton != null) {
		this.redactionButton.setActive(false);
		this.validationButton.setActive(true);
		this.publishedButton.setActive(this._rights["validation"]);
		this.redactionButton.setState(true);
		this.validationButton.setState(false);
		this.publishedButton.setState(false);
	}
	return true;
}

PgCommandManager.prototype.toValidate = function()
{
	if(this._status == 2) {
		this.clonePageVersion(1, null, new ToValidatePostProcessor(), false, null);
		this.validationButton.setState(false);
		return false;
	}
	return this.toValidateWork();
}

PgCommandManager.prototype.toValidateWork = function()
{
	this.setStatus(1);
	this.redactionButton.setActive(true);
	this.validationButton.setActive(false);
	this.publishedButton.setActive(this._rights["validation"]);
	this.redactionButton.setState(false);
	this.validationButton.setState(true);
	this.publishedButton.setState(false);
	return true;
}

PgCommandManager.prototype.publish = function()
{
	var version = parseFloat(this.lastPublishedVersion);
	var nextVersion;
	if(isNaN(version)) {
		nextVersion = 1;
	} else {
		var nextMinorVersion = version + 0.001;
		var nextMajorVersion = Math.floor(version) + 1;
		var msg = "Souhaitez-vous juste changer le numéro mineur à " + this.formatVersion(nextMinorVersion);
		msg += " au lieu changer le numéro majeur à " + this.formatVersion(nextMajorVersion) + "?";
		if(!confirm(msg)) {
			nextVersion = nextMajorVersion;
		} else {
			nextVersion = nextMinorVersion;
		}
		var newPublishedButtonLabel = "Publié (version " + this.formatVersion(nextVersion) + ")";
		var button = this.publishedButton.setLabel(newPublishedButtonLabel);
		if(this._versionSelect) {
			this._versionSelect.removeEditionOption();
			this._versionSelect.addOption(this.pageVersionOid, nextVersion.toFixed(3));
		}
	}
	if(this._editMode) {
		this._editMode = false;
		this.updateEditMode();
	}
	if(this._mode == "pg:workshop") {
		var button = this._navigationButtonByDefinition["pg:preview"];
		if(button != null) {
			this.highlightNavigationButton(button);
		}
		this._mode = "pg:preview";
		this.hideElementToolBar();
		this.elementToolBarButton.setVisible(false);
		this.elementToolBarButton.setState(false);
	}
	this.setStatus(2, nextVersion);
	this.redactionButton.setActive(true);
	this.validationButton.setActive(true);
	this.publishedButton.setActive(false);
	this.redactionButton.setState(false);
	this.validationButton.setState(false);
	this.publishedButton.setState(true);
}

PgCommandManager.prototype.showVersion = function(versionOid)
{
	var href = this.getCurrentPageUrl();
	href += "?page=" + this.pageOid + "&pageVersion=" + versionOid;
	document.location.assign(href);
}

PgCommandManager.prototype.toggleConsole = function(evt)
{
	this._consoleVisible = !this._consoleVisible;
	if(this._consoleVisible) {
		this._consoleForm.style.display = "block";
	} else {
		this._consoleForm.style.display = "none";
	}
	this.resize();
}
PgCommandManager.prototype.showProfile = function(evt)
{
	return isoProfiler.show();
}
PgCommandManager.prototype.toggleCssConsole = function(evt)
{
	pgCssInspector.toggle()
}


PgCommandManager.prototype.siteMapMoveLeft = function(evt)
{
	pgHierarchyEditor.moveLeft();
}
PgCommandManager.prototype.siteMapMoveDown = function(evt)
{
	pgHierarchyEditor.moveDown();
}
PgCommandManager.prototype.siteMapMoveUp = function(evt)
{
	pgHierarchyEditor.moveUp();
}
PgCommandManager.prototype.siteMapMoveRight = function(evt)
{
	pgHierarchyEditor.moveRight();
}
PgCommandManager.prototype.siteMapDeleteFolder = function(evt)
{
	pgHierarchyEditor.deleteFolder();
}

PgCommandManager.prototype.clearConsole = function()
{
	this._consoleFlow.innerHTML = "";
}

PgCommandManager.prototype.evalConsole = function(evt)
{
	var code = this._consoleInput.value;
	try {
		this.consoleDump(eval(code), code);		
	} catch(e) {
		this.consoleDump(e.description);
	}
	this.stopEvent(evt);
}

PgCommandManager.prototype.updateElementHTML = function(oid, htmlElement)
{
	var webPart = this._webParts[oid];
	if(webPart) {
		// update
		var webPart = this.getWebPart(oid);
		if(webPart == null) return alert("no webPart " + oid);
		webPart.updateHTML(htmlElement);
	} else {
		// creation
		var zone = parseInt(htmlElement.getAttribute("zone"), 10);
		var rank = parseInt(htmlElement.getAttribute("rank"), 10);
		var flow = this.getFlow(zone);
		if(flow == null) return;
		flow.createWebPart(oid, htmlElement, rank);
	}
}
PgCommandManager.prototype.generateJobId = function()
{
	return this._clientId + this.jobId++;
}
PgCommandManager.prototype.postCommand = function(command, jobPostProcessor)
{
//try {
		var jobId;
		if(jobPostProcessor != null) {
			jobId = this.generateJobId();
			command = "ji " + jobId + "\n" + command;
		}
		var form = window.frames[0].document.forms.commandForm;
		var input = form.command;
		input.value = command;
		consoleDump(command);
		if(jobPostProcessor != null) {
			this._jobPostProcessors[jobId] = jobPostProcessor;
		}
		form.submit();
	/*} catch(e) {
		consoleDump(e.description);
		this._delayedCommand += command + "\n";
	}*/
}


PgCommandManager.prototype.reportMessage = function(type, message)
{
	switch(type) {
	case "fatal":
		alert("Erreur fatale sur le serveur:\n" + message);
		break;
	case "alert":
		alert(message);
		break;
	case "console":
		consoleDump(message);
		break;
	default:
		consoleDump("#### " + type + ": " + message);
	}
}

PgCommandManager.prototype.jobCallback = function(jobId, status)
{
	this.consoleDump("callback " + jobId + " " + status);
	var jobPostProcessor = this._jobPostProcessors[jobId];
	if(jobPostProcessor == null) {
		alert("job " + jobId + " inconnu.");
		return;
	}
	this._jobPostProcessors[jobId] = null;
	if(status) jobPostProcessor.commit();
	else jobPostProcessor.cancel();
}

PgCommandManager.prototype.postCommandAndWait = function(command)
{
	this.postCommand(command);
}

PgCommandManager.prototype.getFlow = function(zone)
{
	return this._flows[pgCommandManager.pageVersionOid + "_" + zone];
}

PgCommandManager.prototype.getWebPart = function(webPartOid)
{
	return this._webParts[webPartOid];
}


PgCommandManager.prototype.createElement = function(model, zone, rank)
{
	this._newZone = zone;
	this._newRank = rank;
	this._newModel = model;
	var width = "800";
	var height = "600";
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	var options = 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	var url = pgCommandManager.elementEditorUrl;
	url += "?className=" + escape(model) + "&zone=" + zone + "&pageVersion=" + this.pageVersionOid;
	if(rank != null) url += "&rank=" + rank; 
	//showModalWindow(url, "elementEditor", width, height);
	var win = window.open(url, "elementEditor", options);
	if (win==null)
		alert(objThesaurus.translate("odfCheckPopupBlocker"));
	else
		win.focus();

}

PgCommandManager.prototype.moveElement = function(webPartOid, zone, rank)
{

	var flow = this.getFlow(zone);
	if(flow == null) return alert("no flow " + zone);
	var webPart = this.getWebPart(webPartOid);
	if(webPart == null) return alert("no web part " + webPartOid);

	var command = "me " + pgCommandManager.pageVersionOid + " " + webPartOid + " " + zone + " " + rank;
	pgCommandManager.postCommand(command);
	flow.moveWebPart(webPart, rank);
}

PgCommandManager.prototype.updateLabels = function()
{
	for(var k in this._flows) {
		var flow = this._flows[k];
		flow.updateLabels();
	}
}
function pgOnload()
{
	pgCommandManager.start();
}

var pgLastResize = 0;
function pgResize()
{
	var now = new Date().getTime();
	if(now - pgLastResize < 500) return;
	pgLastResize = now;
	window.setTimeout("pgCommandManager.resize();", 100);
}

function pgToggleEditMode(evt)
{
	evt || (evt = window.event);
	return pgCommandManager.toggleEditMode(evt);
}

function pgToggleConsole(evt)
{
	evt || (evt = window.event);
	return pgCommandManager.pgToggleConsole(evt);
}

function pgClearConsole()
{
	pgCommandManager.clearConsole();
}

function pgEvalConsole(evt)
{
	evt || (evt = window.event);
	pgCommandManager.evalConsole(evt);
}
var pgCommandManager = new PgCommandManager();

function PgFlowManager(pageVersionOid, zone)
{
	pgCommandManager.registerObject(this);
	this._objectType = "PgFlowManager";
	this._pageVersionOid = pageVersionOid;
	this._zone = zone;
	var id = pageVersionOid + "_" + zone
	this._id = id;
	this._htmlRoot = document.getElementById("flow" + id);
	this._webPartList = new Array();
	var children = this._htmlRoot.childNodes;
	for(var i=0;i<children.length;i++) {
		var rootDiv = children[i];
		if(rootDiv.tagName != "DIV" && rootDiv.tagName != "div") continue;
		var dropTarget = rootDiv.getAttribute("droptarget");
		if(dropTarget == null) continue;
		if(dropTarget.substr(0, 1) == "t") {
			this._lastSeparator = rootDiv;
			continue;
		}
		var div = rootDiv.firstChild;
		if(div == null) continue;
		var id = div.id;
		id = parseInt(id.replace(/^html/, ""), 10);
		var rank = this._webPartList.length;
		var webPart = this._webPartList[rank] = new PgWebPartManager(id, zone, rank, rootDiv, rootDiv.previousSibling);
		pgCommandManager.registerWebPart(webPart);
	}
	this._lastSeparator.className = "pgLastDropTarget";
	this._lastSeparator.innerHTML = "Utilisez le glisser-déplacer pour déplacer ici un contenu existant ou pour créer ici un contenu à partir de la palette.";
}

PgFlowManager.prototype.setEditMode = function(bEditMode, readOnly)
{
	if(bEditMode) {
		this._htmlRoot.className = "pgFlowContainerEdit";
		this._lastSeparator.style.display = readOnly ? "none": "block";
	} else {
		this._htmlRoot.className = "pgFlowContainer";
		this._lastSeparator.style.display = "none";
	}
	for(var i=0;i<this._webPartList.length;i++) {
		var webPart = this._webPartList[i]; 
		webPart.setEditMode(bEditMode, readOnly);
		webPart.updateLabel();
	}
}

PgFlowManager.prototype.createCreationSelect = function()
{
	var p = this._insertPara = document.createElement("p");
	p.className = "addElementPara";
	this._htmlRoot.appendChild(p);
	var html = "<select onchange='pgInsertElement(this, " + this._pageVersionOid + ", " + this._zone +")'>";
	html += "<option value=''>--- Insérer un élément --</option>";
	var elementModels = pgCommandManager._elementModels;
	for(var name in elementModels) {
		var elementModel = elementModels[name];
		html += "<option value=\"" + name + "\">" + elementModel.label + "</option>\n";
	}
	p.innerHTML = html;

}

PgFlowManager.prototype.removeWebPart = function(webPart)
{
	var list = new Array();
	for(var i=0;i<this._webPartList.length;i++) {
		var wp = this._webPartList[i];
		if(wp == webPart) continue;
		var nr = list.length;
		list[nr] = wp;
		if(i != nr) {
			wp.setIds(nr, this._zone);
		}
	}
	this._webPartList = list;
	webPart.disconnect();
}

PgFlowManager.prototype.insertWebPart = function(webPart, rank)
{
	var list = new Array();
	var wpPivot = this._webPartList[rank];
	var done = false;
	for(var i=0;i<this._webPartList.length;i++) {
		var wp = this._webPartList[i];
		var nr = list.length;
		if(nr == rank) {
			list[list.length] = webPart;
			webPart.setIds(nr, this._zone);
			done = true;
		}
		var nr = list.length;
		list[nr] = wp;
		if(i != nr) {
			wp.setIds(nr, this._zone);
		}
	}
	if(!done) {
		var nr = list.length;
		list[list.length] = webPart;
		webPart.setIds(nr, this._zone);
	}
	this._webPartList = list;
	var pivot;
	if(wpPivot != null) {
		pivot = wpPivot.getSeparator();
	} else {
		pivot = this._lastSeparator;
	}
	webPart.connect(pivot, rank, this._zone);
}

PgFlowManager.prototype.updateLastSeparator = function()
{
	var position = this._zone + ":" + this._webPartList.length;
	this._lastSeparator.setAttribute("droptarget", "t:" + position);
}

PgFlowManager.prototype.updateLabels = function()
{
	for(var i=0;i<this._webPartList.length;i++) {
		var wp = this._webPartList[i];
		wp.updateLabel();
	}
	this.updateLastSeparator();
}
PgFlowManager.prototype.moveWebPart = function(webPart, rank)
{
	var flow = pgCommandManager.getFlow(webPart._zone);
	if(flow != this) {
		flow.removeWebPart(webPart);
	} else {
		this.removeWebPart(webPart);
	}
	this.insertWebPart(webPart, rank);
	this.updateLabels();
	if(flow != this) {
		flow.updateLabels();
	} 
}

PgFlowManager.prototype.createWebPart = function(oid, htmlElement, rank)
{
	var rootDiv = document.createElement("div");
	rootDiv.id = "D" + oid;
	rootDiv.setAttribute("elementName", htmlElement.getAttribute("elementName"));
	rootDiv.setAttribute("elementLabel", htmlElement.getAttribute("elementLabel"));
	var div = document.createElement("div");
	rootDiv.appendChild(div);
	div.id = "html" + oid;
	div.className = "pgWebPartContent";
	div.innerHTML = htmlElement.innerHTML;
	var sepDiv = document.createElement("div");
	sepDiv.className ="pgDropTarget";
	var webPart = new PgWebPartManager(oid, this._zone, rank, rootDiv, sepDiv);
	pgCommandManager.registerWebPart(webPart);
	this.insertWebPart(webPart, rank);
	webPart.setEditMode(pgCommandManager._editMode, false);
	this.updateLabels();
}

function PgWebPartManager(oid, zone, rank, htmlRoot, htmlSeparator)
{
	pgCommandManager.registerObject(this);
	this._oid = oid;
	this._zone = zone;
	this._flowId = pgCommandManager.pageVersionOid + "_" + zone;
	this._rank = rank;
	this._htmlRoot = htmlRoot;
	this._contentDiv = htmlRoot.firstChild;
	this._elementName = htmlRoot.getAttribute("elementName");
	this._htmlSeparator = htmlSeparator;
	this._buttonBar = null;
	this._toolBarButtons = new Object();
	this._elementModel = pgCommandManager.getElementModel(this._elementName);
}
PgWebPartManager.prototype.updateHTML = function(htmlContent)
{
	var html = htmlContent.innerHTML;
	this._contentDiv.innerHTML = html;
}

PgWebPartManager.prototype.setIds = function(rank, zone)
{
	this._zone = zone;
	this._flowId = pgCommandManager.pageVersionOid + "_" + zone;
	this._rank = rank;
	var position = this._zone + ":" + this._rank;
	this._htmlRoot.setAttribute("droptarget", "o:"+position);
	this._htmlSeparator.setAttribute("droptarget", "t:"+position);
	this.updateLabel();
}

PgWebPartManager.prototype.setEditMode = function(bEditMode, readOnly)
{
	if(bEditMode) {
		this._htmlRoot.className = "pgWebPartContainerEdit";
		this.showButtonBar(readOnly);
	} else {
		this._htmlRoot.className = "pgWebPartContainer";
		this.hideButtonBar();
	}
}
PgWebPartManager.prototype.getSeparator = function()
{
	return this._htmlSeparator;
}

PgWebPartManager.prototype.disconnect = function()
{
	var parentNode = this._htmlRoot.parentElement;
	if(parentNode == null) return;
	this._flowId = null;
	parentNode.removeChild(this._htmlRoot);
	parentNode.removeChild(this._htmlSeparator);
}

PgWebPartManager.prototype.connect = function(pivot, rank,  zone)
{
	this.disconnect();
	var parentNode = pivot.parentElement;
	parentNode.insertBefore(this._htmlSeparator, pivot);
	parentNode.insertBefore(this._htmlRoot, pivot);
	this.setIds(rank,  zone);
}

PgWebPartManager.prototype.showButtonBar = function(readOnly)
{
	if(this._buttonBar != null) {
		this._buttonBar.style.display = "block";
	} else {
		this.createButtonBar();
	}
	this.setReadOnly(readOnly);
}

PgWebPartManager.prototype.setReadOnly =  function(readOnly)
{
	if(this._readOnly == readOnly) return;
	this._readOnly = readOnly;
	this._toolBarButtons.edit.setVisible(!readOnly);
	this._toolBarButtons.remove.setVisible(!readOnly);
	this._toolBarButtons.moveUp.setVisible(!readOnly);
	this._toolBarButtons.moveDown.setVisible(!readOnly);
	this._buttonBar.className = readOnly ? "pgWebPartButtonBarInactive" : "pgWebPartButtonBar pgProtect";
}

PgWebPartManager.prototype.hideButtonBar = function()
{
	if(this._buttonBar != null) {
		this._buttonBar.style.display = "none";
	}
}

PgWebPartManager.prototype.getLabel = function()
{
	var label = "";
	if(false && pgCommandManager._debug) {
		label += this._oid + " ";
		var flow = pgCommandManager.getFlow(this._zone);
		var lastRank = flow._webPartList.length;
		label += this._zone + " - " + this._rank + "/" + lastRank + " ";
	}
	if(this._elementModel) label += this._elementModel.label;
	return label;
}

PgWebPartManager.prototype.updateLabel = function()
{
	if(this._titleDiv == null) return;
	var flow = pgCommandManager.getFlow(this._zone);
	var lastRank = flow._webPartList.length -1;
	this._titleDiv.innerHTML = this.getLabel();
	var moveUpButton = this.getToolBarButton("moveUp");
	var moveDownButton = this.getToolBarButton("moveDown");
	moveUpButton.setVisible(this._rank != 0); 
	moveDownButton.setVisible(this._rank != lastRank); 
}

PgWebPartManager.prototype.createButtonBar = function()
{
	var div = this._buttonBar = document.createElement("div");
	div.className = "pgWebPartButtonBar pgProtect";
	div.setAttribute("dragType", "webPart");
	this._htmlRoot.insertBefore(div, this._htmlRoot.firstChild);
	if(this._elementModel != null && this._elementModel.icon != null) {
		var img = document.createElement("img");
		img.src = "./imageProvider.asp?private_image=" + this._elementModel.icon;
		img.className = "format_png";
		img.className = "pgSymbolIcon";
		div.appendChild(img);
	}
	var titleDiv = this._titleDiv = document.createElement("div");
	div.appendChild(titleDiv);
	titleDiv.innerHTML = this.getLabel();
	titleDiv.className = "pgWebPartTitle";
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgRemove.gif", "Supprimer l'élément", false, "remove", this._objectId, true, false));
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgEdit.gif", "Éditer l'élément", false, "edit", this._objectId, true, false));
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgUp.gif", "Monter l'élément", false, "moveUp", this._objectId, true, false));
	this.addToolBarButton(new PgToolBarButton("iso_icons/pgDown.gif", "Descendre l'élément", false, "moveDown", this._objectId, true, false));
}

PgWebPartManager.prototype.edit = function()
{
	var url = pgCommandManager.elementEditorUrl + "?contentElement=" + this._oid;
	url += "&pageVersion=" + pgCommandManager.pageVersionOid;
	url += "&className=" + this._elementName;
	var width = "800";
	var height = "600";
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	var options = 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	//showModalWindow(url, "elementEditor", width, height);
	var win = window.open(url, "elementEditor", options);
	if (win==null)
		alert(objThesaurus.translate("odfCheckPopupBlocker"));
	else
		win.focus();
}

PgWebPartManager.prototype.remove = function()
{
	if(!confirm("Êtes-vous sûr de détruire cet élément?")) return;
	var command = "re " + this._oid + " " + this._elementName;
	var flow = pgCommandManager.getFlow(this._zone);
	if(flow == null) return alert("no flow " + this._flowId);
	pgCommandManager.postCommand(command);
	flow.removeWebPart(this);
	flow.updateLabels();
	pgCommandManager.unregisterObject(this);
	pgCommandManager.unregisterWebPart(this);	
}

PgWebPartManager.prototype.moveUp = function()
{
	var rank = this._rank-1;
	pgCommandManager.moveElement(this._oid, this._zone, rank);
}

PgWebPartManager.prototype.moveDown = function()
{
	var rank = this._rank+1;
	pgCommandManager.moveElement(this._oid, this._zone, rank)
}

PgWebPartManager.prototype.getToolBarDiv = function()
{
	return this._buttonBar;
}
PgWebPartManager.prototype.addToolBarButton = function(button)
{
	this._toolBarButtons[button._eventFuncName] = button;
}
PgWebPartManager.prototype.getToolBarButton = function(name)
{
	return this._toolBarButtons[name];
}

function PgVersionSelect(versionMap, versionOid, status, editionVersionOid, reducedSize)
{
	pgCommandManager.registerObject(this);
	this._objectType = "PgVersionSelect";
	var span = this._select = document.createElement("span");
	span.className = "pgToolbarSelectSpan";
	pgCommandManager.getToolBarDiv().appendChild(span);

	var select = this._select = document.createElement("select");
	span.appendChild(select);
	select.className = "pgToolbarSelect";
	var editionOption = null;
	if(editionVersionOid != null) {
		var option = document.createElement("option");
		option.text = "Version en édition";
		option.value = editionVersionOid;
		select.add(option);
		this.editionOption = editionOption = option;
	}
	var hasSelection = false;
	for(var oid in versionMap) {
		var versions = versionMap[oid].split("|");
		var version = versions[0];
		var date = new Date(parseFloat(versions[1]));
		var option = document.createElement("option");
		if(version == "0.000") {
			option.text = "version du studio";
		} else {
			if(reducedSize) {
				option.text = version;
			} else {
				option.text = version + " (" + pgCommandManager.formatDate(date, true) + " )";
			}
		}
		option.value = "" + oid;
		select.add(option);
		if(versionOid == oid) {
			option.selected = true;
			hasSelection = true;
		}
	}
	if(editionOption != null && editionVersionOid == versionOid) {
		editionOption.selected = true;
	}
	select.setAttribute("objectId", this._objectId);
	select.onchange = pgVersionSelectChange;
	select.style.fontSize = "9px";
}

PgVersionSelect.prototype.change = function()
{
	var versionOid = this._select.value;
	pgCommandManager.showVersion(parseInt(versionOid, 10));
}

PgVersionSelect.prototype.removeEditionOption = function()
{
	if(this.editionOption == null) return;
	this._select.removeChild(this.editionOption);
	this.editionOption = null;
}

PgVersionSelect.prototype.addOption = function(oid, version)
{
	var option = document.createElement("option");
	option.text = version + " (publiée)";
	option.value = "" + oid;
	this._select.add(option, this.editionOption ? 1:0);
	option.selected = true;

}

function PgToolBarButton(imgUrl, toolTip, isToggle, eventFuncName, ownerId, isRight, showLabel)
{
	pgCommandManager.registerObject(this);
	this._objectType = "PgToolBarButton";
	this._imgUrl = imgUrl;
	this._toolTip = toolTip;
	this._isToggle = isToggle;
	this._eventFuncName = eventFuncName;
	this._ownerId = ownerId;
	this._state = false;
	this._isRight = isRight;
	this._isVisible = true;
	this._showLabel = showLabel == true;
	this._active = true;
	var owner = pgCommandManager.getObject(ownerId);

	var buttonLink = this._rootHTML = document.createElement("a");
	buttonLink.title = toolTip;
	buttonLink.href = "#";
	buttonLink.onclick = pgButtonClick;
	buttonLink.mousedown = pgNop;
	buttonLink.setAttribute("objectId", this._objectId);

	var img = document.createElement("img");
	img.src = imgUrl;
	img.className = "format_png";
	img.width = 16;
	img.height = 16;
	img.border = 0;
	buttonLink.appendChild(img);
	
	if(this._showLabel) {
		var span = this._spanLabel = document.createElement("span");
		toolTip = toolTip.replace(/ /g, "&nbsp;");
		span.innerHTML = "&nbsp;" + toolTip;
		buttonLink.appendChild(span);
	}
	
	owner.getToolBarDiv().appendChild(buttonLink);
	this.refresh();
}

PgToolBarButton.prototype.click = function(evt)
{
	if(!this._active) return;
	if(this._isToggle) this._state = !this._state;
	var owner = pgCommandManager.getObject(this._ownerId);
	this.refresh();
	this.stopEvent(evt);
	if(typeof(owner[this._eventFuncName]) == "function") {
		owner[this._eventFuncName](evt, this._state);
	} else {
		alert("methode inconnue: " + this._eventFuncName);
	}
}

PgToolBarButton.prototype.setVisible = function(visible)
{
	this._isVisible = visible;
	if(visible) {
		//this._rootHTML.style.visibility = "visible";
		this._rootHTML.style.display = "block";
	} else {
		//this._rootHTML.style.visibility = "hidden";
		this._rootHTML.style.display = "none";
	}
}

PgToolBarButton.prototype.setState = function(state)
{
	this._state = state;
	this.refresh();
}

PgToolBarButton.prototype.setLabel = function(label)
{
	if(this._spanLabel != null) this._spanLabel.innerHTML = label;
}

PgToolBarButton.prototype.refresh = function()
{
	var className = "pgToolbarButton";
	if(this._state) className += "Down";
	if(!this._active) className += "Inactive";
	className += this._isRight ? " pgRight" : " pgLeft";
	this._rootHTML.className = className;
}

PgToolBarButton.prototype.setActive = function(active)
{
	this._active = active;
	if(this._active) {
		this._rootHTML.href = "#";
	} else {
		this._rootHTML.href = null;
	}
	this.refresh();
}

PgToolBarButton.prototype.stopEvent = function(ev) 
{
	ev || (ev = window.event);
	if (document.all != null) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
}

function pgButtonClick(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	while(element && element.nodeType == 1) {
		var objectId = element.getAttribute("objectId");
		if(objectId != null) break;
		element = element.parentNode;
	}
	var button = pgCommandManager.getObject(objectId);
	if(button == null) return;
	button.click(evt);
	return false;
}

function pgVersionSelectChange(evt)
{
	evt || (evt = window.event);
	var element = pgGetElement(evt);
	while(element && element.nodeType == 1) {
		var objectId = element.getAttribute("objectId");
		if(objectId != null) break;
		element = element.parentNode;
	}
	var select = pgCommandManager.getObject(objectId);
	if(select == null) return;
	select.change(evt);
	return false;
}

function pgCloseButtonClick(evt)
{
	pgCommandManager.hideElementToolBar();
}
function pgNop(evt)
{
	evt || (evt = window.event);
	if (document.all != null) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
}
function consoleDump(obj, title)
{
	if(pgCommandManager == null) return;
	pgCommandManager.consoleDump(obj, title);
}

function NopPostProcessor()
{
}

NopPostProcessor.prototype.commit = function()
{
}

NopPostProcessor.prototype.cancel = function()
{
}

function ToEditPostProcessor()
{
}

ToEditPostProcessor.prototype.commit = function()
{
	pgCommandManager.toEditWork();
}

ToEditPostProcessor.prototype.cancel = function()
{
}

function ToValidatePostProcessor()
{
}

ToValidatePostProcessor.prototype.commit = function()
{
	pgCommandManager.toValidateWork();
}

ToValidatePostProcessor.prototype.cancel = function()
{
}


function ToEditPagePostProcessor()
{
}

ToEditPagePostProcessor.prototype.commit = function()
{
	pgCommandManager.editPageWork();
}

ToEditPagePostProcessor.prototype.cancel = function()
{
}

function ReloadPostProcessor()
{
}

ReloadPostProcessor.prototype.commit = function()
{
	pgCommandManager.reload();
}

ReloadPostProcessor.prototype.cancel = function()
{
}
var cm = pgCommandManager;
pgCommandManager.setDebug(true);

