$(document).ready(function() {
  $("#nav div").click(function(e) {
    openNavigationLink.call(this, e)
    return false;
  });
  $("#secondary-nav div").click(function(e) {
    openNavigationLink.call(this, e)
    return false;
  });
  $("#toggle-image").click(function(e) {
    toggleInput("image")
    return false;
  });
  $("#toggle-video").click(function(e) {
    toggleInput("video")
    return false;
  });
  if ($("input.focusonload")) { $("input.focusonload").focus() }
  if ($("#add-location-tooltip")) { 
    setTimeout(function() {
      $("#add-location-tooltip").fadeIn('slow');
    }, 2000)
    setTimeout(function() {
      $("#add-location-tooltip").fadeOut('slow');
    }, 7000)
  }
  if( $("#flash_notice") ){ 
    setTimeout(function() {
      $("#flash").fadeOut('slow');
    }, 6000)
  }
});

$(document).ready(function() {
  setupRatingLinks()
  setupUserSearchField()
});

var inactive_color = '#ccc'; // Colour of default text

function setupUserSearchField() {
  $("#users-search input").css("color", inactive_color);
  $("#users-search input").attr('value', "Pesquisar usuários")
  $('#users-search input').focus(function() {
    if (this.value == "Pesquisar usuários") {
      this.value = '';
      this.style.color = '';
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = "Pesquisar usuários";
      }
    });
  })
}

function setupRatingLinks() {
  $(".positive-rating").fadeTo('slow', 0.33)
  $(".negative-rating").fadeTo('slow', 0.33)
  $(".positive-rating").mouseover(function(e) { $(this).fadeTo('fast', 1);   tooltip.show("Gostei do lugar") })
  $(".negative-rating").mouseover(function(e) { $(this).fadeTo('fast', 1);   tooltip.show("Não gostei") })
  $(".positive-rating").mouseout(function(e) { $(this).fadeTo('fast', 0.33); tooltip.hide() })
  $(".negative-rating").mouseout(function(e) { $(this).fadeTo('fast', 0.33); tooltip.hide() })
}

function openNavigationLink(e) {
  linkNode = this.getElementsByTagName('a').item(0)
  if (!linkNode) { return; }
  if (!linkNode.getAttribute('class') || linkNode.getAttribute('class').indexOf('unimplemented') == -1) { 
    window.location = linkNode.getAttribute('href')
  } else {
    unimplemented()
  }
}

function toggleInput(type){
  $("#" + type).toggle();
  $("#" + type + "_url").toggle();
  $("#" + type + "_file_help").toggle();
  $("#" + type + "_help").toggle();
  linkText = $("#" + type).is(":visible") ? "(Usar URL)" : "(Usar arquivo)"
  $("#toggle-" + type).text(linkText);
};

function unimplemented() {
  alert("Esta funcionalidade não foi implementada ainda. Aguarde.");
}

function ratingCallback(responseText, id) {
  if (responseText != 'success') {
    alert('Houve um problema ao adicionar sua avaliação.')
  } else {
    $("#rating-" + id).fadeOut('slow')
  }
}

function unlockMapAfterLogin() {
  
}