//
//

//<![CDATA[
var Content = [];
//if(navigator.userAgent.indexOf('MSIE')!=-1) {
	Event.observe(window, "load", jsLaunch);
//} else {
	//document.observe("contentloaded", jsLaunch);
//}
function jsLaunch(){
  if(typeof $!='function') return;
  if($('capture')){
	   $('capture').onmouseover = function(){this.style.cursor='pointer';this.down().src="http://intranet.cxnx.net/images/Snowball/site_logo_hover.gif";};
       $('capture').onmouseout = function(){this.down().src="http://intranet.cxnx.net/images/Snowball/site_logo.gif";};
       $('capture').onclick = function(){window.location="/";};
  }
  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(typeof CalInfo != 'undefined'){
		if(CalInfo.length>0){
	        $A(CalInfo).each(function(info,i){
			        Cals.list.push(new Calendar($('calendar_'+info.ru_id),info.rates,info.bookings,i));
			    });
	    }
	}
	if($('enquiry_form')&& window.location.toString().indexOf('fpa')==-1 && window.location.toString().indexOf('property')==-1){
		if(document.all){
			FormFactory = new FF();
		    setTimeout("FormFactory.Launch($('enquiry_form'));",1000);
		} else {
		    FormFactory.Launch($('enquiry_form'));	
		}
	}
	if($('FPAFacilities')) 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();
	    

};

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+'cxnx.net';
			}
		});
}
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-10+"px";
	    calendar.style.height = p<c? "" : p-10+"px";
	}	
}

function showFlashContent(href){
	if(navigator.platform.indexOf('Mac')!=-1){ // on a mac, just open a new window
		window.open(href);
		return;
	}
	var els = [$('Content').descendants(),$('rightCol').descendants()].flatten();
	
	$A(els).each(function(el){
		    Content.push(el);
		});
    var iframe = document.createElement('iframe');
	var s = iframe.style;
	s.display = 'none';
	s.width = "988px"; //$('Content').getWidth()+$('rightCol').getWidth()+"px";
	s.height = "650px";
	s.position = 'absolute';
	s.border = '4px solid #fff';
	s.top = "20px";
	s.left = ((Position.cumulativeOffset($('leftCol'))[0]))+"px";
	s.padding = "0";
	s.overflow = 'hidden';
	s.zIndex = '3000';
	iframe.id = "FlashFrame";
	iframe.src = href;
	//document.onclick = function(){ alert( "click" ); };
	//$('Content').innerHTML = "";
	//$('rightCol').innerHTML = "";
	//$('Content').appendChild(iframe);
	$('wrapper').appendChild(iframe);
	fadeOut($('Content'));
	fadeOut($('leftCol'));
	fadeOut($('rightCol'));
	if($('PageBanner')){
	    fadeOut($('PageBanner'));
	}
	setTimeout("$('FlashFrame').style.display='block';",1000);
	setTimeout("openCloseFlash();",1200);
}
function openCloseFlash(){
	var ff = $('FlashFrame');
	var pos = Position.cumulativeOffset(ff),
	    w = ff.getWidth();
	var img = document.createElement('img');
	img.id = "CloseFlash";
	var d = img.style;
	d.position = 'absolute';
	var top = pos[1]+"px";
	d.top = top;
	d.left = pos[0]+w-210+"px";
	d.width = "200px";
	d.height = "40px";
	img.src = "http://intranet.cxnx.net/images/Snowball/closeFlash.gif";
	d.border = "4px solid #fff";
	d.background = "#eee";
	d.cursor = 'pointer';
	d.zIndex= 4000;
	img.onclick = replaceFlashContent;
	$('wrapper').appendChild(img);
};

function replaceFlashContent(){
    fadeIn($('Content'));
	fadeIn($('leftCol'));
	fadeIn($('rightCol'));
	fadeOut($('FlashFrame'));
	if($('PageBanner')){
	    fadeIn($('PageBanner'));
	}
	setTimeout("$('FlashFrame').parentNode.removeChild($('FlashFrame'));",1000);
	$('CloseFlash').parentNode.removeChild($('CloseFlash'));
	
};

function setDateTime(){
	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()-1;
	hours = ""+hours; if(hours.length==1) hours = "0"+hours;
	var html = "Madeira "+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.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 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!='liftStatus'; });
	$A(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
								});
	$A(tabs).each(function(el){
			Event.observe(el,'mouseover',el.onclick);
		    Event.observe(el,'mouseover',function(){ $(el.id).style.cursor='pointer';});
		});
};

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

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

/*function updatePageBanner( ){
	if( !$( 'PageBanner' ) ) return;
	$( 'PageBanner' ).style.left = 2+Position.cumulativeOffset($('leftCol'))[0]+$('leftCol').getWidth()+'px';
	$( 'PageBanner' ).style.bottom = '0px';
	
	if( navigator.userAgent.indexOf( "MSIE" ) != -1 ) {
	  $( '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)+'px';
	/*
	var pageWidth = ( document.body.scrollWidth < 0 )?
		    document.body.scrollWidth : self.innerWidth || document.documentElement.clientWidth;
		$( 'PageBanner' ).style.right = (pageWidth-($('rightCol').getWidth()+$('rightCol').getStyle('left')))+'px';*/	//}

//};
function updatePageBanner( ){
	if( !$( 'PageBanner' ) ) return;
	if(navigator.userAgent.indexOf( "MSIE" ) == -1){
	  $( 'PageBanner' ).style.left = 2+Position.cumulativeOffset($('leftCol'))[0]+$('leftCol').getWidth()+'px';
	  return;
	}
	$( 'PageBanner' ).style.left = 9+$('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].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 weatherStep(n){
  CURRENT_ICON += n;
  var len = weather_days.length-1; // because in the morning we only have 6 days forecast not 7, len==6
  if(CURRENT_ICON>len) { CURRENT_ICON=len; return; }
  if(CURRENT_ICON<0) { CURRENT_ICON=0; return; }
  fadeOut($('mini_post_it'),1);
  fadeOut($('weather_icon_img'),1);
  setTimeout("$('weather_temp_high').innerHTML = '"+weather_highs[CURRENT_ICON]+"';",250);
  setTimeout("$('weather_temp_low').innerHTML = '"+weather_lows[CURRENT_ICON]+"';",250);
  setTimeout("$('weather_temp_precipitation').innerHTML = '"+weather_precipitation[CURRENT_ICON]+"';fadeIn($('mini_post_it'),1);",250);
  setTimeout("$('weather_icon_img').src = 'http://madeira24.com/images/weather/"+weather_icons[CURRENT_ICON]+".gif'; fadeIn($('weather_icon_img'),1);",250);
  $('weather_day_text').innerHTML = "<strong>"+ weather_days[CURRENT_ICON] + "</strong>";
};
//]]>