/* 
 * dhLib: Simple Dynamic-HTML-Library
 * Konrad Schandera X / MMVII 
 * 
 * draganddrop-section
 */ 






/******************************************
 ***** P R I V A T E   S E C T I O N ******
 ******************************************

 section contains functions/variables used 
 only internally. */




/* when drag-and-drop is used, this one holds the currently dragged element */
var dhLib_currentlyDraggedElement = document;
var dhLib_currentlyDraggedBar = document;

/* true when dragging (that is, mouse is down), false otherwise */
var dhLib_dragging = false;


/* the offset of the mouse inside the containing element when clicking or something*/
var dhLib_dragElementOffsetX = 0;
var dhLib_dragElementOffsetY = 0;

var dhLib_dragableElements = new Array();
var dhLib_dragableElementsBars = new Array();

var dhLib_useAlphaOnClick = false;
var dhLib_alphaOnClick = 100;

var dhLib_dragbarOffsetX = 0;
var dhLib_dragbarOffsetY = 0;

/* 
 * === Drag and Drop of elements === 
 */ 


/*
 * Function wich is called whenever the mouse is moved, thus updating the
 * position of the dragged element
 */

function dhLib_drag(e) {
	e = dhLib_getEvent(e);
	if ( dhLib_dragging ) {
		dhLib_currentlyDraggedElement.style.left	= dhLib_getWindowScrollOffsetX() + e.clientX - dhLib_dragElementOffsetX - dhLib_dragbarOffsetX;
		dhLib_currentlyDraggedElement.style.top		= dhLib_getWindowScrollOffsetY() + e.clientY - dhLib_dragElementOffsetY - dhLib_dragbarOffsetY;
		dhLib_currentlyDraggedElementBar.style.left	= dhLib_getWindowScrollOffsetX() + e.clientX - dhLib_dragElementOffsetX;
		dhLib_currentlyDraggedElementBar.style.top	= dhLib_getWindowScrollOffsetY() + e.clientY - dhLib_dragElementOffsetY;
	} // end.we are currently dragging an element
} // end.drag


/*
 * Function indicating an element to be dragged, onmousedown-attached, usually
 */
function dhLib_startDrag(e) { 
	e = dhLib_getEvent(e);
	element = dhLib_getEventElement(e);
	elementFound = false;
	elementIndex = -1;
	for ( x=0 ; x<dhLib_dragableElementsBars.length ; x++ ) { /* look if we got the element registered, to prevent capturing/bubbling-stuff */
		if ( dhLib_dragableElementsBars[x] == element.id ) {
			elementFound = true;
			elementIndex = x;
		} // end.element has been registered
	} // end.itor through elements

	if ( elementFound ) {
		dhLib_currentlyDraggedElement = document.getElementById( dhLib_dragableElements[ elementIndex ] );
		dhLib_currentlyDraggedElementBar = document.getElementById( dhLib_dragableElementsBars[ elementIndex ] );

		dhLib_dragElementOffsetX = dhLib_getOffsetX(e);
		dhLib_dragElementOffsetY = dhLib_getOffsetY(e);
		dhLib_dragging = true; 
		// window.defaultStatus = dhLib_currentlyDraggedElement.id + "XOffset: "+dhLib_getOffsetX(e);
	} // end.element has been registered

} // end.setDrag

/*
 * Function indicating that an element is no longer being dragged, onmouseup-attached...
 */
function dhLib_releaseDrag()	{ 
	dhLib_dragging = false; 
} // end.releaseDrag











/******************************************
 ***** P U B L I C     S E C T I O N ******
 ******************************************

 section contains functions/variables 
 to be used from within html/scripts, that
 is to say, the public interface. */










/*
 * when using drag and drop, this function sets an element to be draggable
 */

function dhLib_registerDragableElement( containerId , dragbarId ) {
	if ( document.getElementById(dragbarId) ) {
		document.getElementById(dragbarId).onmousedown = dhLib_startDrag;
		document.getElementById(dragbarId).onmouseup = dhLib_releaseDrag;
		document.onmousemove = dhLib_drag;
		document.onmouseup = dhLib_releaseDrag; /* fallback for 'escaped' mouse-cursor */
		dhLib_dragableElementsBars[ dhLib_dragableElementsBars.length ] = dragbarId; 
		dhLib_dragableElements[ dhLib_dragableElements.length ] = containerId;
	} // end.elementFound
} // end.registerDragableElement


function dhLib_setAlphaOnClick( alpha ) {
	dhLib_useAlphaOnClick = true;
	dhLib_alphaOnClick = alpha;
} // end.setAlphaOnClick

function dhLib_setDragbarOffsetX(o) { dhLib_dragbarOffsetX=o; }
function dhLib_setDragbarOffsetY(o) { dhLib_dragbarOffsetY=o; }

