ZeroClipboard.setMoviePath( '/flash/ZeroClipboard.swf' );

$(function(){
  $('a[rel=external]').click(function(e){
    window.open($(this).attr('href'));
    e.preventDefault();
  });
  var clip = new ZeroClipboard.Client();
  var copyLink = $('<div class="copy"><a href="#">Copy to clipboard</a></div>');
  clip.setText($('div.result h2 input').val());
  $('div.result h2').after(copyLink);
  clip.glue(copyLink.find('a')[0], copyLink[0]);
  clip.addEventListener( 'onComplete', onComplete );

  function onComplete( client, text ) {
    var span = $('<span>Copied!</span>');
    $('div.copy a').replaceWith(span);
    setTimeout(function(){ span.fadeOut('slow',function(){$(this).remove();}); client.destroy(); }, 5000);
    clip.hide();
  }
});