
function setWindowPosition(uw, X, Y) {
	var ww = 0;
	var wh = 0;
	if (isIE()) {
		ww = document.body.clientWidth + document.body.scrollLeft;
		wh = document.body.clientHeight + document.body.scrollTop;
	} else {
		ww = document.documentElement.clientWidth + document.body.scrollLeft;
		wh = document.documentElement.clientHeight + document.body.scrollTop;
	}

	X = X - uw.offsetWidth / 2;
	if (X + uw.offsetWidth > ww - 10) {
		X = ww - uw.offsetWidth - 10;
	}
	if (Y + uw.offsetHeight > wh - 10) {
		Y = wh - uw.offsetHeight - 10;
	}
	if (X < 10) X = 10;
	if (Y < 10) Y = 10;
	uw.style.left = X;
	uw.style.top = Y;
}

function setWindowPosition2(uw, X, Y) {
	uw.style.left = X;
	uw.style.top = Y;
}

function unlock(blog, e) {
	var uw = document.getElementById('unlockWindow');
	var X = e.clientX + document.body.scrollLeft;
	var Y = e.clientY + document.body.scrollTop;
	setWindowPosition(uw, X, Y);
	document.unlockForm.blog.value = blog;
	showWindow('unlockWindow');
	document.unlockForm.pin.focus();
	return false;
}

function showWindow(id) {
	var obj = document.getElementById(id);
	obj.style.zIndex = 1000000;
	obj.style.visibility='visible';
}

function hideWindow(id) {
	var obj = document.getElementById(id);
	obj.style.zIndex = -1000000;
	obj.style.visibility='hidden';
	document.onmouseup = null;
	document.onmousedown = null;
}

var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var nograb = false;

var dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (NS passes the event)

  if (e) { 
    if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
    } else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
    }  
  }
}

function update(e) {
	getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grab(context, e) {
	update(e);
	if (nograb) {
		nograb = false;
		return;
	}
	document.onmousedown = falsefunc;
	dragobj = context;
	dragobj.style.zIndex = 10; // move it to the top
	document.onmousemove = drag;
	document.onmouseup = drop;
	document.unlockForm.pin.blur();
	grabx = mousex;
	graby = mousey;
	elex = orix = dragobj.offsetLeft;
	eley = oriy = dragobj.offsetTop;
	update();
	return true;
}

function drag(e) {
	if (dragobj) {
		elex = orix + (mousex-grabx);
		eley = oriy + (mousey-graby);
		dragobj.style.position = "absolute";
		dragobj.style.left = (elex).toString(10) + 'px';
		dragobj.style.top  = (eley).toString(10) + 'px';
	}
	update(e);
	// in IE this prevents cascading of events, so text selection is disabled
	return false;
}

function drop() {
	if (dragobj) {
		dragobj.style.zIndex = 0;
		dragobj = null;
	}
	update();
	document.onmousemove = update;
	document.onmouseup = null;
	document.onmousedown = null;   // re-enables text selection on NS
}


