<?php
  require("php/common_begin.php");

  $lst = Listopia::getInstance();
  // -------------------------------
  if (!$lst->isLoggedIn())
  {
    $lst->unloggedInPage();
    return;
  }
  // -------------------------------


  $lst->pageBegin(LS_NEWLIST_DOCUMENT_TITLE, "A stomach full of shoulders");
  
  $sidebar = array(
    SideBarItem::NewListInstructions
    );
    
  $lst->contentsBeginWithSidebar(
    $sidebar, 
    LS_NEWLIST_TITLE,
    LS_NEWLIST_DESC);

  if ($lst->isLoggedIn())
  {
    if ($lst->isUserOnProbation())
    {
      $lst->UI_Misc()->openGenericBlock("Nope");
      print("<p>You're on probation, you can't add lists. Tough shit.</p>");      
      $lst->UI_Misc()->closeGenericBlock();
    }
    else
    {
  
      $lst->UI_Misc()->openGenericBlock("The Details");
      {
        $lst->UI_Misc()->GenTextEntry("newList_title", "Title", 75, 1);
        $lst->UI_Misc()->GenTextEntry("newList_desc", "Description", 160, 1);
        print("<br/>");
        $lst->UI_Misc()->GenTextEntry("newList_rules", "List Rules", 300);
        print("<div><p>Seperate rules with two @ signs - not line breaks.<br/><em>Eg</em> - Don't fill the list with shit, you cunt@@I didn't mean that, let's have a cuddle.</p></div>");
        print("<br/>");
        
        print("<p><label style='display: inline; padding-right: 8px' for='CharacterLimit'>Character Limit:</label><input readonly type='text' id='CharacterLimit' style='display: inline; margin:0px; border:1px solid #ddd; color:#f6931f; font-weight:bold; width: 100px;' /><div style='margin: 0px 5px 25px 5px;' id='slider'></div></p>");
        
        print("<p>By default, lists entries are sorted based on how many thumb-ups or thumb-downs they get. However, some lists only make sense when they're in order.</p>");
        print("<input type='checkbox' name='OrderedList' />Create an ordered list<br/>");

        print("<br/>");
        print("<p>Fond of fine art exhibitions, or endless pages of spunking dicks? Make this list all about silly pictures instead of silly words. <em>Character Limit</em> will obviously then be ignored.</p>");
        print("<input type='checkbox' name='SketchList' />Create a picture list<br/>");
      }
      $lst->UI_Misc()->closeGenericBlock();
      
      $crIndex = mt_rand(0, 5);
?>
    <div class='lst-pf-block' style='padding: 10px;'>
      <img style='border:2px solid #333; margin: 0px auto; display:block' src='images/list-create<?=$crIndex?>.jpg'/>
      <p style='text-align:center'><?=LS_NEWLIST_AREYOUREADY?></p>
      <a href='#' tabindex='0' id="UICreateListButton" style='cursor:pointer;' class='full-width button'>Yes. Create The List.</a>
      <div style='clear:left'></div>
    </div>
<?php
    }
  }
  else
  {
    $lst->UI_Misc()->AddCenteredMessageBlock(LS_NEWLIST_NOTLOGGEDIN);
  }
?>


<?php
  $lst->contentsEnd();

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

<!-- Local JS -->
<script type="text/javascript"">
$(function() 
{
  $("#UICreateListButton").click(function(ev)
  {
    // get all the guts
    var listTitle = $("#newList_title #fmTextContent").val();
    var listDesc = $("#newList_desc #fmTextContent").val();
    var listRules = $("#newList_rules #fmTextContent").val();
    var listMax = $("#CharacterLimit").val();
    var listOrdered = $("input[name=OrderedList]").is(":checked");
    var listSketch = $("input[name=SketchList]").is(":checked");
    
    // validate the basics before we sent it off
    if (listTitle.length < 2)
    {
      window.alert("Try actually typing a list title!");
      ev.preventDefault();
      ev.stopPropagation();
      return;
    }
    if (listDesc.length < 2)
    {
      window.alert("Type something for the list description");
      ev.preventDefault();
      ev.stopPropagation();
      return;
    }
    
    // disable the button while we work
    var thisButton = $(this);
    var prevOnclick = thisButton.attr('onclick'); 
    thisButton.attr('onclick','');
    thisButton.fadeTo('fast', 0.5);
    thisButton.html('<img src="images/ajax-loader-fb.gif"/>');
    {
      // request creation
      $.post(
        "php/ajax_listCreate.php", 
        { 
          title: listTitle, 
          desc: listDesc,
          rules: listRules,
          maxchars: listMax,
          ordered: listOrdered,
          sketch: listSketch
        },
        function(data) 
        { 
          // we're done, re-enable the button
          thisButton.fadeTo('fast', 1.0);
        
          if (data['success'] == undefined)
          {
            thisButton.html(data['result']);
          }
          else
          {
            window.location = "list.php?id=" + data['result'];
          }
          
          thisButton.attr('onclick', prevOnclick);
        },
        "json");  
    }

    ev.preventDefault();
    ev.stopPropagation();
  });
  
  $("#slider").slider(
  {
    value:500,
    min: 20,
    max: 500,
    step: 10,
    slide: function( event, ui ) 
    {
      $( "#CharacterLimit" ).val( ui.value );
    }
  });
  $( "#CharacterLimit" ).val( $( "#slider" ).slider( "value" ) );
  
});
</script>

<?php
  $lst->pageEnd();

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