## version: $Id:  $
<!-- chef_resources_reorder.vm, use with org.sakaiproject.tool.content.ResourcesAction.java -->
<!-- this is the reorder template -->

<div class="portletBody">
	<form name="reorderForm" id="reorderForm" action="#toolForm("ResourcesAction")" method="post">
		<input name="folderId" id="folderId" value="$folderId" type="hidden" />
		<input name="sakai_action" id="sakai_action" value="doSaveOrder" type="hidden" />
		<div class ="breadCrumb specialLink">
			<h3>
				$tlang.getString("sort.title")
				<img src = "#imageLink($contentTypeImageService.getContentTypeImage("folder"))" border="0"  alt="folder" />
				#foreach ($item in $collectionPath)
					$validator.escapeHtml("$item.name")
					#if(! $item.isLast()) / #end
				#end
			</h3>
		</div>
		<div class="instruction">
			$tlang.getString("sort.instruct")
		</div>
		<table class="listHier lines nolines" summary="$tlang.getString("sort.summary")" border="0" cellspacing="0" cellpadding="0">
			<caption class="skip" style="display: none;">$tlang.getString("sort.caption")</caption>
			<tbody>
			<tr>
				<th id="expansion">
				</th>
				<th id="moveUp">
				</th>
				<th id="moveDown">
				</th>
				<th id="title">
					$tlang.getString("gen.title1")
				</a>
				</th>
## gsilver - take out access column				
##				<th id="access">
##					$tlang.getString("gen.access")
##				</th>

				<th id="creator">
					$tlang.getString("gen.created")
				</th>
				<th id="modified">
					$tlang.getString("gen.last1")
				</th>
## gsilver - take out size column
##				<th id="size">
##					$tlang.getString("gen.size")
##			</th>
				<th id="postion">
					$tlang.getString("gen.position")
				</th>
			</tr>
			#foreach($root in $this_site)
				<tr>
					<td headers="expansion" class="arrowHere">
					</td>
					<td headers="moveUp" class="moveUp">
					</td>
					<td headers="moveDown" class="moveDown">
					</td>
					<td headers="title" class="specialLink">
						<h3> 
							<img src="#imageLink("sakai/dir_open.gif")" border="0">
							$root.name
						</h3>	
					</td>
## gsilver - take out action column					
##					<td headers="actions">
##					</td>
## gsilver - adjust colspan to account for new col number
					<td colspan="5">
					</td>
				</tr>	
				#set($collectionMembers = $root.members)
				#set($count = 1)
				#foreach($member in $collectionMembers)
					<tr class="sortable">
						<td headers="expansion">
						</td>
						<td headers="moveUp" class="moveUp">
							<a#if($count <= 1) style="display:none;"#end href="#" onclick="moveRow(this,'up')" title="$tlang.getString("sort.moveup")">
								<img src="#imageLink("sakai/arrowUp.gif")" alt="move item up" border="0">
							</a>
						</td>
						<td headers="moveDown" class="moveDown">
							<a href="#"#if($count >= $collectionMembers.size())  style="display:none;"#end onclick="moveRow(this,'down')" title="$tlang.getString("sort.movedn")">
								<img src="#imageLink("sakai/arrowDown.gif")" alt="move item down" border="0">
							</a>
						</td>
						<td headers="title" style="text-indent: 1em;" class="specialLink">
							<h4>
								#if($member.isFolder())
									<img src="#imageLink($contentTypeImageService.getContentTypeImage("folder"))" border="0">
								#else
									<img src="#imageLink($contentTypeImageService.getContentTypeImage("$member.type"))" border="0">
								#end
								$member.name
							</h4>	
						</td>
#* gsilver - take out access column
						#set($effectiveAccess = $member.effectiveAccess)
						<td headers="access" title="$member.effectiveGroups">
							#if($member.isPubviewInherited() || $member.isPubview())
								$tlang.getString("access.public")
							#elseif($member.isInDropbox())
								$tlang.getString("access.dropbox")
							#elseif("$effectiveAccess" == "$INHERITED_ACCESS" || "$effectiveAccess" == "$SITE_ACCESS")
								$tlang.getString("access.site")
							#elseif("$effectiveAccess" == "$GROUP_ACCESS")
								$tlang.getString("access.group")
							#end
						</td>
*#
						<td headers="creator">
							$member.createdBy
						</td>
						<td headers="modified">
							$member.ModifiedTime
						</td>
## gsilver - take out  size  column						
##						<td headers="size">
##							$member.size
##						</td>
						
						<td headers="position">
							<select name="position_$validator.escapeUrl("$member.id")" onChange="moveRow(this,this.value);">
								#foreach($i in [1..$collectionMembers.size()])
									<option value="$i"#if("$i" == "$count") selected="selected"#end>$i</option>
								#end
							</select>
						</td>
					</tr>
					#set($count = $count + 1)
				#end
			#end
			</tbody>
		</table>
		<p class="act" style="margin:0;padding:.3em 0;">
			<input name="flow" id="save" value="$tlang.getString("sort.save")" type="submit" class="active"/> 
			<input name="flow" id="cancel" value="$tlang.getString("sort.cancel")" type="submit" />																			
		</p>
	</form>						

</div>
<script language="JavaScript" type="text/javascript">
	<!--
		function submitform(id)
		{
			var theForm = document.getElementById(id);
			if(theForm && theForm.onsubmit)
			{
				theForm.onsubmit();
			}
			if(theForm && theForm.submit)
			{
				theForm.submit();
			}
		}
		function moveRow (clickedObj, destination) {
			
			var row = clickedObj.parentNode;
			// Finding the nearest containing TR element
			while (row.parentNode && row.tagName != "TR") {
				row = row.parentNode;
			}

			var rowParent = row.parentNode;
			var childNodes = rowParent.childNodes;
			
			var childTrs = new Array();
			for (var i=0; i < childNodes.length; i++){
				if (childNodes[i].tagName == "TR" && childNodes[i].className == "sortable"){
					childTrs[childTrs.length] = childNodes[i];
				}
			}
			var curRowPos = getRowPosition(row,childTrs);
			/*
				NOTE: first condition of if blocks does boundary checking to make sure we don't 
				move out of the bounds
			*/
			var destIndex = null;
			if ((curRowPos < childTrs.length-1) && destination && destination.toLowerCase() == "down") {
				destIndex = curRowPos + 1;
				// Set the destination integer for consumption below
				destination = destIndex+1;
				} else 
			if ((curRowPos > 0) && destination && destination.toLowerCase() == "up") {
				destIndex = curRowPos - 1;
				// Set the destination integer for consumption below
				destination = destIndex+1;
			}
			if (destination && ("" + destination).match(/^[0-9]+$/) && destination > 0 && destination <= childTrs.length) {

				destIndex = destination-1;
				

				if (curRowPos < destIndex) {
					/*
						"insertAfter" functionality as per:
						http://developer.mozilla.org/en/docs/DOM:element.insertBefore
					*/
					rowParent.insertBefore(row,childTrs[destIndex].nextSibling);
				} else {
					rowParent.insertBefore(row,childTrs[destIndex]);
				}
				
			}
			
			var newChildTrs = new Array();
			for (var i=0; i < childNodes.length; i++){
				if (childNodes[i].tagName == "TR" && childNodes[i].className == "sortable"){
					newChildTrs[newChildTrs.length] = childNodes[i];
				}
			}
			updateUserInterface(newChildTrs,destIndex);
		}
		
		function getRowPosition(currentRow,rowsArray) {
			for (var i=0; i < rowsArray.length; i++){
				if (rowsArray[i] == currentRow){
					return i;
				}
			}
			return "cat";
		}

		function updateUserInterface(rowsArray,targetRow) {
			for (var curRow=0; curRow < rowsArray.length; curRow++){
				var rowChildNodes = rowsArray[curRow].childNodes;
				
				if (rowsArray[curRow].tagName == "TR") {
					if (curRow == targetRow) {
## gsilver - target a class/value instead					
						rowsArray[curRow].id = "selectedReorder";
## gsilver - give the first cell in the row a special class						
						rowsArray[curRow].cells[0].className = "arrowHere";
						
##						rowsArray[curRow].style.background = "red";
##						rowsArray[curRow].style.color = "white";
						// We updated the H4s below in the innermost loop
					} else {
## gsilver - let standard redenring take place via the old class					
							rowsArray[curRow].id = "sortable";		

##						rowsArray[curRow].style.background = "white";
##						rowsArray[curRow].style.color = "black";
					}
				}
				
				for (var curRowChild=0; curRowChild < rowChildNodes.length; curRowChild++){
					
					var hasHeaders = false;
					if (rowChildNodes[curRowChild].hasAttribute) {
						hasHeaders = rowChildNodes[curRowChild].hasAttribute("headers");
					} else {
						/* 
						Coping with lack of hasAttribute() method in IE	
						*/
						if(rowChildNodes[curRowChild].headers != "") {
							hasHeaders = true;
						}
					}

					if (rowChildNodes[curRowChild].tagName == "TD" && hasHeaders) {
						
					var cellChildNodes = rowChildNodes[curRowChild].childNodes;
						for (var curCellChild=0; curCellChild < cellChildNodes.length; curCellChild++) {
							if (cellChildNodes[curCellChild].tagName == "A") {
								if ((curRow == 0 && rowChildNodes[curRowChild].getAttribute("headers") == "moveUp") || (curRow == rowsArray.length-1 && rowChildNodes[curRowChild].getAttribute("headers") == "moveDown")) {
									cellChildNodes[curCellChild].style.display = "none";
								} else {
									cellChildNodes[curCellChild].style.display = "inline";
								}
							}
							if (cellChildNodes[curCellChild].tagName == "SELECT") {
								cellChildNodes[curCellChild].selectedIndex = curRow;
							}
							
							if (cellChildNodes[curCellChild].tagName == "H4") {
								if (curRow == targetRow) {
##									cellChildNodes[curCellChild].style.color = "white";
								} else {
##									cellChildNodes[curCellChild].style.color = "black";
								}
							}

						}
					}
					
				}
			}
		}
#*	gsilver - comment this out - leave here for later (makes reference to elements that do not exist in this context)
		function setVisibility(thisInt) {
			var radioElements = document.getElementsByName("selectedMembers");
			
			for(var i = 0; i < radioElements.length; i++) {
				if (i != thisInt) {
					document.getElementById("buttons"+i).style.display="none";
					document.getElementById("radio"+i).style.display="block";
				} else {
					document.getElementById("buttons"+i).style.display="block";
				    document.getElementById("radio"+i).style.display="none";
					if (i == 0) {
					  document.getElementById("upbutton"+i).disabled=true;
					}
					if (i == radioElements.length-1) {
					  document.getElementById("downbutton"+i).disabled=true;
					}
					
				}
			}
		}
				document.getElementById("selectall").disabled = false;			
				function openCopyrightWindow(theURL,winName,winSettings) 
		{ 
		  window.open(theURL,winName,winSettings);
		  return false;
		}
		function toggleSelectAll(caller, elementName)
		{
			var newValue = caller.checked;
			var elements = document.getElementsByName(elementName);
			
			if(elements)
			{
				for(var i = 0; i < elements.length; i++)
				{
					elements[i].checked = newValue;
				}
			}
			
			if(newValue == true)
			{
				caller.title = "Uncheck All";
				var copyButton = document.getElementById("copyButton");
				if(copyButton)
				{
					copyButton.disabled = false;
					copyButton.className='enabled';
				}
				var moveButton = document.getElementById("moveButton");
				if(moveButton)
				{
					moveButton.disabled = false;
					moveButton.className = 'enabled';
				}
				var removeButton = document.getElementById("removeButton");
				if(removeButton)
				{
					removeButton.disabled = false;
					removeButton.className = 'enabled';
				}
				document.getElementById("copyCount").value = "3";
				document.getElementById("moveCount").value = "0";
				document.getElementById("removeCount").value = "0";
			}
			else
			{
				caller.title = "Check All";
				var copyButton = document.getElementById("copyButton");
				if(copyButton)
				{
					copyButton.disabled = true;
					copyButton.className='disabled';
				}
				var moveButton = document.getElementById("moveButton");
				if(moveButton)
				{
					moveButton.disabled = true;
					moveButton.className = 'disabled';
				}
				var removeButton = document.getElementById("removeButton");
				if(removeButton)
				{
					removeButton.disabled = true;
					removeButton.className = 'disabled';
				}
				document.getElementById("removeCount").value = "0";
				document.getElementById("moveCount").value = "0";
				document.getElementById("copyCount").value = "0";
			}
		}
		
		function adjustCount(caller, countName, buttonName)
		{
			var counter = document.getElementById(countName);
			var button = document.getElementById(buttonName);
			
			if(caller && caller.checked && caller.checked == true)
			{
				counter.value = parseInt(counter.value) + 1;
			}
			else
			{
				counter.value = parseInt(counter.value) - 1;
			}
	
			if(button)
			{
				if(counter.value > 0)
				{
					button.disabled = false;
					button.className='enabled';
				}
				else
				{
					button.disabled = true;
					button.className='disabled';
				}
			}
		}
		
		function disableLinks()
		{
			if(document.getElementsByName)
			{
				var enabledLinks = document.getElementsByName("enabledActionLinks");
				var disabledLinks = document.getElementsByName("disabledActionLinks");
				if(enabledLinks)
				{
					for(var i = 0; i < enabledLinks.length; i++)
					{
						//enabledLinks[i].innerHTML = disabledLinks[i].innerHTML;
						enabledLinks[i].style.display="none";
						disabledLinks[i].style.display="block";
					}
				}			
			}
		}
	*#	
	//-->
</script>
