/* 
LiveEdition v. 1 dragdrop.js
Eva Kaniasty 01/06/2006

drag & drop script - I modified a basic script found online

*/

var ie=document.all;
var nn6=document.getElementById&&!document.all; 

var isdrag=false;
var x,y;
var dobj;
var nsFlag=false;


	//set Netscape flag
	if (navigator.userAgent.indexOf('Netscape')>0) 
	{
	nsFlag=true;
	}


/* bugs */

//in netscape, zoom 3+ lets you drag too far horizontally



function movemouse(e)
{
  if (isdrag)
  {
  

  
  //figure out new top/left position
  
    newLeft = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
	newTop  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
	
	//figure out whether the tileset is being dragged beyond the limits of the viewer window
	
	
	var vDiff=parseInt(tileDiv.style.height) + newTop - parseInt(tileViewer.style.height);
	
	//netscape is calculating positioning differently from IE & Firefox - add currentLeft to equation to get true position
	if (nsFlag)
	{
	
		var hDiff=parseInt(tileDiv.style.width) + newLeft + currentLeft - parseInt(tileViewer.style.width);
		
	
	}
	
	else
	
	{
		var hDiff=parseInt(tileDiv.style.width) + newLeft - parseInt(tileViewer.style.width);
	
	}

	
    //if the tileset fits in the window horizontally & vertically, move it to new position
	//set a new left position for use by zoom


	if (newLeft<0 && hDiff>0)
	{
			if (nsFlag)
			{
			dobj.style.left=(newLeft)+'px';
			currentLeft=newLeft;
			}
			else
			{
			dobj.style.left=newLeft+'px';
			currentLeft=newLeft;
			}
	

	
	}
	
	if (newTop<0 && vDiff>0)
	{
		dobj.style.top=newTop+'px';
		currentTop=newTop;
	}
   

	
	
	return false;
  }
}

function selectmouse(e) 
{
  var fobj       = nn6 ? e.target : event.srcElement;
  //var topelement = nn6 ? "HTML" : "BODY";
	var topelement =  'HTML' ;
  while (fobj.tagName != topelement && fobj.className != 'dragme')
  {

      fobj = nn6 ? fobj.parentNode : fobj.parentElement;

    }

  
  
  if (fobj.className=='dragme')
  {
    isdrag = true;
    dobj = fobj;
	
		
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);

	//alert (parseInt(dobj.style.width));
	
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }
}

document.onmousedown=selectmouse;
document.onmouseup=new Function('isdrag=false');


