/* 
	Create a box that contain the blocks to hide
	Add a button for the 'edit' mode
*/

function content_initialization(mode, path_img_right, path_img_down)
{	
	/* Initialization */	
	var separator_start = document.getElementById("hidden_separator_start");
	if(separator_start == null)
		return error("separator start not found !");
	
	var separator_start_block = separator_start.parentNode;
	if(separator_start_block == null)
		return error();
		
	var frame = separator_start_block.parentNode;
	if(frame == null)
		return error();
		
	var separator_end = document.getElementById("hidden_separator_end");
	if(separator_end == null)
		return error("separator end not found !");
		
	var separator_end_block = separator_end.parentNode;
	if(separator_end_block == null)
		return error();

	/* Creation of the box that will contain the blocks to hide */
	var box_out = document.createElement("div");
	var box_in = document.createElement("div");
	box_out.appendChild(box_in);
	box_out.id = "box_"+separator_start.value;  // Identifier to distinguish the pair of separators
	// Style
	box_out.style.paddingLeft = "9px";
	box_in.style.paddingLeft = "14px";
	box_in.style.borderLeft = "1px dotted #335E88";

	/* Moving div nodes between the separators into the box */
	var current_node = separator_start_block.nextSibling;  // Initialization
	
	while(!is_separator_end_block(current_node) && current_node != null)
	{
		next_node = current_node.nextSibling;
		box_in.appendChild(current_node.cloneNode(1));  // Copy
		frame.removeChild(current_node);  // Removing
		current_node = next_node;
	}
	if(current_node == null)
		error();
		
	/* Add of a button for the content edit mode */
	if(mode == "edit")
	{
		/* Button creation */
		var button_div = document.createElement("div");  // Button block
		button_div.className = "block";
		button_img = "<img src='"+path_img_right+"' onClick='javascript:swap(this, \""+path_img_right+"\", \""+path_img_down+"\");' style='cursor: pointer;'";
		button_img += "title='"+separator_start.title+"' class='right' id='"+separator_start.value+"' />";
		button_lab = separator_start.name;
		button_div.innerHTML = "<table><tr><td>"+button_img+"</td><td>&nbsp;"+button_lab+"</td></tr></table>";  

		/* Button insertion */
		frame.insertBefore(button_div, separator_end_block);
	}
	
	/* Box insertion */	
	frame.insertBefore(box_out, separator_end_block);
	
	/* Hiding of the separators and the box */
	separator_start_block.style.display = "none";
	separator_end_block.style.display = "none";
	box_out.style.display = "none";
	
	/* Removing separators id (if more than two separators) */ 
	separator_start.removeAttribute("id");
	separator_end.removeAttribute("id");
}

/*
	Show/hide the box
*/
function swap(button, path_img_right, path_img_down)
{	
	var box = document.getElementById("box_"+button.id);  // Must be found
	
	if(button.className == "right")
	{
		button.className = "down";
		button.src = path_img_down;
		box.style.display = "block";
	}
	else{
		button.className = "right";
		button.src = path_img_right;
		box.style.display = "none";
	}
}

/*
	Test if the node given has a separator end for a child
*/
function is_separator_end_block(node)
{
	var children = node.childNodes;
	for(x in children)
	{
	   	if(children[x].id == "hidden_separator_end")
   			return true;
   	}
   	
	return false;		
}

/*
	Print error message
*/
function error(message)
{
	if(message)
		alert("extension adminhideblocks error : "+message);
	else
		alert("extension adminhideblocks");
		
	return false;
}
