function eleId(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id);
	}
	return false;
}

function focusOn(id) {
	if (eleId(id)) {
		eleId(id).focus();
	}
}

function redirectTo(url, openInNewWindow) {
	if (openInNewWindow == true) {
		window.open(url);
	} else {
		document.location.href = url;
	}
}

function trck(ele, s) {
	if (document.images){
		s = (isNaN(parseInt(s)) ? 'k=' + s : 'n=' + s);
		u = (ele ? escape(ele.href) : 'false');
        (new Image()).src = '/tc.php?' + s + '&u=' + u;
    }
    return true;
}

function writeHTML(text, id) {
	if (document.getElementById) {
		x = eleId(id);
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
	}
}


/* Zoekbalk
------------------------------------------------------------------*/
function expSearch(lower) {
	searchExp = eleId('expand');
	if (searchExp) {
		var exp = eleId('exp');
		var btnSearchFast = eleId('btnSearchFast');
		var btnSearchExp = eleId('btnSearchExp');
		var btnExpandBar = eleId('btnExpandBar');

		if (searchExp.style.display == 'none') {
			trck(false, 'exp-out');
			searchExp.style.display = '';
			btnSearchFast.style.display = 'none';
			exp.value = '1';
			
			if (btnSearchExp) {
				btnSearchExp.style.display = '';
			}
			if (btnExpandBar) {
				btnExpandBar.innerHTML = 'Weniger Suchoptionen';
			}
		} else {
			trck(false, 'exp-in');
			searchExp.style.display = 'none';
			btnSearchFast.style.display = '';
			exp.value = '0';
			
			if (btnSearchExp) {
				btnSearchExp.style.display = 'none';
			}
			if (btnExpandBar) {
				btnExpandBar.innerHTML = 'Mehr Suchoptionen';
			}
		}
		eleId('q').focus();
		
		return false;
	}
	
	return true;
}

function locSelect(ele) {
	if (ele.id == 'pv' && eleId('pv').selectedIndex > 0) {
		eleId('pcd').selectedIndex = 0;
	} else if (eleId('pcd').selectedIndex > 0) {
		eleId('pv').selectedIndex = 0;
	}
}


/* Afbeelding
------------------------------------------------------------------*/
function imgShrink(ele, usewidth, useheight, checksize) {
	if (usewidth == 0) {
		usewidth = 64;
	}
	if (useheight == 0) {
		useheight = 64;
	}
	
	if (ele.width > usewidth) {
		ele.height = ((usewidth / ele.width) * ele.height); 
		ele.width = usewidth; 
	}
	
	if (ele.height > useheight) {
		ele.width = ((useheight / ele.height) * ele.width); 
		ele.height = useheight; 
	}
	
	if (!ele.height || !ele.width || ele.height > useheight || ele.width > usewidth) {
		ele.width = usewidth;
		ele.height = useheight;
	}
	
	if (checksize) {
		var checkImage = new Image();
		checkImage.src = ele.src;
			
		if (checkImage.width < ele.width) {
			ele.width = checkImage.width;
		}
		if (checkImage.height < ele.height) {
			ele.height = checkImage.height;
		}
	}
}

function imgNo(ele, type, zoom) {
	if (type == 'small') {
		ele.src = noImgSmall;
		ele.style.width = '64px';
		ele.style.height = '64px';
	} else {
		ele.src = noImgMedium;
		ele.style.width = '150px';
		ele.style.height = '150px';
	}
	
	if (zoom && eleId(zoom)) {
		eleId(zoom).style.display = 'none';
	}
}

function hover(ele) {
	if (!ele.onmouseout) {
		ele.onmouseout = new Function('this.className = \'' + ele.className + '\';');
	}
	ele.className = ele.className + ' hover';
}

function showMode(ele) {
	trck(ele, ele.id);
	if (eleId('mode-list')) {
		eleId('mode-list').className = (ele.id == 'mode-list' ? 'selected' : '');
	}
	if (eleId('mode-gallery')) {
		eleId('mode-gallery').className = (ele.id == 'mode-gallery' ? 'selected' : '');
	}
	
	tableElements = eleId('results').getElementsByTagName('table');
	for (i = 0; i < tableElements.length; i++) {
		table = tableElements[i];
		if (table.className.indexOf('gallery') >= 0) {
			table.style.display = (ele.id == 'mode-gallery' ? '' : 'none');
		} else if (table.className.indexOf('list') >= 0) {
			table.style.display = (ele.id == 'mode-list' ? '' : 'none');
		} else if (table.className.indexOf('summary') >= 0) {
			table.style.display = (ele.id == 'mode-list' ? '' : 'none');
		}
	}
	
	eleId('dpm').value = (ele.id == 'mode-gallery' ? 'gallery' : 'list');
	
	return false;
}

function nav(ele) {
	if (eleId('dpm')) {
		url = ele.href;
		url = url.replace("&dpm=list", "");
		url = url.replace("&dpm=gallery", "");
		ele.href = url + '&dpm=' + eleId('dpm').value;
	}
}


/* Google Maps
------------------------------------------------------------------*/
var GMFrame = false;
var GMDiv = false;
var GMEle = false;
var GMIcon = false;
function GMInit() {
	if (GBrowserIsCompatible() && !GMEle) {
		// Google Maps
		GMDiv = document.createElement('div');
			with (GMDiv.style) {
			height = '390px';
			width = '390px';
			margin = '5px';
		}
		
		// Frame rond Google Maps
		GMFrame = document.createElement('div');
		with (GMFrame.style) {
			height = '420px';
			width = '400px';
			border = 'solid 1px #b5b5b5';
			padding = '0';
			backgroundColor = '#ffffff';
			position = 'absolute';
			top = '0px';
			left = '50%';
			marginLeft = '-206px'; // 400 /2 = 200 + 5 padding + 1 border
			textAlign = 'center';
		}
		GMFrame.appendChild(GMDiv);
		
		// Sluit venster onder Google Maps
		var GMClose = document.createElement("a");
		GMClose.href = '#';
		GMClose.onclick = new Function('GMHide(); return false;');
		GMClose.innerHTML = 'Schluss Fenster';
		with (GMClose.style) {
			lineHeight = '20px';
			height = '20px';
			width = '100%';
			display = 'block';
			color = '#ffffff';
			backgroundColor = '#000000';
		}
		
		GMFrame.appendChild(GMClose);
		document.body.appendChild(GMFrame);
		
		GMEle = new GMap2(GMDiv);
		
		GMEle.enableDoubleClickZoom();
		GMEle.addControl(new GLargeMapControl());
		GMEle.addControl(new GMapTypeControl());
		GMEle.setCenter(new GLatLng(52.156160556, 5.087638889), 7);
		GMEle.setZoom(10);
			
		document.body.setAttribute('onunload', 'GUnload();');
				
		GMIcon = new GIcon();
		GMIcon.image = "/images/icon/gm_minired.png";
		GMIcon.shadow = "/images/icon/gm_minishadow.png";
		GMIcon.iconSize = new GSize(12, 20);
		GMIcon.shadowSize = new GSize(22, 20);
		GMIcon.iconAnchor = new GPoint(6, 20);
		GMIcon.infoWindowAnchor = new GPoint(5, 1);
		window.onscroll = GMCenter;
	}
	GMFrame.style.display = '';
}
function GMShow(lat, lng) {
	GMInit();
	GMCenter();
	trck(false, 'location');
	GMEle.clearOverlays();
	GMEle.addOverlay(new GMarker(new GLatLng(lat, lng), GMIcon));
	GMEle.setCenter(new GLatLng(lat, lng));
}
function GMHide() {
	if (GMFrame) {
		GMFrame.style.display = 'none';
	}
}
function GMCenter() {
	if (GMFrame && GMFrame.style.display == '') {
		fs = getFrameSize(2000, 2000);
		GMFrame.style.top = (fs.sT + 210) + 'px';
	}
}


/* FrameSize
------------------------------------------------------------------*/
function getFrameSize(dW, dH) {
	// Credits: http://www.quirksmode.org/
	doc = document;
	
	// Scrollbar
	var x,y;
	if (self.pageYOffset) {
		// all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (doc.documentElement && doc.documentElement.scrollTop) {
		// Explorer 6 Strict
		x = doc.documentElement.scrollLeft;
		y = doc.documentElement.scrollTop;
	} else if (doc.body) {
		// all other Explorers
		x = doc.body.scrollLeft;
		y = doc.body.scrollTop;
	}
	
	// Page frame size
	if (self.innerWidth) {
		// all except Explorer
		fWidth = self.innerWidth;
		fHeight = self.innerHeight;
	} else if (doc.documentElement && doc.documentElement.clientWidth) {
		// Explorer 6 Strict
		fWidth = doc.documentElement.clientWidth;
		fHeight = doc.documentElement.clientHeight;
	} else if (document.body) {
		// all other Explorers
		fWidth = doc.body.clientWidth;
		fHeight = doc.body.clientHeight;
	}
	
	if (fWidth && fHeight) {
		return {fW: fWidth, sW: fWidth + x, fH: fHeight, sH: fHeight + y, sL: x, sT: y};
	}
	return {fW: dW, sW: dW + x, fH: dH, sH: dH + y, sL: x, sT: y};
}


/* Picture view
------------------------------------------------------------------*/
function PicView() {
	this.pointerX = false;
	this.pointerY = false;
	this.allow = (document.images ? true : false);
	
	this.div = false;
	this.img = false;
	this.src = false;
	
	this.ele = false;
	this.hide = false;
	
	this.frame = false;
}
PicView.prototype.show = function(e, src, hide) {
	this.src = src;
	this.hide = hide;
	
	if (this.allow) {
		if (!e) var e = window.event;
		this.pointerX = e.clientX;
		this.pointerY = e.clientY;
		
		this.ele = (e.srcElement ? e.srcElement : e.target);
		if (!this.ele.onmouseout) {
			this.ele.onmouseout = function() { pV.div.style.display = 'none'; };
		}
	
		if (!this.div) {
			// Afbeelding frame
			this.div = document.createElement('div');
			with (this.div.style) {
				position = 'absolute';
				backgroundColor = 'white';
				border = 'solid 1px #d5d5d5';
				padding = '5px';
			}
						
			document.body.appendChild(this.div);
		}
		
		// Browser frame grootte
		this.frame = getFrameSize(2000, 2000);
	
		// Afbeelding frame tonen
		with (this.div.style) {
			top = (this.pointerY + this.frame.sT) + 'px';
			left = (this.pointerX + 15 + this.frame.sL) + 'px';
			display = '';
		}
		
		this.div.innerHTML = 'Das Bild wirt geladen...';
		this.img = document.createElement('img');
		this.img.onload = function() { pV.loaded();	}
		this.img.onerror = function() { pV.error(); }
		this.img.src = this.src;
	}
}
PicView.prototype.loaded = function() {
	// Maximale afbeelding grote
	imgShrink(this.img, 500, 500);
	
	// Overschrijding browser frame verticaal
	picHeight = (this.img.height + 12);
	picBottom = (picHeight + (this.pointerY + this.frame.sT));
	if (picBottom > this.frame.sH) {
		newTop = (this.pointerY + this.frame.sT);
		newTop -= (picBottom - this.frame.sH);
		this.div.style.top = (newTop - 24) + 'px';
	}
	
	// Overschrijding browser frame horizontaal
	if (this.pointerX > 540) {
		picWidth = (this.img.width + 32);
		picRight = (picWidth + (this.pointerX + this.frame.sL));
		if (picRight > this.frame.sW) {
			newLeft = (this.pointerX + this.frame.sL);
			newLeft -= picWidth;
			this.div.style.left = (newLeft - 15) + 'px';
		}
	}

	while (this.div.hasChildNodes()) {
		this.div.removeChild(this.div.firstChild);
	}
	this.div.appendChild(this.img);
}
PicView.prototype.error = function() {
	this.div.innerHTML = 'Das Bild is nicht gefunden...';
	if (this.hide) {
		this.ele.style.display = 'none';
	}
}

pV = new PicView();