window.onError=null;

var isNewPage = true;
var pageMenu = "";
var curSlide = null;
var stopShow = true;

// Von umliegenden Frameset befreien
if (top!=self) top.location=self.location;

function swapText(text) {
	var desctext = document.getElementById("desctext");
	if (desctext == null) return;

	var newtext = document.createTextNode(text);

	if (desctext.firstChild != null) {
		desctext.replaceChild(newtext, desctext.firstChild);
	}	else {
		desctext.appendChild(newtext);	
	}
}

function menuOver(divmenu) {
	if (divmenu == pageMenu) return;
	getChildNodeByTagName(divmenu, "img").style.visibility = "visible";
}

function menuOut(divmenu) {
	if (divmenu == pageMenu) return;
	getChildNodeByTagName(divmenu, "img").style.visibility = "hidden";
}

function activateMenuByDivId(divID) {
	var menu = document.getElementById(divID);

	if (menu != null) {
		if (menu.nodeName.toLowerCase() != "div") return;
		pageMenu = menu;
		activateMenuInner(menu);

		if (isNewPage) {
			isNewPage = false;
			setTimeout("activateMenuByDivId2('"+divID+"')", 500);
		}
	} else {
		setTimeout("activateMenuByDivId('"+divID+"')", 500);
	}
}

function activateMenuByDivId2(divID) {
	var menu = document.getElementById(divID);
	var style = menu.style.display;
	menu.style.display = "none";
	menu.style.display = style;
}

function activateMenuByAnchor(anchor) {
	var menu = anchor.parentNode;
	if (menu == null || menu.nodeName.toLowerCase() != "div") return;
	activateMenuInner(anchor.parentNode);
}

function activateMenuInner(menuentry) {
	if (menuentry == null) return;
	var parmenu = null;
	if (menuentry.className == "menuentry1") parmenu = menuentry;
	else if (menuentry.className == "menuentry2")	{
		var parlist = getChildrenByClass(menuentry, "menuentry2", false);
		var firstChild = (parlist.length > 0) ? parlist[parlist.length-1] : menuentry;
		parlist = getChildrenByClass(firstChild, "menuentry1", false);
		if (parlist.length == 0) return;
		parmenu = parlist[0];
	}
 	else return;
 	
	var showmenu = getChildrenByClass(parmenu, "menuentry2", true);
	var hidemenu1 = getChildrenByClass(parmenu, new Array("menuentry1", "menuentry2"), false);
	var lastActiv = (showmenu.length == 0) ? menuentry : showmenu[showmenu.length-1];
	var hidemenu2 = getChildrenByClass(lastActiv, new Array("menuentry1", "menuentry2"), true);

	var i;
	for (i=0; i<showmenu.length; i++) showmenu[i].style.display = "block";
	var hidemenu = hidemenu1.concat(hidemenu2);

	for (i=0; i<hidemenu.length; i++) {
		var me = hidemenu[i];
		if (me.className == "menuentry1") continue;
		me.style.display = "none";
	}
	
	setSelection(pageMenu, false);
	pageMenu = menuentry;
	setSelection(menuentry, true);
}

function setSelection(parent, isSelected) {
	var menuimg = getChildNodeByTagName(parent, "img");
	if (menuimg == null) return;
	menuimg.className = (isSelected) ? "menuimgSelected" : "menuimg";
	menuimg.style.visibility = (isSelected) ? "visible" : "hidden";
	var nextAnchor = getChildNodeByTagName(parent, "a");
	if (nextAnchor == null) return;
	nextAnchor.className = (isSelected) ? "anchorSelected" : "";
}

function getChildNodeByTagName(parent, nodename) {
	var node = parent.firstChild;
	while (node != null) {
		if (node.nodeName.toLowerCase() == nodename.toLowerCase()) return node;
		node = node.nextSibling;
	}
	return null;	
}

function getChildrenByClass(current, classes, dirNext) {
	var retChildren = new Array();
	if (typeof(classes) == "string") classes = new Array(classes);

byClassEnde:
	for (;;) {
		current = (dirNext) ? current.nextSibling : current.previousSibling;
		if (current == null) break;
		if (current.nodeType != 1) continue;
		var i;
		for (i=0; i<classes.length; i++) {
			if (current.className == classes[i]) {
				retChildren[retChildren.length] = current;
				break;
			}	
		}
		if (i==classes.length) break byClassEnde;
	}
	
	return retChildren;
}

function initYearFields() {
	var ijahr = (new Date()).getYear();
	if (ijahr < 1900) ijahr += 1900;
	document.getElementById("von").value = "01.05." + ijahr.toString();
	document.getElementById("bis").value = "30.09." + ijahr.toString();
}

function validateRequest() {
	return validateCommon(new Array("name", "vorname", "strasse", "ort", "email", "wwlaenge"));
}

function validateReservation() {
	if (!validateCommon(new Array("name", "vorname", "strasse", "ort", "tel", "von", "bis", "favorplnr", "altplnr", "email", "wwlaenge"))) return false;
	var vonDate = parseDate(document.getElementById("von").value);
	var bisDate = parseDate(document.getElementById("bis").value);

	if (vonDate == null || bisDate == null) {
		alert("Ungültiges Datumsformat");
		return false;
	}

	var vonDate2 = new Date(vonDate.getFullYear(), vonDate.getMonth(), vonDate.getDate()+7);
	if (vonDate2 > bisDate) {
		alert("Die Mindestdauer einer Reservierung beträgt 7 Tage");
		return false;
	}

	return true;
}

function parseDate(dateIn) {
	if (dateIn == null) {
		return null;
	}
	
	// dd-mm-yyyy
	if(dateIn.match(/^(0[1-9]|[12][0-9]|3[01])([- \/.])(0[1-9]|1[012])([- \/.])(\d\d?\d\d)$/)) {
		res = dateIn.match(/^(0[1-9]|[12][0-9]|3[01])([- \/.])(0[1-9]|1[012])([- \/.])(\d\d?\d\d)$/);
		y = res[5];
		m = res[3];
		d = res[1];
		return new Date(y, m, d);	
	}
	return null;
}

function validateCommon(frmvals) {
	for (var i=0; i<frmvals.length; i++)
	{
		var inpval = document.getElementById(frmvals[i]);
		if (inpval != null && inpval.value.length == 0) {
			alert("Bitte alle markierten Felder vollständig ausfüllen");
			return false;
		}
	}

	return true;
}

function exchangeGallery(img, txt) {
	var gal = document.getElementById("gallery");
	var thisSlide = gal.firstChild;
	while (thisSlide != null) {
		if (thisSlide.nodeName.toLowerCase() != "a") {
			thisSlide = findTagByNameRelative(thisSlide, "a");
			continue;
		}
		if (thisSlide.href.indexOf(img) != -1) {
			curSlide = thisSlide;
			break;
		}
		thisSlide = findTagByNameRelative(thisSlide, "a");
	}

	var myPic = 	document.getElementById("gallery-img");
	myPic.src = "images/transpixel.gif";
	myPic.src = img;
	var txtnode = document.getElementById("gallery-txt");
	txtnode.removeChild(txtnode.firstChild);
	txtnode.appendChild(document.createTextNode(txt));

	var mySave = 	document.getElementById("imageSave");
	mySave.href = img;
}

function exchangeMovie(img, txt) {
	var myUrl = document.URL.replace(/\\/g, "/");
	myUrl = myUrl.substring(0, myUrl.lastIndexOf("/")+1);
	
	var qtvr = (document.panorama_mov_embed) ? document.panorama_mov_embed : document.panorama_mov_object;
	
	qtvr.SetResetPropertiesOnReload(false);
	qtvr.SetURL(myUrl + img);
	// rectangle: x,y,width,height
	//qtvr.SetRectangle("10,10,500,400");
	// qtvr.SetControllerVisible(true);
	var txtnode = document.getElementById("gallery-txt");
	txtnode.removeChild(txtnode.firstChild);
	txtnode.appendChild(document.createTextNode(txt));

	var mySave = 	document.getElementById("imageSave");
	mySave.href = myUrl + img;
}

function exchangePicture(imgpnt, img) {
	imgpnt.src = img;
}

function helpMovie() {
	alert("Click with the mouse in the picture and drag/drop to any direction.\n"+
		"Or use the cursor keys.\n"+
		"Use the [Shift]-key to zoom in or [Ctrl]/[Strg]-key to zoom out.");
}

function fullScreen() {
	window.open(document.URL, "fullkiwi", "height="+screen.height+",width="+screen.width+",location=no,menubar=no,resizable=yes,screenX=0,left=0,screenY=0,top=0,scrollbars=yes,status=no,toolbar=no");
}

function slideShow() {
	var showBtn = document.getElementById("slideShow");
	showBtn.className = (stopShow ? "slideStop" : "slidePlay");
	stopShow = !stopShow;
	slideShowHelper();
}

function slideShowHelper() {
	if (stopShow) return;
	
	do {
		if (curSlide == null) {
			var gal = document.getElementById("gallery");
			curSlide = gal.firstChild;
			
			// Mozilla detects some texts before the anchors ...
			if (curSlide.nodeName.toLowerCase() != "a") {
				curSlide = findTagByNameRelative(curSlide, "a");
			}
		} else {
			curSlide = findTagByNameRelative(curSlide, "a");
		}
		// curSlide could be null, if last picture was already shown -> start again
	} while (curSlide == null);

	var evalCmd = curSlide.href.substring("javascript:".length);
	eval(decodeURI(evalCmd));
	
	window.setTimeout("slideShowHelper()", 3000)
}

function findTagByNameRelative(start, name) {
	name = name.toLowerCase();
	do {
		start = start.nextSibling;
		if (start == null) return null;
	} while (start.nodeName.toLowerCase() != name);
	return start;
}
