//fades the background in and out
function changeBackground(i) {
  
  options = {
    fadeTime: 1000,
    pauseTime: 5000
  }

  if (i > $('.page-backgrounds span').length - 1)
   i = 0;
       
  var image = $('.page-backgrounds span').eq(i).text();
    
  $('#fader').css({
      'background-image':"url('"+image+"')",
      'background-repeat':'no-repeat',
      'background-position':'center top',      
      'display':'none',
      'z-index':10,
      'background-position':'center top',
      'height': $('#fader').parent().height() + 'px',
      'width': '100%',
      'margin':'0 auto'
    }).fadeIn(options.fadeTime,function() {
      $(this).css('display','none').parent().css({
        'background-image':"url('"+image+"')",
        'background-repeat':'no-repeat',
        'background-position':'center top'
        });
      i++;
      setTimeout('changeBackground('+i+')',options.pauseTime);
  });

}

//center the content if a scroll bar pops up
function fixContentOverflow() {
  
  //don't do this if it's ie
  if(jQuery.browser.msie)
     return;

  if ($('#content').outerHeight() > 793) {
    $('#content, #heading .wrapper').css({
      'position':'relative',
      'left':'7px'
    });
  }
}

//fixes the nav bar to the bottom of the screen
function fixBottomNav(obj) {
  
  if ($(window).height() > 865) {
    $('#bottom').css({
     'position':'absolute'
    });
    $('#top').height(793);
    $('body, #wrapper').height(865); 
  } else {
    $('#bottom').css('position','fixed');
    $('#top, body, #wrapper').height($(window).height() - $('#bottom').height());    
  }

}

//checks to see if it's a mobile browser
function isMobile() {
  
  var mobileBrowsers = new Array('mobile','ipad','windows phone','blackberry','android','sony','nokia','samsung','epoc','opera mini','nitro','audiovox','panasonic','philips','sanyo','pocketpc','smartphone');
  
  for(var i=0;i<=mobileBrowsers.length-1; i++) {
    if (navigator.userAgent.search(new RegExp(mobileBrowsers[i], "i")) != -1) {
      return true;  
    }    
  }
  
  return false;
}


jQuery(document).ready(function($) {

  if ($('body').hasClass('home'))
    setTimeout('changeBackground(0)',8000);

    //start the slideshow animation
    function startSlide(hotel) {
        
      var elem = $('#'+hotel+'-canvas');
        
      if ($(elem).hasClass('started'))
        return;
        
      $(elem).addClass('started')
        .nivoSlider({
          effect:'slideInRight',
          directionNav:false,
          controlNav:true,
          controlNavThumbs:true,
          controlNavThumbsFromRel:true,
          pauseTime:10000,
          manualAdvance:true
        });
    }
  	
  	//show and hide the hotel descriptions
    $('.hotel-nav li').not('.popup-link').click(function() {
        
        var hotel = $(this).attr('rel');
        var elem = $('#'+ hotel + '-slide');
        
        startSlide(hotel);

        if (!$(elem).hasClass('up')) {
          $(elem).css({'z-index':'1'}).animate({
            'top':'-410px'
          },function(){
            $(this).addClass('up');
          });
        }
                     
        $('.slide.up').animate({
          'top':'0px'
        },function(){
          $(this).removeClass('up');
        });
        
        if ($('.bottom-nav').hasClass('up'))
          $('.popup-link').trigger('click');
    
    });
    
    //close the hotel description
  	$('.close-icon').click(function() {
  		$(this).parent().animate({
  			'top':'0px'
  		},function() {
  		  $(this).removeClass('up');
  		});
  	});
  	
  	//make the footer nav popup
  	$('.popup-link').toggle(function() {
  	  $(this).fadeOut('fast',function() {
        $(this).css('background-position','right -20px').fadeIn('fast');	    
  	  });
	    $('.bottom-nav').slideDown().addClass('up');
	    $('.close-icon').trigger('click');
  	},function() {
  	  $(this).fadeOut('fast',function() {
        $(this).css('background-position','right top').fadeIn('fast');	
  	  });
  	  $('.bottom-nav').slideUp().removeClass('up');  	  
  	})

    
  	//add a special class if it's an ipad
  	if (isMobile())
  	  $('body').addClass('mobile');
  	  	

  	//fix the bottom navigation to the browser bottom until the browser gets sized to a certain height  	
  	if (!isMobile()) {
      fixContentOverflow();
    	fixBottomNav();
    	$(window).resize(function() {
        fixBottomNav(this);
    	});
	  }

    //handle the contact form labels
  	$('.contact-form input, .contact-form textarea').formLabels({
  	  valueSource:'values',
  	  values: {
  	    txtFirstName:'First Name',
  	    txtLastName: 'Last Name',
  	    txtEmail: 'Email Address',
  	    txtDetails: 'Comments'
  	  }
  	});
  	
  	//turn select boxes into fancy ones
  	$('select').fancySelect({
  	  initialItems:10,
  	  noScroll: true
  	});

});
