// JavaScript Document

/************************************************
 * Popup generieren         										*
 ************************************************/
document.write('<div id="popup" style="margin:0;padding:0;"></div>');
var elm = new Object();
var elmLeft = 0, elmTop = 0, elmWidth = 0, elmHeight = 0;

var docEl = ( typeof document.compatMode != "undefined" && document.compatMode != "BackCompat" )? "documentElement" : "body";
 
/************************************************
 * Mausbewegung initialisieren									*
 ************************************************/
function init_mousePosition() {
    if(document.layers) document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove =	get_mousePosition;
}
 
function get_mousePosition(e) {
 
	// Position, wo der Maus sich befindet
	var xPos =  e? e.pageX : window.event.clientX;
	var yPos =  e? e.pageY : window.event.clientY;
 
	// für IE mit Scroll-Position
	if (document.all && !document.captureEvents) {
		xPos += document[docEl].scrollLeft; 
		yPos += document[docEl].scrollTop;
	}
	// Maus außerhalb des Image und Popup wird Popup geschloßen
	if (getObjekt("object")) {
		var popup = getObjekt("object");
		if ( (xPos < elmLeft || xPos > elmLeft + elmWidth || yPos < elmTop || yPos > elmTop + elmHeight ) &&
				 (xPos < popup.offsetLeft || xPos > popup.offsetLeft + popup.offsetWidth || yPos < popup.offsetTop || yPos > popup.offsetTop + popup.offsetHeight) ){
			
			getObjekt("popup").innerHTML = "";
			
			elmLeft = 0;
			elmTop = 0;
			elmWidth = 0;
			elmHeight = 0;
			//getObjekt("text").innerHTML = "Left: " + xPos + " Top: " + yPos + "<br>eL: " + elmLeft + " eW: " + elmWidth + "<br>eT: " + elmTop + " eH: " + elmHeight + "<br>pL: " + popup.offsetLeft + " pW: " + popup.offsetWidth + "<br>pT: " + popup.offsetTop + " pH: " + popup.offsetHeight;
		}
	}
	if (document.layers) routeEvent(e);
}

/************************************************
 * Objekt initialisieren								        *
 ************************************************/
function getObjekt(objID) {
	if (document.getElementById)	return document.getElementById(objID);
	else if (document.all) 				return document.all[objID]; 
	else if (document.layers) 		return document.layers[objID];
}

/************************************************
 * Setz Left-Position des Popup-Fensters        *
 ************************************************/
function setLeft(obj) {
	var curleft = obj.offsetWidth/2;
	elmWidth = obj.offsetWidth;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			if (obj.offsetLeft > 0) {elmLeft = obj.offsetLeft; }
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

/************************************************
 * Setz Top-Position des Popup-Fensters         *
 ************************************************/
function setTop(obj) {
	//var browserWidth  = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
	var browserHeight = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
	var curtop = Math.round(obj.offsetHeight/2);
	elmHeight = obj.offsetHeight;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			if (obj.offsetTop > 0) elmTop = obj.offsetTop;
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	if (curtop + 271 > browserHeight + document[docEl].scrollTop) curtop -= curtop + 271 - browserHeight - document[docEl].scrollTop; // 271 ist die Höhe des Popupfenster
	return curtop;
}

/************************************************
 * Popup-Fenster                              	*
 ************************************************/
function getWindow(logo, id, image, descr, link, info_link) {
	var info = new Array();
	info['image'] = 'img/'+image;
	info['logo'] = 'img/'+logo;
	info['text'] = descr; 
	info['href-1'] = link;
	info['href-1-text'] = link.replace(/http:\/\//,'');
	if (info['href-1-text'].length > 18) info['href-1-text'] = info['href-1-text'].substring(0, 18) + '...';
	info['href-2'] = info_link;

	var output;
	output  = '<table id="object" style="display:none;z-index:1000;" onclick="schliessen()">' +
						'<tr id="head-line"><td class="head-line-td" id="ff">Infobox</td></tr>' +
						'<tr valign=top>' +
						'<td class="td-left" valign=top>' +
						'<img src="' + info['image'] + '" />' +
						'</td>' +
						'<td class="td-right"' +
						'<div id="logo"><img src="' + info['logo'] + '" /></div>' +
						'<div id="text">' + info['text'] + '</div>' +
						'</td>' +
						'</tr>' +
						'<tr>' +
						'<td class="td-left"  id="href-left">' +
						'<a href="' + info['href-1'] + '" target="_blank" title="' + info['href-1'] + 
								'" onclick="getHttpRequest(' + id + ', 1)" >' + info['href-1-text'] + '</a>' +
						'</td>' +
						'<td class="td-right"  id="href-right">' +
						'<a href="' + info['href-2'] + '" target="_blank" title="' + info['href-2'] +
								'" onclick="getHttpRequest(' + id + ', 1)" >weitere Infos...</a>' +
						'</td>' +
						'</tr>' +
						'</table>';

	return output;
}


/************************************************
 * Popup-Fenster anzeigen                      	*
 ************************************************/
function oeffnen(elm, id, logo, image, descr, link, info_link) {
	// elm: zum positionieren
	//elm = elem;
	getObjekt("popup").innerHTML 	= getWindow(logo, id, image, descr, link, info_link);
	getObjekt('object').style.left = parseInt(setLeft(elm)) + "px";
	getObjekt('object').style.top 	= parseInt(setTop(elm)) + "px";
	getObjekt('object').style.display = '';
	getHttpRequest(id, 0);
}

/************************************************
 * Popup-Fenster schliessen                    	*
 ************************************************/
function schliessen() {
	getObjekt("popup").innerHTML = '';
}


/************************************************
 * HttpRequest zum Klickzählen                  *
 ************************************************/
function getHttpRequest(id, klick) {
   
	var xmlhttp = null;
	// Mozilla
	if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
 
	xmlhttp.open("GET", "countclick.php?id="+id+'&action='+klick, true);
	/*xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//alert(xmlhttp.responseText);
		}
	}*/
	xmlhttp.send(null);
} 