/* dragdrop.js	v. 3.1
The latest version is available at http://www.walterzorn.com/
Copyright (c) 2002 Walter Zorn. All rights reserved.
Created 26. 8. 2002 by Walter Zorn <walter@kreuzotter.de>
ActiveWin 08. 1. 2003 by Daniel Subat <daniel.subat@cocoyam.de>
*/
function ERR()
{
	return true;
}
function DDClass(t)
{
	this.u = "undefined";
	this.db = (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body? document.body : null;
	this.n = navigator.userAgent.toLowerCase();
	this.op = (window.opera && document.getElementById)? true : false;
	this.op6 = this.op && parseInt(this.n.substring(this.n.indexOf("opera")+6))<7;
	this.ie = this.n.indexOf("msie")!=-1 && document.all && this.db && !this.op;
	this.iemac = this.ie && this.n.indexOf("mac")!=-1;
	this.ie4 = this.ie && !document.getElementById;
	this.n4 = document.layers? true : false;
	this.n6 = (this.n6i=this.n.indexOf("gecko"))!=-1 || this.n.indexOf("galeon")!=-1;
	this.n7 = this.n6 && parseInt(this.n.substring(this.n6i+6, this.n6i+12))>200209;
	this.kq = (this.kqi = this.n.indexOf("konqueror"))>-1 && parseInt(this.n.substring(this.kqi+10))>2;
		this.kq_new = this.kq && this.n.substring(this.kqi+10, this.kqi+13)!="3.0";
	this.ce = document.captureEvents && document.releaseEvents;
	this.element = new Array();
	this.defzIndex = 1;
	this.Int = new Function('x','y',
		'return isNaN(y = parseInt(x))? 0 : y;'
	);
	this.setEvtHdl = new Function('i',
		'if (dd.op6) WINSZ(2);'+
		'if (dd.kq) setTimeout("dd.recalc()", 50);'+
		'if (dd.n4 || dd.n6 && !dd.n7) for (var z = 0; z<dd.element.length; z++)'+
		'{'+
			'if ((i = dd.element[z]).img)'+
			'{'+
				'(i = dd.element[z]).resizeTo(i.w+2, i.h+2);'+
				'i.resizeTo(i.w-2, i.h-2);'+
			'}'+
		'}'+
		'if (dd.loadFunc) dd.loadFunc();'+
		'dd.downFunc = document.onmousedown || null;'+
		'dd.moveFunc = document.onmousemove || null;'+
		'dd.upFunc = document.onmouseup || null;'+
		'if (dd.ce) document.captureEvents(Event.MOUSEDOWN);'+
		'document.onmousedown = PICK;'
	);
	this.evt = new Function('e','t',
		'this.but = (e = e || window.event).which || e.button;'+
		'this.src = e.target || e.srcElement;'+
		'this.x = dd.Int(e.pageX || e.clientX || 0)+dd.Int((dd.ie || dd.kq_new)? dd.db.scrollLeft : 0);'+
		'this.y = dd.Int(e.pageY || e.clientY || 0)+dd.Int((dd.ie || dd.kq_new)? dd.db.scrollTop : 0);'
	);
	this.recalc = new Function(
		'var i, z = 0; while (z<dd.element.length)'+
		'{'+
			'if ((i = dd.element[z++]).img)'+
			'{'+
				'if (Math.abs(i.x-i.defx)<=10 && Math.abs(i.y-i.defy)<=10)'+
				'{'+
					'i.getImg(i.imgname);'+
					'i.moveTo(i.defx, i.defy);'+
				'}'+
				'else i.getImg(i.imgname);'+
			'}'+
			'else i.getWH();'+
		'}'
	);
}
var dd = new DDClass();

dd.c = 'cUrsOR:';

// Optional commands on the html-page:
var NO_DRAG		   = 'N0d4Ag';
var NODRAG		   = NO_DRAG;
var RESET_ZINDEX   = 'r35E7z1Nd';
var NO_ALT		   = 'n01a1T8t';
var NOALT		   = NO_ALT;
var CLONE		   = 'C10onE';

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

function DDObj(i)
{
	this.getCmd = new Function('cmd',
		'var i = this.id.indexOf(cmd),'+
		'y = (i > -1);'+
		'if (y) this.id = this.id.substring(0, i) + this.id.substring(i + cmd.length);'+
		'return y;'
	);
	this.getDiv = new Function('i',
		'if (dd.n4) return document[i] || null;'+
		'if (dd.ie) return document.all[i] || null;'+
		'else return document.getElementById(i) || null;'
	);
	this.getImg = new Function('i','d',
		'this.defx = 0;'+
		'this.defy = 0;'+
		'd = d || window.document;'+
		'if (d.layers)'+
		'{'+
			'if (d.images[i])'+
			'{'+
				'this.defx += d.images[i].x;'+
				'this.defy += d.images[i].y;'+
				'return d.images[i];'+
			'}'+
			'for (var z = 0; z<d.layers.length; z++)'+
			'{'+
				'var y = this.getImg(i, d.layers[z].document);'+
				'if (y)'+
				'{'+
					'this.getDefXY(d.layers[z]);'+
					'return y;'+
				'}'+
			'}'+
			'return null;'+
		'}'+
		'else'+
		'{'+
			'var x, y = ((x = d.images[i]) && x.name==i)? x : null;'+
			'if (y) this.getDefXY(y);'+
			'return y;'+
		'}'
	);
	this.getDefXY = new Function('i',
		'if (dd.n4)'+
		'{'+
			'this.defx += i.left;'+
			'this.defy += i.top;'+
		'}'+
		'else while (i)'+
		'{'+
			'this.defx += dd.Int(i.offsetLeft);'+
			'this.defy += dd.Int(i.offsetTop);'+
			'i = i.offsetParent;'+
		'}'+
		'if (dd.iemac) this.defy += dd.Int(dd.db.currentStyle.marginTop) + dd.Int(dd.db.currentStyle.paddingTop);'
	);
	this.getWH = new Function(
		'this.w = dd.Int('+
			'dd.n4? this.div.clip.width'+
			': this.css.pixelWidth? this.css.pixelWidth'+
			': this.div.offsetWidth'+
		');'+
		'this.h = dd.Int('+
			'dd.n4? this.div.clip.height'+
			': this.css.pixelHeight? this.css.pixelHeight'+
			': this.div.offsetHeight'+
		');'
	);
	this.resetXY = new Function(
		'this.x = this.defx;'+
		'this.y = this.defy;'
	);
	this.activate = new Function(
		'(this.css || this.div).zIndex = this.zIndex = (++dd.zIndex);'
	);
	this.moveTo = new Function('x','y','i',
		'if (this.div)'+
		'{'+
			'this.x = x;'+
			'this.y = y;'+
			'(i = this.css || this.div).left = this.x + this.px;'+
			'i.top = this.y + this.px;'+
		'}'
	);
	this.deactivate = new Function(
		'if (this.res_zi)'+
			'(this.css || this.div).zIndex = this.zIndex = this.defzIndex;'
	);
	this.show = new Function(
		'if (!this.div) return;'+
		'(this.css ||  this.div).visibility = "visible";'+
		'this.visible = true;'
	);
	this.hide = new Function(
		'if (!this.div) return;'+
		'(this.css || this.div).visibility = "hidden";'+
		'this.visible = false;'+
		'DROP();'
	);
	this.resizeTo = new Function('w','h','i',
		'if (!this.div) return;'+
		'if (dd.n4)'+
		'{'+
			'this.div.clip.width = w;'+
			'this.div.clip.height = h;'+
			'if (this.img)'+
			'{'+
				'with (this.div.document)'+
				'{'+
					'open();'+
					'write(\'<img src="\' + this.orig.src + \'" width="\' + w + \'" height="\' + h + \'">\');'+
					'close();'+
					'this.div.document.images[0].src = this.orig.src;'+
				'}'+
			'}'+
		'}'+
		'else if (typeof this.css.pixelWidth != dd.u)'+
		'{'+
			'this.css.pixelWidth = w;'+
			'this.css.pixelHeight = h;'+
			'if ((dd.op6 || dd.kq) && this.img)'+
			'{'+
				'(i = eval(\'document.images["\' + (this.id+"img") + \'"].style;\')).pixelWidth = w;'+
				'i.pixelHeight = h;'+
			'}'+
		'}'+
		'else'+
		'{'+
			'this.css.width = w + this.px;'+
			'this.css.height = h + this.px;'+
			'if ((dd.n6 && !dd.n7 || dd.kq) && this.img) eval(\'document.images["\' + (this.id+"img") + \'"];\').src = this.img.src;'+
		'}'+
		'if (this.css && typeof this.css.overflow != dd.u) this.css.overflow = "hidden";'+		
		'this.getWH();'
	);
	this.id = i;
	
	this.nodrag = this.getCmd(NODRAG);
	this.res_zi = this.getCmd(RESET_ZINDEX);
	this.noalt = this.getCmd(NOALT);
	this.cln = this.getCmd(CLONE);
	
	this.px = (dd.op6 || dd.n4)? '' : 'px';
	this.img = this.getImg(this.id);
	if (this.img)
	{
		this.imgname = this.id;
		this.id += 'div';
		this.w = dd.Int(this.img.width);
		this.h = dd.Int(this.img.height);
		if (dd.n4)
		{
			this.orig = new Image();
			this.orig.src = this.img.src;
		}
		this.htm = '<div id="' + this.id +
			'" style="position:absolute;'+
			'z-index:' + dd.defzIndex + ';'+
			'left:' + this.defx + 'px;'+
			'top:' + this.defy + 'px;'+
			'width:' + this.w + 'px;'+
			'height:' + this.h + 'px;">'+
			'<img name="' + (this.id+'img') + '"'+
			' src="' + this.img.src + '" '+
			'alt="' + ((dd.noalt || this.noalt)? '' : (this.img.alt || '')) + '" '+
			((dd.n4 || dd.op6 || dd.kq)? ('width="' + this.w + '" height="' + this.h + '"')
				: 'width="180" height="180"')+
			'><\/div>\n';
	}
	else
	{
		this.div = this.getDiv(this.id);
		if (!this.div) return;
		if (this.div.style)	this.css = this.div.style;
		this.getDefXY(this.div);
		this.getWH();
	}
	
	this.resetXY();
}

function WINSZ(x)
{
	if (x==1 || x==2)
	{
		if (dd.n4 || dd.op6 && x==2)
		{
			dd.iW = innerWidth;
			dd.iH = innerHeight;
			if (dd.op6) setTimeout("WINSZ()", 2<<8);
		}
		window.onresize = WINSZ;
	}
	else if ((dd.n4 || dd.op6) && (innerWidth!=dd.iW || innerHeight!=dd.iH))
		location.reload();
	else if (dd.op6) setTimeout("WINSZ()", 2<<8);
	else setTimeout('dd.recalc()', 5<<1);
}
WINSZ(1);

var activeObj = null;

function PICK(ev)
{
	var e = new dd.evt(ev);
	var i, cmp = -1, z = 0;


	
	while (z<dd.element.length)
	{
		i = dd.element[z++];
		if (dd.n4 && e.but>1 && e.src==i.img && !i.cln) return false;
		if (e.but<=1)
		{
			if (i.nodrag || !i.visible);
			else if (e.x>i.x && e.x<i.x+i.w && e.y>i.y && e.y<i.y+i.h)
			{
				if (i.zIndex>cmp)
				{
					cmp = i.zIndex;
					dd.obj = i;
				}
			}
		}
	}
	if (dd.obj)
	{
		if (dd.ce) document.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		document.onmousemove = DRAG;
		document.onmouseup = DROP;
		dd.obj.activate();

		/*
		// activate window: change color
		if (activeObj != null)
		{	
			winInactive(activeObj);	
		}
		
		winActive(dd.obj);
		*/
		activeObj = dd.obj;
	
		dd.prex = e.x;
		dd.prey = e.y;
		return !(dd.n4 && dd.obj.img || (dd.n6 || dd.kq) && (i = e.src.toString().toLowerCase()).indexOf('inputelement')==-1 && i.indexOf('textarea')==-1);
	}
	if (dd.downFunc) dd.downFunc(ev);
	return true;
}

function DRAG(e)
{
if (dd.obj)
{
	if (dd.ie4 || dd.kq || dd.n6 && !dd.n7)
	{
		if (dd.wait) return false;
		dd.wait = true;
		setTimeout('dd.wait = false;', 5);
	}

	dd.obj.moveTo(dd.obj.x + (e = new dd.evt(e)).x - dd.prex, dd.obj.y + e.y - dd.prey);
	
	dd.prex = e.x;
	dd.prey = e.y;
}
	return false;
}

function DROP()
{
	if (dd.obj)
	{
		if (!dd.obj.img) dd.obj.getWH();
		if (dd.ce && !dd.moveFunc && !dd.upFunc) document.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		dd.obj.deactivate();
		dd.obj = null;
		document.onmousemove = dd.moveFunc;
		document.onmouseup = dd.upFunc;
	}
}

function SET_DRAGGABLE()
{

	if (!(dd && (dd.n4 || dd.n6 || dd.ie || dd.op || dd.kq))) return;
	for (var a = SET_DRAGGABLE.arguments, htm = '', i, z = 0; z<a.length; z++)
	{
		// intra-loop delay for opera < 7
		if (dd.op6)
		{
			var t0 = (new Date()).getTime();
			while ((new Date()).getTime() < t0+120);
		}
		// global cursor cmd
		if (a[z].indexOf(dd.c)==0) dd.curs = a[z];
		// global no-alt cmd
		else if (a[z]==NO_ALT) dd.noalt = 1;
		else
		{
			i = new DDObj(a[z]);
			
			if (i.img || i.div)
			{
				dd.element[dd.element.length] = dd.element[i.imgname || i.id] = i;
				htm += i.htm || '';
			}
		}
	}
	if (dd.element.length)
	{
	
	_wel = document.createElement( "div" );
	_wel.className = "sdas";
	_wel.style.width = "110px"; 
	_wel.style.left = "0px";
	_wel.style.top = "0px";
	_wel.style.visibility = 'visible';
	_wel.innerHTML = '<div style="position:absolute;"><\/div>\n' + htm;
//	document.body.insertBefore( _wel,document.getElementById('test').parentNode.firstChild );
	
		dd.zIndex = Math.max(100, dd.element.length);
		z = 0; while (z<dd.element.length)
		{
			if ((i = dd.element[z++]).img)
			{
				i.div = i.getDiv(i.id);
				if (i.div.style) i.css = i.div.style;
				if (!i.cln)
				{
					if (dd.n4) i.img.src = self.location.href;
					else i.img.style.visibility = 'hidden';
				}
			}
			// set cursor
			if (i.css && dd.curs && !i.nodrag) i.css.cursor = dd.curs.substring(dd.c.length);

			// save initial value if z-index of element is to be restored after drag-end
			if (i.res_zi)
			{
				if (document.defaultView && typeof document.defaultView.getComputedStyle != dd.u)
					i.defzIndex = document.defaultView.getComputedStyle(i.div, '').getPropertyValue('z-index');
				else if (i.div.currentStyle)
					i.defzIndex = i.div.currentStyle.zIndex;
				else
					i.defzIndex = dd.Int((i.css || i.div).zIndex);
			}
			i.zIndex = (i.res_zi)? i.defzIndex : 1;
			
			// initial visibility of item is true
			i.visible = true;
		}
		if (window.onload) dd.loadFunc = window.onload;
		window.onload = dd.setEvtHdl;
	}
}

function getWinObj(element)
{
	lftObj = element.div.getElementsByTagName("div").item(0);
	mid0Obj = element.div.getElementsByTagName("div").item(1);
	mid1Obj = element.div.getElementsByTagName("div").item(2);
	rgtObj = ns6?element.div.getElementsByTagName("div").item(3):element.div.getElementsByTagName("div").item(5);
}
	
function winActive(element)
{
	getWinObj(element);
	lftObj.style.background='url(img/tab/sel-left.gif) no-repeat';
	mid0Obj.style.background='url(img/tab/sel-mid.gif) repeat';
	mid1Obj.style.background='url(img/tab/sel-mid.gif) repeat';
	rgtObj.style.background='url(img/tab/sel-right.gif) no-repeat';			
}

function winInactive(element)
{
	getWinObj(element);
	lftObj.style.background='url(img/tab/bg-left.gif) no-repeat';
	mid0Obj.style.background='url(img/tab/bg-mid.gif) repeat';
	mid1Obj.style.background='url(img/tab/bg-mid.gif) repeat';
	rgtObj.style.background='url(img/tab/bg-right.gif) no-repeat';			
}

dd.d = document; // for back-compat to versions prior to v. 2.72
