$(document).ready(function(){
  function format(user) {
    if(user.name == null)
      return user.login;
    else
      return user.login + " (" + user.name + ")";
  }

  $("#collaborator").autocomplete("/repositories/collaborators", {
    width: 260,
    dataType: "json",
    parse: function(data) {
      return $.map(data, function(row) {
        return {
          data: row,
          value: row.value,
          result: row.login
        }
      });
    },
    formatItem: function(item) {
      return format(item);
    },
    selectFirst: false,
    matchSubset: false
  });

  $('a[rel*=facebox]').facebox();
  $('button[rel*=facebox]').facebox();
  $('.relatize').relatizeDate();
  $('.linkbutton').click(function() { window.location = $(this).attr('href'); });
  $(".canhide").append("<div class='close2'></div>").css("position", "relative");
  $(".close2").click(function() {
    $(this).hide();
    $(this).parent().slideUp(300);
  });

  function getFlashMovieObject(movieName)
  {
    if (window.document[movieName])
      {
        return window.document[movieName];
      }
      if (navigator.appName.indexOf("Microsoft Internet")==-1)
        {
          if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
        }
        else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
          {
            return document.getElementById(movieName);
          }
  }

  $('#ssh-url').click(function() {
    $('#url').text($(this).attr('url'));
    return false;
  });

  $('#https-url').click(function() {
    $('#url').text($(this).attr('url'));
    return false;
  });

  function show() {
    var menu = $(this);
    menu.children(".actions").slideDown();
  }

  function hide() {
    var menu = $(this);
    menu.children(".actions").slideUp();
  }

  $(".hover-menu").hoverIntent({
    sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
    interval: 50,   // number = milliseconds for onMouseOver polling interval
    over: show,     // function = onMouseOver callback (required)
    timeout: 300,   // number = milliseconds delay before onMouseOut
    out: hide       // function = onMouseOut callback (required)
  });

  $(".diffclose").live('click', function() {
    var linkid = $(this).attr('linkid');
    $('#'+linkid).attr('on', 'no');
    $(this).parent().parent().parent().remove();
    return false;
  });

  $(".diffurl").click(function() {
    if($(this).attr('on') != 'yes') {
      var viewurl = $(this).attr('viewurl');
      var file = $(this).text();
      var linkid = $(this).attr('id');
      $.get(
        $(this).attr('href'),
        { 'inbrowser' : '1' },
        function(data) {
          $('#diffs').append('<div class="grid_12"><ul class="options"><li><a href="'+viewurl+'">view full file</a></li><li><a class="diffclose" linkid="'+linkid+'" href="#">close</a></li></ul><h3>'+file+'</h3><div class="box file">'+data+'</div></div>');
          $('#'+linkid).attr('on', 'yes');
        }
      );
    }
    return false;
  });
});

