var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;

function bncReorderList(sourceid,targetid) {
	this.createFormElements = function() {
		// remove previously created elements
		var deletediv = Dom.get(this.sourceid+"_div");
		if(deletediv!=null) {
			deletediv.parentNode.removeChild(deletediv);
		}
		deletediv = Dom.get(this.targetid+"_div");
		if(deletediv!=null) {
			deletediv.parentNode.removeChild(deletediv);
		}
		var sdiv = document.createElement("div");
		sdiv.id = this.sourceid+"_div";
		sdiv.style.display = "none";
		Dom.get(this.sourceid).appendChild(sdiv);
		
		var asource = this.parseList(this.sourceid);
		var counter = 0;
		for(var i=0; i<asource.length; i++) {
			if(document.getElementById(asource[i]).style.display!="none") {
				var sinp = document.createElement("input");
				sinp.type = "hidden";
				sinp.id = this.sourceid+"_id"+counter;
				sinp.name = this.sourceid+"_id"+counter;
				sinp.value = asource[i];
				sdiv.appendChild(sinp);
				counter++;
			}
		}

		if(this.targetid!=null) {
			var tdiv = document.createElement("div");
			tdiv.id = this.targetid+"_div";
			tdiv.style.display = "none";
			Dom.get(this.targetid).appendChild(tdiv);

			var atarget = this.parseList(this.targetid);
			counter = 0;
			for(var i=0; i<atarget.length; i++) {
				if(document.getElementById(atarget[i]).style.display!="none") {
					var tinp = document.createElement("input");
					tinp.type = "hidden";
					tinp.id = this.targetid+"_id"+counter;
					tinp.name = this.targetid+"_id"+counter;
					tinp.value = atarget[i];
					tdiv.appendChild(tinp);
					counter++;
				}
			}
		}
	}
	this.sourceid = sourceid;
	this.targetid = targetid;
	
	// unregister previously registered elements
	var ldd = DDM.getDDById(sourceid);
	if(ldd!=null) {
		var addobjects = DDM.getRelated(ldd,false);
		for(var i=0; i<addobjects.length; i++) {
			addobjects[i].unreg();
		}
	}
	ldd = DDM.getDDById(targetid);
	if(ldd!=null) {
		var addobjects = DDM.getRelated(ldd,false);
		for(var i=0; i<addobjects.length; i++) {
			addobjects[i].unreg();
		}
	}
	DDM.refreshCache();
	
	// use sourceid as groupid also!
	var st = new YAHOO.util.DDTarget(sourceid,sourceid);
	var asource = this.parseList(sourceid);
	for(var i=0; i<asource.length; i++) {
		new bncDDList(this,asource[i],sourceid);
	}
	if(targetid!=null) {
		var tt = new YAHOO.util.DDTarget(targetid,sourceid);
		var atarget = this.parseList(targetid);
		for(var i=0; i<atarget.length; i++) {
			new bncDDList(this,atarget[i],sourceid);
		}
	}
	this.createFormElements();
}

bncReorderList.prototype.parseList = function(listid) {
	var parent = Dom.get(listid);
    var items = parent.getElementsByTagName("li");
    var alist = new Array();
    for (i=0;i<items.length;i++) {
		if(items[i].parentNode==parent) {
        	alist.push(items[i].id);
		}
    }
    return alist;
}

bncDDList = function(owner, listid, sGroup, config) {

    bncDDList.superclass.constructor.call(this, listid, sGroup, config);

	this.owner = owner;
	
    this.logger = this.logger || YAHOO;
    var el = this.getDragEl();
    Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

    this.goingUp = false;
    this.lastY = 0;
};

YAHOO.extend(bncDDList, YAHOO.util.DDProxy, {

    startDrag: function(x, y) {
		
        this.logger.log(this.id + " startDrag");

        // make the proxy look like the source element
        var dragEl = this.getDragEl();
        var clickEl = this.getEl();
        Dom.setStyle(clickEl, "visibility", "hidden");

        dragEl.innerHTML = clickEl.innerHTML;

		Dom.setStyle(dragEl, "zIndex", "110000");
        Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
        Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
        Dom.setStyle(dragEl, "border", "2px solid gray");
    },

    endDrag: function(e) {

        var srcEl = this.getEl();
        var proxy = this.getDragEl();

        // Show the proxy element and animate it to the src element's location
        Dom.setStyle(proxy, "visibility", "");
        var a = new YAHOO.util.Motion( 
            proxy, { 
                points: { 
                    to: Dom.getXY(srcEl)
                }
            }, 
            0.2, 
            YAHOO.util.Easing.easeOut 
        )
        var proxyid = proxy.id;
        var thisid = this.id;

        // Hide the proxy and show the source element when finished with the animation
		// also empty the proxy, otherwise we have double id's!
        a.onComplete.subscribe(function() {
                Dom.setStyle(proxyid, "visibility", "hidden");
                Dom.setStyle(thisid, "visibility", "");
				proxy.innerHTML = "";
            });
        a.animate();
    },

    onDragDrop: function(e, id) {

        // If there is one drop interaction, the li was dropped either on the list,
        // or it was dropped on the current location of the source element.
        if (DDM.interactionInfo.drop.length === 1) {

            // The position of the cursor at the time of the drop (YAHOO.util.Point)
            var pt = DDM.interactionInfo.point; 

            // The region occupied by the source element at the time of the drop
            var region = DDM.interactionInfo.sourceRegion; 

            // Check to see if we are over the source element's location.  We will
            // append to the bottom of the list once we are sure it was a drop in
            // the negative space (the area of the list without any list items)
            if (!region.intersect(pt)) {
                var destEl = Dom.get(id);
                var destDD = DDM.getDDById(id);
                destEl.appendChild(this.getEl());
                destDD.isEmpty = false;
                DDM.refreshCache();
            }
        }
			
		this.owner.createFormElements();
    },

    onDrag: function(e) {

        // Keep track of the direction of the drag for use during onDragOver
        var y = 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 = Dom.get(id);

        // We are only concerned with list items, we ignore the dragover
        // notifications for the list.
        if (destEl.nodeName.toLowerCase() == "li") {
            var orig_p = srcEl.parentNode;
            var p = destEl.parentNode;

            if (this.goingUp) {
                p.insertBefore(srcEl, destEl); // insert above
            } else {
                p.insertBefore(srcEl, destEl.nextSibling); // insert below
            }

            DDM.refreshCache();
        }
    }
});
