// FIXME yeah, this needs doing properly ^^
var ajax_supported = XMLHttpRequest ? true : false;

/**
 * Registers an event for when the remove help link is clicked.
 */
function prepareRemoveHelpLink() {
  if ( !ajax_supported ) return false;
  $( '#remove_page_help a' ).click( removeHelp );
  return true;
}

/**
 * Removes the help text from the page and sends a request to the server to
 * make the change persistent.
 */
function removeHelp() {
  // slide the help out
  $( '#page_help' ).slideUp().remove();
  // make a call to the server to persist the change
  // TODO deal with a failure to persist in a sensible way
  var url = $( '#remove_page_help a' ).get( 0 ).href;
  $.get( url );
  // stop the browser following the link
  return false;
}

function prepareRevokeInviteLinks() {
  return true; // DISABLE since group member limit interferes with this a bit
  if ( !ajax_supported ) return false;
  $( '#pending-invitations td.revoke-invite a' ).click( revokeInvite );
  return true;
}

function revokeInvite() {
  $( this ).css( 'background-image', 'url(/css/images/ajax_load.gif)');

  var tr = $( this ).parent().parent();
  var url = this.href + '/ajax';
  $.get( url, function( json ) { processRevokeInviteResponse ( json, tr ); } );

  // stop the browser following the link
  return false;
}

function processRevokeInviteResponse( json, tr ) {
  var response = eval( '(' + json + ')' );

  if ( response.result == 'success' ) {
    removeTableRow( tr );
  } else {
    tr.find( 'a' ).css( 'background-image', 'url(/css/images/cross.png' );
  }
}

function removeTableRow( tr ) {
  // TODO make animation work!
  tr.remove();
}

function initChangeGroupName() {
  $( '#group-name a' ).click( changeGroupName );
}

function changeGroupName() {
  var name = $( '#group-name span' ).text();
  $( '#group-name' )
    .after( '<div id="inline-edit"><input type="text" value="' + name + '" /><input type="button" value="Change name" onclick="saveGroupName()" /></div>' )
    .hide();
  return false;
}

function saveGroupName() {
  var name = $( '#inline-edit input' ).val();
  $.getJSON( $('#group-name a').href() + '/ajax', { "group_name": name }, saveGroupNameResponse );
}

function saveGroupNameResponse( json ) {
  if ( json.result == 'success' ) {
    $( '#group-name span' ).html( $('#inline-edit input').val() );
    $( '#group-name' ).show();
    $( '#inline-edit' ).remove(); 
  } else {
    alert( 'Sorry, the name change failed: ' + json.toSource() );
  }
}

// gives focus to the first form input in the page with class `auto-focus`
function autoFocus() {
  $( 'input.auto-focus, select.auto-focus' ).each( function() {
    this.focus();
  });
}

// make the whole of the list items clickable, not just the link
$( function() { $( 'ul.package-summaries ul.actions li' ).click( function() { window.location = $( 'a', this )[ 0 ].href; } ); } );

function packageFade() {
  if ( $( '#system-message.sys-success' ).length != 1 ) return;

  $( 'div.package-complete' )
    .animate( { backgroundColor: '#fef454' }, 1000 )
    .animate( { backgroundColor: '#e8e8e8' }, 1000 );
}

function trackPdf() {
  $( 'li.pdf a, li.download a, li.email a' ).click( function() {
    if ( window.urchinTracker !== undefined ) {
      urchinTracker( this.href );
    } else if ( window.pageTracker !== undefined ) {
      pageTracker._trackPageview( this.href );
    }
  });
}

function newWindowLinks() {
  $( 'li.pdf a' ).click( function() {
    window.open( this.href );
    return false; // prevent link from firing
  });
}

$( document ).ready( autoFocus );
$( document ).ready( initChangeGroupName );
$( document ).ready( prepareRevokeInviteLinks );
$( document ).ready( prepareRemoveHelpLink );
$( document ).ready( packageFade );
$( document ).ready( trackPdf );
$( document ).ready( newWindowLinks );
