/*
City of Saint Paul Open Source License

LICENSE

Copyright © 2006 City of Saint Paul, Minnesota. 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following terms and conditions:

1.The above copyright notice and this permission notice shall be included in all copies of this Software or works derived from this Software.
2.Whenever a Recipient distributes or redistributes the Software, a copy of this agreement must be included with each copy of the Software
3.If Recipient distributes or redistributes the Software in any form other than source code, Recipient must also make the source code freely available, and must provide with each copy of the Software information on how to obtain the source code in a reasonable manner on or through a medium customarily used for software exchange
4.A Recipient may not make any representation in the Software or in any promotional, advertising or other material that may be construed as an endorsement by the City of Saint Paul of any product or service provided by Recipient, or that may seek to obtain commercial advantage by the fact of the City of Saint Paul’s participation in this Agreement.

DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION

No Warranty:  The Software is provided “as is” without any warranty of any kind, either expressed, implied, or statutory, including, but not limited to, any warranty that the Software will conform to specifications, any implied warranties of merchantability, fitness for a particular purpose, or freedom from infringement, any warranty that the Software will be error free, or any warranty that documentation, if provided, will conform to the Software. This agreement does not, in any manner, constitute an endorsement by the City of Saint Paul or any prior recipient of any results, resulting designs, hardware, software products or any other applications resulting from use of the Software. Further, the City of Saint Paul disclaims all warranties and liabilities regarding third-party software, if present in the original Software, and distributes it “as is.”

Waiver and Indemnity:  Recipient agrees to waive any and all claims against the City of Saint Paul, its contractors and subcontractors, as well as any prior recipient.  If recipient's use of the Software results in any liabilities, demands, damages, expenses or losses arising from such use, including any damages from products based on, or resulting from, recipient's use of the Software, recipient shall indemnify and hold harmless the City of Saint Paul, it’s contractors and subcontractors, as well as any prior recipient, to the extent permitted by law. Recipient's sole remedy for any such matter shall be the immediate, unilateral termination of this agreement.

SUPPORT STATEMENT 

The Software is freely provided by the City of Saint Paul as a public service.  The recipient is herby notified that the provision and distribution of the Software should in no way be construed to mean that the City of Saint Paul is obligated to provide any level of support for the Software, now or in the future.  Support in this context shall be construed to mean providing coordination, technical assistance, responding to questions, releasing bug fixes, providing future releases, or otherwise obligating the City of Saint Paul to commit resources to maintain this Software or assist in its implementation by any person or entity.  The City of Saint Paul may, at it’s sole option, and without notice or obligation to the recipients, provide varying levels of support for the Software or terminate the agreement.  Provision of such support shall not alter the condition that the Software is provided without support as stated above.
*/
function DragAndDropLibrary(SelfName) {
	var Targets = new Array();
	this.SourceObject;
	this.HandleObject;
	this.ParentObject;
	this.Container;
	this.ListStyle = false;

	this.onDrop = function(src, target) {
		alert('Drop Made '+src.id+' '+target.id);
	}

	function intersects(obj, x, y) {
		var w = obj.clientWidth;
		var h = obj.clientHeight;
		var top = getRealTop(this.Container, obj);
		var left = getRealLeft(this.Container, obj);
		return ( x >=left && x <=left+w && y >= top && y <= top+h);
	}

	function determineTarget(ev, Container) {
		var targetsArr = new Array();
		for(var i = 0; i < Targets.length; i++) {
			if(intersects(Targets[i], ev.clientX, ev.clientY)) {
				targetsArr[targetsArr.length] = Targets[i];
			}
		}
		return targetsArr;
	}

	this.dragContainer = function(ev, container) {
		if(this.SourceObject != null) {
			this.moveDragging(ev, this.HandleObject);		
		}
	}

	this.addTarget = function(obj) {
		Targets[Targets.length] = obj;
	}
	

	this.removeTarget = function(obj) {
		var newTargets = new Array();
		for(var i = 0; i < Targets.length; i++) {
			if(Targets[i] != obj) {
				newTargets[newTargets.length] = Targets[i];
			}
		}
		Targets = newTargets;
	}

	this.startDragging = function(ev, obj, dragValue) {
		if(window.event) { ev = window.event; }

		var dragDiv = document.createElement('div');

		var mySelf = this;

/*		if(document.all) { // IE6
			dragDiv = document.createElement("<div onmousemove='"+SelfName+".moveDragging(event, this)'>"); // onmouseup='"+SelfName+".removeHandle(event);'>");
			/*dragDiv.onmouseup = function (ev) {
				if(window.event) { ev = window.event; }
				mySelf.removeHandle(ev);
			}
		}*/

		dragDiv.innerHTML = dragValue;
		dragDiv.style.top = ev.clientY - 8 - this.Container.offsetTop;//+this.Container.scrollTop;
		dragDiv.style.left = ev.clientX - 8 - this.Container.offsetLeft;//+this.Container.scrollLeft;
		dragDiv.style.width = obj.clientWidth+'px';
		dragDiv.style.height = obj.clientHeight+'px';
		dragDiv.style.position = 'absolute';
		dragDiv.style.border = 'solid 1px black';
		dragDiv.style.background = '#FFE';
		dragDiv.className = 'dragging';
		dragDiv.SourceObject = obj;

		dragDiv.onmousemove = function (event) { 
			if(window.event) { event = window.event; }
			mySelf.moveDragging(event, this); 
			if(mySelf.ListStyle) {
				var arr = determineTarget(event, this.parentNode);
				for(var i = 0; i < Targets.length; i++) {
					if(Targets[i].oldBottomBorder) {
						Targets[i].style.borderBottom = Targets[i].oldBottomBorder;
					} else {
						Targets[i].style.borderBottom = 'none';
					}
					if(Targets[i].oldTopBorder) {
						Targets[i].style.borderTop = Targets[i].oldTopBorder;
					} else {
						Targets[i].style.borderTop = 'none';
					}

				}
				for(var i = 0; i < arr.length; i++) {
					if(arr[i].offsetTop < this.SourceObject.offsetTop) {
						arr[i].oldTopBorder = arr[i].style.borderTop;
						arr[i].style.borderTop = 'dashed 2px black';
					} else if(arr[i] != this.SourceObject) {
						arr[i].oldBottomBorder = arr[i].style.borderBottom;
						arr[i].style.borderBottom = 'dashed 2px black';

					}
				}
			}
		};

		dragDiv.onDrop = this.onDrop;

		dragDiv.onmouseup = function (event) { 
			if(window.event) { event = window.event; }
			//mySelf.removeHandle(event);
			var p = this.parentNode;
			var arr = determineTarget(event, this.parentNode);
			for(var i = 0; i < arr.length; i++) {
				this.onDrop(this.SourceObject, arr[i]);
			}
			p.removeChild(this);
		};

		this.Container.appendChild(dragDiv);

		this.SourceObject = obj;
		this.HandleObject = dragDiv;
		//obj.className = 'source';

		dragDiv.style.cursor = 'move';

		return false;
	}



	this.removeHandle = function(event) {
		this.Container.removeChild(this.HandleObject);
		this.determineTarget(event);
		//this.SourceObject.className = '';
	}

	this.moveDragging = function(ev, obj) {
		if(window.event) { ev = window.event; } 
		obj.style.left = ev.clientX-8 - this.Container.offsetLeft;//+this.Container.scrollLeft-obj.offsetLeft;
		obj.style.top = ev.clientY-8 - this.Container.offsetTop;//+this.Container.scrollTop-obj.offsetTop;
	}
}
