var treeModule = '';
var addParentID = '';
var ddTargetType = 'li';
var resortColor = false;

DDListItem = function(id, sGroup, config) {
	DDListItem.superclass.constructor.call(this, id, sGroup, config);
	
	var el = this.getDragEl();
	YAHOO.util.Dom.setStyle(el, "opacity", 0.67);
	
	this.goingUp = false;
	this.lastY = 0;
};
YAHOO.extend(DDListItem, YAHOO.util.DDProxy, {
	startDrag: function(x, y) {
		var dragEl = this.getDragEl();
		var clickEl = this.getEl();
		
		YAHOO.util.Dom.setStyle(clickEl, "visibility", "hidden");
		dragEl.innerHTML = clickEl.innerHTML;
		
		YAHOO.util.Dom.setStyle(dragEl, "width", "350"); 
		YAHOO.util.Dom.setStyle(dragEl, "color", YAHOO.util.Dom.getStyle(clickEl, "color")); 
		YAHOO.util.Dom.setStyle(dragEl, "backgroundColor", YAHOO.util.Dom.getStyle(clickEl, "backgroundColor")); 
		YAHOO.util.Dom.setStyle(dragEl, "border", "2px solid gray");
	},
	
	endDrag: function(e) {
		var srcEl = this.getEl();
		var proxy = this.getDragEl();
		
		if(treeModule != '') {
			var serial = '';
			$(srcEl.parentNode.parentNode).children().each(function(i) { 
				$(this).children().each(function(j) {
				    if(this.id != '' && this.id != null && $(this).hasClass('serialize')) {
					    serial += 'subcontent_sort='+this.id+'&';
					}
				});
			});
			
			$.ajax({
				type: 'POST',
				url: 'Module.aspx?go='+treeModule+'.admin.resort',
				data: '&'+serial,
				dataType: 'json',
				async: false,
				success: function(json) {
					
					if(json.status == "1") {
						// Do nothing, the sorting was successful.
					} else if(json.status == "0") {
						showMessageDialog('Error', json.msg, YAHOO.widget.SimpleDialog.ICON_ALARM);
					} else {
					    showMessageDialog('Error', '<span class="yui_tpi_title">An error has occurred.</span><br /><p>An error occurred while sorting the items.</p>', YAHOO.widget.SimpleDialog.ICON_ALARM);
					}
				}, 
				error: function(request, status, error) {

				showMessageDialog('Error', '<span class="yui_tpi_title">An error has occurred.</span><br /><p>The error reported was ' + request.responseText + '</p>', YAHOO.widget.SimpleDialog.ICON_ALARM);
				}
			});
		}
		
		YAHOO.util.Dom.setStyle(proxy, "visibility", "");
		var a = new YAHOO.util.Motion(proxy, { points: { to: YAHOO.util.Dom.getXY(srcEl) } }, 0.2, YAHOO.util.Easing.easeOut);
		var proxyid = proxy.id;
		var thisid = this.id;
		
		a.onComplete.subscribe(function() {
			YAHOO.util.Dom.setStyle(proxyid, "visibility", "hidden");
			YAHOO.util.Dom.setStyle(thisid, "visibility", "");
		});
		a.animate();
	},
	
	onDragDrop: function(e, id) {
		
		if(YAHOO.util.DragDropMgr.interactionInfo.drop.length === 1) {
			var pt = YAHOO.util.DragDropMgr.interactionInfo.point;
			var region = YAHOO.util.DragDropMgr.sourceRegion;
			
			if(region) {
				if(!region.intersect(pt)) {
					var destEl = YAHOO.util.Dom.get(id);
					var destDD = YAHOO.util.DragDropMgr.getDDById(id);
					destEl.appendChild(this.getEl());
					destDD.isEmpty = false;
					YAHOO.util.DragDropMgr.refreshCache();
				}
			} else {
				this.endDrag(e);
			}
		}
	},
	
	onDrag: function(e) {
		var y = YAHOO.util.Event.getPageY(e);
		
		if(y < this.lastY) {
			this.goingUp = true;
		} else if(y > this.lastY) {
			this.goingUp = false;
		}
		
		this.lastY = y;
	},
	
	onDragOver: function(e, id) {
		var srcEl = this.getEl();
		var destEl = YAHOO.util.Dom.get(id);
		
		if(destEl.nodeName.toLowerCase() == ddTargetType) {
			var orig_p = srcEl.parentNode;
			var p = destEl.parentNode;
			
			if(this.goingUp) {
				p.insertBefore(srcEl, destEl);
			} else {
				p.insertBefore(srcEl, destEl.nextSibling);
			}
			
			YAHOO.util.DragDropMgr.refreshCache();
		}
	}
});
