
// Utility //
function startsWith( value, expected ) {
  return expected.length <= value.length
    && value.substring(0, expected.length) == expected;
}

function endsWith( value, expected ) {
  return expected.length <= value.length
    && value.substring(value.length - expected.length, value.length) == expected;
}

function trim( value ) { return value.replace( /\s/g, '' ).toLowerCase( ); }

function isMatch( trimmed, value ) { return trimmed == trim( value ); }

function rollover( img_name, img_src ) { document[img_name].src = img_src; }

var randomIndex_last = -1;  // Avoid immediate duplicates.
function randomIndex( value ) {
  var index;
  do {
    index = Math.floor( Math.random() * value.length );
  } while( index == randomIndex_last && value.length > 1 )
  
  randomIndex_last = index;  
  return index;
}

function quoteChar( value ) {
  var old_last = randomIndex_last;
  var index = randomIndex( value );
  randomIndex_last = old_last;
  var c = value[index].toUpperCase( );
  var prefix = 'AEFHILMNORSX'.indexOf( c ) >= 0 ? 'an' : 'a';
  
  return prefix + ' &ldquo;' + c + '&rdquo;';
}

function getShowSpeed( ) {
  var isIE = navigator.userAgent.indexOf( 'MSIE' ) >= 0;
  return isIE ? 0 : 'slow';
}


var alternateAnswer_last = '';
function alternateAnswer( value, first, second ) {
  if( alternateAnswer_last.length == 0 || isMatch( value, alternateAnswer_last ) ) {
    alternateAnswer_last = isMatch( value, first ) ? second : first;
  }

  return alternateAnswer_last;
}

function keyedAnswer( value, answers, defaultKey ) {
  return answers[value] ? answers[value] : answers[defaultKey];
}

function randomAnswer( ignore, answers ) {
  var index = randomIndex( answers );
  var answer = answers[index];
  
  if( trim(answer) == ignore ) { answer = answers[(index + 1) % answers.length]; }
  return answer;
}

var randomItem_last = -1;
function randomItem( ignore, answers ) {
  var save_randomIndex_last = randomIndex_last;
  randomIndex_last = randomItem_last;
  var answer = randomAnswer( ignore, answers );
  randomItem_last = randomIndex_last;
  randomIndex_last = save_randomIndex_last;
  
  return answer;
}


// Post //
function showEntryCC( ) {
  $('.entry-cc').show( );
}

function showTranscript( ) {
  $('#transcript').html( 'Transcripted' );
  $('.post-cc').show( );
}

function showCC( ) {
  showTranscript( );
  $('.cc-logo').html( '<span>CC</span>&rsquo;d' );
  $('.cc').show( );
  $('.transcript').css( 'padding-left', '1.5em' );
}


// Comments //
function hideCommentArea( ) {
  var formExists = $('#commentarea').length;
  var formFilled = $('#author').val() || $('#email').val() || $('#url').val();
  
  $('.captchaarea').show( );
  if( formExists && !formFilled ) {
    $('#commentarea').hide( );
    $('#respond').html( '<a title="Click to Leave a Reply" href="javascript:showCommentArea( );">Click to Leave a Reply</a>' );
  }
}

function showCommentArea( ) {
  var showSpeed = getShowSpeed( );
  var target = $('#captcha')
  if( target.length == 0 ) { target = $('#author'); }
  
  $('#respond').html( 'Leave a Reply' );
  $('#commentarea').show( showSpeed, function() { target.focus( ); });
  
  // http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
  var submit = $('#submit');
  if( submit.length == 0 ) { submit = $('.captcha'); }
  if( submit.length > 0 ) {
    var submitTop = submit.offset( ).top;
    $('html, body').animate( {scrollTop: submitTop}, 'slow' );
  }
}

function setCaptchaWarning( value ) {
  var showSpeed = getShowSpeed( );
  
  $('#captcha_warning').slideUp( showSpeed, function() {
    $('#captcha_warning').html( value ).slideDown( showSpeed );
  } );
}


// Sidebar //
function highlightCurrentPost( post_id ) {
  var postLinks = $('#archives').add('#rss-links').children( 'ul' ).children( );
  var currentPost = postLinks.filter(function( index ) {
    var href = $(this).children( ).attr( 'href' );
    return endsWith( href, post_id );
  });
  
  currentPost.attr( 'id', 'currentpost' );
}


// http://code.google.com/apis/ajaxlibs/
google.load( 'jquery', '1' );
