function feedback_onsubmit() {
  doLoad('/jsapi.php5', $('#feedback').serialize())
  return false;
}

var feedback_timeout_id = null;

function feedback_collapse() {
  feedback_timeout_id = setTimeout(function() {
    if (!$('#feedback').hasClass('expanded')) return;
    if ($('#feedback').removeClass('expanded'));
    $('#feedback .hide').slideUp(200);
    setTimeout(function() {
      $('#feedback textarea').animate({"height": "90px", "width": "370px"}, 200);
    }, 200);
  }, 500);
}

function feedback_extend() {
  clearTimeout(feedback_timeout_id);
  if ($('#feedback').hasClass('expanded')) return;
  if ($('#feedback').addClass('expanded'));
  $('#feedback textarea').animate({"height": "300px", "width": "700px"}, 200);
  setTimeout(function() { $('#feedback .hide').slideDown(200); }, 200);
}

function addSlideshowGroup(group_name) {
  hs.addSlideshow({
    slideshowGroup: group_name,
    interval: 5000,
    repeat: false,
    useControls: true,
    fixedControls: 'fit',
    overlayOptions: {
        opacity: .8,
        position: 'bottom center',
        hideOnMouseOut: true
    }
  });
}

function booking_change_building() {
  var b = $('#building').val();
  $("#room option").hide();
  $("#room option.empty, #room option.b"+b).show();
  if (!$('#room option:selected').hasClass('b'+b)) {
    $('#room option.empty').attr('selected', 'selected');
  }
}

function copy_to_submenu($node) {
  if ($node.parent().find('ul').length) {
    $('#submenu').html('<ul>'+$node.parent().find('ul:first').html()+'</ul>');
    $('#submenu li li').append('<li>/</li>');
    $('#submenu li ul').each(function(index) {
      $(this).find('li:last').remove();
    });
  }
}

$(document).ready(function() {
  // обелопушиваем формы
  $('.text, textarea').focus(function() {
    $(this).addClass('focused');
    //$('label[for='+$(this).attr('id')+']').addClass('b');
  }).blur(function() {
    $(this).removeClass('focused');
    //$('label[for='+$(this).attr('id')+']').removeClass('b');
  });
  $('.required').append('<span class="star"> *</span>');
  
  // обратная связь
  $('#feedback *').focus(feedback_extend)
  $('body').click(feedback_collapse);
  $('#feedback').click(function() {return false});
  
  // Отправка обратной связи
  $('#feedback').submit(feedback_onsubmit);
  $('#feedback .submit').click(feedback_onsubmit);
  
  // выбор корпуса
  booking_change_building();
  $('#building').change(booking_change_building);
  
  // глагне меню
  $('#mainmenu a').each(function(index) {
    if (document.location.pathname.indexOf($(this).attr('href')) != -1) {
      $(this).addClass('sel');
      $(this).parents('li').find('a:first').addClass('sel');
    }
  });
  copy_to_submenu($('#mainmenu>li>a.sel'));
  /*if ($('#mainmenu>li>a.sel').length) {
    $
    if ($('#mainmenu>li>a.sel').parent().find('ul').length) {
      $('#submenu').html('<ul>'+$('#mainmenu>li>a.sel').parent().find('ul:first').html()+'</ul>');
    }
  }*/
  // клик на глагне меню
  $('#mainmenu>li>a').click(function() {
    copy_to_submenu($(this));
  });
});
