<center>
<div style="padding:10px;">
  <h2>Welcome to Fail Fail!</h2><br />
  <br />
  Are you ready, NON FAILBOOK USER?<br />
  <br />
  <div style="font-family:sans-serif;">You will see the top part of a picture.  See if you can figure out what kind of fail the picture is - mutt (dogs/animals) or epic (humans/signs/etc)!  After you guess, the full picture will be revealed!  Scores are not saved, no personal information is stored, all rights reserved, and use at your own risk.  Images are from the excellent <a href="http://failblog.org">failblog.org</a> and <a href="http://faildogs.com">faildogs.com</a>, and the game code for Fail Fail is made by <a href="http://vazor222.livejournal.com">http://vazor222.livejournal.com</a>.  Enjoy! </div> 
  <br />
  <br />
  <form action="/failfail/nonfailbook.php" method="post">
<?php
$score = 0;
if( isset($_POST['score']) )
  $score = $_POST['score'];
if( isset($_POST['guess']) )
{
  // print the full picture
  if( isset($_POST['imgsrc']) )
  {
    print "<img src='".$_POST['imgsrc']."' /><br /><br />\n";
  }
  // print the result
  if( isset($_POST['imgtype']) )
  {
    if( $_POST['imgtype'] == $_POST['guess'] )  // 0=mutt, 1=epic
    {
      print "WIN!<br />\n";
      ++$score;
    }
    else
    {
      print "FAIL!<br />\n";
    }
  }
  // print the total score so far
  print "Score so far for this session:".$score;
  print "<input type='hidden' name='score' value='".$score."' />\n";
  print "<br />\n<input type='submit' value='Continue' />\n";
}
else
{
  // decide random type
  $imgtype = mt_rand(0,1);

  // get random page
  if( $imgtype == 0 )  // 0=mutt, 1=epic
  {
    // mutt - dogs = 71
    $pagenum = mt_rand(1,71);
    $failpage = "http://faildogs.com/page/".$pagenum."/";
    $pagematch = "@<p[^>]*><img src=\"([^\"]*)\"@i";
  }
  else
  {
    // epic - g-rated = 143
    $pagenum = mt_rand(1,143);
    $failpage = "http://failblog.org/tag/g-rated/page/".$pagenum."/";
    $pagematch = "@src=\"(http://failblog.files.wordpress.com/[^\"]*)\"@i";
  }
  // connect to fail blog
  //print "<br />\nopening failpage:".$failpage;
  $file = fopen ($failpage, "r");
  if (!$file) {
      echo "Fail Fail remote open FAIL! Unable to open remote file, please try again later.<br /></form></div></center>\n";
      exit;
  }
  $imgsrc = "http://failblog.files.wordpress.com/2009/09/epic-fail-police-crash-fail.jpg";
  $randpick = mt_rand(1,9);
  //print "<br />\ngetting imgsrc randpick:".$randpick;
  while (!feof ($file)) {
      $line = fgets ($file, 1024);
      /* This only works if the title and its tags are on one line */
      //print "<br />\nmatching with ".htmlspecialchars($pagematch)." on line ".htmlspecialchars($line)." ";
      if (preg_match ($pagematch, $line, $out)) {
          //print "<br />\ngetting imgsrc matched line:".$line;
          $imgsrc = $out[1];
          //print "<br />\ngetting imgsrc imgsrc:".$imgsrc;
          if( --$randpick == 0 )
            break;
      }
  }
  fclose($file);
  //print "<br />\ngot imgsrc imgsrc:".$imgsrc."<br />\n";
  print "<input type='hidden' name='score' value='".$score."' />\n";
  print "<input type='hidden' name='imgtype' value='".$imgtype."' />\n";
  // print half of it
  $img_w = 500;
  list($img_w,$img_h) = @getimagesize($imgsrc);
  print "<table><tr><td width=$img_w height=100 background='$imgsrc'></td></tr></table>\n";
  print "<input type='hidden' name='imgsrc' value='".$imgsrc."' /><br /><br />\n";
  // print guess form
  ?>
  <br />
  <input type='radio' name="guess" value=0 />Mutt Fail
  <input type='radio' name="guess" value=1 />Epic Fail <br />
  <input type="submit" value="Make Your Guess" />
  <?php
}
?>
<br />
<br />
</form>
  <div style="clear: both;"/>
</div>
</center>
