var testing = 1;

var Max1LvlEntries = 6;
var Max2LvlEntries = 3;
var MenuStayTime = 500;
var Active1LvlEntry = 0;

var aktiv;

function msg(text) {
	if (testing!=0) alert(text);
}

function menu_default() {
	menu_reset();
	if (menu1!=0) ActivateMenuEntry('menu1_'+menu1);
	if (menu2!=0) {
		ActivateMenuEntry('menu1_'+menu1+'SL_'+menu2);
	}
}

function menu_reset() {
	Reset1Lvl();
	Reset2Lvl();
}

function Reset1Lvl() {
	for (i=1;i<=Max1LvlEntries;i++)
	try { document.getElementById('menu1_'+i).className=''; }
	catch(e) { }
}

function Reset2Lvl() {
	try { document.getElementById('menu1_'+Active1LvlEntry+'SL').style.visibility='hidden'; }
	catch(e) {}
	for (i=1;i<=Max2LvlEntries;i++)
		try { document.getElementById('menu1_'+Active1LvlEntry+'SL'+i).className=''; }
		catch(e) { }
}

function ActivateMenuEntry(ObjId) {
	deActivateSingleMenuEntry('menu1_'+menu1);
	deActivateSingleMenuEntry('menu1_'+menu1+'SL_'+menu2);
	window.clearTimeout(aktiv);
	obj = document.getElementById(ObjId);
	if (ObjId.search('SL')==-1) {
		menu_reset();
		try { document.getElementById(ObjId+'SL').style.visibility='visible'; }
		catch(e) {}
		Active1LvlEntry = ObjId.substring(6,ObjId.length);
	}
	if ((obj!=null)&&(obj.className!='over')) obj.className='over';
}

function deActivateSingleMenuEntry(ObjId) {
	try {
		obj = document.getElementById(ObjId);
		if (obj.className=='over') obj.className='';
	}
	catch(e) { }
}

function deActivateMenuEntry(ObjId) {
	obj = document.getElementById(ObjId);
	if (obj.className=='over') obj.className='';
	aktiv=window.setTimeout('menu_default();',MenuStayTime);
}

function startmove(betrag) {
	scroller = window.setTimeout('move('+betrag+');',10);
}

function move(betrag) {
	oldMargin = document.getElementById('thumbs_innen').style.marginLeft;
	oldMargin = oldMargin.substring(0,oldMargin.length-2);
	if (oldMargin!='') oldMargin=parseInt(oldMargin);

	ScrollerWidth = document.getElementById('thumbs_innen').parentNode.style.width;
	ScrollerWidth = ScrollerWidth.substring(0,ScrollerWidth.length-2);

	newMargin = oldMargin + betrag;
	thumbanzahl = Math.floor(document.getElementById('thumbs_innen').childNodes.length/2);
	thumbbreite = thumbanzahl*35 + (thumbanzahl-1)*8;
	document.getElementById('thumbs_innen').parentNode.style.width
	if (newMargin <= 0 ? proof1 = true : proof1 = false);
	if (newMargin >= (ScrollerWidth - thumbbreite) ? proof2 = true : proof2 = false);

	if (proof1 && proof2) {
		newMargin = newMargin + 'px';
		document.getElementById('thumbs_innen').style.marginLeft = newMargin;
	}

	startmove(betrag);
}

function ChangeImg(pfad,obj) {
	document.getElementById('foto').src = pfad;
	obj.blur();
	return false;
}

function scroller_activateImg(nr) {
	ScrollerWidth = document.getElementById('thumbs_innen').parentNode.style.width;
	ScrollerWidth = ScrollerWidth.substring(0,ScrollerWidth.length-2);
	ItemWidth = 35 + 8;
	AnzBreite = Math.round(ScrollerWidth/ItemWidth);
	Mitte = Math.round(AnzBreite/2);
	thumbanzahl = Math.floor(document.getElementById('thumbs_innen').childNodes.length/2);

	if (thumbanzahl > AnzBreite) {

		if (nr<=Mitte) return;

		newMargin = nr-Mitte;
		if ((nr-Mitte)>(thumbanzahl-AnzBreite)) newMargin = thumbanzahl-AnzBreite;
		newMargin = newMargin*ItemWidth*(-1);
		newMargin = newMargin + 'px';
		document.getElementById('thumbs_innen').style.marginLeft = newMargin;
	}

}


function getEl(id) {
	return document.getElementById(id);
}

function scroller_loadDefaultOffset(actBildNr) {
	scrollerOffset = parseInt(getEl('Z_SCROLLER_OFFSET').value);
	getEl('thumbs_innen').style.marginLeft = scrollerOffset + 'px';

	// prüfen, ob Seite über Bildernavigation aufgerufen wurde
	if (typeof(actBildNr)!='undefined') {
		actBildLeft = (actBildNr-1) * (35 + 8);
		
		// prüfe, ob Bild zu weit links steht
		if (actBildLeft + parseInt(getEl('thumbs_innen').style.marginLeft) < 43) {
			newLeft = (-1) * actBildLeft + 43;

			if (newLeft > 0) newLeft = 0;
			
			getEl('thumbs_innen').style.marginLeft = newLeft + 'px';
			getEl('Z_SCROLLER_OFFSET').value = newLeft;
		}
		
		// prüfe, ob Bild zu weit rechts steht
		else if (actBildLeft + parseInt(getEl('thumbs_innen').style.marginLeft) > 301 - 43) {
			newLeft = 301 - actBildLeft;

			sichtbar = parseInt(getEl('thumbs').style.width);
			breite = parseInt(getEl('thumbs_innen').childNodes.length/2) * (35 + 8) - 8;
			if (newLeft < (sichtbar-breite)) newLeft = (sichtbar - breite);

			getEl('thumbs_innen').style.marginLeft = newLeft + 'px';
			getEl('Z_SCROLLER_OFFSET').value = newLeft;
		}

	}
}

function stopMove() {
	window.clearTimeout(scroller);
	getEl('Z_SCROLLER_OFFSET').value = parseInt(getEl('thumbs_innen').style.marginLeft);
}

function correctLink(linkObj) {
	// Bild links am Rand?
	if (parseInt(linkObj.offsetLeft) < 43) {
		diff = 43-parseInt(linkObj.offsetLeft);
		oldLeft = parseInt(getEl('thumbs_innen').style.marginLeft);
		newLeft = oldLeft + diff;
		
		// Streifen zu weit nach rechts geschoben?
		if (newLeft > 0) newLeft = 0;
		getEl('thumbs_innen').style.marginLeft = newLeft + 'px';
		getEl('Z_SCROLLER_OFFSET').value = newLeft;
	}

	// Oder Bild rechts am Rand?
	else if (parseInt(linkObj.offsetLeft) > 301) {
		diff = parseInt(linkObj.offsetLeft) - 301;
		oldLeft = parseInt(getEl('thumbs_innen').style.marginLeft);
		newLeft = oldLeft - diff;
		
		// Streifen zu weit nach links geschoben?
		sichtbar = parseInt(getEl('thumbs').style.width);
		breite = parseInt(getEl('thumbs_innen').childNodes.length/2) * (35 + 8) - 8;
		if (newLeft < (sichtbar-breite)) newLeft = (sichtbar - breite);
		getEl('thumbs_innen').style.marginLeft = newLeft + 'px';
		getEl('Z_SCROLLER_OFFSET').value = newLeft;
	}
		
	oldLink = linkObj.href;
	newLink = oldLink + '&scrollpos=' + parseInt(getEl('Z_SCROLLER_OFFSET').value);
	linkObj.href = newLink;
	return true;
}

function correctNaviLink(linkObj) {
	correctLink(linkObj);
	oldLink = linkObj.href;
	newLink = oldLink + '&direct=1';
	linkObj.href = newLink;
	return true;
}