ajax.dnd = {};

ajax.dnd.DNDManager = function() {
	this.dropTargetList = new Array();
	this.dragSourceList = new Array();
	
	this.mouseDown = ajax.Event.bindAsListener(this.doMouseDown, this);
	this.mouseMove = ajax.Event.bindAsListener(this.doMouseMove, this);
	this.mouseUp = ajax.Event.bindAsListener(this.doMouseUp, this);
	
	this.selectedDragSource = null;
}
ajax.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;
		ajax.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 {
				ajax.Event.removeListener(
					dragSource.getElement(),
					"mousedown", this.mouseDown);
			}
		}
		this.dragSourceList = newList;
	},
	doMouseDown: function(e) {
		var event = window.event || e;
		if (!ajax.Event.isLeftButton(event)) return;
		
		var target = ajax.Event.getTarget(event);
		//var selectedDataSource_id = target.parentNode.parentNode.parentNode.parentNode.id;
		
		while (target && !target.dragSource) {
			target = target.parentNode;
		}
		this.selectedDragSource = target.dragSource;
		this.selectedDragSource.selectDrag(event);
		
		////////////////////////////////////////////////////
		/*var target_id = target.parentNode.value;
		
		alert(target_id);
		if(target_id.indexOf("center")==0){
			var contenttype = this.selectedDragSource.contenttype;
			var code = this.selectedDragSource.code;
			var code_nm = this.selectedDragSource.code_nm;
			
			var newitem = document.createElement("div");
			newitem.setAttribute("id", "test_data_source");
			newitem.setAttribute("value", code + "|" + contenttype);
			newitem.className ="top_wrap";
			var html = '<div class="bottom"><span class="ulbl"><p><a href="#" onclick=javascript:goContent("'+ code + '")>' + code_nm + '</a></p></span></div>';
			newitem.innerHTML = html;
			
			var itemListNode = document.getElementById(target_id);
			itemListNode.appendChild(newitem);
				
			this.addDragSource(new ajax.dnd.DragSource("test_data_source", code_nm, contenttype));
		}*/
		ajax.Event.addListener(document, "mousemove", this.mouseMove);
		ajax.Event.addListener(document, "mouseup", this.mouseUp);
		ajax.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);
		
		ajax.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 = ajax.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;
				}
			}
			//dragSource.cancelDrag(event);
			if (dropTarget) {
				dragSource.endDrag(event);
				dropTarget.drop(dragSource);
				
				f_mouseup(dragSource,dropTarget);
			} else {
				dragSource.cancelDrag(event);
			}
		}
		ajax.Event.removeListener(
			document, "mousemove", this.mouseMove);
		ajax.Event.removeListener(
			document, "mouseup", this.mouseUp);
		ajax.Event.stopEvent(event);
	}
}

ajax.dnd.DropTarget = function(elementId, contenttype) {
	this.element = document.getElementById(elementId);
	this.contenttype = contenttype;
}
ajax.dnd.DropTarget.prototype = {
	checkInDropTarget: function(dragSource, mouseXY) {
		var bounds = ajax.GUI.getBounds(this.element);
		
		if(this.contenttype == ""){	//나의 여행만들기 일정에 드랍했을 경우
			//alert("this.contenttype is null");
			return bounds.x <= mouseXY.x && bounds.x + bounds.width >= mouseXY.x &&
			       bounds.y <= mouseXY.y && bounds.y + bounds.height >= mouseXY.y;
		}else if(this.contenttype =="wastebasket"){	//휴지통에 드랍했을 경우
			return bounds.x <= mouseXY.x && bounds.x + bounds.width >= mouseXY.x &&
			       bounds.y <= mouseXY.y && bounds.y + bounds.height >= mouseXY.y;
		}else if(this.contenttype != dragSource.contenttype){
			//관광지 타입이 14일 경우도 관광명소에 추가해야함
			if((this.contenttype=="12" && dragSource.contenttype == "14") || (this.contenttype=="76" && dragSource.contenttype == "78")){
				//target안에 드랍이 이루어질 경우 무조건 true를 리턴
				return bounds.x <= mouseXY.x && bounds.x + bounds.width >= mouseXY.x;
			}
			return false;
		}else if(this.contenttype == dragSource.contenttype){	//target과 source가 같을 경우 
			return bounds.x <= mouseXY.x && bounds.x + bounds.width >= mouseXY.x;	//target안에 드랍이 이루어질 경우 true 리턴
		}else{
		
			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);
	}
}

ajax.dnd.DragSource = function(elementId, code, code_nm, contenttype) {
	this.element = document.getElementById(elementId);
	this.element.dragSource = this;
	this.selected = false;
	this.dragging = false;
	this.diff = null;
	this.contenttype = contenttype;
	this.code = code;
	this.code_nm = code_nm;
}
ajax.dnd.DragSource.prototype = {
	getElement: function() {
		return this.element;
	},
	selectDrag: function(event) {
		this.selected = true;
		
		var elementXY = ajax.GUI.getBounds(this.element);
		var mouseXY = ajax.Event.getMouseXY(event);
		this.diff = {
			x: mouseXY.x - elementXY.x,
			y: mouseXY.y - elementXY.y
		};
	},
	deselectDrag: function(event) {
		this.selected = false;
		this.diff = null;
	},
	startDrag: function(event) {
		this.dragging = true;
		
		var elementXY = ajax.GUI.getBounds(this.element);
		this.element.style.position = "absolute";
		ajax.GUI.setOpacity(this.element, 0.60);
	},
	isDragging: function() {
		return this.dragging;
	},
	moveDrag: function(event) {
		if(event.y < 30){
			upscroll();
		}
		var mouseXY = ajax.Event.getMouseXY(event);
		var newXY = {
			x: mouseXY.x - this.diff.x,
			y: mouseXY.y - this.diff.y
		}
		ajax.GUI.setXY(this.element, newXY.x, newXY.y);
	},
	endDrag: function(event) {
		this.dragging = false;
		this.element.style.position = "";
		ajax.GUI.setOpacity(this.element, 1.0);
		this.element.parentNode.removeChild(this.element);
	},
	cancelDrag: function(event) {
		this.dragging = false;
		this.element.style.position = "";
		ajax.GUI.setOpacity(this.element, 1.0);
	}
}
