$(document).ready(function() {
    
  $('#OR_date_from, #OR_date_to').datepicker({
    minDate:0,
    firstDay: 1,
    dateFormat: 'dd.mm.yy',
    clearText: 'Leeren',
    closeText: 'Schließen',
    prevText: '<',
    nextText: '>',
    currentText: 'Heute',
    monthNames: ['Januar',
    'Februar',
    'März',
    'April',
    'Mai',
    'Juni',
    'Juli',
    'August',
    'September',
    'Oktober',
    'November',
    'Dezember'
    ],
    yearRange: '-40:60',
    dayNamesMin: ['So',
    'Mo',
    'Di',
    'Mi',
    'Do',
    'Fr',
    'Sa'
    ]
  });
  
  /* CURRENT-DATE */
  var date     = new Date(),
      day      = date.getDate(),
      month    = date.getMonth() + 1,
      year     = date.getFullYear();
  
  /* TOMORROW-DATE */
  date.setDate(date.getDate() + 1);
  var tomorrow = date.getDate();
  
  var to = $('input#OR_date_to').val();
    if(typeof to != "undefined" )
      to = to.split('.');
    else
      return false;
      
  var from = $('input#OR_date_from').val();
    if(typeof from != "undefined" )
      from = from.split('.');
    else
      return false;
    
  if(from.length < 3 || to.length < 3) {
    $('input#OR_date_to').val('');
    $('input#OR_date_from').val('');
  }
  
  /* SET DATES TO BOOKING-BLOCK */
  if($('input#OR_date_from').val() == '')
    $('input#OR_date_from').val((day < 10 ? '0' + day : day) + '.' + (month < 10 ? '0' + month : month) + '.' + year);
  if($('input#OR_date_to').val() == '')
    $('input#OR_date_to').val((tomorrow < 10 ? '0' + tomorrow : tomorrow) + '.' + (month < 10 ? '0' + month : month) + '.' + year);
  
  $('input#OR_date_from, input#OR_date_to').change(function() {
      
    var date_to = new Date(to[2],(to[1] - 1),to[0]);
    var date_from = new Date(from[2],(from[1] - 1),from[0]);
    
    if(date_to.getTime() <= date_from.getTime()) {
      if($(this).attr('id') == 'OR_date_from') {
        date_from.setDate(date_from.getDate() + 1);
        $('input#OR_date_to').val((date_from.getDate() < 10 ? '0' + date_from.getDate() : date_from.getDate()) + '.' + ((date_from.getMonth() + 1) < 10 ? '0' + (date_from.getMonth() + 1) : (date_from.getMonth() + 1)) + '.' + date_from.getFullYear());
      } else {
        date_to.setDate(date_to.getDate() - 1);
        $('input#OR_date_from').val((date_to.getDate() < 10 ? '0' + date_to.getDate() : date_to.getDate()) + '.' + ((date_to.getMonth() + 1) < 10 ? '0' + (date_to.getMonth() + 1) : (date_to.getMonth() + 1)) + '.' + date.getFullYear());
      }
    }
    
  });
  
  if($('#OR .error-msg').length > 0) {
    window.setTimeout(function() {
        $('#OR .error-msg').slideUp();
    }, 3000);
  }
  
  
  /* SELECT */
  $('.RP_booking_block select[name=room_quantity]').change(function() {
    $('.RP_booking_block span.OR_select_button').text($(this).val());
  }).mouseenter(function() { /* same like :hover in CSS */
    $('.RP_booking_block span.OR_select_button').css('background-position','0 -63px');
  }).mouseout(function() { /* same like normal status in CSS */
    $('.RP_booking_block span.OR_select_button').css('background-position','0 -42px');
  }).mousedown(function() { /* same like :active in CSS */
    $('.RP_booking_block span.OR_select_button').css('background-position','0 -63px');
  });
  
});

