/************************************************************************************************************
Drag and drop for blocks
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com


************************************************************************************************************/	
		
	var JSEbadgeObj;
		
	/* Constructor */
	function JSDragDropEbadge()
	{
		var idOfBlockList;
		//var menuKey='';
		var imageFolder ="M/i/";
		var dragNode_source;
		var dragNode_parent;
		var dragNode_sourceNextSib;
		var dragNode_noSiblings;
		var dragNode_relatedBigNode;
		var ajaxObjects;
		
		var dragNode_destination;
		var floatingContainer;
		var dragDropTimer;
		var dropTargetIndicator;
		var insertAsSub;
		var indicator_offsetX;
		var indicator_offsetX_sub;
		var indicator_offsetY;
		
		this.imageFolder = 'M/i/';

		var additionalRenameRequestParameters = {};
		var additionalDeleteRequestParameters = {};

		var currentlyActiveItem;
		var contextMenu;
		var currentItemToEdit;		// Reference to item currently being edited(example: renamed)
		var helpObj;
		
		this.contextMenu = false;
		this.floatingContainer = document.createElement('DIV');
		this.floatingContainer.style.position = 'absolute';
		this.floatingContainer.style.display='none';
		this.floatingContainer.id = 'floatingContainer';
		this.insertAsSub = false;
		document.body.appendChild(this.floatingContainer);
		this.dragDropTimer = -1;
		this.dragNode_noSiblings = false;
		this.currentItemToEdit = false;
		
		if(document.all){
			this.indicator_offsetX = 2;	// Offset position of small black lines indicating where nodes would be dropped.
			this.indicator_offsetX_sub = 4;
			this.indicator_offsetY = 2;
		}else{
			this.indicator_offsetX = 1;	// Offset position of small black lines indicating where nodes would be dropped.
			this.indicator_offsetX_sub = 3;
			this.indicator_offsetY = 2;			
		}
		if(navigator.userAgent.indexOf('Opera')>=0){
			this.indicator_offsetX = 2;	// Offset position of small black lines indicating where nodes would be dropped.
			this.indicator_offsetX_sub = 3;
			this.indicator_offsetY = -7;				
		}

		this.currentlyActiveItem = false;
		this.filePathRenameItem = 'folderTree_updateItem.php';
		this.filePathDeleteItem = 'folderTree_updateItem.php';
		this.ajaxObjects = new Array();
		this.helpObj = false;
		
		this.RENAME_STATE_BEGIN = 1;
		this.RENAME_STATE_CANCELED = 2;
		this.RENAME_STATE_REQUEST_SENDED = 3;
		this.renameState = null;
		
		this.EvntFunction = "SetNewBlockOrder";
		this.AjaxFunction = "changeMenuSelection";
		
	}
	
	
	/* JSDragDropTree class */
	JSDragDropEbadge.prototype = {
		// {{{ addEvent()
	    /**
	     *
	     *  This function adds an event listener to an element on the page.
	     *
	     *	@param Object whichObject = Reference to HTML element(Which object to assigne the event)
	     *	@param String eventType = Which type of event, example "mousemove" or "mouseup"
	     *	@param functionName = Name of function to execute. 
	     * 
	     * @public
	     */	
		addEvent : function(whichObject,eventType,functionName)
		{ 
		  if(whichObject.attachEvent){ 
		    whichObject['e'+eventType+functionName] = functionName; 
		    whichObject[eventType+functionName] = function(){
				    	whichObject['e'+eventType+functionName]( window.event );
				    } 
		    whichObject.attachEvent( 'on'+eventType, whichObject[eventType+functionName] ); 
		  } else 
		    whichObject.addEventListener(eventType,functionName,false); 	    
		} 
		// }}}	
		,	
		// {{{ removeEvent()
	    /**
	     *
	     *  This function removes an event listener from an element on the page.
	     *
	     *	@param Object whichObject = Reference to HTML element(Which object to assigne the event)
	     *	@param String eventType = Which type of event, example "mousemove" or "mouseup"
	     *	@param functionName = Name of function to execute. 
	     * 
	     * @public
	     */		
		removeEvent : function(whichObject, eventType, functionName)
		{ 
		  if(whichObject.detachEvent){ 
		    whichObject.detachEvent('on'+eventType, whichObject[eventType+functionName]); 
		    whichObject[eventType+functionName] = null; 
		  } else 
		    whichObject.removeEventListener(eventType,functionName,false); 
		} 

		,	
		setImageFolder : function(path)
		{
			this.imageFolder = path;	
		}
		,		
		setBlockListId : function(idOfBlockList)
		{
			this.idOfBlockList = idOfBlockList;			
		}	
		,
		getTopPos : function(obj){
			var top = obj.offsetTop/1;
			while((obj = obj.offsetParent) != null){
				if(obj.tagName!='HTML')top += obj.offsetTop;
			}			
			if(document.all)top = top/1 + 13; else top = top/1 + 4;		
			return top;
		}
		,	
		getLeftPos : function(obj){
			var left = obj.offsetLeft/1 + 1;
			while((obj = obj.offsetParent) != null){
				if(obj.tagName!='HTML')left += obj.offsetLeft;
			}
			if(document.all)left = left/1 - 2;
			return left;
		}	
		,
		cancelEvent : function()
		{
			return false;	
		}
		,
		cancelSelectionEvent : function()
		{
			if(JSEbadgeObj.dragDropTimer<10)	return true;
			return false;	
		}
		,highlightItem : function(inputObj,e)
		{
			if(JSEbadgeObj.currentlyActiveItem)JSEbadgeObj.currentlyActiveItem.className = '';
			this.className = 'highlightedNodeItem';
			JSEbadgeObj.currentlyActiveItem = this;
		}
		,
		removeHighlight : function()
		{
			if(JSEbadgeObj.currentlyActiveItem)JSEbadgeObj.currentlyActiveItem.className = '';
			JSEbadgeObj.currentlyActiveItem = false;
		}
		,
		hasSubNodes : function(obj)
		{
			var subs = obj.getElementsByTagName('SPAN');
			if(subs.length>0)return true;
			return false;	
		}
		,
		__refreshDisplay : function(obj)
		{
			if(this.hasSubNodes(obj))	return;

			var img = obj.getElementsByTagName('IMG')[0];
			img.style.visibility = 'hidden';	
		}

		,
		createDropIndicator : function()
		{
			this.dropTargetIndicator = document.createElement('DIV');
			this.dropTargetIndicator.style.position = 'absolute';
			this.dropTargetIndicator.style.display='none';			
			var img = document.createElement('IMG');
			img.src = this.imageFolder + 'dragDrop_ind1.gif';
			img.id = 'dragDropIndicatorImage';
			this.dropTargetIndicator.appendChild(img);
			document.body.appendChild(this.dropTargetIndicator);
		}
		,
		initBlockList : function()
		{
			JSEbadgeObj = this;
			JSEbadgeObj.createDropIndicator();
			document.documentElement.onselectstart = JSEbadgeObj.cancelSelectionEvent;
			document.documentElement.ondragstart = JSEbadgeObj.cancelEvent;
			document.documentElement.onmousedown = JSEbadgeObj.removeHighlight;

			var theBlockList = $('#'+JSEbadgeObj.idOfBlockList);
			//** Initialisation des blocks de la version publiée:
			$('tr[@id*=row]', theBlockList).filter('contains(idblock)').each(
				function()
				{
					var IdBlock = this.getAttribute('idblock');
					currentRow = this;
					$('img', currentRow).filter('contains(arrow)').each(
							
							function(){
								arrowSens = this.getAttribute('arrow');
								switch (arrowSens)
								{
									case 'Down': this.setAttribute('id',"IMGUP_"+IdBlock) ; break;
									case 'UP':	 this.setAttribute('id',"IMGDOWN_"+IdBlock) ;  break; 
									default: break;
								} 
								this.style.cursor = 'move'; 
								this.onmousedown = JSEbadgeObj.initDrag; 
								this.onmousemove = JSEbadgeObj.moveDragableNodes;
							}
					 );
				}
			);
			
			//** Les sous blocks de la version publiée:
			$('td[@id*=Col_]', theBlockList).each(
				function()
				{
					 $('img[@id*=SUBIMGUP_]', this).each(
						function(){
							this.style.cursor = 'move'; 
							this.onmousedown = JSEbadgeObj.initDrag ; 
							this.onmousemove = JSEbadgeObj.moveDragableNodes ;
						}
					 );	 
					 
					 $('img[@id*=SUBIMGDOWN_]', this).each(
						function(){
							this.style.cursor = 'move'; 
							this.onmousedown = JSEbadgeObj.initDrag; 
							this.onmousemove = JSEbadgeObj.moveDragableNodes;
						}
					 );
					 
					 //** Les lignes des champs la fiche invitée
					 $('img[@id*=FieldUp_]', this).filter('contains(arrow)').each(
							
							function()
							{
								this.style.cursor = 'move'; 
								this.onmousedown = JSEbadgeObj.initDrag; 
								this.onmousemove = JSEbadgeObj.moveDragableNodes;
							/*	var parent = this;
								while(parent.tagName!='TR' && parent.parentNode){
										parent = parent.parentNode;
								}
								parent.onmousedown = JSEbadgeObj.initDrag; 
								parent.onmousemove = JSEbadgeObj.moveDragableNodes;

							*/
							}
					 );
					
					$('img[@id*=FieldDown_]', this).filter('contains(arrow)').each(
							function()
							{
								this.style.cursor = 'move'; 
								this.onmousedown = JSEbadgeObj.initDrag; 
								this.onmousemove = JSEbadgeObj.moveDragableNodes;
							}
					 );
					 
					
				}
			);
			
			//** Liste des champs pour les formulaires:
			$('tr[@id*=Row_]', theBlockList).each(
				function()
				{
					
					
					$('img[@id*=IMGUP_]', this).filter('contains(arrow)').each(
							
							function()
							{
								this.style.cursor = 'move'; 
								this.onmousedown = JSEbadgeObj.initDrag; 
								this.onmousemove = JSEbadgeObj.moveDragableNodes;
							}
					 );
					
					$('img[@id*=IMGDOWN_]', this).each(
							function()
							{
								this.style.cursor = 'move'; 
								this.onmousedown = JSEbadgeObj.initDrag; 
								this.onmousemove = JSEbadgeObj.moveDragableNodes;
							}
					 );
					 
				}
			);
			
			theBlockList.mousemove(JSEbadgeObj.moveDragableNodes);
			theBlockList.mouseup(JSEbadgeObj.dropDragableNodes);
			
		}
		
		,
		/* Initialize drag */
		initDrag : function(e)
		{
			if(document.all)  e = event;	
	
			sourceId = this.id;	
			var DragType = sourceId.substring(0, sourceId.indexOf("_"));
			
			switch (DragType)
			{
				case 'IMGUP': 
				case 'IMGDOWN':   JSEbadgeObj.EvntFunction = "SetNewBlockOrder";
								  //JSEbadgeObj.AjaxFunction = "changeMenuSelection"; 
								  break;
								  
				case 'SUBIMGUP':	 
				case 'SUBIMGDOWN':	JSEbadgeObj.EvntFunction = "SetNewSubBlockList";
								  	//JSEbadgeObj.AjaxFunction = "changeMenuSelection";  
									break;
				case 'FieldUp': 
				case 'FieldDown': JSEbadgeObj.EvntFunction = "setNewInvitationFieldList";
								  JSEbadgeObj.AjaxFunction = "setNewFieldDataList"; 
								  break;					
				default: break;
			} 
			
			JSEbadgeObj.dragNode_source = document.getElementById(sourceId);
			JSEbadgeObj.dragNode_parent = JSEbadgeObj.dragNode_source.parentNode;
			JSEbadgeObj.dragNode_sourceNextSib = false;

			JSEbadgeObj.dragNode_destination = false;
			JSEbadgeObj.dragDropTimer = 0;
			JSEbadgeObj.timerDrag();
			return false;
		}
		
		,
		timerDrag : function()
		{	
			if(this.dragDropTimer>=0 && this.dragDropTimer<10){
				this.dragDropTimer = this.dragDropTimer + 1;
				setTimeout('JSEbadgeObj.timerDrag()',20);
				return;
			}
			if(this.dragDropTimer==10)
			{
				JSEbadgeObj.floatingContainer.style.display='block';
				JSEbadgeObj.floatingContainer.appendChild(JSEbadgeObj.dragNode_source);
				JSEbadgeObj.floatingContainer.style.opacity=0.4;
			}
		}
		,
		moveDragableNodes : function(e)
		{
			if(JSEbadgeObj.dragDropTimer<10) return;
			
			if(document.all)e = event;
			dragDrop_x = e.clientX/1 + 2 + document.body.scrollLeft;
			dragDrop_y = e.clientY/1 + 2 + document.body.scrollTop;	
			
			JSEbadgeObj.floatingContainer.style.left = dragDrop_x + 'px';
			JSEbadgeObj.floatingContainer.style.top = dragDrop_y + 'px';
			
			var thisObj = this;
			while(thisObj.tagName!='IMG' && thisObj.id.indexOf('IMG')==-1){
				return;
			}
			
			JSEbadgeObj.dragNode_noSiblings = false;
				
			if(thisObj && thisObj.id)
			{
				JSEbadgeObj.dragNode_destination = thisObj;
				var tmpObj= JSEbadgeObj.dropTargetIndicator;
				tmpObj.style.display='block';
				
				var eventSourceObj = this;
				if(JSEbadgeObj.dragNode_noSiblings && eventSourceObj.tagName=='IMG')
					eventSourceObj = eventSourceObj.nextSibling;
				//** style pour l'indicateur tmpObj qui contient tmpImg(flèche):
				var tmpImg = tmpObj.getElementsByTagName('IMG')[0];
				if(this.tagName=='A' || JSEbadgeObj.dragNode_noSiblings){
					tmpImg.src = tmpImg.src.replace('ind1','ind2');
					JSEbadgeObj.insertAsSub = true;
					tmpObj.style.left = (JSEbadgeObj.getLeftPos(eventSourceObj) + JSEbadgeObj.indicator_offsetX_sub) + 'px';
				}
				else{
					tmpImg.src = tmpImg.src.replace('ind2','ind1');
					JSEbadgeObj.insertAsSub = false;
					tmpObj.style.left = (JSEbadgeObj.getLeftPos(eventSourceObj) + JSEbadgeObj.indicator_offsetX) + 'px';
				}
				
				tmpObj.style.top = (JSEbadgeObj.getTopPos(thisObj) + JSEbadgeObj.indicator_offsetY) + 'px';
			
			}
			return false;
		}
		,
		
		dropDragableNodes:function()
		{
			if(JSEbadgeObj.dragDropTimer<10){				
				JSEbadgeObj.dragDropTimer = -1;
				return;
			}
		
			if(JSEbadgeObj.dragNode_destination){	
				
				if(JSEbadgeObj.dragNode_destination.parentNode){
						JSEbadgeObj.dragNode_destination.parentNode.insertBefore(JSEbadgeObj.dragNode_source,JSEbadgeObj.dragNode_destination);
				}	
				
				if(document.getElementById(JSEbadgeObj.idOfBlockList)!=null)
				{
					element = document.getElementById(JSEbadgeObj.idOfBlockList);
					AjaxFunction = element.getAttribute("AjaxFunction");
					EvntFunction = element.getAttribute("EvntFunction");
				}
				if(!AjaxFunction)
					AjaxFunction = JSEbadgeObj.AjaxFunction;
				
				if(!EvntFunction)
					EvntFunction = JSEbadgeObj.EvntFunction;
				
				//** *****Traitement spécifique pour le contenu transversal
				name     = document.getElementById(JSEbadgeObj.dragNode_source.id).getAttribute('name');
				totalRow = document.getElementById(JSEbadgeObj.dragNode_source.id).getAttribute('totalrow');
				sourceRowNum = document.getElementById(JSEbadgeObj.dragNode_source.id).getAttribute('rownum');
				destRowNum = document.getElementById(JSEbadgeObj.dragNode_destination.id).getAttribute('rownum');
				
				JSBlockObj.dropTargetIndicator.style.display='none';
				
				if(name && totalRow)
				{
					SQ_FormGeneric(Data,'Event',EvntFunction,'_SQ','Y',
										'DivId',JSEbadgeObj.idOfBlockList,	
										'IsClick','N',
										'Name',name,
										'TotalRow',totalRow,
									    'SourceRowNum',sourceRowNum,
									    'DestRowNum',destRowNum,
							 		    'AjaxFunction',AjaxFunction,
									    'SourceNodeId', JSEbadgeObj.dragNode_source.id,
									    'DestNodeId', JSEbadgeObj.dragNode_destination.id);
				}
				//** Traitement de base:
				else
				{
					SQ_FormGeneric(Data,'Event',EvntFunction,'_SQ','Y',
										'DivId',JSEbadgeObj.idOfBlockList,	
										'IsClick','N',
								 		'AjaxFunction',AjaxFunction,
										'SourceNodeId', JSEbadgeObj.dragNode_source.id,
										'DestNodeId', JSEbadgeObj.dragNode_destination.id);
				}
				
				var ProcessInit = true;
			}
			else{
				// Putting the item back to it's original location
				if(JSEbadgeObj.dragNode_sourceNextSib){
					JSEbadgeObj.dragNode_parent.insertBefore(JSEbadgeObj.dragNode_source,JSEbadgeObj.dragNode_sourceNextSib);
				}
				else{
					JSEbadgeObj.dragNode_parent.appendChild(JSEbadgeObj.dragNode_source);
				}			
			}
			
			JSEbadgeObj.dropTargetIndicator.style.display='none';
			JSEbadgeObj.floatingContainer.style.display='none';
			JSEbadgeObj.dragNode_parent.appendChild(JSEbadgeObj.dragNode_source);
			JSEbadgeObj.dragDropTimer = -1;	
			//** Réinitialisation de la liste des blocks:
			if(ProcessInit==true)
				JSEbadgeObj.initBlockList();
			
		}
		,__addAdditionalRequestParameters : function(ajax, parameters)
		{
			for (var parameter in parameters) {
				ajax.setVar(parameter, parameters[parameter]);
			}
		}
	}
