function change_game(index) {
  if ((index+1) > servers.length) {
    index = 0
  }
  buttons.removeClass('active');
  $(buttons[index]).addClass('active');
  
  game_div = $(servers[index]);
  game_div.fadeOut(function(){
    servers.css('display', 'none');
    $(game_div).fadeIn().css('display', 'block');
  });
}

function change_game_to_next() {
  if (continue_changing == 1) {
    current_button = current_button+1;
    if (current_button > buttons_length) {
      current_button = 1;
    }
    change_game(current_button);
  }
}

$(document).ready(function(){
  $('#popular_games table td, #rodyti_po li').hover(function() {
    $(this).addClass('active');
  }, function() {
    $(this).removeClass('active');
  });

  $('ul.menu li.lang').hover(function() {
    $('ul.menu li.lang ul').css('display', 'block');
  }, function() {
    $('ul.menu li.lang ul').css('display', 'none');
  });
  
  $('ul.menu li.lang ul li').hover(function() {
    $(this).addClass('active');
  }, function() {
    $(this).removeClass('active');
  });
  buttons = $('.white ul li');
  buttons_length = $('.white ul li').length;
  current_button = 0;
  servers = $('.top1_server_info div');
  continue_changing = 1;
  buttons.click(function() {
    change_game(buttons.index(this));
    continue_changing = 0;
  });
  
  setInterval('change_game_to_next()', 5000);
  $(".country_flag").tipsy({fade: true});
  $("#more_flags").tipsy(
    {fade: true, html: true, trigger: 'manual', gravity: "ew", opacity: 1, offset: 0, center: true, element: $("#all_langs")}
  );
  $("#more_flags").click(function() {
    if ($(this).hasClass("hidden") == true) {
      $(this).addClass("show");
      $(this).removeClass("hidden");
      $(this).tipsy("show");
    } else {
      $(this).addClass("hidden");
      $(this).removeClass("show");
      $(this).tipsy("hide");
    }
  });
});

