﻿var Position;
var DocPosition;
var PostDocPosition;
var IE;
var constTimer;
var timer;
var ZIndex;
var dragok;
var y, x, d, dy, dx;

//alert("Document.body.clientWidth = "+document.body.clientWidth
//+"\n\nDocument.body.clientHeight = "+ document.body.clientHeight);
//+"\n\nDocument.body.offsetWidth = "+document.body.offsetWidth
//+"\n\nDocument.body.offsetHeight = "+document.body.offsetHeight);
//+"\n\nWindow.innerWidth = "+window.innerWidth
//+"\n\nWindow.innerHeight = "+window.innerHeight
//+"\n\nWindow.outerWidth = "+window.outerWidth
//+"\n\nWindow.outerHeight = "+window.outerHeight);

initEvents();

function XY(x, y) {
	this.X=x;
	this.Y=y;
	this.Set=function(x, y) { this.X=x; this.Y=y; };
	this.ToString=function() { alert([this.X,this.Y].join(' ')); };
}
function initEvents() {
	Position		=new XY(0,0);
	DocPosition		=new XY(0,0);
	PostDocPosition	=null;
	IE				=document.all ? true : false;
	constTimer		=10000;
	timer			=10000;
	ZIndex			=10000;
	dragok			=false;

	if (!IE) {
		document.captureEvents(Event.MOUSEMOVE);
	}

/*
	if (IE) {
		document.onclick		=RunClickAnchorAction;
		document.onmouseover	=RunMouseOverAnchorAction;
		document.onmouseout		=RunMouseOutAnchorAction;
	}
	else {
		window.onclick			=RunClickAnchorAction;
		window.onmouseover		=RunMouseOverAnchorAction;
		window.onmouseout		=RunMouseOutAnchorAction;
	}
*/
	if (window.opera && $$('Q')) {
		//Stop Opera selecting anything whilst dragging.
		var q=document.createElement('input');
		q.type='hidden';
		q.id='Q';
		q.value=' ';
	}

	document.onmousedown = down;
	document.onmouseup = up;
	document.onmousemove = SetXY;
}
function move(e){
//	alert('move');
	if (!e) e = window.event;
	if (dragok){
		if (IE) {
//			d.style.left = Math.max(Math.min(document.body.clientWidth-d.offsetWidth,dx + e.clientX - x), 0) + "px";
//			d.style.top  = Math.max(dy + e.clientY - y, 0) + "px";
			d.style.left = Math.max(dx + e.clientX - x, 0) + "px";
			d.style.top  = Math.max(dy + e.clientY - y, 0) + "px";
//			window.status=[dx,document.body.clientWidth,d.offsetWidth].join(' ');
		}
		else {
			d.style.left = Math.max(Math.min(window.innerWidth-d.offsetWidth,dx + e.clientX - x), 0) + "px";
			d.style.top  = Math.max(dy + e.clientY - y, 0) + "px";
//			window.status=[dx,window.innerWidth,d.style.left].join(' ');
		}
		return false;
	}
}
function down(e) {
//	alert('down');
	try {
		if (!e) e = window.event;
		var obj= (typeof e.target != "undefined") ? e.target : e.srcElement;
//		if (ParentClassNameContains(obj,'PopUp')==true) {
		if (obj.parentNode.className=='PopUp') {
			if (window.opera) $$('Q').focus();
			dragok = true;
			obj.parentNode.style.zIndex = ZIndex++;
			d = obj.parentNode;
			dx = parseInt(obj.parentNode.style.left+0);
			dy = parseInt(obj.parentNode.style.top+0);
			x = e.clientX;
			y = e.clientY;
			document.onmousemove = move;
			return false;
		}
	} catch(e) { 
//		alert(e); 
	}
}
function up() {
//	alert('up');
	dragok = false;
	document.onmousemove = SetXY;
}
function SetXY(e) {
//	alert('SetXY');
	if (IE) {
		try {
			Position.Set(event.clientX + document.body.scrollLeft, event.clientY + document.body.scrollTop);
			DocPosition.Set(document.documentElement.scrollLeft, document.documentElement.scrollTop);
		} catch(e) { ; }
//		if (document.documentElement && !document.documentElement.scrollTop)
//		// IE6 +4.01 but no scrolling going on
//		else if (document.documentElement && document.documentElement.scrollTop)
//		// IE6 +4.01 and user has scrolled
//		else if (document.body && document.body.scrollTop)
//		// IE5 or DTD 3.2
	} else {
		Position.Set(e.pageX, e.pageY);
		DocPosition.Set(window.pageXOffset, window.pageYOffset);
	}  
	// catch possible negative values in NS4
	if (Position.X < 0){Position.X=0;}
	if (Position.Y < 0){Position.Y=0;}  
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY
	return(true)
};
function ParentClassNameContains(obj,className) {
	var out=false;
	if (obj.className) {
		out=(obj.className==className) ? true : false;
	}
	if (!out) {
		if (obj.parentNode!=null) {
			out=ParentClassNameContains(obj.parentNode,className);
		}
	}
	return(out);
}
function UnloadObject(obj) {
	obj=null;
}

/*
function RunClickAnchorAction(e){
	try {
		if (!e) e = window.event;
		var el = (typeof e.target != "undefined") ? e.target : e.srcElement;
		if (el.tagName=='A') {
			if (el.innerHTML.toLowerCase().indexOf('close')>-1) {
				HidePopUp(el);
			}
			else if (el.id.length>0) {
				//alert([el.id,el.className,el.title,el.innerHTML,el.onclick].join('\n'));
				if (el.className=='Book') {
					//no action
				}
				else if (el.parentNode.id=='Occurrences') {
					GoToID(el.id,el,true);
				}
				else {
					ShowPopUpTermDetails(el);
				}
			}
		}
	} catch(e) { alert(['RunClickAnchorAction',e].join('\n')); }
}
function RunMouseOverAnchorAction(e) {
	try {
		if (!e) e = window.event;
		var el = (typeof e.target != "undefined") ? e.target : e.srcElement;
		if (el.tagName=='A') {
			el.style.textDecoration='underline';
			if (HelpText=='on' && el.className.match(/[vbnadrcpxti]/)) SetHelpText(el);
		}
	} catch(e) { alert(new Array('RunMouseOverAnchorAction',e).join('\n')); }
}
function RunMouseOutAnchorAction(e) {
	try {
		if (!e) e = window.event;
		var el = (typeof e.target != "undefined") ? e.target : e.srcElement;
		if (el.tagName=='A') {
			el.style.textDecoration='none';
		}
	} catch(e) { alert(new Array('RunMouseOutAnchorAction',e).join('\n')); }
}
*/
