/**
* utf-8
*/
var startTarget = '';

mouse.dnd = {};
mouse.dnd.SimpleDragSource = function(elementId) {
	this.id = elementId;
	this.element = document.getElementById(elementId);
	this.dragging = false; // 현재 드래그중인지 여부 표시
	this.selected = false; // 현재 마우스다운 상태인지 표시
	this.diff = null; // 마우스 위치와 객체 위치

	this.x = 0;
	this.y = 0;
	
	this.mouseDown = mouse.Event.bindAsListener(this.doMouseDown, this);
	this.mouseMove = mouse.Event.bindAsListener(this.doMouseMove, this);
	this.mouseUp = mouse.Event.bindAsListener(this.doMouseUp, this);
	
	mouse.Event.addListener(
		this.element, 'mousedown', this.mouseDown);
}
mouse.dnd.SimpleDragSource.prototype = {
	doMouseDown: function(e)
	{
		var event = window.event || e;
		if (!mouse.Event.isLeftButton(event)) return;
		
		this.selected = true;
		
		var elementXY = mouse.GUI.getXY(this.element);
		var mouseXY = mouse.Event.getMouseXY(event);
		this.diff = {
			x: mouseXY.x - elementXY.x,
			y: mouseXY.y - elementXY.y
		};

		mouse.Event.addListener(document, 'mousemove', this.mouseMove);
		mouse.Event.addListener(document, 'mouseup', this.mouseUp);
		mouse.Event.stopEvent(event);
	},
	doMouseMove: function(e) {
		if (!this.selected) return false;
		
		if (!this.dragging) {
			this.dragging = true;
			mouse.GUI.setOpacity(this.element, 0.70);
		}
		
		var event = window.event || e;
		var mouseXY = mouse.Event.getMouseXY(event);
		var newXY = {
			x: mouseXY.x - this.diff.x,
			y: mouseXY.y - this.diff.y
		}
		mouse.GUI.setXY(this.element, newXY.x, newXY.y);
		
		mouse.Event.stopEvent(event);
	},
	doMouseUp: function(e)
	{
		if (!this.selected) return false;
		
		this.selected = false;
		this.diff = null;
		
		var event = window.event || e;

		if (this.dragging) {
			this.dragging = false;
			mouse.GUI.setOpacity(this.element, 1.0);
		}
		mouse.Event.removeListener(	document, 'mousemove', this.mouseMove);
		mouse.Event.removeListener(	document, 'mouseup', this.mouseUp);
		mouse.Event.removeListener(	this.element, 'mousedown', this.mouseDown);
		mouse.Event.stopEvent(event);
	}
}

mouse.dnd.WinResize = function(elementId, eventId, maxW, maxH, minW, minH, method) {
	this.call = method;
	
	this.id = elementId; // 사이즈 조절당함
	this.element = document.getElementById(elementId);
	
	this.evtid = eventId; // 사이즈 조절이벤트가 일어나는 아이디
	this.evtelement = document.getElementById(eventId);
	
	this.dragging = false; // 현재 드래그중인지 여부 표시
	this.selected = false; // 현재 마우스다운 상태인지 표시
	this.diff = null; // 마우스 위치와 객체 위치
	this.width = parseInt(this.element.style.width);
	this.height = parseInt(this.element.style.height);
	this.maxWidth = parseInt(maxW);
	this.maxHeight= parseInt(maxH);
	this.minWidth = parseInt(minW);
	this.minHeight= parseInt(minH);

	this.x = 0;
	this.y = 0;
	
	this.elementXY = mouse.GUI.getXY(this.element);
	
	this.mouseDown = mouse.Event.bindAsListener(this.doMouseDown, this);
	this.mouseMove = mouse.Event.bindAsListener(this.doMouseMove, this);
	this.mouseUp = mouse.Event.bindAsListener(this.doMouseUp, this);
	
	mouse.Event.addListener(
		this.evtelement, 'mousedown', this.mouseDown);
}
mouse.dnd.WinResize.prototype = {
	doMouseDown: function(e)
	{
		var event = window.event || e;
		if (!mouse.Event.isLeftButton(event)) return;
		
		this.selected = true;

		mouse.Event.addListener(document, 'mousemove', this.mouseMove);
		mouse.Event.addListener(document, 'mouseup', this.mouseUp);
		mouse.Event.stopEvent(event);
	},
	doMouseMove: function(e) {
		if (!this.selected) return false;
		
		if (!this.dragging) {
			this.dragging = true;
			mouse.GUI.setOpacity(this.element, 0.60);
		}
		
		var event = window.event || e;
		var mouseXY = mouse.Event.getMouseXY(event);
		this.diff = {
			x: mouseXY.x - this.elementXY.x,
			y: mouseXY.y - this.elementXY.y
		};
		
		var rw = this.diff.x - 5;
		var ry = this.diff.y - 5;
		
		if(rw>this.minWidth && mouseXY.x<this.maxWidth){ this.element.style.width = rw+'px'; 	}
		if(ry>this.minHeight && mouseXY.y<this.maxHeight){ this.element.style.height = ry+'px'; }
		
		mouse.Event.stopEvent(event);
	},
	doMouseUp: function(e)
	{
		if (!this.selected) return;
		
		this.selected = false;
		this.diff = null;
		
		var event = window.event || e;
		
		if (this.dragging) {
			this.dragging = false;
			mouse.GUI.setOpacity(this.element, 1.0);
		}
		
		this.evtelement.style.position = 'relative';
		
		mouse.Event.removeListener(	document, 'mousemove', this.mouseMove);
		mouse.Event.removeListener(	document, 'mouseup', this.mouseUp);
		mouse.Event.stopEvent(event);
		
		if ( this.call){
			this.call( this.id);
		}
	}
}

mouse.dnd.DNDManager = function() {
	this.dropTargetList = new Array();
	this.dragSourceList = new Array();
	
	this.mouseDown = mouse.Event.bindAsListener(this.doMouseDown, this);
	this.mouseMove = mouse.Event.bindAsListener(this.doMouseMove, this);
	this.mouseUp = mouse.Event.bindAsListener(this.doMouseUp, this);
	
	this.selectedDragSource = null;
}
mouse.dnd.DNDManager.prototype = {
	addDropTarget: function(dropTarget)
	{
		this.dropTargetList[this.dropTargetList.length] = dropTarget;
	},
	
	removeDropTarget: function(dropTarget)
	{
		var newList = new Array();
		for (var i = 0 ; i < this.dropTargetList.length ; i++)
		{
			if (this.dropTargetList[i] != dropTarget) {
				newList[newList.length] = this.dropTargetList[i];
			}
		}
		this.dropTargetList = newList;
	},
	
	addDragSource: function(dragSource)
	{
		this.dragSourceList[this.dragSourceList.length] = dragSource;
		mouse.Event.addListener(dragSource.getElement(), "mousedown", this.mouseDown);
	},
	
	removeDragSource: function(dragSource)
	{
		var newList = new Array();
		for (var i = 0 ; i < this.dropTargetList.length ; i++)
		{
			if (this.dragSourceList[i] != dragSource)
			{
				newList[newList.length] = this.dragSourceList[i];
			} 
			else
			{
				mouse.Event.removeListener( dragSource.getElement(), "mousedown", this.mouseDown);
			}
		}
		this.dragSourceList = newList;
	},
	
	doMouseDown: function(e)
	{
		var event = window.event || e;
		if (!mouse.Event.isLeftButton(event)) return;

		var target = mouse.Event.getTarget(event);
		while (target && !target.dragSource) {
			target = target.parentNode;
		}
		this.selectedDragSource = target.dragSource;
		this.selectedDragSource.selectDrag(event);

		mouse.Event.addListener(document, 'mousemove', this.mouseMove);
		mouse.Event.addListener(document, 'mouseup', this.mouseUp);
		mouse.Event.stopEvent(event);
	},
	
	doMouseMove: function(e)
	{
		if (!this.selectedDragSource) return;

		var event = window.event || e;
		if (!this.selectedDragSource.isDragging()) {
			this.selectedDragSource.startDrag();
		}
		
		this.selectedDragSource.moveDrag(event);
		
		mouse.Event.stopEvent(event);
	},

	doMouseUp: function(e)
	{
		if (!this.selectedDragSource) return;
		
		var dragSource = this.selectedDragSource;
		this.selectedDragSource = null;
		
		var event = window.event || e;
		
		dragSource.deselectDrag(event);
		
		if (dragSource.isDragging()) {
			var mouseXY = mouse.Event.getMouseXY(event);
			
			var dropTarget = null;
			for (var i = 0 ; i < this.dropTargetList.length ; i++)
			{
				var droppable = this.dropTargetList[i].checkInDropTarget( dragSource, mouseXY);
				
				if (droppable)
				{
					dropTarget = this.dropTargetList[i];
					break;
				}
			}

			if (dropTarget)
			{
				dragSource.endDrag(event);
				dropTarget.drop(dragSource);
			} 
			else
			{
				dragSource.cancelDrag(event);
			}
		}
		mouse.Event.removeListener( document, "mousemove", this.mouseMove);
		mouse.Event.removeListener( document, "mouseup", this.mouseUp);
		mouse.Event.stopEvent(event);
	}
}

mouse.dnd.DropTarget = function(elementId) {
	this.element = document.getElementById(elementId);
}
mouse.dnd.DropTarget.prototype = {
	// 마우스 포인터가 드롭 대상 요소 안에 위치
	checkInDropTarget: function(dragSource, mouseXY)
	{
		var bounds = mouse.GUI.getBounds(this.element);

		return bounds.x <= mouseXY.x && bounds.x + bounds.width >= mouseXY.x &&
		       bounds.y <= mouseXY.y && bounds.y + bounds.height >= mouseXY.y;
	},
	
	// 드롭처리
	drop: function(dragSource)
	{		
		// 드래그 요소를 드롭 대상 노드에 자식으로 추가
		var element = dragSource.getElement();
		this.element.appendChild(element);

		this.plusdrop();
	},

	plusdrop : function()
	{
		// 현재 타켓 ID
		var targetName = this.element.getAttribute('id');
		//alert(targetName);

		// 현재 타켓의 자식 노드 ID
		targetName = document.getElementById(targetName);

		var childNodeList = targetName.childNodes;
		dogTarget = '';
		for (var i=0; i<childNodeList.length; i++ )
		{
			dogTarget = childNodeList.item(i).getAttribute('id');
			if (dogTarget){
				break;
			}
		}

		addSource = document.getElementById(dogTarget);
		//alert(dogTarget);

		// 드래그 요소를 드롭 대상 노드에 자식으로 추가
		st = document.getElementById(startTarget);
		st.appendChild(addSource);
	}
}

mouse.dnd.DragSource = function(divid)
{
	this.element = document.getElementById(divid);

	this.element.dragSource = this;
	this.selected = false;
	this.dragging = false;
	this.diff = null;
}
mouse.dnd.DragSource.prototype = {
	getElement: function() {
		return this.element;
	},
	selectDrag: function(event)
	{
		this.selected = true;

		//this.element.className = "dndStart"; // css name

		var elementXY = mouse.GUI.getBounds(this.element);
		var mouseXY = mouse.Event.getMouseXY(event);
		this.diff = {
			x: mouseXY.x - elementXY.x,
			y: mouseXY.y - elementXY.y
		};
	},
	// 마우스 버튼을 땔 때 호출
	deselectDrag: function(event)
	{
		//this.element.className = "dndEnd"; // css name
		this.selected = false;
		this.diff = null;
	},
	
	startDrag: function(event)
	{
		this.dragging = true;

		startTarget = this.element.parentNode.getAttribute('id');
		//alert(this.element.parentNode.getAttribute('id')); //타켓 id명
		//alert(this.element.getAttribute('id')); //본인 아이디 명

		//this.element.className = "dndStart"; // css name
		var elementXY = mouse.GUI.getBounds(this.element);
		this.element.style.position = 'absolute';
		mouse.GUI.setOpacity(this.element, 0.60);
	},
	
	isDragging: function(){
		return this.dragging;
	},
	
	moveDrag: function(event)
	{		
		var mouseXY = mouse.Event.getMouseXY(event);
		var newXY = {
			x: mouseXY.x - this.diff.x,
			y: mouseXY.y - this.diff.y
		}
		
		//this.element.className = "dndStart"; // css name
		
		// 마우스 이동만큼 드래그 요소의 좌표 이동
		mouse.GUI.setXY(this.element, newXY.x, newXY.y);
	},
	
	endDrag: function(event)
	{
		this.dragging = false;
		this.element.style.position = '';
		
		//this.element.className = "dndEnd"; // css name
		mouse.GUI.setOpacity(this.element, 1.0);

		//alert(this.element.parentNode.getAttribute('id')); //타켓 id명
		//alert(this.element.getAttribute('id')); //본인 아이디 명
		
		// 드래그 요소를 요소의 부모 노드에서 제거
		this.element.parentNode.removeChild(this.element);
	},
	
	cancelDrag: function(event)
	{
		this.dragging = false;
		this.element.style.position = '';

		//this.element.className = "dndEnd"; // css name
		mouse.GUI.setOpacity(this.element, 1.0);
	}
}


// 심플드래그
function APM_mndEvent(eid){
	new mouse.dnd.SimpleDragSource(eid);
}