var followcost = {
  detect_user_enter_key: function() {
    $("#username").keydown(function(event) {
      if (13 == event.which) {
        followcost.loading_and_direct_to_results();
      }
    });
  },
  
  is_iphone: function() {
    return /Mobile\/.+ Safari\/.+/.test(navigator.userAgent);
  },
  
  loading_and_direct_to_results: function() {
    var username = $('#username').val();
    var prompt   = $('#prompt');
    var results  = $('#results');

    prompt.html('Contacting Twitter...');
    results.html(followcost.loading_graphics(3));
    
    // TODO: Could probably build a recursive function, pass in array of loading messages,
    //       delete from array on each pass, one more loading graphic per pass.  Could also
    //       use a random number generator to make load times appear variant.
    //       1200ms + random_number for the Timeout value.
    setTimeout(function() {
      prompt.html('Reticulating splines...');
      results.html(followcost.loading_graphics(4));
      setTimeout(function() {
        prompt.html('All your base are belong to us!');
        results.html(followcost.loading_graphics(5));
        setTimeout(function() { 
          prompt.html('Splicing cost factors...');
          results.html(followcost.loading_graphics(6));
          setTimeout(function() {
            followcost.go_to_username(username);
          }, 1000);
        }, 500);
      }, 1000);
    }, 1000);
  },
  
  go_to_username: function(username) {
    window.location.href = '/' + username;
  },
  
  // Hokey
  loading_graphics: function(number_loaded) {
    var total_graphics     = 7;
    var number_yet_to_load = total_graphics - number_loaded;
    var html = '';
    
    for(i=0; i<number_loaded; i++) {
      html += "<div class='loaded'></div>";
    }
    
    for(k=0; k<number_yet_to_load; k++) {
      html += "<div class='loading'></div>";
    }
    
    return html;
  },
  
  politics_animation: function() {
    setTimeout(function() {
      $('#header').hide('slide', {direction:'up'}, 500);
      setTimeout(function() {
        // $('#header .politics_nut').fadeIn(1);
        $('#header .politics_nut').show();
        $('#header').show('slide', {direction:'up'}, 500);
        $('#header').effect('pulsate', {times:3}, 500);
      }, 500);
    }, 2000);
  },
  
  golden_animation: function() {
    setTimeout(function() {
      $('body').addClass('golden');
      setTimeout(function() {
        $('body').removeClass('golden');
        setTimeout(function() {
          $('body').addClass('golden');
          setTimeout(function() {
            $('body').removeClass('golden');
            setTimeout(function() {
              $('body').addClass('golden');
              setTimeout(function() {
                $('body').removeClass('golden');
                setTimeout(function() {
                  $('body').addClass('golden');
                  setTimeout(function() {
                    $('body').removeClass('golden');
                    setTimeout(function() {
                      $('body').addClass('golden');
                      setTimeout(function() {
                        $('body').removeClass('golden');
                        setTimeout(function() {
                          $('#header').hide('slide', {direction:'up'}, 500);
                          setTimeout(function() {
                            $('#header .golden').show();
                            $('body').addClass('golden');
                            $('#header').show('slide', {direction:'up'}, 500);
                            $('#header').effect('pulsate', {times:3}, 500);
                            setTimeout(function() {
                              $('#container').addClass('golden');
                              $('#footer').addClass('golden');
                            }, 2000);
                          }, 500);
                        }, 500);
                      }, 70);
                    }, 50);
                  }, 50);
                }, 70);
              }, 50);
            }, 500);
          }, 30);
        }, 30);
      }, 20);
    }, 2000);
  },
  
  nuclear_animation: function() {
    setTimeout(function() {
      $('.result.nuclear').effect('shake', {times:3}, 80);
        setTimeout(function() {
          $('body > div').fadeOut("slow");
          setTimeout(function() {
            $('#header .nuclear').fadeIn(1);
            $('#header .nuclear').show();
            $('#header').show('slide', {direction:'up'}, 500);
            $('#header').effect('pulsate', {times:3}, 500);
            setTimeout(function() {
              $('body > div').fadeIn(10000)
              // followcost._fade_in_ippnw_link();
            }, 2000);
          }, 1000);
        }, 3000);
    }, 2000);
  },
  
  ippnw_animation: function() {
    setTimeout(function() {
      $('#header').hide('slide', {direction:'up'}, 500);
      setTimeout(function() {
        $('#header .nuclear').show();
        $('#header').show('slide', {direction:'up'}, 500);
        $('#header').effect('pulsate', {times:3}, 500);
        followcost._fade_in_ippnw_link();
      }, 500);
    }, 2000);
  },
  
  ataholic_animation: function() {
    setTimeout(function() {
      $('#header').hide('slide', {direction:'up'}, 500);
      setTimeout(function() {
        $('#header .ataholic').show();
        $('#header').show('slide', {direction:'up'}, 500);
        $('#header').effect('pulsate', {times:3}, 500);
      }, 500);
    }, 2000);
  },
  
  _fade_in_ippnw_link: function() {
    setTimeout(function() {
      $('#shared_links').append("<p id='ippnw_link' style='display:none;'><a href='http://ippnw.org' target='_blank'><img src='/ippnw_sm.jpg' width='16' height='16' border='0'></a> <a href='http://ippnw.org' target='_blank'>Learn more about nuclear weapons</a></p>")
      $('#ippnw_link').fadeIn(5000);
    }, 3000); 
  }
}