// array with ads
var adcount = 0;
var ads = new Array();

// Speichert das erste Keyword
var gKeyword = getKw();

// write code and register
function setad(adid, adanchor, width, height, float) {
	var ad = new Object();
	ad.adid = adid;
	ad.adanchor = adanchor;
	ad.width = width;
	ad.height = height;
	ad.float = float;
	ads[adcount] = ad;
	adcount++;
}
// get anchor positions
function getAnchorPosition(ad) {
	var coordinates = new Object();
	var x = 0,y = 0;
	var use_gebi = false, use_css = false, use_layers = false;
	if (document.getElementById) { 
		use_gebi = true; 
	} else if (document.all) { 
		use_css = true; 
	} else if (document.layers) { 
		use_layers = true; 
	}

 	if (use_gebi) {
		var anchorObject = top.document.getElementById(ad.adanchor);
		x = AnchorPosition_getPageOffsetLeft(anchorObject);
		y = AnchorPosition_getPageOffsetTop(anchorObject);
   } else if (document.all) {
		x = AnchorPosition_getPageOffsetLeft(document.all[ad.adanchor]);
  		y = AnchorPosition_getPageOffsetTop(document.all[ad.adanchor]);
   } else if (use_css) {
		x = AnchorPosition_getPageOffsetLeft(document.all[ad.adanchor]);
		y = AnchorPosition_getPageOffsetTop(document.all[ad.adanchor]);
	} else if (use_layers) {
		var found = 0;
		for (var i = 0; i<document.anchors.length; i++) {
			if (document.anchors[i].name == ad.adanchor) { 
				found=1; break; 
			}
		}
	
		if (found == 0) { 
			coordinates.x = 0; coordinates.y = 0; return coordinates;
		}
		x = document.anchors[i].x;
		y = document.anchors[i].y;
	} else {
		coordinates.x = 0; coordinates.y = 0; return coordinates;
	}
	coordinates.x = x;
	coordinates.y = y;
	return coordinates;
}
function AnchorPosition_getPageOffsetLeft (el) {
	try { 
		var ol = el.offsetLeft;
		while ((el = el.offsetParent) != null) { 
			ol += el.offsetLeft; 
		}
		return ol;
	} catch(err) { }
}
	
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}


function AnchorPosition_getPageOffsetTop (el) {
	try {
		var ot = el.offsetTop;
		while((el = el.offsetParent) != null) { 
			ot += el.offsetTop; 
		}
		return ot;
	} catch(err) { }
}
	
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}
// activate registered ads
function activateads() {
	
	if (typeof(skipads) != "undefined" && skipads == true) {
		return;
	}
	
	for (x in ads) {
		
		var ad = ads[x];
		var anchorObject = top.document.getElementById(ad.adanchor);
		var adContainerObject = top.document.getElementById(ad.adid);
		if (anchorObject) {
			
			if (ad.height != null && ad.height != 0) {
				anchorObject.style.height = ad.height + 'px';
			}
			if (ad.width != null && ad.width != 0) {
				anchorObject.style.width = ad.width + 'px';
			}

			adContainerObject.style.display = 'block';
			adContainerObject.style.visibility = 'hidden';
			anchorObject.style.display = 'block';

			if (ad.height == null || ad.height == 0) {
				anchorObject.style.height = adContainerObject.offsetHeight + 'px';
			}
			if (ad.width == null || ad.width == 0) {
				anchorObject.style.width = adContainerObject.offsetWidth + 'px';
			}
			
			anchorObject.style.cssFloat = ad.float;
			anchorObject.style.styleFloat = ad.float;
			
			var c = getAnchorPosition(ad);
			adContainerObject.style.left = c.x + 'px';
			adContainerObject.style.top = c.y + 'px';
			adContainerObject.style.visibility = 'visible';						
		}

	}
}

/*
 * Extrahiert das erste Keyword der Google Suchabfrage.
 */
function getKw()
{
	// return-value
	var returnKw = "";
	// Referrer sichern
	var ref = document.referrer;

	// Pruefen ob Referrer von Google
	if(ref.indexOf("http://www.google.") >= 0)
	{
		// Inhalt des Parameters "q" holen
		var regEx = /&q=([^&]*)/i;
		// In q[1] wird der Inhalt des Parameters gespeichert
		var q = regEx.exec(ref);
		// Array aller Keywords erzeugen
		var keywords = q[1].split("+");
		
		var i;
		var len = keywords.length;
		for(i = 0; i < len; ++i)
		{
			var found = keywords[i].search(/^[^-].*/i);
			if (found != -1)
			{	
				keywords[i]=keywords[i].replace(/%2B/,"");
				break;
			}
		}
		
		// Das erste Keyword sichern
		returnKw = keywords[i];
	}
	
	return returnKw;
}
