$(function () {
  var active_userlink;
  
	$("input[type='hidden']").css('display', 'none');
	if (typeof(console) == 'undefined') console = {};
	if (typeof(console.log) == 'undefined') console.log = function(){};
	if (typeof(console.warn) == 'undefined') console.warn = function(){};
	if (typeof(console.error) == 'undefined') console.error = function(){};
	
	
  $('#menu li').hoverIntent(
    function () {
      if (this.id == 'link-search') { $('.subnav').hide(); $('#search.subnav').fadeIn(); $('#SearchQuery').focus(); }
      if (this.id == 'link-home')   { $('.subnav').hide(); $('#greets.subnav').fadeIn(); }
    },
    function () { }
    );
  $('.subnav').hide();
  $('.subnav:first').show();
  $("#SearchQuery, #QuickSearchQuery, #SubtitleTitle").autocomplete("/site/search_suggest", {  
          matchContains: true,
          selectFirst: false,
          height: '250px',
          formatItem: function(row, i, max, term) {
            term = $('#SearchQuery').val();
            return "<img src='"+row[2]+"' alt='' />" +row[0].replace(new RegExp("(" + term + ")", "gi"), "<strong>$1</strong>") + "<br><span style='font-size: 90%; color: #999;'>" + row[1] + "</span>";
          }
          }
  );
  $('#download-link').click(function () { this.select(); });
  
  $('.overlay.shadow').fadeTo(0, 0.6);
/*
  $('.overlay.onhover').hide();
  $('.frame').hover(
      function () { $(this).children('.onhover').slideDown('fast'); },
      function () { $(this).children('.onhover').slideUp('fast'); }
    );
*/  
  $('.user-profile').hoverIntent(
      function () {  
        slug = $(this).attr('href').substr(10);
        if (slug) {
          active_userlink = $(this); 
          show_usercard(slug); 
        }
      },
      function () { active_userlink = false; }
  );
  
  function show_usercard(slug) {
      if (active_userlink) {
          if (! slug) return;
          hide_card();     

          var offset = active_userlink.offset();
          var result = $(document.body).data(slug);
          $(document.body).append('<div id="user-profile-wrapper"><div class="shadow"></div><div class="user-profile-card"></div>');

          card = $('#user-profile-wrapper')
              .css('top', offset.top + 2)
              .css('left', offset.left - 4)
              .mouseleave(hide_card)
          $('#user-profile-wrapper .user-profile-card')
              .append(active_userlink.clone());
          $('#user-profile-wrapper .shadow')
            .width(card.width())
            .height(card.height())
            .fadeTo(0, 0.5);

          if (result) {
              display_card(slug, result);
          } else {
              $('#user-profile-wrapper .user-profile-card').addClass('loading');
              $.get('/users/usercard/' + slug, {}, function (data) {
                  $('#user-profile-wrapper .user-profile-card').removeClass('loading');
                  $(document.body).data(slug, data);
                  display_card(slug, data);
              });
          }
      }
  }
  
  function hide_card() {
      $('#user-profile-wrapper').remove();
  }
  
  function display_card(slug, data) {
      $('#user-profile-wrapper .card-info').remove();
      $('#user-profile-wrapper .user-profile-card')
        .remove('.card-info')
        .append(data)
        .children('.card-info')
          .hide()
          .fadeIn();
  }
});
