// var slideshows;
var room_slideshow_ticker;

$(document).ready(function() {
  
  // Fix highlight
  $( '#' + $('#GeneratedNav li.here').eq(0).attr('id').replace('GeneratedNav', 'Nav') ).addClass('here');

  // Activate success
  if ( location.hash == '#Success' ) {
    $('#Success').show();
  }
  
  // Match sizes
  var sidebar = $('div#Content div.inset_sidebar, div#Content div#AvailabilityAndOffers');
  if ( sidebar.length > 0 ) {
    var sidebar_bottom = sidebar[0].offsetHeight + sidebar.position().top;
    if ( sidebar_bottom > $('div#Content').height() ) {
      $('div#Content').height( sidebar_bottom );
    }
  }

  // Adjust date to today
  var now = new Date();
  $('#ci_day').val( now.getDate() );

  $('p.show_full_year a').click( function(e) {
    var jq_obj = $(this);
    if ( jq_obj.hasClass('open') ) {
      jq_obj.html('Show full year');
      $('div.full_year').hide();
      jq_obj.removeClass('open');
    } else {
      jq_obj.html('Hide full year');
      $('div.full_year').show();
      jq_obj.addClass('open');
    }
    return false;
  });
  
  // No events? Remove events
  if ( $('div.inset_sidebar div.event').length > 0 ) {
    $('div.inset_sidebar div.no_events').remove();
  }
  
  // Give an id to all H1's without it, so we can pick up our image replacements
  $('h1').each( function(i) {
    var jq_obj = $(this);
    if ( jq_obj.attr('id') == "" ) {
      jq_obj.attr('id', 'TITLE_' + jq_obj.text().replace( /(^|\s)([a-z])/g , function (m, p1, p2) { return p1 + p2.toUpperCase(); } ).replace( /\s/g, '') );
      jq_obj.addClass('image_replacement');
    }
  });
  
  // Replace input[type=submit].replace with buttons
  $('input[type=submit].replace').each( function(i) {
    var jq_obj = $(this);
    jq_obj.after('<a href="#" class="button"><span>' + jq_obj.attr('value') + '</span></a>');
    jq_obj.next('a.button').click( function(e) {
      $(this).parents('form').eq(0).submit();
      return false;
    });
    jq_obj.hide();
  });
  
  // Stripe - 0 indexed
  $('ul').each( function(i) {
    $(this).children('li:odd').addClass('odd');
  });
  
  // Slideshow on homepage
  $('body#Home div#MainImage div#Slideshow').nivoSlider({
    effect: 'fade',
    animSpeed: 1000,
	  pauseTime: 4000,
    directionNav: false,
	  controlNav: false
  });
  
  $('#RoomSwitcher div.room_selector ul li a').click( function(e) {
    var jq_obj = $(this);
    $('#RoomSwitcher div.room_selector ul li').removeClass('selected');
    jq_obj.parents('li').addClass('selected');
    var slide = $( $(this).attr('href') );
    $('#RoomSwitcher>ul.room_tabs>li').removeClass('selected');
    slide.addClass('selected');
    return false;
  });
  
  $('#RoomSwitcher ul.room_tabs li ul.images').each( function(i) {
    var jq_obj = $(this);
    jq_obj.find('li').hide();
    jq_obj.find('li:first').show();
  });
  
  if ( $('#RoomSwitcher').length > 0 ) {
    room_slideshow_ticker = window.setInterval(change_room_slide, 5000);
  }
  
});

function change_room_slide() {
  var slides = $('#RoomSwitcher>ul.room_tabs>li.selected ul.images li');
  if ( slides.length < 2 ) { return; }
  slides.eq(1).fadeIn(500, function () {
    var prev_li = $(this).prev('li');
    prev_li.hide();
    prev_li.appendTo( $(this).parents('ul.images').eq(0) );
  });
}
