function DragAndDropSrcPanel(panelDescription, direction, parent)
{
	this.description = panelDescription;
	this.direction = direction;
   	this.dndpanel = parent;
   	this.div;
   	this.isIE;
   	this.top;
   	this.left;
   	this.width;
   	this.height;
	
	DragAndDropSrcPanel.prototype.createNewSrc = function()
	{
		this.InitEvents();
		var newdiv  = window.parent.ContentFrame.document.getElementById(this.description);
		newdiv.type = "dndsrc";
		newdiv.style.zIndex = 1000;
		this.top = parseInt(getStyle(this.description, "top"))
		this.left = parseInt(getStyle(this.description, "left"))
		this.width = parseInt(getStyle(this.description, "width"))
		this.height = parseInt(getStyle(this.description, "height"))
		newdiv.parent = this.dndpanel.getDiv();
		newdiv.dndpanel = this.dndpanel;
		newdiv.srcobj = this;
		this.div = newdiv;
		if(document.all)
		{
			newdiv.isIE = true;
			this.isIE = true;
		}
	}
	
	DragAndDropSrcPanel.prototype.InitEvents = function() 
	{
		window.parent.ContentFrame.document.onmousedown = window.parent.dragHandler;
		window.parent.ContentFrame.document.onmouseup = window.parent.releaseHandler;
	}	
	
	DragAndDropSrcPanel.prototype.getWidth = function()
	{
		return this.width
	}
	
	DragAndDropSrcPanel.prototype.getHeight = function()
	{
		return this.height
	}
	
	DragAndDropSrcPanel.prototype.getDescription = function()
	{
		return this.description
	}
	
	DragAndDropSrcPanel.prototype.getTop = function()
	{
		return this.top
	}	
	
	DragAndDropSrcPanel.prototype.getLeft = function()
	{
		return this.left
	}
	
	DragAndDropSrcPanel.prototype.getDiv = function()
	{
		return this.div
	}
	
	DragAndDropSrcPanel.prototype.getCenterX = function()
	{
		var left = new Number(this.div.style.left.substring(0, this.div.style.left.length-2));
		return (left+this.getWidth()/2);	
	}
	
	DragAndDropSrcPanel.prototype.getCenterY = function()
	{
		var top = new Number(this.div.style.top.substring(0, this.div.style.top.length-2));
		return (top+this.getHeight()/2);	
	}
	
	DragAndDropSrcPanel.prototype.restoreOriginalLocation = function()
	{
		this.getDiv().style.top = this.getTop()+'px';
		this.getDiv().style.left = this.getLeft()+'px';	
	}
	
	DragAndDropSrcPanel.prototype.getVerticalOnly= function()
	{
		if(this.direction == "2")
			return true;
	}
	
	DragAndDropSrcPanel.prototype.getHorizontalOnly= function()
	{
		if(this.direction == "1")
			return true;
	}	
	
	this.createNewSrc();
}

var offsetX = 0;
var offsetY = 0;
var srcdiv = null;

function dragHandler(event)
{
      var htype = '-moz-grabbing';
      if (event == null) 
      {
	event = window.parent.ContentFrame.event; 
	htype = 'move';
      }
      var target = event.target != null ? event.target : event.srcElement;
      target = target.parentNode;
      if(target != null)
      {
	 var IE = target.isIE;
	 if (((IE && event.button <= 1) || (!IE && event.button == 0)) && target.type == 'dndsrc')
	 {
		 target.style.cursor=htype;
		 offsetX = event.clientX-parseInt(getStyle(target.srcobj.getDescription(), "left"));
		 //alert("offsetX = " + offsetX);
		 offsetY = event.clientY-parseInt(getStyle(target.srcobj.getDescription(), "top"));
		 //alert("offsetY = " + offsetY);
		 srcdiv = target;

	         window.parent.ContentFrame.document.onmousemove = window.parent.moveHandler;
        
		 // cancel out any text selections
		 if(IE)
		 {
		 	document.body.focus();

		 	// prevent text selection in IE
		 	document.onselectstart = function () { return false; };
		 	// prevent IE from trying to drag an image
		 	target.ondragstart = function() { return false; };
		 }else
		 {
		   	// prevent text selection (except IE)
		 	return false;		 
		 }
	 }
      }else
      	alert("dragHandler target == null");
}

function releaseHandler()
{
    if (srcdiv != null)
    {
    	var dndpanObj = srcdiv.dndpanel;
	var targets = dndpanObj.getTargetList();
	if(targets != null)
	{
		var targetAccepted = false;
		for(var x=0; x<targets.length; x++)
		{
			var target = targets[x];
			var go = true;
			if(target.acceptSource(srcdiv.srcobj))
			{    			
				targetAccepted = true;
				if(dndpanObj.snapBack())
				{
					var sList = target.getSourceList();
					var correct = sList[0];
					if(correct != srcdiv.srcobj.getDescription())
					{
						targetAccepted = false;
						go = false;
					}
				}
				target.highlightBorder(srcdiv)
				if(go)
				{
					if(dndpanObj.snapSourceToTarget())
						target.snapToCenter(srcdiv.srcobj);
					target.currentsrc = srcdiv.srcobj;
				}
			}else if(target.currentsrc == srcdiv.srcobj)
			{
				target.currentsrc = null;				
				target.highlightBorder(srcdiv, "#000000")
			}
			
		}
		if(!targetAccepted)
			srcdiv.srcobj.restoreOriginalLocation();
	}
	window.parent.ContentFrame.document.onmousemove = null;
	window.parent.ContentFrame.document.onselectstart = null;
	srcdiv.ondragstart = null;
	srcdiv.style.cursor = "default";
	srcdiv = null;
    }
}

function moveHandler(event)
{
	if (event == null) 
		event = window.parent.ContentFrame.event;  
	if(srcdiv != null)
	{
		var dndpanObj = srcdiv.dndpanel;
		var dndsrcobj = srcdiv.srcobj;
		var parenttop = parseInt(getStyle(dndpanObj.getDescription(), "top"))
		var parentleft = parseInt(getStyle(dndpanObj.getDescription(), "left"))
		var parentwidth = parseInt(getStyle(dndpanObj.getDescription(), "width"))
		var parentheight = parseInt(getStyle(dndpanObj.getDescription(), "height"))
		if((dndpanObj.getVerticalOnly() || dndsrcobj.getVerticalOnly()) && event.clientY > parenttop && event.clientY < (parenttop+parentheight))
			srcdiv.style.top = event.clientY-offsetY+'px';
		else if((dndpanObj.getHorizontalOnly() || dndsrcobj.getHorizontalOnly()) && event.clientX > parentleft && event.clientX < (parentleft+parentwidth))
			srcdiv.style.left = event.clientX-offsetX+'px';
		else if(event.clientX > parentleft && event.clientX < (parentleft+parentwidth) &&
			event.clientY > parenttop && event.clientY < (parenttop+parentheight))
		{
			srcdiv.style.left = event.clientX-offsetX+'px';
			srcdiv.style.top = event.clientY-offsetY+'px';
		}
		return false;
	}
}

function reset(event)
{
      if (event == null) 
      	event = window.parent.ContentFrame.event; 
      var target = event.target != null ? event.target : event.srcElement;
      target = target.parentNode;
      if(target != null)
      {
      	var id = target.id;
      	while(id.length == 0)
      	{
      		target = target.parentNode;	
      		if(target == null)
      			break;
      		else
      			id = target.id
      	}
      	if(id.length > 0)
		target.dndpanel.reset();
      }
}

function answerDND()
{
	answerQuestion("", "DNDAnswer");
}