<span tal:replace="structure here/manage_page_header" />
<span tal:replace="structure here/manage_tabs" />

<p class="form-help">
	Use this page to customize the right side portlets for the front page and Naaya folders.
</p>

<script type="text/javascript">
<!--

function add()
{
	var selectleft = document.frmportlets.available;
	var selectright = document.frmportlets.portlets;
	var i;
	for(i = 0; i < selectleft.options.length; i++)
	{
		if(selectleft.options[i].selected)
		{
			var o = document.createElement( "option");
			o.text = selectleft.options[i].text;
			o.value = selectleft.options[i].value;
			selectright.options[selectright.options.length] = o;
			selectleft.remove(i);
			i--;
		}
	}
}
function remove()
{
	var selectleft = document.frmportlets.available;
	var selectright = document.frmportlets.portlets;
	var i;
	for( i = 0; i < selectright.options.length; i++)
	{
		if(selectright.options[i].selected)
		{
			var o = document.createElement( "option");
			o.text = selectright.options[i].text;
			o.value = selectright.options[i].value;
			selectleft.options[selectleft.options.length] = o;
			selectright.remove(i);
			i--;
		}
	}
}

function moveup()
{
	var selectright = document.frmportlets.portlets;
	var i;
	for( i = 0; i < selectright.options.length; i++)
	{
		if(selectright.options[i].selected && (i > 0))
		{
			var o1 = document.createElement( "option");
			o1.text = selectright.options[i-1].text;
			o1.value = selectright.options[i-1].value;
			var o2 = document.createElement( "option");
			o2.text = selectright.options[i].text;
			o2.value = selectright.options[i].value;
			selectright.options[i-1] = o2;
			selectright.options[i] = o1;
			selectright.options[i-1].selected = true;
		}
	}
}

function movedown()
{
	var selectright = document.frmportlets.portlets;
	var i;
	for( i = selectright.options.length-1; i >= 0; i--)
	{
		if(selectright.options[i].selected && (i < selectright.options.length-1))
		{
			var o1 = document.createElement( "option");
			o1.text = selectright.options[i].text;
			o1.value = selectright.options[i].value;
			var o2 = document.createElement( "option");
			o2.text = selectright.options[i+1].text;
			o2.value = selectright.options[i+1].value;
			selectright.options[i] = o2;
			selectright.options[i+1] = o1;
			selectright.options[i+1].selected = true;
		}
	}
}

function dosubmit()
{
	var selectright = document.frmportlets.portlets;
	var i;
	for( i = 0; i < selectright.options.length; i++)
	{
		selectright.options[i].selected = true;
	}
	document.frmportlets.submit();
}

// -->
</script>

<tal:block
tal:define="mode python:request.get('mode', '');
			location python:request.get('location', '');
			location_obj python:test(location=='', None, here.getFolderByPath(location));
			records here/get_right_portlets_locations">

<form name="frmportlets" method="post" action="manage_set_right_portlets">
<input type="hidden" name="mode" tal:attributes="value mode" />
<input type="hidden" name="location" tal:attributes="value location" />
<table border="0" cellspacing="4" cellpadding="4">
	<tr>
		<td class="form-label" width="20%" style="vertical-align: top">Location</td>
		<td colspan="4">
			<tal:block tal:condition="python:mode=='edit'">
				<strong tal:condition="python:location_obj is None">Portal main page</strong>
				<strong tal:condition="python:location_obj is not None" tal:content="location_obj/title_or_id">location</strong>
				<input type="hidden" name="folder" tal:attributes="value location" />
			</tal:block>
			<tal:block tal:condition="python:mode!='edit'">
			<script type="text/javascript">
			<!--
			function fTreeClick(expand)
			{
				var selectleft = document.frmportlets.available;
				var selectright = document.frmportlets.portlets;
				var i;
				for( i = 0; i < selectleft.options.length; i++)
					selectleft.options[i].selected = true;
				for( i = 0; i < selectright.options.length; i++)
					selectright.options[i].selected = true;
				document.frmportlets.action = '';
				document.frmportlets.expand.value = expand;
				document.frmportlets.submit();
			}
			function fTreeNodeClick(node_path)
			{
				document.frmportlets.folder.value = node_path;
			}
			//-->
			</script>
			<tal:block tal:define="expand python:request.get('expand', '');
									expand_list python:here.splitToList(expand);
									tree python:here.getSiteMap(expand_list, None, 0);
									folder python:request.get('folder', '')">
			<table border="0" cellspacing="1" cellpadding="1" width="100%">
				<tr tal:repeat="tree_node tree">
					<td>
						<table border="0" cellspacing="1" cellpadding="1">
							<tr>
								<td>
									<img tal:repeat="i python:range(0, tree_node[2])" src="misc_/Naaya/square.gif" alt="" />
									<tal:block tal:condition="python:tree_node[1]==-1">
										<img src="misc_/Naaya/square.gif" alt="" />
									</tal:block>
									<tal:block
										tal:define="node_expand python:here.processCollapse(expand_list, tree_node[0].absolute_url(1))"
										tal:condition="python:tree_node[1]==0">
										<a tal:attributes="onclick string:javascript:fTreeClick('${node_expand}')"><img src="misc_/Naaya/minus.gif" alt="Collapse" /></a>
									</tal:block>
									<tal:block
										tal:define="node_expand python:here.processExpand(expand_list, tree_node[0].absolute_url(1))"
										tal:condition="python:tree_node[1]==1">
										<a tal:attributes="onclick string:javascript:fTreeClick('${node_expand}')"><img src="misc_/Naaya/plus.gif" alt="Expand" /></a>
									</tal:block>
								</td>
								<td><img tal:attributes="src python:tree_node[0].icon" alt="" /></td>
								<td><a tal:define="node_path python:tree_node[0].absolute_url(1)" tal:attributes="href string:javascript:fTreeNodeClick('${node_path}')" tal:content="python:tree_node[0].title_or_id()">folder</a></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						<input type="text" name="folder" tal:attributes="value folder" size="35" readonly="readonly" />
					</td>
				</tr>
			</table>
			<input type="hidden" name="expand" tal:attributes="value expand" />
			</tal:block>
			</tal:block>
		</td>
	</tr>
	<tr>
		<td class="form-label" style="vertical-align: top">Available portlets</td>
		<td>
			<select name="available" size="10" multiple="multiple"
				tal:define="available_portlets python:test(mode=='edit', here.get_available_right_locations_objects(location), here.getPortlets())">
				<option tal:repeat="item available_portlets"
						tal:attributes="value item/id">
					<span tal:replace="item/title_or_id" /> (<span tal:replace="item/get_type_label" />)
				</option>
			</select>
		</td>
		<td style="vertical-align: middle">
			<input type="button" value="Add ->" onclick="javascript:add();"/>
			<br /><br />
			<input type="button" value="<- Remove" onclick="javascript:remove();" />
		</td>
		<td>
			<select name="portlets" size="10" multiple="multiple"
				tal:define="current_portlets python:test(mode=='edit', here.get_right_portlets_locations_objects(location_obj), [])">
				<option tal:repeat="item current_portlets"
						tal:attributes="value item/id">
					<span tal:replace="item/title_or_id" /> (<span tal:replace="item/get_type_label" />)
				</option>
			</select>
		</td>
		<td style="vertical-align: middle">
			<input type="button" value="Move Up" onclick="javascript:moveup();" />
			<br /><br />
			<input type="button" value="Move Down" onclick="javascript:movedown();" />
		</td>
	</tr>
	<tr>
		<td class="form-label"></td>
		<td colspan="4"><input type="button" value="Save changes" onclick="javascript:dosubmit();" /></td>
	</tr>
</table>
</form>

<form method="post" action="manage_delete_right_portlets">
<table border="0" cellspacing="0" cellpadding="4" width="100%">
	<tr class="row-header">
		<td width="1%" class="cell-header"></td>
		<td width="60%" class="cell-header">Location</td>
		<td width="40%" class="cell-header">Portlets</td>
	</tr>
	<tr tal:repeat="record python:records.keys()"
		tal:attributes="class python:test(path('repeat/record/odd'), 'row-odd', 'row-even')">
		<td></td>
		<td style="vertical-align: top">
			<a tal:attributes="href string:?mode=edit&location=${record}" tal:content="python:here.getFolderByPath(record).title_or_id()">location</a>
		</td>
		<td>
			<tal:block tal:repeat="item python:map(here.getPortletById, records[record])">
				<tal:block tal:condition="python:item is not None">
					<input type="checkbox" name="ids" tal:attributes="value python:'%s||%s' % (record, item.id)" />
					<a tal:attributes="href string:${item/absolute_url}/manage_workspace" title="Edit portlet">
						<span tal:replace="item/title_or_id" /> (<span tal:replace="item/get_type_label" />)
					</a>
					<br />
				</tal:block>
			</tal:block>
		</td>
	</tr>
	<tr>
		<td></td>
		<td colspan="2"><input type="submit" name="delete" value="Delete" /></td>
	</tr>
</table>
</form>

</tal:block>

<span tal:replace="structure here/manage_page_footer" />
