// javascript popup by Wouter Hager

var dragDiv;

function attachDrag(e) {
	if(dragDiv != null) return;
	dragDiv = this.parentNode;
	var mouse = new MousePos(e);
	var myPos = new ElmPos(dragDiv);
	$("test").style.display="block";
	dragDiv.dx = myPos.x - mouse.x;
	dragDiv.dy = myPos.y - mouse.y;
	
	document.onmousemove = function(e) {
		if (!e) var e = window.event;
		// clear selections when dragging, does not work in Safari
		
		if (window.getSelection) {
			var s = window.getSelection();
			if(s.removeAllRanges) s.removeAllRanges();
		} else if(document.getSelection) {
			var s = document.getSelection();
			if(s.removeAllRanges) s.removeAllRanges();
		}
		else if (document.selection && document.selection.empty) document.selection.empty();
		
		var mouse = new MousePos(e);
		dragDiv.style.left = mouse.x+dragDiv.dx+'px';
		dragDiv.style.top = mouse.y+dragDiv.dy+'px';
		$("popupm").style.display = "inline";
	}
	document.onmouseup = function(e) {
		$("test").style.display="none";
		dragDiv = document.onmousemove = document.onmouseup = null;
	}
}
easeOut = function (t, b, c, d) {
    return -c * (t /= d) * (t - 2) + b;
};

var doMove;
function popup(name, x, y, z, w, h, text, closepop, xpic, mpic) {
	var popup = document.createElement('div');
	popup.id = name;
	popup.className = 'popup';
	popup.style.position = 'absolute';
	popup.style.left = x +'px';
	popup.style.top = y +'px';
	popup.style.zIndex = z;
	popup.style.width = w +'px';
	popup.style.height = h +'px';
	var top = document.createElement('div');
	top.className = 'popuptop';
	top.onmousedown = attachDrag;
	top.onmouseover = function(e) {
		this.className = 'popuptopover';
	}
	top.onmouseout = function(e) {
		this.className = 'popuptop';
	}

	popup.movePopTo = function(x,y) {
		var ppp = this;
		var p = new ElmPos(ppp);
		var step = 0;
		var t = 0;
		var totalD=len(x - p.x, y - p.y);
		doMove = function() {
			step = easeOut(t, 0, totalD, 10)
			var a=Math.acos((y - p.y) / totalD);
			if(step < totalD) {
				var d = Math.sin(a)*step;
				with(ppp) {
					style.left = (x - d < p.x - d) ? p.x - d +'px' : p.x + d +'px';
					style.top = p.y + (Math.cos(a)*step) +'px';
				}
				t+=0.5;
				setTimeout('doMove()', 20);
			} else {
				with(ppp) {
					style.left = x+'px';
					style.top = y+'px';
				}
			}
		}
		setTimeout('doMove()', 200);
	}
	
	var a = document.createElement('a');
	a.href = 'javascript:void(0)';
	var x = document.createElement('img');
	x.src = mpic;
	x.className = x.id = 'popupm';
	x.onmousedown = function() {
		this.style.display = "none";
		popup.movePopTo((getPageWidth()>780)? ((getPageWidth()/2)+390)-260 : getPageWidth()-260, 5);
	};
	a.appendChild(x);
	top.appendChild(a);
	
	a = document.createElement('a');
	a.href = 'javascript:void(0)';
	x = document.createElement('img');
	x.src = xpic;
	x.className = 'popupx';
	popup.close = function() {
		closepop();
		dragDiv = document.onmousemove = document.onmouseup = null;
		document.body.removeChild(popup);
	}
	x.onmousedown = function() {
		popup.close();
	};
	a.appendChild(x);
	top.appendChild(a);
	
	popup.appendChild(top);
	var span = document.createElement('span');
	span.className = 'popuphtml';
	span.id = 'popuphtml';
	span.innerHTML = text;
	popup.appendChild(span);
	document.body.appendChild(popup);
}
