<script type="text/javascript">
	gekkoLinkPlacer = new Object();
	gekkoLinkPlacer.positions = new Array();
	gekkoLinkPlacer.compile = function(form) {
		var buff = new Array();
		for (var i = 0; i < gekkoLinkPlacer.positions.length; i++) {
			var container = gekkoLinkPlacer.positions[i];
			container.items = new Array();
			for (var j = 0; j < container.childNodes.length; j++) {
				var block = container.childNodes[j];
				if (block.className == 'gekkoBlockPlacer')
					container.items.push(block.menuId+'-'+gekkoLinkPlacer.linkGetLevel(block.firstChild));
			}
			buff.push(container.id.substr(11)+':'+container.items.join(','));
		}
		form.positions.value = buff.join('|');
		return true;
	}
	gekkoLinkPlacer.getParent = function(button) {
		if (!button.parent) {
			var parent = button;
			while (!parent.id || parent.id.substr(0, 8) != 'menuLink')
				parent = parent.parentNode;
			parent.level = (parseInt(parent.style.paddingLeft)-10)/20;
			button.parent = parent;
		}
		return button.parent;
	}
	gekkoLinkPlacer.linkSetLevel = function(link, level) {
		link.style.paddingLeft = level*20+10+'px';
		link.level = level;
	}
	gekkoLinkPlacer.linkGetLevel = function (link) {
		return (parseInt(link.style.paddingLeft)-10)/20;
	}
	gekkoLinkPlacer.getPrevBrother = function (object) {
		var parent = object.parentNode;
		var brother = parent.previousSibling;
		while (brother && brother.nodeName.toLowerCase() != 'div')
			brother = brother.previousSibling;
		if (brother && brother.className == 'gekkoBlockPlacer') {
			brother = brother.firstChild;
			brother.level = gekkoLinkPlacer.linkGetLevel(brother);
			return brother;
		}
		return null;
	}
	gekkoLinkPlacer.getNextBrother = function (object) {
		var parent = object.parentNode;
		var brother = parent.nextSibling;
		while (brother && brother.nodeName.toLowerCase() != 'div')
			brother = brother.nextSibling;
		if (brother && brother.className == 'gekkoBlockPlacer') {
			brother = brother.firstChild;
			brother.level = gekkoLinkPlacer.linkGetLevel(brother);
			return brother;
		}
		return null;
	}
	gekkoLinkPlacer.moveLeft = function(button, parent) {
		if (!parent)
			var parent = gekkoLinkPlacer.getParent(button);

		var brother = gekkoLinkPlacer.getNextBrother(parent);

		if (parent.level > 0)
			gekkoLinkPlacer.linkSetLevel(parent, parent.level-1);

		while (brother && (brother.level - parent.level) > 1)
			gekkoLinkPlacer.moveLeft(null, brother);

	}
	gekkoLinkPlacer.moveRight = function(button, parent) {
		if (!parent)
			var parent = gekkoLinkPlacer.getParent(button);

		var brother = gekkoLinkPlacer.getPrevBrother(parent);

		if (((parent.level+1) - brother.level) > 1)
			return false;

		gekkoLinkPlacer.linkSetLevel(parent, parent.level+1);

		var brother = gekkoLinkPlacer.getNextBrother(parent);

		while (brother && (parent.level == brother.level))
			gekkoLinkPlacer.moveRight(null, brother);

	}
	gekkoLinkPlacer.init = function() {

		var divs = document.getElementsByTagName('DIV');

		var control = gekkoDragAndDrop.newControl();

		gekkoDragAndDrop.setOption(control, 'showContainerId', true);
		gekkoDragAndDrop.setOption(control, 'dragOnly', false);

		for (var i = 0; i < divs.length; i++) {
			if (divs[i].id.substr(0, 10) == 'menuPlacer') {

				for (j = 0; j < divs[i].childNodes.length; j++) {

					var child = divs[i].childNodes[j];

					if (child.nodeName.toLowerCase() == 'div') {

						var container = gekkoDragAndDrop.createContainer();

						container.menuId = child.id.replace(/^[^_]*_/, '');

						if (container.menuId) {
							divs[i].insertBefore(container, child);
							container.appendChild(child);
							gekkoDragAndDrop.makeDraggable(control, container);
						}
					}
				}
				gekkoLinkPlacer.positions.push(divs[i]);
				gekkoDragAndDrop.addContainer(control, divs[i]);
			}
		}
	};

	document.setEventListener(window, "load", function() { gekkoLinkPlacer.init(); });
</script>
{V_ACTIONS}
<!--if($BUFFER.MENU)-->
<h2>{L_MANAGE}</h2>

<form action="{C_SITE.URL}modules/menu-editor/main.act.php" method="post" onsubmit="return gekkoLinkPlacer.compile(this);">
	<input type="hidden" name="positions" value="" />
	<input type="hidden" name="action" value="update_position" />
	<input type="hidden" name="return" value="" />
	{=mkmessagebox("info", "{L_MENU_DRAG_AND_DROP_MODE}")=}
	<div class="buttons">
	<button type="submit">{=mkicon("backup.png", 16)=} {L_SAVE_CHANGES}</button>
	</div>
</form>

<!--{bgn: MENU}-->
<div class="{V_SW_CLASS}">
	<div class="item">
		{V_ACTIONS}
		<h3>{V_TITLE}</h3>
		<div id="menuPlacer_{V_ID}" class="{V_SW_CLASS}">
		<!--if($BUFFER.MENU.LINK)-->
			<!--{bgn: LINK}-->
				<div id="menuLink_{V_ID}" style="padding-left: {=intval({V_LEVEL}*20+10)=}px;">
					<b>{=mkicon("{V_ICON}", 16)=} {V_TITLE}</b><br />
					<div><small>{V_LINK}</small></div>
					<div class="dragIgnore" style="cursor: default">{V_ACTIONS}</div>
				</div>
			<!--{end: LINK}-->
		<!--endif-->
		</div>
		{V_ACTIONS}
	</div>
</div>
<!--{end: MENU}-->
<!--endif-->
