// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var auto_slider_timer;
var enabled = true;

function disableControls() {
  enabled = false;
  setTimeout("enabled = true", 1100);
}

// function centerSlideVertically( slide ) {
//   var diff = 290 - $(slide).children('img').height();
//   if(diff <= 1 || $(slide).children('img').height() <= 1) {return;}
//   $(slide).children('img').css('padding-top', diff/2);
// }

function slideNext() {
  if($('#upcoming_events .slides .slide').length <= 1)
    return;
  var slide_to_hide = $('#upcoming_events .slides .slide:first');
  $('#upcoming_events .slides .slide:first + .slide:first').show();
  slide_to_hide.fadeOut(1000);
  slide_to_hide.remove();
  $('#upcoming_events .slides').append(slide_to_hide);
}

// function slidePrev() {
//   if($('#upcoming_events .slides .slide').length <= 1)
//     return;
//   disableControls();
//   var slide_to_hide = $('#upcoming_events .slides .slide:first');
//   var slide_to_move = $('#upcoming_events .slides .slide:last');
//   slide_to_move.remove();
//   $('#upcoming_events .slides').prepend(slide_to_move);
//   slide_to_move.show();
//   slide_to_hide.fadeOut(1000);
// }

$(document).ready(function() {
  $('#upcoming_events .slides').children().hide();
  $('#upcoming_events .slides .slide:first').show();
  
  auto_slider_timer = setInterval('slideNext()', 7000);
  
  // $('a#next-control').click(function(event) {
  //   event.preventDefault();
  //   if(enabled) {
  //     clearInterval(auto_slider_timer);
  //     slideNext();
  //     auto_slider_timer = setInterval('slideNext()', 10000);
  //   }
  // });
  // 
  // $('a#prev-control').click(function(event) {
  //   event.preventDefault();
  //   if(enabled) {
  //     clearInterval(auto_slider_timer);
  //     slidePrev();
  //     auto_slider_timer = setInterval('slideNext()', 10000);
  //   }
  // });
  
  //autocomplete
  $('input.autocomplete').each(function(){
    var input = $(this);
      input.autocomplete(input.attr('autocomplete_url'),{
      matchContains:1,//also match inside of strings when caching
//    mustMatch:1,//allow only values from the list
//    selectFirst:1,//select the first item on tab/enter
      removeInitialValue:0//when first applying $.autocomplete
    });
  });
});
