registerNS("OT");

// OT.prefix must be set - it defines the ids of the tables/rows created (the passed id
// is appended onto the prefix.
// There should be also be a div with an id set to OT.prefix created already with an
// empty table (no rows in it).
//
// If there is something to be shown once rows have been repositioned, it should be in a div
// with an id of 'OT.prefix + moved' - this div will be made visible once rows are moved, if it exists
//
// The move buttons and the contents of OT.rowCodeGroupsRight will only be shown if OT.showRightCol
// has been set to true

// We have a 20px indent on each layer of the hierarchy by default
if(OT.tablemargin == null){
	OT.tablemargin = 20;
}


OT.rowExists = function(rowID){
	var existingRow = document.getElementById(OT.prefix + rowID);
	return (exisitingRow != null);
}


// Function to add a row to a pre-existing table
// level is used to populate hidden inputs used for reordering the rows
OT.addRow = function(rdata, rowID, parentRowID, level){
	
	// Find the parent table
	if(parentRowID){
		var parentRow = document.getElementById(OT.prefix + parentRowID);
	}
	
	if(!parentRow) {
		var parentRow = document.getElementById(OT.prefix);
		var isTopLevel = true;
	}
		
	// If the parentRow already has a table in it, then that's where
	// the row we're adding will go
	// If not, we need to create the table
	var parentsTables = parentRow.getElementsByTagName('table');
	
	if(parentsTables.length == 0){
		
		// Create a new table
		if(isTopLevel) // We're dealing with a div instead of a table
			var parentsCell = parentRow;
		else
			var parentsCell = parentRow.getElementsByTagName('td')[0];
		
		var cellHTML = parentsCell.innerHTML;
		cellHTML += "<table cellpadding='0' cellspacing='0' width='100%'></table>";
		parentsCell.innerHTML = cellHTML;
		var parentsTable = parentsCell.getElementsByTagName('table')[0];
	
	} else {
		
		var parentsTable = parentsTables[0];
	
	}
	
	// Add a new row at the end of the parent's table to contain the child
	var newRow = Misc.insertRow(parentsTable, parentsTable.rows.length);
	newRow.id = OT.prefix + rowID;
	OT.rowCode(rdata, rowID, newRow, level);
	
}


// Given data and the row to put it in, puts it into the row
OT.rowCode = function(rdata, rowID, newRow, level){
	var dataCell = newRow.insertCell(newRow.cells.length);
	if(level && level != 0){ // Don't pad the first level
		dataCell.style.paddingLeft = OT.tablemargin + "px";
	}
	var rowCode = 
		"<div class='"+ OT.prefix + level + "' style='border-bottom:1px transparent solid'>"; 
	if(OT.showRightCol){	
		rowCode += "<div style='float:right' onMouseOver='OT.mouseOverRow(this.parentNode)' onMouseOut='OT.mouseOutRow(this.parentNode)'>"+
			"</div>" + 
			"<div style='float:right;padding-right:7px' onMouseOver='OT.mouseOverRow(this.parentNode)' onMouseOut='OT.mouseOutRow(this.parentNode)'>"+
				OT.rowCodeGroupsRight(rdata)+
			"</div>";
	}
	rowCode += OT.rowCodeGroupsLeft(rdata) +
				"<input type='hidden' name='" + OT.prefix + "[" + rdata[0] + "]' value='" + level + "' />" +
			"<div style='clear:both;'></div>" +
		"</div>";
	
	dataCell.innerHTML = rowCode;
	
	if(OT.showRightCol){
		OT.drawMoveButtons(newRow.parentNode.parentNode);
	}
}

// The code for the given row to be left-justified
OT.rowCodeGroupsLeft = function(rdata){
	
	return "<a href='index.php?option=com_oe&task=viewGroupRecord&groupID=" + rdata[0] + "'>" + rdata[1] + "</a>";
	
}

// The code for the given row to be right-justified
OT.rowCodeGroupsRight = function(rdata){
	
	var code = "<a href='index.php?option=com_oe&task=addGroup&groupID=" + rdata[0] + "'>[edit]</a> "; 
	if(!rdata[2]) // If this group is not a locked group
		code += "<a href='index.php?option=com_oe&task=groups&operation=delete&group=" + rdata[0] + "' onclick=\"return confirm('The deletion of this group is an irreversible process. ALL sub-groups of this group will also be deleted. Are you sure you want to proceed?')\">[delete]</a> ";
	
	return code;
	
}




OT.drawMoveButtons = function(rtable){
	
	var numRows = rtable.rows.length;
	
	for(var i = 0; i < rtable.rows.length; i++){
		// Get the div to put the buttons in
		var currentRow = rtable.rows[i];
		var buttonDiv = currentRow.getElementsByTagName('div')[1];
		
		// Work out what should go in it
		var suffix = "";
		if(i != 0)
			suffix += "<a onClick='OT.moveRow(this.parentNode.parentNode.parentNode.parentNode, -1)'><img src='images/uparrow.png' title='Move Up' style='border:0px'/></a>";
		else
			suffix += "<img src='images/uparrowdisabled.png'/>";
			
		if(i != numRows-1)
			suffix += "<a onClick='OT.moveRow(this.parentNode.parentNode.parentNode.parentNode, 1)'><img src='images/downarrow.png' title='Move Down' style='border:0px'/></a>";
		else
			suffix += "<img src='images/downarrowdisabled.png'/>";
		
		buttonDiv.innerHTML = suffix;
		
	}
	
}



// Moves a row the specified number of rows up or down
OT.moveRow = function(oldRow, offset){
	
	var table = oldRow.parentNode.parentNode;

	if(offset > 0) offset++;

	var oldIndex = oldRow.rowIndex;
	var newIndex = oldIndex + offset;
	
	if(newIndex < 0 || newIndex > table.rows.length) return;
	
  var newRow = Misc.insertRow(table, newIndex);
	
	// Copy all the cells from the old row into the new row
  for (var c = 0; c < oldRow.cells.length; c++) {
    
    var oldCell = oldRow.cells[c];
    var newCell = newRow.insertCell(newRow.cells.length);
    
    newCell.innerHTML = oldCell.innerHTML;
    
    OT.copyChildNodeValues(oldCell, newCell);
  }
  
  // For some reason the values don't seem to copy...
  newRow.cells[0].getElementsByTagName("input")[0].value = oldRow.cells[0].getElementsByTagName("input")[0].value;
  
  // Neither do the padding values
  newRow.cells[0].style.paddingLeft = oldRow.cells[0].style.paddingLeft;
  
  // Undo any effects that we might have had on that row
  OT.mouseOutRow(newRow.getElementsByTagName('div')[0]);
  
	// Delete the old row
	table.deleteRow(oldRow.rowIndex); 
	
	OT.drawMoveButtons(table);
	
	OT.rowsMoved();
	
}


OT.copyChildNodeValues = function(sourceNode, targetNode) {
	
  for (var n = 0; n < sourceNode.childNodes.length; n++) {
    try{
      //targetNode.childNodes[n].value = sourceNode.childNodes[n].value;
      targetNode.childNodes[n].value = 'no!';
    }
    catch(e){
			alert('something went wrong');
    }
  }
}


// Called when the order of the rows has been changed. Shows the save button
OT.rowsMoved = function(){
	if(OT.alreadyMoved) return;
	
	var movedDiv = document.getElementById(OT.prefix + "moved");
	if(!movedDiv) return;
	
	movedDiv.style.display = "block";
	
	OT.alreadyMoved = true;
}




OT.mouseOverRow = function(victim){
	
	//victim.style.backgroundColor = "gray";
	victim.style.borderBottom = "1px #d3d4dc solid";
	
}


OT.mouseOutRow = function(victim){
	
	//victim.style.backgroundColor = "transparent";
	victim.style.borderBottom = "1px transparent solid";
	
}
