// JavaScript Document
// Author: gordon.bank@ragbit.de

var pictures = document.images;
var anquors = document.links;
var popupView;

var close_delay = 500;

if (pictures) for(i=0; i < pictures.length; i++) {
	if (pictures[i].popup = pictures[i].getAttribute("popup")) {
		initPic(pictures[i]);
	}
}

if (anquors) for(i=0; i < anquors.length; i++) {
	if (anquors[i].popup = anquors[i].getAttribute("popup")) {
	  anquors[i].popup = anquors[i].href;
	  anquors[i].href = "javascript:void(0)";
	  anquors[i].target = "";
		initLink(anquors[i]);
	}
}

function initPic(p) {
	var tmp_img, tmp_frame, sw, sh, sx, sy, x, y;
	p.setAttribute("title","Click to enlarge");
	if (p.style) {
		p.style.cursor = "pointer"; //(isOpera)? "pointer" : (isIE)? "url(images/cursors/wagtail.ani), pointer" : "pointer";
		/*
		p.style.border = "1px solid #0094A8";
		p.onmouseover = function() { this.tmp_border = this.style.border; this.style.border = "1px solid #0094A8"; }
		p.onmouseout = function() { this.style.border = this.tmp_border; }
		*/
	}
	p.onclick = function(event) {
		var e = (window.event)? window.event : event;

		if (popupView) {
			popupView.close();
		}
		
		tmp_img = document.createElement("IMG");
		tmp_img.setAttribute("style","margin:5px; cursor:pointer");
	  tmp_img.setAttribute("title","Click to close");
		tmp_img.src = this.popup;
		popupsize = getPopupSize(this.getAttribute("popw"),this.getAttribute("poph"));
		w = (popupsize[0])? popupsize[0] : this.width;
		h = (popupsize[1])? popupsize[1] : this.height;
		while (!w) { w = this.width; }
		while (!h) { h = this.height; }
		sw = e.clientX;
		sh = e.clientY;
		sx = getScrollPos(self)[0];
		sy = getScrollPos(self)[1];
		x = Math.abs(parseInt(sw - (w/2) + sx));
		y = Math.abs(parseInt(sh - (h/2) + sy));
		//alert('popup url='+this.popup+', w='+popupsize[0]+', h='+popupsize[1]+', ws='+popupsize[3]+', hs='+popupsize[4]);
		//alert('y = Math.abs(parseInt(sh - (h/2) + sy))\n'+y+' = Math.abs(parseInt('+sh+' - ('+h+'/2) + '+sy+'))');
		tmp_frame = document.createElement("DIV");
		tmp_frame.appendChild(tmp_img);

		popupView = document.body.appendChild(tmp_frame);
		if (popupView) {
			if (isIE) {
				popupView.style.position = "absolute";
				popupView.style.zIndex   = 10;
				popupView.style.left     = x+"px";
				popupView.style.top      = y+"px";
				popupView.style.backgroundColor = "white";
				popupView.style.border   = "1px solid black";
				popupView.style.width    = "auto";
				popupView.style.padding  = 5+"px";
				popupView.style.float    = "left";
			}
			else {
				popupView.setAttribute("style","position:absolute; left:"+x+"px; top:"+y+"px; z-index:10; background-color:white; border:1px solid black; width:auto; margin:auto; float:left");
			}
			popupView.close = function() {
				if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
			  popupView = removeObj(this);
			}
			popupView.onmouseover = function() {
				if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
			}
			popupView.onmouseout = function() {
				var closeObj = this;
				this.mouseoutTimer = window.setTimeout(function() {	closeObj.close(); },close_delay);
			}
/*
*/
			popupView.onclick = function() {
        this.close();
			}
		}
	}
}

function initLink(a) {
	var tmp_img, tmp_frame, sw, sh, sx, sy, x, y, i;
	//a.setAttribute("title","Click to enlarge");
	a.onclick = function() {
		//var e = (window.event)? window.event : event;

		while (popupView) {
			popupView.close();
		}
		
		tmp_img = document.createElement("IMG");
		tmp_img.src = this.popup;
		if (isIE) {
			tmp_img.style.margin = 5+"px";
			tmp_img.style.cursor = "pointer";
		}
		else {
			tmp_img.setAttribute("style","margin:5px; cursor:pointer");
			tmp_img.setAttribute("title","Click to close");
		}
		popupsize = getPopupSize(this.getAttribute("popw"),this.getAttribute("poph"));
		w = (popupsize[0])? popupsize[0] : (tmp_img.width)? tmp_img.width : 0;
		h = (popupsize[1])? popupsize[1] : (tmp_img.height)? tmp_img.height : 0;
		sw = getOffset(this)[1];
		sh = getOffset(this)[0];
		sx = getScrollPos(self)[0];
		sy = getScrollPos(self)[1];
		x = sw;
		y = sh - h - 10 - ((isIE)? this.offsetHeight : 0);
		//alert('popup url='+this.popup+', w='+popupsize[0]+', h='+popupsize[1]+', ws='+popupsize[3]+', hs='+popupsize[4]);
		//alert('y = Math.abs(parseInt(sh - (h/2) + sy))\n'+y+' = Math.abs(parseInt('+sh+' - ('+h+'/2) + '+sy+'))');
		tmp_frame = document.createElement("DIV");
		tmp_frame.img = tmp_frame.appendChild(tmp_img);

		popupView = document.body.appendChild(tmp_frame);
		if (popupView) {
			if (isIE) {
				popupView.style.position = "absolute";
				popupView.style.zIndex   = 10;
				popupView.style.left     = x+"px";
				popupView.style.top      = y+"px";
				popupView.style.backgroundColor = "white";
				popupView.style.border   = "1px solid black";
				popupView.style.width    = "auto";
				popupView.style.padding  = 5+"px";
				popupView.style.float    = "left";
				i = 0;
				while((i < 32) && ((popupView.img.width + popupView.img.height)==0)) {
					window.setTimeout(void(0),1000);
					i++;
				}
				popupView.img.title = popupView.img.src+', ('+popupView.img.width+' x '+popupView.img.height+')'+'\015\012Complete:'+popupView.img.complete+'\015\012Click to close';
			}
			else {
				popupView.setAttribute("style","position:absolute; left:"+x+"px; top:"+y+"px; z-index:10; background-color:white; border:1px solid black; width:auto; margin:auto; float:left");
			}
			popupView.close = function() {
				if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
			  popupView = removeObj(this);
			}
			popupView.onmouseover = function() {
				if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
			}
			popupView.onmouseout = function() {
				var closeObj = this;
				this.mouseoutTimer = window.setTimeout(function() {	closeObj.close(); },close_delay);
			}

			this.onmouseover = function() {
				if (popupView && popupView.mouseoutTimer) window.clearTimeout(popupView.mouseoutTimer);
			}
			this.onmouseout = function() {
				var closeObj = popupView;
				if (popupView) popupView.mouseoutTimer = window.setTimeout(function() {	closeObj.close(); this.onmouseover = function() {}; this.onmouseout = function() {}; },1000);
			}
/*
*/
			popupView.onclick = function() {
        this.close();
			}
		}
		return true;
	}
}

function popupRel(event,linkobj,dx,dy) {

	var e = (window.event)? window.event : event;
	var url;
	var x = y = 0;
	var w, h, align;

	if (arguments.length > 4) {
		w = arguments[4];
		h = arguments[5];
	}

	if (arguments.length > 6) {
		align = arguments[6];
	}

	if (align) {
		pos = getOffset(linkobj);
		x = pos[1];
		y = pos[0];
	}
	
	if (x && y) {
		// void(0);
	}
	else if (e) {
		x = e.clientX;
		y = e.clientY;
	}

	while (popupView) {
			popupView.close();
	}
		
	if (linkobj.tmp_url) {
  		url = linkobj.tmp_url;
	}
	else {
		url=linkobj.href;
		linkobj.tmp_url=linkobj.href;
		linkobj.href="javascript:void(0)";
		linkobj.target="";
	}

	
	linkobj.setAttribute("title","Click to enlarge");

	tmp_img = document.createElement("IMG");
	tmp_img.src = url;
	if (isIE) {
		tmp_img.style.margin = 5+"px";
		tmp_img.style.cursor = "pointer";
	}
	else {
		tmp_img.setAttribute("style","margin:5px; cursor:pointer");
		tmp_img.setAttribute("title","Click to close");
	}

	ofsx = getOffset(this)[1];
	ofsy = getOffset(this)[0];
	sclx = getScrollPos(self)[0];
	scly = getScrollPos(self)[1];
	x = x + dx + sclx;
	y = y + dy + scly;

	tmp_frame = document.createElement("DIV");
	tmp_frame.img = tmp_frame.appendChild(tmp_img);

	popupView = document.body.appendChild(tmp_frame);
	if (popupView) {
		if (isIE) {
			popupView.style.position = "absolute";
			popupView.style.zIndex   = 10;
			popupView.style.left     = x+"px";
			popupView.style.top      = y+"px";
			popupView.style.backgroundColor = "white";
			popupView.style.border   = "1px solid black";
			popupView.style.width    = "auto";
			popupView.style.padding  = 5+"px";
			popupView.style.float    = "left";
			i = 0;
			while((i < 32) && ((popupView.img.width + popupView.img.height)==0)) {
				window.setTimeout(void(0),1000);
				i++;
			}
			popupView.img.title = popupView.img.src+', ('+popupView.img.width+'\327'+popupView.img.height+'), Click to close';
		}
		else {
			popupView.setAttribute("style","position:absolute; left:"+x+"px; top:"+y+"px; z-index:10; background-color:white; border:1px solid black; width:auto; margin:auto; float:left");
		}
		popupView.close = function() {
			if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
			popupView = removeObj(this);
		}
		popupView.onmouseover = function() {
			if (this.mouseoutTimer) window.clearTimeout(this.mouseoutTimer);
		}
		popupView.onmouseout = function() {
			var closeObj = this;
			this.mouseoutTimer = window.setTimeout(function() {	closeObj.close(); },close_delay);
		}

		linkobj.onmouseover = function() {
			if (popupView && popupView.mouseoutTimer) window.clearTimeout(popupView.mouseoutTimer);
		}
		linkobj.onmouseout = function() {
			var closeObj = popupView;
			if (popupView) popupView.mouseoutTimer = window.setTimeout(function() {	closeObj.close(); this.onmouseover = function() {}; this.onmouseout = function() {}; },1000);
		}
/*
*/
		popupView.onclick = function() {
			this.close();
		}
	}
}

function getPopupSize() {
	var w = h = 0;
	if (w = arguments[0]) w = parseInt(w);
	if (h = arguments[1]) h = parseInt(h);
	return Array(w,h);
}

window.document.onclick = function() { // close the popupView when click outside
	if (popupView && !popupView.onclick) popupView = removeObj(popupView);
}
