<?php

  require("php/common_begin.php");
  require_once("php/Markdown/markdown.php");

  $lst = Listopia::getInstance();
  // -------------------------------
  if (!$lst->isLoggedIn())
  {
    $lst->unloggedInPage();
    return;
  }
  // -------------------------------
  
  $lst->pageBegin("Lonely Planet's Guide To...", "Bigman, pigman, ha ha, charade you are", array("js/listopia-lists.js"));
  
  $sidebar = array(
    SideBarItem::MiniStats,
    SideBarItem::BasicListNavigator,
    SideBarItem::Profile    
    );
    
  $lst->contentsBeginWithSidebar($sidebar, "The Finer Things", "A selection of choice shavings from the listopia-kebab");

  function emitEntry($lst, $entryID, $commentary = null) 
  { 
    $query = $lst->Database()->select(array(
                        'fields' => 't_items.*, users.user_name, users.user_id, t_lists.list_headline',
                        'table' => 't_items',
                        'join' => 'users on users.user_id = t_items.item_createdby join t_lists on t_lists.list_id = t_items.item_list',
                        'condition' => 't_items.item_id = ' . $entryID
                        ));
    
    $query = $query[0];
    
    $mainHTML = "";
    if ($commentary != null)
    {
      $mainHTML = "<div class='guide-entry'>" . $commentary . "</div>";
    }
    
    $listAnch = ListLink($query['t_items']['item_list'], "#entry_" . $entryID );

    $entryComments = $lst->getListEntryComments($query['t_items']['item_list']);
    
    $isImagePost = ($query['t_items']['item_extra'] == 'image');
    $posterProfileLink = ProfileLink($query['t_items']['item_createdby'], '#PageContent');

    $entryDisc = $entryComments[$entryID];
    $commentCount = 'No';
    if (isset($entryDisc))
    {
      $commentCount = count($entryDisc);
    }
    
    if ($isImagePost)
      $postContent = stripslashes($query['t_items']['item_blurb']);
    else
      $postContent = ConditionTextForDisplay($query['t_items']['item_blurb']);    

      if ($isImagePost)
      {
$mainHTML .= <<<HTML
        <div class='lst-entry'><div>
          <a id='entry_$entryID' name='entry_$entryID'></a>
          <img title='#$entryID' style='margin:4px auto 4px auto;' class='ui-corner-all lst-avatar-img' src='{$postContent}'/>
HTML;
      }
      else
      {
        $contentPrefix = "<p title='#$entryID'>";
        $contentSuffix = "</p>";
      
$mainHTML .= <<<HTML
        <div class='lst-entry'><div>
          <a id='entry_$entryID' name='entry_$entryID'></a>
          {$contentPrefix}{$postContent}{$contentSuffix}
HTML;
      }

      $commentPrefix = "<img src='images/chat-guide.png' style='vertical-align: text-bottom; padding-right: 4px;'/>";
      
$mainHTML .= <<<HTML
          <div class='lst-post-author'><a $posterProfileLink >- {$query['users']['user_name']}</a></div>
          <code><a tabindex='0' style='cursor:pointer;' onclick='$(this.parentNode.parentNode.parentNode).next().toggle("blind");'>
          $commentPrefix $commentCount Comments
        </a></code>
        </div></div>
      
      <div class='lst-comment-pile-entries'>
      <div id='lst-comment-replacable-$entryID'>
HTML;
      if ($commentCount > 0)
      {
        foreach ($entryDisc as &$comment)
        {
          $mainHTML .= $lst->generateDiscussionCommentHTML($comment);
        }
      }
      $mainHTML .= "</div></div>";
      $mainHTML .= "<div style='font-size:0.8em; float:right;'>... from '<a " . $listAnch . ">" . stripslashes($query['t_lists']['list_headline']) . "</a>'</div><div style='clear:right;'></div><br>";

      echo $mainHTML;
  }
  
  function emitListOfLists($lst, $listIDs)
  {
    print("<ul class='lst-sidebar'>");
    
    foreach ($listIDs as &$list)
    {
      $query = $lst->Database()->select(array(
                              'fields' => 'list_id, list_headline, list_type',
                              'table' => 't_lists',
                              'condition' => 'list_id = ' . $list
                              ));
      if (!empty($query[0]))
      {
        $suffix = "";
        $headline = stripslashes($query[0]['t_lists']['list_headline']);
        if ($query[0]['t_lists']['list_type'] == "image")
          $suffix = $lst->getImageListAdornmentHTML();
        
        printf("<li style='margin-bottom: 6px'><a %s>%s</a>%s</li>\n", ListLink($list), $headline, $suffix);
      
      }
    }    
    
    print("</ul>\n");  
  }
  
  $lst->UI_Misc()->openGenericBlock("What's all this bollocks");
  {
  ?>
  <span style='font-size:0.9em'>
  Perhaps you haven't been stalking this site for 4 years. Perhaps you have but you don't obsessively read all the comments. Or maybe you do, but you missed the one where seaotters recalls the time he piped one out to Vanessa Feltz; and missing out on <strong>that</strong> is a crying shame. There is a staggering amount of .. stuff .. on this site now and some things get buried or missed by getting lost in the noise.<br><br>Here's a bundle of things from around the site that make me laugh. If you want to nominate something to be added to the page, message <a href="http://www.listopia.co.uk/profile.php?id=101">Kodiak</a> with it or leave a comment at the bottom of the page. I'll be adding to the guide as I dig stuff up.<br><br>
  Click the titles next to the books to show the entries. Don't be afraid to check the comments, as that's often where the mad shit ends up.
  </span>
  <?php
  }
  $lst->UI_Misc()->closeGenericBlock();


  $lst->UI_Misc()->openToggleBlock("Hors d'oeuvre", "Whole lists full of fun, filth or frivolity or none of those things.");
  {
    $listA = array(
      2175,
      2089,
      1952,
      2371,
      429,
      103,
      918,
      317,
      609,
      276,
      2211,
      1082,
      2232,
      186,
      984
    );
    emitListOfLists($lst, $listA);
    ?>
    <p>Or check out the top-voted lists in order, <a href="http://www.listopia.co.uk/overview.php?mode=listvote">SHEEPLE</a></p>
    <?php
  }
  $lst->UI_Misc()->closeToggleBlock();  

  $lst->UI_Misc()->openToggleBlock("The Substitumonicon", "If exchanging words in songs for swears and laughing is wrong, then we are FUCKED. Absolutely FUCKED. If Listopia was a car, song-title pun lists would be our petrol, or perhaps bio-diesel.");
  {  
    $listIDs = array(
      81,
      5,
      257,
      302,
      118,
      159,
      691,
      493,
      699,
      440,
      676,
      680,
      828,      
      1054,
      1149,
      1046,
      1974,
      1958,
      1133,
      1297,
      1163,
      1089,
      1822,
      1827,
      1763,
      2018,
      2052,
      2135,
    );
    emitListOfLists($lst, $listIDs);
  }
  $lst->UI_Misc()->closeToggleBlock();  

  $lst->UI_Misc()->openToggleBlock("Rambling", "Things occasionally 'go off' in the comments. Stories, fights, occasionally an entire list.");
  {
    emitEntry($lst, 80456, "This entry has the most comments on the site by a fair margin. You've never seen so many people get so excited about assfucking.");
    emitEntry($lst, 79645, "Do you know about cat skeletons? You will soon. You'll also know that Jimleh had a sad, pathetic childhood.");
    emitEntry($lst, 76955, "In which we discover one of us is an East London arty ponce in claret slacks");
    emitEntry($lst, 75697, "Kodiak vs McGinty in an epic tale of bullshit, magic and dog fellatio. Guest appearance by Danny Dyer.");
    emitEntry($lst, 83527, "We get all Inception and create a list within a list (and it's good, too)");
  }
  $lst->UI_Misc()->closeToggleBlock();
  
  $lst->UI_Misc()->openToggleBlock("Horror", "Our proud members come up with some truly disgusting shit sometimes, bless 'em.");
  {
    emitEntry($lst, 48312, "...");

    emitEntry($lst, 77417, "You'll never ejaculate again");
    emitEntry($lst, 6725, "You'll never ejaculate again, again");
    emitEntry($lst, 9643, "Sex is entirely off the table forever now");

    emitEntry($lst, 72487, "Featuring genuine disgust!");
    emitEntry($lst, 69463, "This isn't mentioned in the Kama Sutra. Don't miss Bloblet's one-up in the comments.");
    emitEntry($lst, 68543, "Like a what? Oh my.");
    emitEntry($lst, 33042, "Katie Price doesn't have broadband, right? right?");

    emitEntry($lst, 58114, "It's not just that this is gross, it's that Mr DrunkCountry's girlfriend clearly does this, for reals.");
    emitEntry($lst, 29803, "This has actually happened");
  }
  $lst->UI_Misc()->closeToggleBlock();
  
  $lst->UI_Misc()->openToggleBlock("Memes", "Origins of many of our stupid recurring themes");
  {
    emitEntry($lst, 3718,  "");
    emitEntry($lst, 12411, "Classic");
    emitEntry($lst, 19977, "Classic");
    emitEntry($lst, 8437,  "BMXing was on a roll");
    emitEntry($lst, 75727, "Mrs McGinty. She turns up in the strangest places.");
    emitEntry($lst, 71173, "I'm an attractive lady");
    emitEntry($lst, 60230, "That's my evening sorted");
    emitEntry($lst, 40256);
    emitEntry($lst, 34575,  "I'm out");
    emitEntry($lst, 71202, "Something about hoeves and sweating appealed to a select few");
    emitEntry($lst, 63650, "The best kind of wolf");
    emitEntry($lst, 43484, "Whould yoa laahk to haave a loook at mah penees?");
    emitEntry($lst, 41325, "Is this the earliest instance of WILLIES?");
    emitEntry($lst, 43717, "THAS TAHNS AHP AHVRAYWHAR");
    emitEntry($lst, 72578, "SAH?");
    emitEntry($lst, 52217, "Barbersmith has pointed out his views on football 45 times so far. Memes are for gays.");
    emitEntry($lst, 26959, "From one of our more confusing lists, 'call center fiction fricasee'");    
  }
  $lst->UI_Misc()->closeToggleBlock();
  
  $lst->UI_Misc()->openToggleBlock("Actual Skills", "We added picture lists in 2012. They were about as popular as Video Lists or cystitis, but sometimes people did some beautiful scribbles");
  {
    emitEntry($lst, 81356, "She has an 8ft long tentacle penis that ejaculates pig's blood.");
    emitEntry($lst, 76341, "Excellent ripple shading");
    emitEntry($lst, 75425, "Mr. Buscemi! what are you doing here? Oh it's a drawing.");
    emitEntry($lst, 79380, "It's-a me, Kodiak-io!.");
    emitEntry($lst, 75541, "Superb flame work, there.");
  }
  $lst->UI_Misc()->closeToggleBlock();
  
  $lst->UI_Misc()->openToggleBlock("Routine", "Listopia wouldn't be the same without routine. It wouldn't have been banned in 4 countries, for a start.");
  {
    emitEntry($lst, 52241, "Routine and his racist grandad");
    emitEntry($lst, 82565, "History Lesson");    
    emitEntry($lst, 46316, "Routine smells lovely");
    emitEntry($lst, 36506, "Routine and lockwood flirting");
    emitEntry($lst, 45041, "Routine and lockwood flirting II");
    emitEntry($lst, 52605, "Routine explores some other personalities");  
    emitEntry($lst, 68542, "Erotic poetry");
  }  
  $lst->UI_Misc()->closeToggleBlock();
  
  $lst->UI_Misc()->openToggleBlock("Seaotters", "As funny and lovable as he is utterly fucking crackers. Seaotters weaves many beautiful, insane meanderings that get lost in the comments.");
  {
    emitEntry($lst, 71827, "Here's a good introduction to otters and his masturbation techniques");
    emitEntry($lst, 58933, ".. and it continues");
    emitEntry($lst, 81754, ".. and it worsens");

    emitEntry($lst, 63887, "Fanfic");
    emitEntry($lst, 81580, "Party tips");
    
    emitEntry($lst, 68867, "He loves jokes");
    emitEntry($lst, 20399, "He loves halloween");
    emitEntry($lst, 36508, "He loves Billie");
    emitEntry($lst, 59294, "He loves YouTube");

    emitEntry($lst, 80535, "McGee dog libel");
    
    emitEntry($lst, 34147, "This might actually make you cry a bit");
    emitEntry($lst, 43422, "BASIC");
  }
  $lst->UI_Misc()->closeToggleBlock();

/*
  $lst->UI_Misc()->openToggleBlock("Fights", "");
  {  
    emitEntry($lst, 45221, "Bleasdale");
  }
  $lst->UI_Misc()->closeToggleBlock();
  */
  
  
  $lst->UI_Misc()->openGenericBlock("Hot Tips");
  {
    $listID = 9999;
  ?>
      <div class="lst-comment-pile-list" style="margin-top:0px !important">
        <div id="lst-comment-replacable-<?=$listID?>">
<?php
      $listComments = $lst->getListComments($listID);
      $listCommentCount = 0;
      if (isset($listComments))
        $listCommentCount = count($listComments);
        
        if ($listCommentCount > 0)
        {
          foreach ($listComments as &$comment)
          {
            echo $lst->generateDiscussionCommentHTML($comment);
          }
        }
        else
        {
          print("<div class='lst-comments lst-comment-entry'>");
          print("<p>No chat here.</p>");
          print("</div>");
        }
        print("</div>"); // close lst-comment-replacable- before the add-new-comment stuff
        
      if ($lst->isLoggedIn())
      {
        // encode the cookie auth key in there as a tiny bit of spam protection (we check it in the ajax php)
        $cookieCodeStamp = $_COOKIE[__LAUTH_KEY__];
        if (!isset($cookieCodeStamp))
          die("AUTH ERROR 56.");      
          
        print("<div class='lst-comments lst-comment-addbox' style='padding:8px'>");
        $lst->UI_Misc()->GenTextEntry("addComment", LS_LIST_COMMENT_ADD_BUTTON, 250, 3);
        print("<a id='UIAddCommentButton' title='list_AddComment($(\"#UIAddCommentButton\"), -1, $listID, $listID, \"$cookieCodeStamp\");' style='cursor:pointer;' class='full-width button'>Add</a>");
        print("</div>");
      }          
?>
      </div>
    
    
  <?php
  }
  $lst->UI_Misc()->closeGenericBlock();

  
  
  // -----------------------------------------------------------------------------------------------
  $lst->contentsEnd();

  $lst->UI_Login()->GenLoginDialog();
  
  // default script loading
  $lst->pageEndScripts();
  
  $lst->UI_Login()->GenLoginScripting();
?>

<!-- Local JS -->
<script type="text/javascript">
$(function() {
  
  <?php // fix a bug on iPad where page scrolling gets reset to the top of the page when we use #PageContent anchors
        if (strpos($_SERVER['HTTP_USER_AGENT'],'iPad')) : ?>
  var removeHash = function() {
    window.location.hash = "Done";
  }
  setTimeout(removeHash, 100);
  <?php endif; ?>

});
</script>  
<?php
  $lst->pageEnd();
  
/*
  $buffer = ob_get_flush();
  $fp = fopen($cachefile, 'w');
  fwrite($fp, $buffer);
  fclose($fp);
*/

  require("php/common_end.php");
?>
