var DragDrop =
{
	firstContainer : null,
	lastContainer : null,
	actContainer : null,
	dropZone : null,
    
	makeListContainer : function(list)
	{
		// each container becomes a linked list node
		if (this.firstContainer == null)
		{
			this.firstContainer = this.lastContainer = list;
			list.previousContainer = null;
			list.nextContainer = null;
		}
		else
		{
			list.previousContainer = this.lastContainer;
			list.nextContainer = null;
			this.lastContainer.nextContainer = list;
			this.lastContainer = list;
		}
		
	  //list.group = group;
    	
		for (var i = 0; i < list.childNodes.length; i++)
		{
			if(list.childNodes[i].nodeType == 1)
				DragDrop.makeItemDraggable(list.childNodes[i]);
		}
		
		list.onDragDrop = function() 
		{
			//var data = DragDrop.serData();
			//document.cookie = "magadoo_links=" + data;
			//alert(data);
		};
	},

	makeItemDraggable : function(item)
	{
		Drag.makeDraggable(item);
		item.onDragStart = DragDrop.onDragStart;
		item.onDrag = DragDrop.onDrag;
		item.onDragEnd = DragDrop.onDragEnd;
		DragUtils.setAction(item);
	},

	onDragStart : function()
	{
		var container = DragDrop.firstContainer;
		
		while (container != null)
		{
			container.northwest = Coordinates.northwestOffset(container, true);
			container.southeast = Coordinates.southeastOffset(container, true);
			container = container.nextContainer;
		}
		
		//initialize the Dropzone
		dropZone = document.createElement("div");
		dropZone.style.marginBottom = "10px";
		dropZone.style.height = this.offsetHeight + "px";
		dropZone.style.width = this.offsetWidth + "px";
		dropZone.style.border = "2px dashed";
		dropZone.id = "dropzone";
		
		// item starts out over current parent
		actContainer = this.parentNode;
		//actContainer.appendChild(dropZone);
		actContainer.insertBefore(dropZone, this);
		//put drag object to body element
		document.body.appendChild(this);
	},

	onDrag : function(mouse, scrollPos)
	{
		var actPos = mouse.plus(scrollPos);
		
		if (!actPos.inside(actContainer.northwest, actContainer.southeast))
		{
			var container = DragDrop.firstContainer;
			var distanceX = 1000;
			
			while (container != null && ((mouse.x + scrollPos.x) >= container.northwest.x))
			{
				if(container != actContainer && actPos.inside(container.northwest, container.southeast))
				{
					actContainer = container;
					break;
				}
				
				actContainer = container;
				container = container.nextContainer;
			}
			
			actContainer.appendChild(dropZone);
		}

		var item = actContainer.getElementsByTagName("div")[0];
		var next = item;

		while ((next != null) && ((mouse.y + scrollPos.y) >= (actContainer.northwest.y + next.offsetTop + (next.offsetHeight / 2))))
		{
			item = next;
			next = DragUtils.nextItem(item);
		}

		actContainer.insertBefore(dropZone, next);
	},

	onDragEnd : function()
	{
		actContainer.replaceChild(this, dropZone);
      		this.style["top"] = "0px";
		this.style["left"] = "0px";
		
		try
		{
			//Try to eliminate the dropzone
			var tempZone = document.getElementById("dropzone");
			tempZone.parentNode.removeChild(tempZone);
		}
		catch(e)
		{}
		
		actContainer.onDragDrop();
	},
	
	serData : function ()
	{
	    var container = DragDrop.firstContainer;
			var j = 0;
	    var outstring = "";
	        
      while (container != null)
      {
      	/*
        if(theid != null && container.id != theid)
        {
            container = container.nextContainer;
            continue;
        }

        if(group != null && container.group != group)
        {
            container = container.nextContainer;
            continue;
        }
        */

        j++;   
               
     		if(j > 1)
        {
          outstring += ":";
        }
        
        //outstring += container.id;
        //outstring += "(";
        //var items = container.getElementsByTagName("div");
        var items = container.childNodes;
        
        if(items.length == 0)
        {
        	outstring += "-";
        }
        else
        {   
					for (var i = 0; i < items.length; i++)
					{
						if(i > 0)
							outstring += ",";
		
						outstring += items[i].id.substr(2);
					}
				}
				
	      //outstring += ")";
				container = container.nextContainer;
			}
			
		return outstring;
	}
};

var DragUtils =
{
	swap : function(container, item1, item2)
	{
		container.insertBefore(item1, item2);
	},

	nextItem : function(item)
	{
		var sibling = item.nextSibling;
		while (sibling != null)
		{
			if (sibling.nodeName == item.nodeName)
				return sibling;
			sibling = sibling.nextSibling;
		}
		return null;
	},

	previousItem : function(item)
	{
		var sibling = item.previousSibling;
		while (sibling != null)
		{
			if (sibling.nodeName == item.nodeName)
				return sibling;
			sibling = sibling.previousSibling;
		}
		return null;
	},
	
	scrollWindow : function(scrollx, scrolly)
	{
		window.scrollBy(scrollx, scrolly);
	},
	
	setAction : function (group)
	{
		var elem = group.getElementsByTagName("div")[2];
		if(elem != null)
		{
			elem.group = group;
			elem.onclick = toggle;
		}
	}
};

function toggle() 
{
	var linkbox = this.group.getElementsByTagName("div")[4];

	if(linkbox != null)
	{
		var innerTitle = this.getElementsByTagName("div")[0];
		
		if(linkbox.style.display == "none")
		{
			linkbox.style.display = "block";
			innerTitle.style.border = "";
			innerTitle.style.borderBottom = "2px solid";
		}
		else
		{
			linkbox.style.display = "none";
			innerTitle.style.borderBottom = "";
			innerTitle.style.border = "1px solid";
			innerTitle.style.borderTop = "2px solid";
		}
	}
	
	return false;
}

function toggleOutOfBounds(elem) 
{
	var linkbox = DragUtils.previousItem(elem);

	if(linkbox != null)
	{
		if(linkbox.style.display == "none")
		{
			linkbox.style.display = "block";
			elem.getElementsByTagName("img")[0].src = "pics/arr_up.gif";
		}
		else
		{
			linkbox.style.display = "none";
			elem.getElementsByTagName("img")[0].src = "pics/arr_down.gif";
		}
	}
	
	return false;
}

/*
function getSort()
{
    order = document.getElementById("order");
    order.value = DragDrop.serData();
}

function showValue()
{
    order = document.getElementById("order");
    alert(order.value);
}
*/