//////////////////////////////////////////////////////////////////////////////
// Requires /js/base.js
// Requires /js/sizes.js
// Requires /js/mainmenu.??.js
//////////////////////////////////////////////////////////////////////////////

	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 == 'po' || par == 'ru') {
					lang = par;
				        if (par == 'en') {
						lang_long = 'english';
					} else if (par == 'lt') {
						lang_long = 'lietuviu';
					} else if (par == 'pl'|| par == 'po') {
						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) {
			ajax_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_deutsch [\n unterschrift_in_polnisch]
		document.getElementById('randomimg').src = "http://files.bildarchiv-ostpreussen.de/files/fotoalbum/fotos/600x400/" + file[1];
		document.getElementById('randomimg').alt = file[2];
		document.getElementById('randomimg').title = file[2];
		document.getElementById('ds_preselect').href="http://www.bildarchiv-ostpreussen.de/suche?lang="+lang+"&ids=" + file[0];
		document.getElementById('randomimg').onload = function() {
			if (document.getElementById('switch_prev').style.display != 'none') {
				showSwitchrow();
			}
		}
		if (file.length > 3) {
			document.getElementById('foreign_title').innerHTML = '('+file[3]+')';
		}
		document.getElementById('foreign_title').style.display = (file.length>3?'':'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() {
		toggleUpdatemode(0);
		if (index == arrindex) {
			selectImg();
			return;
		}
		index++;
		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];
                var w = 754;
                var h = 504;
                popup('/suche/showfoto.html?lang='+lang+'&id='+Math.abs(id),w,h);

	}

	function NumberFromPixel(val) {
		if (val.length < 2) {
			return 0;
		}
		return Number(val.substr(0, val.length - 2));
	}

