/* onlineBuchen-Box */
function submitFeratelBoxForm(position) {
	var selRoomType = document.getElementById("cbShelterType");
	
	if(position == "top")
		selRoomType = document.getElementById("cbShelterTypeTop");
		
	if(selRoomType.selectedIndex==0) {
		if(position=="top") {
			document.getElementById("cbShelterTypeTop").focus();
			document.getElementById("cbShelterTypeTop").style.border="1px solid red";
		} else {
			document.getElementById("cbShelterType").focus();
			document.getElementById("cbShelterType").style.border="1px solid red";
		}
		return false;
	}
	var selectedIndex = selRoomType.selectedIndex;
	var shelterTypeOption = selRoomType.options[selectedIndex];
	if(shelterTypeOption.value=="BHFa") {
			shelterTypeOption.value="BHF";
	}
	
	if(position == "top") {
		document.feratelBoxFormTop.submit();
		return false;
	}
	document.feratelBoxForm.submit();
	return false;
}
function obRoomTypeChanged(position) {
	var selRoomType = document.getElementById("cbShelterType");
	var divShelterCategory = document.getElementById("cbShelterCategory");
	var hidFC = document.getElementById("cbFC");
	
	if(position == "top") {
		selRoomType = document.getElementById("cbShelterTypeTop");
		divShelterCategory = document.getElementById("cbShelterCategoryTop");
		hidFC = document.getElementById("cbFCTop");
	}
	if(selRoomType.selectedIndex!=-1) {
		var shelterTypeValue = selRoomType.options[selRoomType.selectedIndex].value;
		if(shelterTypeValue=="-") {
			selRoomType.selectedIndex=0;
			return;
		}
		if(shelterTypeValue.indexOf("FEWO")!=-1) {
			divShelterCategory.innerHTML = "Apartment";
			hidFC.value = "AP";
		} else {
			divShelterCategory.innerHTML = "Zimmer";
			hidFC.value = "HO";
		}
	}
}
function obSetDates(position) {
	var now = new Date();

	var nowYear = now.getFullYear()*1;

	var selYear  = document.getElementById("cbArrivalYear");
	var selMonth = document.getElementById("cbArrivalMonth");
	var selDay   = document.getElementById("cbArrivalDay");

	if(position == "top") {
		selYear  = document.getElementById("cbArrivalYearTop");
		selMonth = document.getElementById("cbArrivalMonthTop");
		selDay   = document.getElementById("cbArrivalDayTop");
	}

	for(var i=0; i<=1; i++) {
		var optYear = new Option();
		optYear.value=nowYear+i;
		optYear.text=nowYear+i;
		if(i==0) optYear.selected = true;
		
		selYear.options[i]=optYear;
	}

	selMonth.selectedIndex = now.getMonth();
	selDay.selectedIndex   = now.getDate()-1;
}
/* ENDE onlineBuchen-Box */

/* Bookmarking */
function CreateBookmarkLink() {
	var title = document.getElementsByTagName("title")[0].innerHTML;
        var url = document.location.href;
			
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}
/* ENDE Bookmarking */

function getPageLocation() {
	var path = document.location.pathname;
	if(!path) {
		return false;
	}
	
	var startIndex = path.indexOf("/main");
	if(startIndex==-1) {
		return false;
	}
	
	var path = path.substring(startIndex+6);
	var aPath = path.split("/");
	if(aPath.length<2) {
		return false;
	}
	
	return aPath[1];
}

function replaceTextFieldValue(textfield, standardValue) {
	if(textfield.value == standardValue) {
		textfield.value = "";
	}
	else {
		if(textfield.value == "") {
			textfield.value = standardValue;
		}
	}
}

function WeatherData() {
	
	this.request = null;

	this.getXmlHttpRequest = function() {
		var xRequest=null;
		if(window.XMLHttpRequest) {
			xRequest=new XMLHttpRequest();
		} else if(typeof ActiveXObject != "undefined") {
			xRequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xRequest;
	}

	this.load = function(dataUrl) {
		this.request = this.getXmlHttpRequest();
		if(this.request) {
			this.request.onreadystatechange = this._onReadyStateChange;
			this.request.open("GET", dataUrl+"?t="+(new Date()).getTime(), true);
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.request.send(null);
		}
	}

	this._onReadyStateChange = function() {
		var ready = WeatherData.request.readyState;
		if(ready==4) {
			var resp = WeatherData.request.responseText;
			if(resp) {
				try {
					eval(resp);
					setDynamicText("compactText", "<strong>"+aWeatherData.temperatureVillage+"&deg;C</strong> "+aWeatherData.text);
					if(aWeatherData.season=="WI") {
						setDynamicText("compactSnow", aWeatherData.snowValley+" - "+aWeatherData.snowGlacier + " cm");
					} else {
						setDynamicText("compactSnow", aWeatherData.countHuts);
					}
					setDynamicText("compactLift", aWeatherData.openLifts);
					setDynamicImage("weatherBoxSymbol", aWeatherData.symbol);
					setDynamicText("weatherBoxDate", aWeatherData.date+", "+aWeatherData.time);
					setDynamicText("weatherBoxText", aWeatherData.text);
					setDynamicTempImage("weatherBoxTempImageValley", aWeatherData.temperatureVillage);
					setDynamicText("weatherBoxTempValley", aWeatherData.temperatureVillage+"&deg;"); 
					setDynamicTempImage("weatherBoxTempImageSki", aWeatherData.temperatureSki);
					setDynamicText("weatherBoxTempSki", aWeatherData.temperatureSki+"&deg;"); 
					setDynamicTempImage("weatherBoxTempImageGlacier", aWeatherData.temperatureGlacier);
					setDynamicText("weatherBoxTempGlacier", aWeatherData.temperatureGlacier+"&deg;");
					for(var i=0; i<aWeatherData.forecast.length ;i++) {
						setDynamicImage("weatherBoxFCSymbol"+i, aWeatherData.forecast[i].symbol);
						setDynamicText("weatherBoxFCDay"+i, aWeatherData.forecast[i].day);
					}
					setDynamicSnowBar("snowBoxValley", aWeatherData.snowValley);
					setDynamicSnowBar("snowBoxSki", aWeatherData.snowSkiarea);
					setDynamicSnowBar("snowBoxGlacier", aWeatherData.snowGlacier); 
				} catch(e) {
					alert(e+"\n"+resp);
				}
			}
			_verticalAlign("snowreportshort");
			_verticalAlign("liftstaturreportshort");
			_verticalAlign("livecams");
			_verticalAlign("booking");
		}
	}
}

var WeatherData = new WeatherData();

function setDynamicText(id, text) {
	var el = document.getElementById(id);
	if(el) {
		el.innerHTML=text;
	}
}
function setDynamicImage(id, src) {
	var el = document.getElementById(id);
	if(el) {
		el.src=src;
	}
}
function setDynamicTempImage(id, temperature) {
	var el = document.getElementById(id);
	if(el) {
		el.src = temperature<0 ? el.src.replace("_red", "_blue") : el.src.replace("_blue", "_red");
	}
}
function setDynamicSnowBar(id, snowHeight) {
	var el = document.getElementById(id);
	if(el) {
		var parentWidth = el.parentNode.offsetWidth;
		var tmpWidth = snowHeight / 3.5;
		tmpWidth = tmpWidth>100 ? 100 : tmpWidth;
		tmpWidth = tmpWidth<0 ? 0 : tmpWidth;
		el.style.width=tmpWidth+"%";
	}
}

function _verticalAlign(theClassName) {
	var oParent = _getFirstNodeByClassName("div", theClassName);
	if(oParent) {
		var oChild = _getFirstNoneTextChildNode(oParent);
		if(oChild) {
			var tmpMargin = (oParent.offsetHeight - oChild.offsetHeight) / 2;
			oChild.style.marginTop=tmpMargin+"px";
		}
	}
}

function _getFirstNoneTextChildNode(theParent) {
	var childNode = theParent.firstChild; 
	while(childNode) {
		if(childNode.nodeType==1) {
			return childNode;
		}
		childNode = childNode.nextSibling;
	}
	return null;
}

function _getFirstNodeByClassName(theTagName, theClassName) {
	var theNodes = document.getElementsByTagName(theTagName);
	if(theNodes) {
		for(var i=0; i<theNodes.length; i++) {
			if(theClassName == theNodes[i].className) {
				return theNodes[i];
			}
		}
	}
	return;
}

function moveHolidaycheckCode() {
	var topevent = $$("#rightContent .CMSContentObject #rightHeaderRegister")[0];
	if(topevent==undefined)
		topevent = $$("#rightContent .CMSContentObject .topEvents")[0];
	var rightContent = $$("#rightContent")[0];
	var container = $$("#rightContent .CMSContainer");
	var holidaycheckiframe = $$("#holidaycheckCode")[0];
	
	if(holidaycheckiframe!=undefined) {
		if(topevent!=undefined) {
			rightContent.insertBefore(holidaycheckiframe, container[1]);
		}
		else {
			rightContent.insertBefore(holidaycheckiframe, container[0]);
		}
		holidaycheckiframe.style.display = "inline";
	}
}

function moveSearchResult() {
	var searchResult = document.getElementById('accommodationSearchResult').innerHTML;
	var rightContentHTML =  document.getElementById('rightContent').innerHTML;
	if(typeof(rightContentHTML) != undefined) {
		document.getElementById('rightContent').innerHTML = "<div id='accommodationSearchResult'>" + searchResult + "</div>" + rightContentHTML;
	}
}

function StartupCall() {
	
	this.request = null;
	this.callBackFunction = null;

	this.getXmlHttpRequest = function() {
		var xRequest=null;
		if(window.XMLHttpRequest) {
			xRequest=new XMLHttpRequest();
		} else if(typeof ActiveXObject != "undefined") {
			xRequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xRequest;
	}

	this.load = function(dataUrl, theFunction) {
		this.request = this.getXmlHttpRequest();
		this.callBackFunction = theFunction;
		if(this.request) {
			this.request.onreadystatechange = this._onReadyStateChange;
			this.request.open("GET", dataUrl+"?t="+(new Date()).getTime(), true);
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.request.send(null);
		}
	}

	this._onReadyStateChange = function() {
		var ready = StartupCall.request.readyState;
		if(ready==4) {
			var resp = StartupCall.request.responseText;
			if(resp) {
				//try {
					var aWeatherData = eval("("+resp+")");
					StartupCall.callBackFunction(aWeatherData);
				//} catch(e) {
					//alert(e+"\n"+resp);
				//}
			}
		}
	}
}

var StartupCall = new StartupCall();


function soeldenStartupCall(response) {
		var weatherPreviewImages = $$("#top .weatherPreview img");
		for(var i=0; i<4; i++) {
			weatherPreviewImages[i].src = response.forecast[i].symbol;
		}
		
		var weatherPreviewText = $$("#top .weatherPreview .day");
		for(var i=0; i<4; i++) {
			weatherPreviewText[i].innerHTML = response.forecast[i].day;
		}
		
		var weatherToday = $$("#top .weatherToday img");
		weatherToday[0].src = response.symbol;
		
		var weatherDegrees = $$("#top .weatherToday .degrees");
		var tmpTempVillage = response.temperatureVillage;
		var tmpTempGlacier = response.temperatureGlacier;
		
		weatherDegrees[0].innerHTML = Math.round(tmpTempVillage)+"&deg;/"+Math.round(tmpTempGlacier)+"&deg;";
		
		var infoLines = $$("#top .quickInfo .infoLine .light");
		if(response.season=="WI") {
			infoLines[0].innerHTML = response.snowValley+"&nbsp;-&nbsp;"+response.snowGlacier+"&nbsp;cm";
			infoLines[1].innerHTML = response.openLifts;
		} else {
			infoLines[0].innerHTML = response.countHuts;
			infoLines[1].innerHTML = response.openLifts;
		}
		
		var liveCams = $$("#top .livecamInfo .camLink");
		livecamImage_set = new Array();
		livecamImage = 1;
		for(var i=0; i<response.livecams.length; i++) {
			if(i<liveCams.length) {
				liveCams[i].innerHTML=response.livecams[i].text;
				var tempImageSrc = response.livecams[i].image;
				eval('liveCams[i].observe("mouseover", function() { livecamImage='+(i+1)+';$$("#top .camImage")[0].src="'+tempImageSrc+'";$$("#top .livecams .imageOverlay")[0].href="'+tempImageSrc+'";$$("#top .livecams .imageOverlay")[0].title="'+response.livecams[i].text+'";});');
				if(i==0) {
					$$("#top .camImage")[0].src=tempImageSrc;
					$$("#top .livecams .imageOverlay")[0].href=tempImageSrc;
					$$("#top .livecams .imageOverlay")[0].title=response.livecams[i].text;
				}
			}
			livecamImage_set[i]={"caption": response.livecams[i].text, "url": response.livecams[i].image};
		}
		
		if(typeof(response.topEvent)!="undefined") {
			var node = $$("#top .event .headline");
			if(node.length>0) {
				node[0].innerHTML=response.topEvent.title;
				node = $$("#top .event .imageLink");
				node[0].href=response.topEvent.url;
				node = $$("#top .event .info");
				node[0].innerHTML="<strong>"+response.topEvent.date+"</strong> "+response.topEvent.text;
			
				if(response.topEvent.image!="null") {
					
					node = $$("#top .event .image");
					node[0].src=response.topEvent.image;
				}
			}
		}
		if(typeof(response.blogHtml)!="undefined") {
			var node = $$("#headerRegister .blog .info");
			if(node.length>0) {
				node[0].innerHTML=response.blogHtml;
			}
			var node = $$("#rightHeaderRegister .blog .info");
			if(node.length>0) {
				node[0].innerHTML=response.blogHtml;
			}
		}
		if(typeof(response.communityHtml)!="undefined") {
			var node = $$("#headerRegister .community .info");
			if(node.length>0) {
				node[0].innerHTML=response.communityHtml;
			}
			var node = $$("#rightHeaderRegister .community .info");
			if(node.length>0) {
				node[0].innerHTML=response.communityHtml;
			}
		}
}
function oetztalStartupCall(response) {		
		var weatherPreviewImages = $$("#top .weatherPreview img");
		for(var i=0; i<4; i++) {
			weatherPreviewImages[i].src = response.forecast[i].symbol;
		}
		
		var weatherPreviewText = $$("#top .weatherPreview .day");
		for(var i=0; i<4; i++) {
			weatherPreviewText[i].innerHTML = response.forecast[i].day;
		}
}

function gurglStartupCall(response) {		
		var weatherPreviewImages = $$("#top .weatherPreview img");
		for(var i=0; i<4; i++) {
			weatherPreviewImages[i].src = response.forecast[i].symbol;
		}
		
		var weatherPreviewText = $$("#top .weatherPreview .day");
		for(var i=0; i<4; i++) {
			weatherPreviewText[i].innerHTML = response.forecast[i].day;
		}
		
		var weatherToday = $$("#top .weatherToday img");
		weatherToday[0].src = response.symbol;
		
		var weatherDegrees = $$("#top .weatherToday .degrees");
		var tmpTempVillage = response.temperatureVillage;
		var tmpTempSki = response.temperatureSki;
		
		weatherDegrees[0].innerHTML = Math.round(tmpTempVillage)+"&deg;/"+Math.round(tmpTempSki)+"&deg;";
		
		var infoLines = $$("#top .quickInfo .infoLine .light");
		if(response.season=="WI") {
			infoLines[0].innerHTML = response.snowValley+"&nbsp;-&nbsp;"+response.snowSkiarea+"&nbsp;cm";
			infoLines[1].innerHTML = response.openLifts;
		} else {
			infoLines[0].innerHTML = response.countHuts;
			infoLines[1].innerHTML = response.openLifts;
		}
}