
var ie = (document.all) ? true : false;
var ns = (document.getElementById && !ie) ? true : false;

if (ns) {
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = setCoords;
}

var ns_y;
var ns_x;
var visibleLayer;

function createLayerObjects(LayerId)
{
	var LayerObject;

	if (ie) {
		LayerObject = document.all[LayerId];
	} else {
		LayerObject = document.getElementById(LayerId);
	}

	return LayerObject;
}

function setCoords(Ereignis)
{
	ns_y = Ereignis.pageY;
	ns_x = Ereignis.pageX;
}

function getEventVerticalPosition(Ereignis)
{
	if(ie) {
		var scrolling;

		if (document.documentElement && document.documentElement.scrollTop) {
			scrolling = document.documentElement.scrollTop;
		} else if (document.body) {
			scrolling = document.body.scrollTop;
		}

		y = window.event.clientY + scrolling;
	} else if (ns) {
		y = ns_y;
	}

	return y;
}

function getEventHorizontalPosition(Ereignis)
{
	if (ie) {
		var scrolling;

		if (document.documentElement && document.documentElement.scrollLeft) {
			scrolling = document.documentElement.scrollLeft;
		} else if (document.body) {
			scrolling = document.body.scrollLeft;
		}

		x = window.event.clientX + scrolling;
	} else if (ns) {
		x = ns_x;
	}

	return x;
}

function show(id) {
	if (ns) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.display = "block";
			//document.getElementById(id).style.visibility = "visible";
		}
	} else if (ie) {
		if (document.all[id]) {
			document.all[id].style.display = "block";
//			document.all[id].style.visibility = "visible";
		}
	}
}

function hide(id)
{
	if (ns) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.display = "none";
//			document.getElementById(id).style.visibility = "hidden";
		}
	} else if (ie) {
		if(document.all[id]) {
			document.all[id].style.display = "none";
//			document.all[id].style.visibility = "hidden";
		}
	}
}

function showDetails(LayerId)
{
	if (visibleLayer) {
		hide(visibleLayer);
	}

	var currentLayer = createLayerObjects(LayerId);
	var parentCoords = new Object();
	parentCoords["top"]  = 0;
	parentCoords["left"] = 0;
	parentCoords = getParentsAbsCoords(currentLayer, parentCoords, LayerId);

	var top  = (getEventVerticalPosition() - parentCoords["top"] + 18);
	var left = (getEventHorizontalPosition() - parentCoords["left"] - 200);


	currentLayer.style.position = "absolute";
	currentLayer.style.top  = getEdgeAdjustmentTop(currentLayer, top) + "px";
	currentLayer.style.left = getEdgeAdjustmentLeft(currentLayer, left)  + "px";
	show(LayerId);
	visibleLayer = LayerId;
}

function getEdgeAdjustmentLeft(layer, leftpos)
{
	var innerwidth;

	if (ns) {
		innerwidth 	= window.innerWidth - 10;
	} else if (ie) {
		innerwidth 	= document.body.offsetWidth - 10;
	}

	if ((leftpos + layer.offsetWidth) >= innerwidth) {
		leftpos = leftpos - layer.offsetWidth - 10;
	}

	return leftpos;
}

function getEdgeAdjustmentTop(layer, toppos)
{
	var innerheight;

	if (ns) {
		innerheight = window.innerHeight - 10;
	} else if (ie) {
		innerheight = document.body.offsetHeight - 10;
	}

	if ((toppos + layer.offsetHeight) >= innerheight) {
		toppos = toppos - layer.offsetHeight + 10;
	}

	return toppos;

}

function getParentsAbsCoords(LayerObject, coords, LayerId)
{
	if (LayerObject.offsetParent) {

		if (LayerObject.id != LayerId) {
			coords["top"]  += parseInt(LayerObject.offsetTop);
			coords["left"] += parseInt(LayerObject.parentNode.offsetLeft);
		}

		return getParentsAbsCoords(LayerObject.offsetParent, coords, LayerId);
	} else {
		return coords;
	}
}


function popup(url,w,h,scrolling){
	
	print_options = true;	
	
	if (!w || !h){ 
		print_options = false;
	}
	
	if (!scrolling){ 
		scrolling='yes';
	}
	
	var generatornum=Math.random();
	var maxnumber=10000;
	var randomnum=Math.round(generatornum*maxnumber);
	var name="popup_" + randomnum;
	
	var win=null;
	LeftPosition=(screen.width)?(screen.width-w)/2:10;
	TopPosition=(screen.height)?(screen.height-h)/2:10;
	ns4=(document.layers)? true:false;
	if (print_options) options='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrolling+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable='+scrolling;
	else options = '';
	if (ns4){
		if (print_options) options='width='+w+',height='+h+',screenY='+TopPosition+',screenX='+LeftPosition+',scrollbars='+scrolling+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable='+scrolling;
		else options = '';
	}
	win=window.open(url,name,options);
	
	
	win.focus();
	
}

/**
 * Open the url in a new window. This way we do not need to write a target=_blank.
 * 
 * @param url
 * @return nothing
 */
function openInNewWindow(url) {
	// Change "_blank" to something like "newWindow" to load all links in the same new window
    var newWindow = window.open(url, '_blank');
    newWindow.focus();
}

