	var randomImages = -1;
	var hideSwitches = -1;
	var imgUpdatesEnabled = 1;
	var index = -1;
	var arrindex = -1;
	var pics = new Array();
	var switches;
	var lang = 'de';
	var lang_long = 'deutsch';

	function InitHomepage() {
		var scripts = document.getElementsByTagName('script');
		for(var n = 0; n < scripts.length; n++) {
			var idx = scripts[n].src.indexOf('/js/startpage.js?lang=');
			if (idx >= 0) {
				var par = scripts[n].src.substr(idx + 22, 2);
				if (par == 'de' || par == 'en' || par == 'lt' || par == 'pl' || par == 'ru') {
					lang = par;
				        if (par == 'en') {
						lang_long = 'english';
					} else if (par == 'lt') {
						lang_long = 'lietuviu';
					} else if (par == 'pl') {
						lang_long = 'polski';
					} else if (par == 'ru') {
						lang_long = 'russki';
					}
				}
				break;
			}
		}

		switches = new Array(document.getElementById('switch_pause'),
				document.getElementById('switch_start'),
				document.getElementById('switch_prev'),
				document.getElementById('switch_next'),
				document.getElementById('switch_prev_dis'),
				document.getElementById('switch_show'));

		selectImg();
		startImgupdate();
		setInterval(stopImgload, 2400000);
	};

	function startImgupdate() {
		if (imgUpdatesEnabled && randomImages == -1) {
			randomImages = setInterval(selectImg, 15000);
		}
	};
	function stopImgload() {
		clearInterval(randomImages);
		randomImages = -1;
		imgUpdatesEnabled = false;
	}
	function selectImg() {
		if (index == arrindex) {
			get("/cgi-bin/bildarchiv/suche/get_random_picture.cgi?lang="+lang+"&x="+new Date().getTime(), "getRandomPicture");
		} else {
			showImage(pics[index]);
			index++;
		}
	}
	function getRandomPicture(text) {
		showImage(text);
		if (arrindex >= 1000) {	// caching max. 1000 pictures
			pics.shift();
		} else {
			index++;
			arrindex++;
		}
		pics[arrindex] = text;
	}
	function showImage(text) {
		var file = text.split("\n");	// id \n filename [\n unterschrift_in_polnisch]
		document.getElementById('randomimg').src = "http://files.bildarchiv-ostpreussen.de/files/fotoalbum/fotos/600x400/" + file[1];
		document.getElementById('ds_preselect').href="http://www.bildarchiv-ostpreussen.de/cgi-bin/bildarchiv/detailsuche/detailsuche.cgi?lang="+lang+"&suchtext=" + file[0];
		document.getElementById('randomimg').onload = function() {
			if (document.getElementById('switch_prev').style.display != 'none') {
				showSwitchrow();
			}
		}
		if (file.length > 2) {
			document.getElementById('foreign_title').innerHTML = '('+file[2]+')';
		}
		document.getElementById('foreign_title').style.display = (file.length>2?'':'none');
	}
	function getRandom(min, max) {
		return( min + parseInt( Math.random() * ( max-min+1 ) ) );
	}


	function showSwitchrow() {
		document.getElementById((imgUpdatesEnabled?'switch_pause':'switch_start')).style.display = '';
		document.getElementById((imgUpdatesEnabled?'switch_start':'switch_pause')).style.display = 'none';
		document.getElementById('switch_prev').style.display = (index > 0?'':'none');
		document.getElementById('switch_next').style.display = '';
		document.getElementById('switch_prev_dis').style.display = (index > 0?'none':'');
		document.getElementById('switch_show').style.display = '';

		var w = AbsLeft(document.getElementById('image_column1')) +
				(AbsLeft(document.getElementById('image_column2')) - AbsLeft(document.getElementById('image_column1'))) / 2;
		w -= 110;	// Breite des Balkens ca. 200 px

		document.getElementById('switch_pause').style.left = document.getElementById('switch_start').style.left =
				w + 0 * AbsLeft(document.getElementById('randomimg')) + "px";
		document.getElementById('switch_prev').style.left = document.getElementById('switch_prev_dis').style.left =
				Number(w + 0 * AbsLeft(document.getElementById('randomimg')) + 33) + "px";
		document.getElementById('switch_next').style.left = Number(w + 0 * AbsLeft(document.getElementById('randomimg')) + 33 + 36) + "px";
		document.getElementById('switch_show').style.left = Number(w + 0 * AbsLeft(document.getElementById('randomimg')) + 33 + 36 + 36) + "px";
	}
	function hideSwitchrow(opacity) {
		if (opacity < 10) {
			for(var i = 0; i < switches.length; i++) {
				switches[i].style.display = 'none';

				switches[i].style.opacity = 1;
				switches[i].style.MozOpacity = 1;
				switches[i].style.filter = "alpha(opacity=100, finishopacity=100, style=3)";
			}
		} else {
			for(var i = 0; i < switches.length; i++) {
				switches[i].style.opacity = opacity/100;
				switches[i].style.MozOpacity = opacity/100;
				switches[i].style.filter = "alpha(opacity=" + opacity + ", finishopacity=" + opacity + ", style=3)";
			}
			opacity -= 20;
			hideSwitches = setTimeout("hideSwitchrow(" + opacity + ")", 50);
		}
	}
	function startHideSwitchrow() {
		hideSwitches = setTimeout("hideSwitchrow(100)", 1400);
	}
	function toggleUpdatemode(enableUpdates) {
		imgUpdatesEnabled = enableUpdates;
		if (imgUpdatesEnabled) {
			document.getElementById('switch_pause').style.display = '';
			document.getElementById('switch_start').style.display = 'none';
		} else {
			document.getElementById('switch_pause').style.display = 'none';
			document.getElementById('switch_start').style.display = '';
		}
		if (imgUpdatesEnabled) {
			if (index < arrindex) { index++; }
			selectImg();
			startImgupdate();
		} else {
			stopImgload();
		}
	}

	function showPrevimg() {
		if (index == 0) { return; }
		index--;
		toggleUpdatemode(0);
		showImage(pics[index]);
	}
	function showNextimg() {
		if (index == arrindex) {
			selectImg();
			return;
		}
		index++;
		toggleUpdatemode(0);
		showImage(pics[index]);
	}
	function mouseOverSwitch(el) {
		clearTimeout(hideSwitches);
		showSwitchrow();
//		window.clearInterval(randomImages);
//		hideSwitches=randomImages=-1;
		hideSwitches=-1;
		el.style.backgroundColor = '#efefef';
	}
	function mouseOutSwitch(el) {
		startHideSwitchrow();
//		startImgupdate();
		el.style.backgroundColor = '#ffffff';
	}
	function popupImg() {
		var id = pics[index].split("\n")[0];
		popup('/cgi-bin/bildarchiv/suche/show_foto.cgi?lang='+lang_long+'&id='+id,800,600);
	}

	function AbsLeft(el) {
		if (el==null) { return 0; }
		return (el.offsetParent)? el.offsetLeft+AbsLeft(el.offsetParent) : el.offsetLeft;
	}
	function AbsTop(el) {
		if (el == null) { return 0; }
		return (el.offsetParent)? el.offsetTop+AbsTop(el.offsetParent) : el.offsetTop;
	}
	function NumberFromPixel(val) {
		if (val.length < 2) {
			return 0;
		}
		return Number(val.substr(0, val.length - 2));
	}

	//////////////////////////////////////////////////////////////////////
	// AJAX und Callback-Funktionen fuer AJAX:
	//////////////////////////////////////////////////////////////////////
	// Aus: http://board.gulli.com/thread/801675-php-script-aus-einem-javascript-aufrufen
	// Soll AJAX sein...:
	function get(url, callback_function, return_xml){
		var http_request = false;

		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml; charset=utf-8');
			}
		} else if(window.ActiveXObject) {
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!http_request) {
			alert("<<text_error_no_ajax>>");
			return false;
		}
		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					if (return_xml) {
						eval(callback_function + '(http_request.responseXML)');
					} else {
						eval(callback_function + '(http_request.responseText)');
					}
				} else {
//					alert('Problem, http request Status: ' + http_request.status);
				}
			}
		};
		http_request.open('GET', url, true);
		http_request.send(null);
		return true;
	}

	function openLanguageSelection() {
		document.getElementById('language_window').style.top = (AbsTop(document.getElementById('open_lang_win')) + 15) + "px";
		document.getElementById('language_window').style.left = AbsLeft(document.getElementById('open_lang_win')) - 90 + "px";
		document.getElementById('language_window').style.display = '';
	}
	/*********************************************************************
	 * No onMouseOut event if the mouse pointer hovers a child element
	 * *** Please do not remove this header. ***
	 * This code is working on my IE7, IE6, FireFox, Opera and Safari
	 *
	 * Usage:
	 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');">
	 *	So many childs
	 * </div>
	 *
	 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com
	**/
	function is_child_of(parent, child) {
		if( child != null ) {
			while( child.parentNode ) {
				if( (child = child.parentNode) == parent ) {
					return true;
				}
			}
		}
		return false;
	}

	function closeLanguageSelection(element, event) {
		var current_mouse_target = null;
		if( event.toElement ) {
			current_mouse_target = event.toElement;
		} else if( event.relatedTarget ) {
			current_mouse_target = event.relatedTarget;
		}
		if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
			document.getElementById('language_window').style.display = 'none';
		}
	}
	/*********************************************************************/
