$(document).ready(function(){
  $('#resources div.resource')
    .live('mouseover', function () {
      $(this).find('div.meta').css({opacity: 0.86}).show();
    })
    .live('mouseout', function () {
      $(this).find('div.meta').css({opacity: 1.0}).hide();
    });

  if (login) {
    $('#resources a.arrow').live('click', vote);
    $('#response_form').submit(createResponse);
    if (setting) {
      $('#add_resource_button').click(function(e) { e.preventDefault();
        $('#add_resource_block').toggle();
      });
      $('#res_form').submit(createResource);
    } else {
      $('#add_resource_button, #additional_link').click(function(e) { e.preventDefault();
        $('#user_form_block').dialog({width: 500});
      });
    }
    $('#close_res_form').click(function (e) { e.preventDefault();
      $('#add_resource_block').hide();
      $('#id_title, #id_description, #id_web').val('');
    });
  } else {
    $('#resources a.arrow').live('click', showLogin);
    $('#response_form').submit(showLogin);
    $('#add_resource_button').click(showLogin);
  }

  $('#resource_list').find('a.res_link, div.text a').decorate();

  // manipulation
  $('#show_manip_form').click(function (e) { e.preventDefault();
    $('#manip_form').toggle();
  });
  $('#manip_form').submit(function () { return false; });
  $('#close_manipform').click(function (e) { e.preventDefault();
    $('#manip_form').hide();
  });

  $('#manip_form input:text').keydown(function () {
    var $t = $(this), keyword = $.trim($t.val());
    $('#resource_list')
      .find('div.resource').hide()
      .find('div.text:Contains("' + keyword + '")').closest('div.resource').show();
  });

  $('#res_order').change(function () {
    var
      $t = $(this).hide(),
      $s = $t.after('<span><img src="/images/loading.gif" alt="loading" class="valign_middle"/> loading...</span>').next();
    $.ajax({
      type: 'GET',
      url: '{% url app.views.list.resources key=list.key %}',
      data: {order: $t.val()},
      dataType: 'html',
      success: function (h) {
        $('#resource_list').html(h).find('a.res_link, div.text a').decorate();
        $('#manip_form input:text').trigger('keydown');
        $s.remove();
        $t.show();
      }
    });
  });

  $('#share_this').click(function (e) { e.preventDefault();
    $('#share_container').toggle(200);
  });
  $('#share_container a.share').click(openShareService);
});

function openShareService(e) { e.preventDefault();
  var $t = $(this), esc = encodeURIComponent, f;
  switch($t.attr('rel')) {
    case 'twitter':
      f = 'http://twitter.com/home?status=' + esc(document.title) + '+' + esc(location.href);
      break;
    case 'ameba':
      var b = esc('<a href="' + location.href + '" target="_blank">' + document.title + '</a>');
      f = 'http://blog.ameba.jp/ucs/entry/srventryinsertinput.do?entry_text=' + b + '&editor_flg=1';
      break;
  }
  window.open(f, 'surfing');
}

