/*********************************************************************/
/*                                                                   */
/* IBM Confidential                                                  */
/*                                                                   */
/* OCO Source Materials                                              */
/*                                                                   */
/* Copyright IBM Corp. 2005, 2007                                    */
/*                                                                   */
/* The source code for this program is not published or otherwise    */
/* divested of its trade secrets, irrespective of what has been      */
/* deposited with the U.S. Copyright Office.                         */
/*                                                                   */
/*********************************************************************/

//-------------------------------------------------------------
// Quickplace Drag & Drop library
//-------------------------------------------------------------

var h_bInsideDragSource = false;
//------------------------------------------------------------------------------------
// Write out the tag to mark the beginning of a drag source area.
//
function QP_DragAndDrop_writeSourceBegin(tagName, szUnid, nChildren, docLevel)
{ 
	// Do not make a doc with response(s) draggable unless manager
	if (UIDragAndDropIsEnabled() && docLevel==1) {

		// Terminate preceeding drag source first
		QP_DragAndDrop_writeSourceEnd(tagName);

		if (!(currentUserAccess<6 && nChildren>0)) {
			document.write('<' + tagName + ' id="dragSrc_' + szUnid + '" class="h-dragSource"'
								+ ' onMouseOver="this.className=\'h-dragSource-selected\'"'
								+ ' onMouseOut="this.className=\'h-dragSource-deselected\'"'
								+ '>');

			h_bInsideDragSource = true;
		}
	}
};


//------------------------------------------------------------------------------------
// Write out the tag to mark the end of a drag source area.
//
function QP_DragAndDrop_writeSourceEnd(tagName)
{ 
	if (h_bInsideDragSource) {
		document.write("</" + tagName + ">");
		h_bInsideDragSource = false;
	}
};


//------------------------------------------------------------------------------------
// Create all objects needed for drag & drop
//
function QP_DragAndDrop_createObjects(tagName, szContainerId)
{
	if (UIDragAndDropIsEnabled())
	{
		QP_DragAndDrop_initDragSources(tagName, szContainerId);
		QP_DragAndDrop_initDropTargets("toc", "div");
		QP_DragAndDrop_createForm();
	}
}

//------------------------------------------------------------------------------------
// Initialize all drag sources
// within a containing table whose id is szContainerId.
//
function QP_DragAndDrop_initDragSources(tagName, szContainerId)
{
	var dl = document.getElementById(szContainerId);
	if (dl != null && UIDragAndDropIsEnabled())
	{
		var dragList = dl.getElementsByTagName(tagName);
		for (var i=0; i < dragList.length; i++)
		{
			if (dragList[i].className == "h-dragSource") 
			{
				var ds = new dojo.dnd.HtmlDragSource(dragList[i], "dojoDragList");

				dojo.event.connect(ds, "onDragEnd", function(e) {

					// Get enclosing drag source element
					if (e.dragObject && e.dragStatus == "dropSuccess") {
						// dragged it to a drop target
						var dragNode = e.dragObject.domNode;
						if (dragNode) {
							var parent = dragNode.parentNode;
							var srcUnid = dragNode.id.substring(8); // FIX ME: magic number
							  
							// alert("onDragEnd: " + "\ndropped " +  dragNode.id + " to " + parent.id);
							QP_DragAndDrop_copyMove(srcUnid, parent.id, "1");
							  
							dojo.dom.removeNode(dragNode);
						}
					}
					//else {
					// Invalid drop
					// alert("drag object="+e.dragObject+"\nstatus="+e.dragStatus);
					//}
				});
			}
		}
	}
}

//------------------------------------------------------------------------------------
// Initialize all drop targets with tag szTag,
// within a containing tag whose id is szContainerId.
//
function QP_DragAndDrop_initDropTargets(szContainerId, szTag)
{
	var container = document.getElementById(szContainerId);
	if (container != null && UIDragAndDropIsEnabled())
	{
		var dropList = container.getElementsByTagName(szTag);

		for (var i=0; i < dropList.length; i++) 
		{
			if (dropList[i].className.indexOf("dropTarget") >= 0) 
			{
				// Don't make CURRENT or Index folder a drop target
				if (dropList[i].id != h_FolderStorage && dropList[i].id != "h_Index") {

					var dt = new dojo.dnd.HtmlDropTarget(dropList[i], ["dojoDragList"]);

					// FIX ME BRR:
					// It would be nice to change the folder icon when dragged over,
					// but these Dojo events don't reliably return the id of the
					// element being dragged over, so can't do this for now.

// 					dojo.event.connect(dt, "onDragOver", function(e) {

// 						// alert (e.target.tagName);
// 						if (e.target.tagName.toLowerCase() == "div") {
// 							var imgs = e.target.getElementsByTagName("img");
// 							if (imgs[0]) {
// 								imgs[0].src = "/qphtml/html/common/folder_selected.gif";
// 							}
// 						}							
// 						else if (e.target.tagName.toLowerCase() == "img")
// 							e.target.src = "/qphtml/html/common/folder_selected.gif";
// 					});
						
// 					dojo.event.connect(dt, "onDragOut", function(e) {
// 						// alert (e.target.tagName);
// 						if (e.target.tagName.toLowerCase() == "img")
// 							e.target.src = "/qphtml/html/common/folder_close.gif";
// 					});
				}
			}
		}
	}
}

//------------------------------------------------------------------------------------
// Initialize a single drop target
//
function QP_DragAndDrop_initDropTarget(obj)
{
	if (UIDragAndDropIsEnabled())
	{
		new dojo.dnd.HtmlDropTarget(obj, ["dojoDragList"]);
	}
}

// --------------------------------------------------
// Copy or move a doc to a folder
// Change to a "wait" cursor until response received.
//
function QP_DragAndDrop_copyMove(srcUnid, destUnid, bMoveIt)
{
	var copyOrMove = (bMoveIt ? "1" : "0");
	var form = document.getElementById("copyMoveForm");
	if (form) {

		form.action = getAbsoluteRoomURL(self) + '/' + h_FolderStorage + '/' + srcUnid + '/?EditDocument';

		form.h_Move.value = copyOrMove;
		form.h_DestRoomNsfName.value = currentRoom.roomNsf;
		form.h_DestFolderUNID.value = destUnid;
		form.h_SetDeleteList.value = srcUnid;
		form.h_HandleResponses.value = "1";

		QPAjax_SubmitForm("copyMoveForm", sFunc, eFunc);
	}
}

function sFunc(data)
{
}

// TODO: Make error string translatable!
// This is sidHaikuPagesNotMoved from nquickplacers.rc.
function eFunc(data)
{
	alert("One or more of the selected pages were not moved because you do not have the required access rights or are currently in a workflow.");
	location.reload();
}



// --------------------------------------------------
// Create the form used to submit an asynch request
// to copy or move a doc to a folder
//
function QP_DragAndDrop_createForm()
{
	// Add form to copy/move docs to this page
	var form = document.createElement("form");
	form.id = "copyMoveForm";
	form.method = "POST";
	form.name="h_CopyMoveForm";
	 
	var html = "";
	html += '<input type="hidden" name="h_EditAction" value="h_Ajax">';
	html += '<input type="hidden" name="h_Move">';
	html += '<input type="hidden" name="h_HandleResponses" value="0">';
	html += '<input type="hidden" name="h_AllDocs" value="">';
	html += '<input type="hidden" name="h_DestRoomNsfName" value="">';
	html += '<input type="hidden" name="h_DestFolderUNID" value="">';
	html += '<input type="hidden" name="h_SetPublishAboveTocEntry" value="">';
	html += '<input type="hidden" name="h_SetDeleteList" value="">';
	html += '<input type="hidden" name="h_SetCommand" value="h_MoveCopyPages">';
	html += '<input type="hidden" name="h_NoSceneTrail" value="1">';
	 
	form.innerHTML = html;
	document.body.appendChild(form);
}		
