var progressEnd = 9;		// set to number of progress <span>'s.
var progressColor = '/images/design/1/bg_progress.gif';	// set to progress bar color
var progressInterval = 1000;	// set to time between updates (milli-seconds)
var t;
var progressAt = progressEnd;
var progressTimer;
var old_hash;
//document.domain = document.domain;

function addEvent(elm, evType, fn, useCapture){
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  }
}

function _toggle(id) {
Effect.toggle(id, 'blind',{ duration: 0.4 });
}

function isBrowser(broserName) {
	if (broserName=="ie") currentName = "Microsoft Internet Explorer";
	if (navigator.appName == currentName) return true;
	return false;
}

function _flash(param) {
if(document.getElementsByTagName("embed")!=null) {
if (document.getElementById('flashcontent')) return;
	  for(i=0; (flash = document.getElementsByTagName("embed")[i]); i++) {
	     
            flash.style.visibility = param;
	  }
	}
	
	if(document.getElementsByTagName("object")!=null) {
if (document.getElementById('flashcontent')) return;
	  for(i=0; (flash = document.getElementsByTagName("object")[i]); i++) {
 
	     flash.style.visibility = param;
	  }
	}

if(document.getElementsByClassName("embed")!=null) {
	  for(i=0; (flash = document.getElementsByClassName("embed")[i]); i++) {
	     flash.style.visibility = param;
	  }
	}
}

function goTo(anchor) {
	window.scrollTo(0,0);
	//window.location.hash = "#" + anchor;
}

function progress_clear() {
	for (var i = 1; i <= progressEnd; i++) document.getElementById('progress'+i).className="progress_bar2";
	progressAt = 0;
}

function progress_update() {
	progressAt++;
	if (progressAt > progressEnd) progress_clear();
	else document.getElementById('progress'+progressAt).className="progress_bar1";
	progressTimer = setTimeout('progress_update()',progressInterval);
}

function progress_stop() {
	clearTimeout(progressTimer);
	progress_clear();
}


function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
    //docHt = docHt + 15;
  }
  return docHt;
}

var isShowOn;
var ownflag;

function setIframeHeightOur(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl ) {
    iframeEl.style.height = "auto";  
    try {
      var docHt = getDocHeight(iframeWin.document);
    } catch(e) { getIFrameHeight($('iFrameContainer'), session_add_link); return true; }

    if (docHt) {
      iframeEl.style.height = docHt + 30 + "px";
      if(ownflag==4){ 
        getScroll();
        newHeightForDisable = docHt + parseInt(pagePosition_y);
        ResizeDisableDiv(newHeightForDisable);
      } else {
        ResizeDisableDiv(docHt);
      }
      if(ownflag==3){ 
        $('tableforiframe').style.padding='0px 50px 0px 80px';
      }
    }
  }
}

var link;

function openPage(url){
	link=url;
	t=setTimeout("CloseInTime();",2000)
}

function showIframeAndGoTop() {
	goTo('top');
	showIframe();
}

function CloseInTime(){
	HideSimpleDiv();
	location.href=link;
}

function OpenPopup(url) {
	var winl = (screen.width - 750) / 2;
	var wint = (document.body.offsetHeight - 100);
	winprops = 'height='+wint+',width=750,top=135,left='+winl+',resizable=1,scrollbars=1';
	window.open(url, "mywindow", winprops);
	
	_flash("hidden");
}

var count;

function createIframe(url) {
  if($('iFrameContainer')) {
    Element.remove('iFrameContainer');
    if(window.frames['iFrameContainer']) {
      delete window.frames['iFrameContainer'];
    }
  }
  new Insertion.Bottom($('tableforiframe'), '<iframe src="' + url + '" height="600" width="100%" frameborder="0" scrolling="no" scrollbar="no" onload="" id="iFrameContainer" name="iFrameContainer"></iframe>');
}

function OpenUrlFrame(url,isLoading, isOwn) {
  ownflag=isOwn;
  if (isLoading) {
    isShowOn=true;
    showLoad();
  } else {
    $('overlayer').style.height= getDocHeight(document) + 20 + "px";
    $('overlayer').style.display = 'block';
  }

  createIframe(url.replace('[%`session_add_link%]', session_add_link));
  //$('iFrameContainer').src = url;

  if (ownflag>100) {
    $('iFrameContainer').style.height = isOwn + 30 + "px";
    $('disable_page').style.height = isOwn + 250 + "px";
    showIframe(); 
  } else {
    addEvent($('iFrameContainer'), "load", iFrameOnload);
  }

  

  _flash("hidden");
}

function getVarValueFromURL(url, varName) {	
	var query = url.substring(url.indexOf('?') + 1);	
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == varName) {
			return pair[1];
		}
	}
	return null;	
} 

function OpenActivitaten(url,height) {
  //var height;
  var width;
  $('overlayer').style.height= getDocHeight(document) + 20 + "px";
  $('overlayer').style.display = 'block';

  createIframe(url);
  //$('iFrameContainer').src = url;

  if($('iFrameContainer').addEventListener){ 
    $('iFrameContainer').addEventListener("load",showIframeAndGoTop, false);		
  } else { // IE
    $('iFrameContainer').attachEvent("onload", showIframeAndGoTop);
  }	

  if (getVarValueFromURL(url,'height')!=null) {
    height = parseInt(getVarValueFromURL(url,'height'));
  }
  if (getVarValueFromURL(url,'width')!=null) {
    width = parseInt(getVarValueFromURL(url,'width'));
    $('headDivLogo').style.width = width + "px";
    $('iFrameContainer').style.width = width - 9 + "px";
  }

  var tmpMaxHeight = 0;
  tmpMaxHeight = Math.max(parseInt($('MainContentDiv').offsetHeight), height + parseInt(getAbsolutePos($('header_pic')).x));
  $('disable_page').style.height = tmpMaxHeight +'px';
  $('iFrameContainer').style.height = height + "px";
_flash("hidden");
}

function iFrameOnload() {
  if(ownflag==2){ 
    // nothing ...
  } else if (ownflag==1 || ownflag==3 || ownflag==4){ 
    setIframeHeightOur('iFrameContainer');
  } else {
    getIFrameHeight($('iFrameContainer'), session_add_link);
  }
  if (isShowOn) HideLoad();
  showIframe(); 
}

function openPopWindow(theURL,winName,features) { window.open(theURL,winName,features); }

function ResizeDisableDiv(iframeHeight){
  var NewiframeHeight = parseInt(iframeHeight)+200;
  var HeightDocument = getDocHeight(document);
  if (NewiframeHeight > HeightDocument){
    $('disable_page').style.height = NewiframeHeight + 70 +'px';
  } else {
    $('disable_page').style.height = HeightDocument + 30 + 'px'; 
  }
}

function showLoad() {
  $('divloading').style.left = parseInt(getAbsolutePos($('header_pic')).x) + 'px';
  $('divloading').style.top = parseInt(getAbsolutePos($('header_pic')).y) + 'px';
  $('divloading').style.visibility = 'visible';
  $('disable_page').style.height = getDocHeight(document) + 20 +'px';
  $('disable_page').style.visibility = 'visible';
  progress_update();
}

function closeLoad(){
  isShowOn=false;
  $('divloading').style.visibility = 'hidden';
  $('disable_page').style.visibility = 'hidden';
  removeEvent($('iFrameContainer'), "load", iFrameOnload);
  Element.remove('iFrameContainer');
  //$('iFrameContainer').src = '/empty/';
  
  _flash("visible");
}

function HideLoad() {
  $('divloading').style.visibility = 'hidden';
  progress_stop();
}

var overlayDuration = 0.2;
var overlayOpacity = 0.8;

function ShowSimpleDiv(url){
  $('disable_page').style.height = getDocHeight(document) +'px';
  $('disable_page').style.visibility = 'visible';
  new Effect.Appear('disable_page', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
  $('ContainerSimple').src = lang_add_link + '/imgPopup/?name=' + url;
  $('imageContainer').style.left = parseInt(getAbsolutePos($('header_pic')).x) + 'px';
  $('imageContainer').style.top = parseInt(getAbsolutePos($('header_pic')).y) + 'px';
  $('imageContainer').style.visibility = 'visible';
}

function HideSimpleDiv(){
  new Effect.Fade('disable_page', { duration: overlayDuration});
  //$('disable_page').style.visibility = 'hidden';
  $('imageContainer').style.visibility = 'hidden';
  clearTimeout(t);
}

function getScroll(){
  if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    pagePosition_x = document.body.scrollLeft;
    pagePosition_y = document.body.scrollTop;
  } else if(document.documentElement) {
    pagePosition_x = document.documentElement.scrollLeft;
    pagePosition_y = document.documentElement.scrollTop;
  }
}

function setIframeHeight() {
  $('disable_page').style.height = $('MainContentDiv').offsetHeight +'px';
}

function showIframe() {
  $('overlayer').style.display = 'none';
  if (ownflag==4) {
    if(!count){
      $('div_hotel_book').style.top = parseInt(pagePosition_y)- 8 + parseInt(getAbsolutePos($('header_pic')).y)  + 'px';
    } else {
      $('div_hotel_book').style.top = parseInt(pagePosition_y) + 135 + 'px';
    }
  } else {
    window.scrollTo(0,0);
    $('div_hotel_book').style.top = parseInt(getAbsolutePos($('header_pic')).y) - 8 + 'px';
  }

  $('div_hotel_book').style.left = parseInt(getAbsolutePos($('header_pic')).x) + 'px';
  $('disable_page').style.visibility = 'visible';

  /* /////  */
  $('disable_page').style.height = getDocumentSize(document).height + "px";
  /* /////  */

  $('div_hotel_book').style.visibility = 'visible';
  return false;
}

function hideIframe() {
$('headDivLogo').style.display="block";
$('headDivLogo').style.width="750px";

$('tableforiframe').style.borderLeft = '1px solid rgb(204, 204, 204)';

var iframe_url = document.getElementById('iFrameContainer').src;
if(iframe_url.match('stc_booking=1') != null) {
var confirm_msgs = new Array(3);
confirm_msgs['de'] = "Mit OK wird das Buchungsfenster geschlossen.\nMit Abbrechen kann die Buchung weitergeführt werden.";
confirm_msgs['en'] = "Press OK to close the booking window.\nPress Cancel to continue the booking.";
confirm_msgs['it'] = "Premendo OK si chiude la maschera prenotazioni.\nCon \"interrompi\" si puo' procedere con la prenotazione.";
confirm_msgs['fr'] = "En cliquant sur OK, vous fermez la fenêtre de réservation.\nEn cliquant sur Annuler, vous pouvez poursuivre la réservation.";
var confirm_close = confirm(confirm_msgs[language]);
} else {
var confirm_close = true;
}

if(confirm_close) {

  if(old_hash!='' && old_hash!='#' && old_hash!=undefined) location.hash=old_hash;;
  $('div_hotel_book').style.visibility = 'hidden';
  $('div_hotel_book').style.height = '0px';
  $('iFrameContainer').style.height = '0px';
  $('disable_page').style.visibility = 'hidden';
  $('disable_page').style.height = '0px';
  hideSpecialContent('empty', 'de');
  /* $('print2div').style.display = "none"; */

  removeEvent($('iFrameContainer'), "load", iFrameOnload);
  count=0;





}
 _flash("visible");

}

var weatherChecker;

function OpenUrlFrameWeather(url,isLoading,isOwn) {
  $('headDivLogo').style.display="none";
  OpenUrlFrame(url,isLoading,isOwn);
}

function hideIframeWeather() {
  $('headDivLogo').style.display="block";
  window.clearInterval(weatherChecker); 
  hideIframe();
}

open_weather = function() {
  if(location.hash!='weather' && location.hash!='#weather') old_hash=location.hash;
  location.hash='weather';
  $('overlayer').style.display='block';
  OpenUrlFrameWeather(lang_add_link+'/weather_iframe/',0,1);
  addEvent($('iFrameContainer'),"load",iWeatherFrameOnload);
  weatherChecker = window.setInterval(function () {
      if(location.hash!='weather' && location.hash!='#weather') { 
        window.clearInterval(weatherChecker);
        hideIframe(); 
      } 
    }, 50);
}

iWeatherFrameOnload = function() {
  $('overlayer').style.display='none';
}

addEvent(window,"load",function() {
  if (location.hash=='weather' || location.hash=='#weather') {
    old_hash=''; open_weather();
  }
});

function checkSearch() {
  if(document.getElmentById('q').value == '' || document.getElmentById('q').value == 'Search_') {
    alert('Bitte geben Sie einen Suchbegriff ein.');
    return false;
  }
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
} 
function remove_hash(s) {
	return s.substring(0,s.indexOf("#"));
}

function getDocumentSize(doc) {
	var r = { width: 0, height: 0 };
	var width1=0, width2=0, width3=0, width4=0, maxWidth=0;
	var height1=0, height2=0, height3=0, height4=0, maxHeight=0;

	if (doc.width) maxWidth = doc.width;	
	if (doc.body) {		
		if (doc.body.scrollWidth) width1 = doc.body.scrollWidth;
		if (doc.body.offsetWidth) width2 = doc.body.offsetWidth;		
	}
	if (doc.documentElement) {
		width3 = doc.documentElement.scrollWidth;		
		width4 = doc.documentElement.clientWidth;
	}	
	maxWidth = Math.max(Math.max(Math.max(width1, width2), Math.max(width3, width4)),maxWidth);

	if (doc.height) maxHeight = doc.height;
	if (doc.body) {
		if (doc.body.scrollHeight) height1 = doc.body.scrollHeight;
		if (doc.body.offsetHeight) height2 = doc.body.offsetHeight;
  	}
	if (doc.documentElement) {
		height3 = doc.documentElement.scrollHeight;
		height4 = doc.documentElement.clientHeight;
	}
	maxHeight = Math.max(Math.max(Math.max(height1, height2), Math.max(height3, height4)),maxHeight);

	r.width = maxWidth;
	r.height = maxHeight;
	return r;
}


function hideSpecialContent(which, language) {
	var backlink_texts = new Array(3);
	backlink_texts['de'] = "zurück zur Suchmaske";
	backlink_texts['en'] = "return to the search form";
	backlink_texts['it'] = "ritorna alla ricerca";
	backlink_texts['fr'] = "retour à l'interface de réservation";
	
	var detail_backlink_texts = new Array(3);
	detail_backlink_texts['de'] = "zurück zur Hotel-Übersicht";
	detail_backlink_texts['en'] = "back to the details of the hotel";
	detail_backlink_texts['it'] = "indietro ai detttagli del hotel";

	var old_c = parent.document.getElementById('the_navbar').innerHTML;
	var new_c = '';

	switch(which) {
		case 'backlink':
    		new_c = '<div style="text-align: left; width: 700px; margin: 0px auto;">';
			new_c += '<a href=';
			new_c += '"';
			new_c += '/winter/';
			new_c += language;
			new_c += '/layer_pages/stc_booking_2/" target="iFrameContainer" class="stc_backlink">';
			new_c += backlink_texts[language];
			new_c += '</a></div>';
			parent.document.getElementById('iframe_special_content').style.paddingTop = '10px';
		break;
		
		case 'progressbar':
    		new_c = old_c;
			parent.document.getElementById('iframe_special_content').style.paddingTop = '35px';
		break;
		
		case 'detail_backlink':
    		new_c = '<div style="text-align: left; width: 700px; margin: 0px auto;">';
			new_c += '<a href=';
			new_c += '"';
			new_c += 'javascript:void(0);"';
			new_c += 'onclick="hideIframe();" class="stc_backlink">';
			new_c += detail_backlink_texts[language];
			new_c += '</a></div>';
			parent.document.getElementById('iframe_special_content').style.paddingTop = '10px';
			parent.document.getElementById('iframe_special_content').style.display = 'block';
		break;
		
		case 'empty':
			new_c = '';
			parent.document.getElementById('iframe_special_content').style.paddingTop = '0px';
			parent.document.getElementById('iframe_special_content').style.display = 'none';
		break;
	}

    parent.document.getElementById('iframe_special_content').style.paddingBottom = '10px';
	parent.document.getElementById('iframe_special_content').innerHTML = new_c;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};


function openBookingLayer() {
OpenActivitaten('/winter/' + lang2 + '/layer_pages/stc_booking_2/?width=750&stc_booking=1',4000);
document.getElementById('iframe_special_content').style.display = 'block';
_flash("hidden");
}



function jsPopWindow(title, content) {
	var jsPopWindow_header = {
			code: '\
				<table cellspacing="5" cellpadding="0" width="100%" style="border-bottom:1px #ccc solid;"> \
				<tr> \
					<td align="left"><a href="javascript:window.close()"><img border="0" alt="Close" src="http://www.engadin.stmoritz.ch/winter/images/design/1/close_btn.gif" /></a></td> \
					<td><a href="javascript:window.close();">Schliessen</a></td> \
					<td align="right" width="100%"><img height="39" width="88" alt="" src="http://www.engadin.stmoritz.ch/winter/images/design/1/logo_small.gif" width="88" height="39" /></td> \
				</tr> \
				</tbody></table><div style="padding:10px;"> \
				'
	}
	var jsPopWindow_footer = {
			code: '\
				</div> \
				'
	}
	var jsPopWindow_head = {
			code: '\
				<style type="text/css" /> \
				body {margin:0px;padding:0px;border:0px;} \
				body, td, a {font-family:Arial;font-size:12px;color:#666; text-decoration:none;} \
				</style> \
				'
	}
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
	}

	top.consoleRef = window.open('','myconsole',
		'width=500,height=500'
		+',menubar=0'
		+',toolbar=0'
		+',status=0'
		+',scrollbars=1'
		+',resizable=1'
		+',screenX=' + (myWidth - 500)/2 + ',screenY=200,left=' + (myWidth - 500)/2 + ',top=200'
	)
	// top.consoleRef.document.open("text/html","replace");
	top.consoleRef.document.writeln(
		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>' + title + '</title>' + jsPopWindow_head.code + '</head>'
		+'<body onLoad="self.focus()">'
		+jsPopWindow_header.code
		+content
		+jsPopWindow_footer.code
		+'</body></html>'
	)
	top.consoleRef.focus();
	top.consoleRef.document.close();
}



function jsPopFewo(title, url) {
	var page_title = [];
	page_title['de'] = "<h1>Ferienwohnungen buchen</h1><h2>Engadin St. Moritz</h2>";
	page_title['en'] = "<h1>Book a holiday home</h1><h2>Engadin St. Moritz</h2>";
	page_title['it'] = "<h1>Prenotazione case vacanze</h1><h2>Engadin St. Moritz</h2>";
	
	var close_text = [];
	close_text['de'] = "Schliessen";
	close_text['en'] = "Close";
	close_text['it'] = "Chiudi";
	
	var jsPopWindow_header = {
			code: '\
				<table cellspacing="5" cellpadding="0" width="100%" style="border-bottom:1px #ccc solid;"> \
				<tr> \
					<td align="left"><a href="javascript:window.close()"><img border="0" alt="Close" src="http://www.engadin.stmoritz.ch/winter/images/design/1/close_btn.gif" /></a></td> \
					<td><a href="javascript:window.close();"> \
					' + close_text[lang2]
					+ '</a></td> \
					<td align="right" width="100%"><img height="39" width="88" alt="" src="http://www.engadin.stmoritz.ch/winter/images/design/1/logo_small.gif" width="88" height="39" /></td> \
				</tr> \
				</tbody></table><div style="padding:10px 0 0 10px;"> \
				'
	}
	var jsPopWindow_footer = {
			code: '\
				</div> \
				'
	}
	var jsPopWindow_head = {
			code: '\
				<style type="text/css" /> \
				body {margin:0px;padding:0px;border:0px;} \
				body, td, a {font-family:Arial;font-size:12px;color:#666; text-decoration:none;} \
				h1 {font-size:30px;margin:0; padding:0;margin-bottom:0;margin-top:8px;font-weight:normal;} \
				h2 {font-size:18px;margin:0;padding:0;padding-bottom:10px;font-weight:normal;} \
				</style> \
				'
	}
	var myWidth = 0;
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		//alert(myHeight);
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		//alert(myHeight);
	
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		//alert(myHeight);
	}
	if(myHeight < 1) {
		myHeight = 800;
	}
	else {
		myHeight = myHeight;
	}

	top.consoleRef = window.open('','myconsole',
		'width=810,height=' + myHeight
		+',menubar=0'
		+',toolbar=0'
		+',status=0'
		+',scrollbars=1'
		+',resizable=1'
		+',screenX=' + (myWidth - 800)/2 + ',screenY=10,left=' + (myWidth - 800)/2 + ',top=10'
	)
	// top.consoleRef.document.open("text/html","replace");
	top.consoleRef.document.writeln(
		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>' + title + '</title>' + jsPopWindow_head.code + '</head>'
		+'<body onLoad="self.focus()">'
		+jsPopWindow_header.code
		+ page_title[lang2]
		+'<iframe width="800" height="' + (myHeight - 150) + '" frameborder="0" scrolling="auto" src="' + url + '"></iframe>'
		+jsPopWindow_footer.code
		+'</body></html>'
	)
	top.consoleRef.focus();
	top.consoleRef.document.close();
}