//
//

//<![CDATA[
var Content = [];

if(navigator.userAgent.indexOf('MSIE')!=-1 || typeof document.observe != 'function') {
	Event.observe(window, "load", jsLaunch);
} else {
	document.observe("dom:loaded", jsLaunch);
}
function jsLaunch(){
  //var t = new Tracker();
  if(typeof $!='function') return;
  if(window.location.toString().indexOf('home.htm') != -1 
	 || window.location.toString().indexOf('late-availability') != -1){
    showLA();
  }
  var s,q={};
    window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"),
		                           function( $0, $1, $2, $3 ){
									   q[ $1 ] = $3;
								   }
			);
	for(var i in q)if(i=='tab')s=true;
	if(s)SwitchTabs(q.tab.charAt(0),5,q.tab.charAt(2));
    
	setJumpTo();
	stripTitles();
	setTargetBlank();
	setTabPointer();
	if(window.location.toString().indexOf('home.htm')!=-1) setInterval("setDateTime();",200);
	
	if($('enquiry_form')&&window.location.toString().indexOf('fpa')==-1){
		if(document.all){
			FormFactory = new FF();
		    setTimeout("FormFactory.Launch($('enquiry_form'));",1000);
		} else {
		    FormFactory.Launch($('enquiry_form'));	
		}
	}
	if($('FPASpiel')) setFPAHeights();
	if($('SiteWrapper').className.indexOf('withPageBanner')!=-1){
		setTimeout("fadeIn($('PageBanner'));",400);
		setInterval( 'updatePageBanner()', 3 );
    }
	if(document.all){
		var h = document.body.clientHeight, ch = $('container').getHeight();
	    if(h>ch) $('container').style.height = h-20+'px';
	}
	setMailto();
	if(typeof CalInfo != 'undefined'){
		if(CalInfo.length>0){
	        CalInfo.each(function(info,i){
			        Cals.list.push(new Calendar($('calendar_'+info.ru_id),info.rates,info.bookings,i,info.month,false,info.year));
			    });
			Cals.list[0].AJAX(0);
	    }
		
	}   

};

function showLA(){
	var LA = $('LateAvailabilityContainer');
	if(!LA) return;
	if(window.location.toString().indexOf('home.htm') != -1){
	  var texts = $A($('Content').getElementsByClassName('Text'));
	  var el = texts.find(function(span){ return span.innerHTML.indexOf('LATE_AVAILABILITY') != -1; });
	} else { // in the LA listings page
	  var el = $('la-listing');
	}
	if(el) el.innerHTML = LA.innerHTML;
}

function setMailto(){
  var links = $A($('container').getElementsByTagName('A'));
	links.each(function(el){
	    if(el.href.indexOf('mailto')!=-1&&el.className.indexOf('changeme')!=-1){
			  el.href = el.href+'chamonix-networks.com';
			}
		});
}
function clearField(e){
	var el = e.target;
	el.$value = el.value;
	el.value = "";
}
function resetField(e){
	var el = e.target;
    if(el.value=="") el.value = el.$value;
}
function setFPAHeights(){
    var spiel = $('FPASpiel'),
	    features = $('FPAFacilities'),
		s = spiel.getHeight(),
		sy = Position.cumulativeOffset(spiel)[1],
		f = features.getHeight(),
		fy = Position.cumulativeOffset(features)[1];
    spiel.style.height = (s+sy>f+fy)? "" : f+fy-sy+"px";
	features.style.height = (s+sy<f+fy)? "" : s+sy-fy-11+"px";

	if($('FPAPricing')){
	    var pricing = $('FPAPricing'),
		    calendar = $('FPACalendar'),
		    p = pricing.getHeight(),
		    c = calendar.getHeight();
		pricing.style.height = p>c? "" : c-12+"px";
	    calendar.style.height = p<c? "" : p-10+"px";
	}	
}

function setDateTime(){
	if(typeof $!='function') return;
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    var d = new Date();
	var secs = d.getSeconds();
	secs = ""+secs; if(secs.length==1) secs = "0"+secs;
	var mins = d.getMinutes();
	mins = ""+mins; if(mins.length==1) mins = "0"+mins;
	var hours = d.getHours();
	hours = ""+hours; if(hours.length==1) hours = "0"+hours;
	var html = "Morzine "+d.getDate()+" "+months[d.getMonth()]+" "+d.getFullYear()+" <span style='font-size:10px'>("+hours+":"+mins+")</span>";
	if(!$('dateTime')){
		new Insertion.Before($('Content').down(),"<h2 id='dateTime'></h2>");
		$('dateTime').innerHTML = html;	
	} else {
	    $('dateTime').innerHTML = html;	
	}
};

function fadeIn(el,speed){
	speed = speed || 10;
	var bool = getBrowserLevel(); // only run it in IE7 and FF
	if(bool&&el){
	  el.style.opacity = 0;
	  el.style.filter = "alpha(opacity:0)";
	  el.style.display = 'block';
	  for(var i=0;i<101;i++){
	    setTimeout("var el=$('"+el.id+"');el.style.opacity="+i/100+";el.style.filter='alpha(opacity:"+i+")'; ",250+(i*speed));
	  }
	} else {
	  el.style.opacity = 1;
	  el.style.filter = "alpha(opacity:100)";
	  el.style.display = 'block';	  
	}
};

function fadeOut(el,speed){
	speed = speed || 10;
	var bool = getBrowserLevel(); // only run it in IE7 and FF
	if(bool){
	  for(var i=100;i>-1;i--){
	    setTimeout("var el=$('"+el.id+"');el.style.opacity="+i/100+";el.style.filter='alpha(opacity:"+i+")'; ",(100-i)*speed);
	  }
	} else {
	  el.style.display = 'none';
	}
};

function slideUp(el,num,r,speed){
	speed = speed || 10;
	var bool = getBrowserLevel(); // only run it in IE7 and FF
	el.$height = el.getHeight();
	el.$padding = el.style.padding;
	el.$margin = el.style.margin;
	if(bool){
	  el = $(el);
	  var h = el.getHeight();
	  for(var i=1;i<100;i++){
		var height = Math.round(h/i); //i<50? Math.round(h/i) : 0;
		height = (num>1&&height<num)? num : height;
		height = (height<5)? 0 : height;
	    setTimeout("var el=$('"+el.id+"');el.style.height='"+height+"px';el.style.padding='0';el.style.margin='0';",100+i*speed);
	  }
	  if(r) setTimeout("$('"+el.id+"').parentNode.removeChild($('"+el.id+"'));",400+100*speed);
	} else {
	  if(r) {
		$(el.id).parentNode.removeChild($(el.id));
	  } else {
	    el.style.height = num? num+'px' : 0+'px';
		el.style.padding = '0';
		el.style.margin = '0';
	  }
	}
	
}
function squishUp(el,num,r,speed){
	speed = speed || 10;
	var bool = getBrowserLevel(); // only run it in IE7 and FF
	el.$height = el.getWidth();
	el.$padding = el.style.padding;
	el.$margin = el.style.margin;
	if(bool){
	  el = $(el);
	  var h = el.getWidth();
	  for(var i=1;i<100;i++){
		var height = Math.round(h/i); //i<50? Math.round(h/i) : 0;
		height = (num>1&&height<num)? num : height;
		height = (height<5)? 0 : height;
	    setTimeout("var el=$('"+el.id+"');el.style.width='"+height+"px';el.style.padding='0';el.style.margin='0';",100+i*speed);
	  }
	  if(r) setTimeout("$('"+el.id+"').parentNode.removeChild($('"+el.id+"'));",400+100*speed);
	} else {
	  if(r) {
		$(el.id).parentNode.removeChild($(el.id));
	  } else {
	    el.style.height = num? num+'px' : 0+'px';
		el.style.padding = '0';
		el.style.margin = '0';
	  }
	}
	
}
function slideDown(el,num){
	if(!el.$height) return; // it hasn't been slid up
	var bool = getBrowserLevel(); // only run it in IE7 and FF
	var currentHeight = el.getHeight();
	var h = el.$height;
	var p = el.$padding;
	var m = el.$margin;
	if(bool){
	  el = $(el);
	  for(var i=0;i<=h;i++){
		currentHeight++;
		currentHeight = (currentHeight<=h)? currentHeight : h;
		setTimeout("var el=$('"+el.id+"');el.style.height='"+currentHeight+"px';el.style.padding='"+p+"';el.style.margin='"+m+"';",100+i*3);
	  }
	} else {
	  el.style.height = h+'px';
	  el.style.padding = p;
      el.style.margin = m;
	}
}


function getBrowserLevel(){
   return window.XMLHttpRequest;
}

function setTargetBlank(){
	var a = [];
	if($('PageBanner')) a.push($('PageBanner').descendants());
	if($('rightCol')) a.push($('rightCol').descendants());
	if($('leftCol')) a.push($('leftCol').descendants());
    var els = $A(a).flatten();
	els = els.findAll(function(el){ 
		return el.tagName=='A' && 
		       el.parentNode.parentNode.className!="leftNavigation" &&
			   el.parentNode.id!='languageSwitcher' &&
				 el.parentNode.id!='CXNXWebcam' &&
				 el.id!='liftStatusImage' &&
			   el.parentNode.id!='liftStatus'; });
	els.each(function(el){
		    el.target = "_blank";
		});
};

function setTabPointer(){
    var els = $('Content').descendants();
	var tabs = els.findAll(function(el){
								    return el.className.indexOf('Tab')!=-1&&el.className.indexOf('Header')!=-1;
								});
	tabs.each(function(el){
		    Event.observe(el,'mouseover',el.onclick);
			el.observe('mouseover',function(){ $(el.id).style.cursor='pointer'; });
		});
};

function setJumpTo(){
    var els = document.getElementsByClassName('JumpTo');
	if(els){
		els.each(function(el){
		    el.cleanWhitespace();
			var icon = el.down(), ancr = el.down().next().down();
			if(ancr){
				if(icon.className=='JumpToIcon'&&ancr.href){
			    icon.onmouseover = function(){this.style.cursor='pointer';}
				icon.onclick = function(){window.location=ancr.href;}	
			}
			}
		});
	}
};

function stripTitles(){
    var els = $('SiteWrapper').descendants();
	$('SiteWrapper').title="";
	els = els.findAll(function(el){ return el.title; });
	els.each(function(el){ el.title="";});
};

function updatePageBanner( ){
	if(typeof $!='function') return;
	if( !$( 'PageBanner' ) ) return;
	if(navigator.userAgent.indexOf( "MSIE" ) == -1){
	  $( 'PageBanner' ).style.left = 9+Position.cumulativeOffset($('leftCol'))[0]+$('leftCol').getWidth()+'px';
	  return;
	}
	$( 'PageBanner' ).style.left = 2+$('leftCol').getWidth()+'px';

	  $( 'PageBanner' ).style.position = 'absolute';       
	  var ch = document.body.clientHeight;
      var st = document.body.scrollTop;
      var pbh = $('PageBanner').getDimensions().height;
      $( 'PageBanner' ).style.top = (ch+st-pbh-20)+'px';
};

function SwitchTabs(id,total,num){
    if(parseInt(id)>0){
	    id=parseInt(id)-1;
	    var element, els = $('Content').getElementsByTagName('li');
        if(els[id]){
		    $(els[id].id).cleanWhitespace();
		    id=els[id].firstChild.id;
		}
	}
	for( var i = 0; i < total; i++ ) {
		var pos = i + 1;
		var currentId = id.slice(0,id.length-1)+(pos);
		var tabId = "tabHeader_"+id.slice(0,(id.length-4))+ "_" + pos;
		var oldTabId = "tabHeader_"+id.slice(0,(id.length-7))+ "_" + pos; // for backwards compatability
		if( num == pos ){
			if($(tabId)){
				$(tabId).addClassName( 'Selected' );
			} else {
				$(oldTabId).addClassName( 'Selected' );
			}
			$(currentId).style.display='block';
		} else {
			if($(tabId)){
				$(tabId).removeClassName( 'Selected' );
			} else {
    			$(oldTabId).removeClassName( 'Selected' );	
			}
			$(currentId).style.display = 'none';
		}
	}
};

function showCalendars(){
        var cons = document.getElementsByClassName('calendar');
        cons.each(function(el,i){
                Cals.push(new Calendar(el,[],[],i));
            });
}
//]]>