/*
dragdrop.js v1.59

This notice must be untouched at all times.
Copyright Walter Zorn 2002.

Drag & drop functionality even for images
which are neither positioned via layers,
nor via stylesheets or any other kind of hard-coding.

Created by Walter Zorn
walter@kreuzotter.de
26. 8. 2002
Last modified: 20. 10. 2002
*/




/*
Path to the transparent one-pixel gif or png.
Default: same directory as this script.
If you wish a different path, adapt the following value:
*/
var clearpixel='transparentpixel.gif';



///// Don't change anything below this line /////




// Optional commands on the html-page.

var CLONE         ='C10onE';

var CURSOR_DEFAULT='',
CURSOR_MOVE       ='cursor:move;',
CURSOR_CROSSHAIR  ='cursor:crosshair;',
CURSOR_POINTER    ='cursor:pointer;cursor:hand;',
CURSOR_E_RESIZE   ='cursor:e-resize;',
CURSOR_NE_RESIZE  ='cursor:ne-resize;',
CURSOR_NW_RESIZE  ='cursor:nw-resize;',
CURSOR_N_RESIZE   ='cursor:n-resize;',
CURSOR_SE_RESIZE  ='cursor:se-resize;',
CURSOR_SW_RESIZE  ='cursor:sw-resize;',
CURSOR_S_RESIZE   ='cursor:s-resize;',
CURSOR_W_RESIZE   ='cursor:w-resize;',
CURSOR_WAIT       ='cursor:wait;',
CURSOR_HELP       ='cursor:help;';


function CATCH_ERR()
{
	return true;
}
window.onerror=CATCH_ERR;


function DDClass(t)
{
	(t=this).curs=CURSOR_DEFAULT;
	t.d=document;
	t.db=(t.d.compatMode && t.d.compatMode!="BackCompat")? t.d.documentElement : t.d.body? t.d.body : null;
	t.n=navigator.userAgent.toLowerCase();
	t.osx=(t.n.indexOf("linux")!=-1);
	t.op=window.opera;
	t.ie=(t.n.indexOf("msie")!=-1 && t.d.all && t.db && !t.op);
	t.ie4=(t.ie && !t.d.getElementById);
	t.n4=t.d.layers? true : false;
	t.ce=(t.d.captureEvents && t.d.releaseEvents);
	t.n6=(t.n.indexOf("gecko")!=-1);
	t.w3cx=((t.n6 || (t.kq=t.n.indexOf("konq"))!=-1 && parseInt(t.n.substring(t.kq+10))>2) && t.osx);
	t.wait=false;
	t.img=new Array();
	t.obj=null;
	t.prex=0;
	t.prey=0;
	t.zInd=100;

	t.Int=new Function('x',
		'return (x==null)? 0 : (typeof x!="number")? parseInt(x) : x;'
	);
	
	t.init=new Function(
		'var z=0; while (z<dd.img.length)'+
			'dd.img[z++].getDiv();'+
		'if (dd.ce) dd.d.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);'+
		'dd.d.onmousedown=DDPICK;'+
		'dd.d.onmouseup=DDDROP;'
	);
	
	t.recalc=new Function(
		'var z=0; while (z<dd.img.length)'+
		'{'+
			'var i=dd.img[z++];'+
			'if (Math.abs(i.x-i.defx)<=10 && Math.abs(i.y-i.defy)<=10)'+
			'{'+
				'i.getImg();'+
				'i.setDefXY();'+
				'i.move();'+
			'}'+
			'else i.getImg();'+
		'}'
	);
	t.png=new Image(); t.png.src=clearpixel;
}
var dd=new DDClass();


function DDImg(i, t)
{
	if (!i) return;
	(t=this).cln=(i.indexOf(CLONE)>-1 && i.indexOf(CLONE)==i.length-CLONE.length)? true : false;
	if (t.cln) i=i.substring(0, i.length-CLONE.length);
	t.id=i+'div';
	t.zIndex=dd.zInd;

	t.getDiv=new Function(
		'this.div ='+
			'dd.n4? dd.d[this.id]'+
			': dd.ie? dd.d.all[this.id].style'+
			': dd.d.getElementById? dd.d.getElementById(this.id).style'+
			': null;'
	);

	t.move=new Function('t',
		'if ((t=this).div)'+
		'{'+
			't.div.left=t.x+t.px;'+
			't.div.top=t.y+t.px;'+
		'}'
	);

	t.activate=new Function('t',
		'if ((t=this).div)'+
		'{'+
			'if (dd.ie4 && t.cln) t.img.src=t.img.orig;'+
			't.div.zIndex=t.zIndex=(++dd.zInd);'+
		'}'
	);

	t.getImg=new Function('d','t',
		'(t=this).defx=0;'+
		't.defy=0;'+
		'var i=t.id.substring(0,t.id.length-3);'+
		'd=d || window.document;'+
		'if (d.layers)'+
		'{'+
			'if (d.images[i])'+
			'{'+
				't.defx+=d.images[i].x;'+
				't.defy+=d.images[i].y;'+
				'return d.images[i];'+
			'}'+
			'for (var z=0; z<d.layers.length; z++)'+
			'{'+
				'var y=t.getImg(d.layers[z].document);'+
				'if (y)'+
				'{'+
					't.defx+=d.layers[z].left;'+
					't.defy+=d.layers[z].top;'+
					'return y;'+
				'}'+
			'}'+
			'return null;'+
		'}'+
		'else'+
		'{'+
			'var y=d.images[i] || null;'+
			'if (y && typeof y.offsetLeft!="undefined" && typeof y.offsetParent!="undefined")'+
			'{'+
				'var el=y;'+
				'while (el)'+
				'{'+
					't.defx+=dd.Int(el.offsetLeft);'+
					't.defy+=dd.Int(el.offsetTop);'+
					'el=el.offsetParent;'+
				'}'+
			'}'+
			'else y=null;'+
			'return y;'+
		'}'
	);

	t.setDefXY=new Function(
		'this.x=this.defx;'+
		'this.y=this.defy;'
	);

	t.img=t.getImg();
	t.px=(dd.op || dd.n4)? '' : 'px';
	if (!t.img) return;
	t.setDefXY();
	t.img.orig=t.img.src;
	t.w=dd.Int(t.img.width);
	t.h=dd.Int(t.img.height);
	t.htm='<div id="'+t.id+
		'" style="position:absolute;left:'+t.x+'px;'+
		'top:'+t.y+'px;'+
		'width:'+t.w+'px;'+
		'height:'+t.h+'px;'+dd.curs+'"'+'>'+
		'<img src="'+t.img.src+'" alt="" '+
		((t.img.border>1 || !dd.op && t.img.border)? ('border="'+t.img.border+'" ') : '')+
		'width="'+t.w+'" height="'+t.h+'">'+
		'<\/div>\n';
	if (!t.cln || t.cln && dd.ie4) t.img.src=dd.png.src;
	if (typeof t.img.oncontextmenu!='undefined') t.img.oncontextmenu=new Function('return false;');
}


function DDRESIZ(x)
{
	if (!dd) return;
	if (x==1)
	{
		if (dd.n4)
		{
			dd.iW=innerWidth;
			dd.iH=innerHeight;
			onresize=DDRESIZ;
		}
		else if (dd.op) window.onload=DDRESIZ;
		else window.onresize=DDRESIZ;
	}
	else if ((dd.n4 || (dd.op && x==2)) && (innerWidth!=dd.iW || innerHeight!=dd.iH))
		location.reload();
	else if (dd.op)
	{
		if (!x)
		{
			dd.iW=innerWidth;
			dd.iH=innerHeight;
		}
		setTimeout("DDRESIZ(2)", 500);
	}
	else setTimeout('dd.recalc()', 20);
}
DDRESIZ(1);


function DDEvt(e, t)
{
	(t=this).but=(e=e || window.event).which || e.button;
	if (e && (dd.n6 || t.but==1 || !t.but))
	{
		t.x=dd.Int(e.pageX || e.clientX || 0)+dd.Int(dd.ie? dd.db.scrollLeft : 0);
		t.y=dd.Int(e.pageY || e.clientY || 0)+dd.Int(dd.ie? dd.db.scrollTop : 0);
		if (e.type=="mousedown")
		{
			var i, comp=-1, et=e.target, es=e.srcElement;
			for (var z=0; z<dd.img.length; z++)
			{
				i=dd.img[z];
				if (dd.n4 && t.x>i.x && t.x<i.x+i.w && t.y>i.y && t.y<i.y+i.h)
				{
					if (i.zIndex>comp)
					{
						comp=i.zIndex;
						dd.obj=i;
					}
				}
				else if (et && et.parentNode && et.parentNode.id==i.id || es && es.parentElement && es.parentElement.id==i.id) dd.obj=i;
			}
		}
	}
	return t;
}


function DDPICK(e)
{
	if (!(e=new DDEvt(e))) return true;
	if (dd.obj)
	{
		if (dd.ce) dd.d.captureEvents(Event.MOUSEMOVE);
		dd.d.onmousemove=DDDRAG;
		dd.obj.activate();
		dd.prex=e.x;
		dd.prey=e.y;
		return false;
	}
	return true;
}


function DDDRAG(e)
{
	if (!dd.obj) return true;
	if (dd.w3cx && dd.wait) return false;
	dd.obj.x+=(e=new DDEvt(e)).x-dd.prex;
	dd.obj.y+=e.y-dd.prey;
	dd.obj.move();
	dd.prex=e.x;
	dd.prey=e.y;
	if (dd.w3cx)
	{
		dd.wait=true;
		setTimeout('dd.wait=false', 5);
	}
	return false;
}


function DDDROP()
{
	if (dd.obj)
	{
		if (dd.ce) dd.d.releaseEvents(Event.MOUSEMOVE);
		dd.d.onmousemove=dd.obj=null;
	}
}


function SET_DRAGGABLE()
{
	if (!(dd && dd.png && (dd.n4 || dd.n6 || dd.ie || dd.op || dd.w3cx))) return;
	for (var z=0, a=SET_DRAGGABLE.arguments; z<a.length; z++)
	{
		if (a[z].indexOf('cursor:')==0)
		{
			dd.curs=a[z];
			break;
		}
	}
	var z=0, htm='', i; while (z<a.length)
	{
		if (dd.op && dd.osx)
		{
			dd.t0=(new Date()).getTime();
			while ((new Date()).getTime()<dd.t0+50);
		}
		if ((i=new DDImg(a[z++])).htm)
		{
			dd.img[dd.img.length]=i;
			htm+=i.htm;
		}
	}
	if (htm)
	{
		dd.d.write('<div style="position:absolute;"><\/div>\n'+htm);
		dd.init();
	}
}

