<%
var hasError = false;
%>
<!DOCTYPE html>

<html>
  <head>
    <%- partial('header_partial') %>
    <script type='text/javascript'>
      $(function() {

        // trigger a analytics event when the page loads
        window.parent._gaq.push(['_trackEvent', 'Locker', 'View Publish Page', '<%= app.name %>']);

        $("#publishedInfo .action-button").click(function() {
            window.parent.location.hash = "";
        });
        $("#forceSync").click(function() {
            // mehnimal
            $("#forceSync").text("checking!");
            $.getJSON("/synclets/github/run?id=repos&force=true", function() {
                window.location.reload();
            });
        });

        $("#submitPublish").click(function() {
            $("#submitPublish").attr("disabled", "disabled").text("PUBLISHING...");
            $("#publishedInfo a.issueLink").show();
            $("#publishedInfo p").text("Publishing...");
            $("#publishedInfo .action-button").hide();
            $("#publishedInfo a.issueLink").hide();
            $("#publishedInfo").show();
            $("#publishOverlay").width($("body").width()).height($("body").height()).show();
            $("body").scrollTop(0);

            // trigger an analytics event when attempting to publish
            window.parent._gaq.push(['_trackEvent', 'Locker', 'Publish App', '<%= app.name %>']);
            $.getJSON("publish/<%= app.name %>", function(info) {

                $("#publishedInfo .action-button").show();
                if (info.err) {
                    if (info.reauth) {
                        $("#publishedInfo p").addClass("error").text("There was an error publishing your app, you may need to re-connect Github in the account settings.");
                    }else{
                        $("#publishedInfo p").addClass("error").html("There was an error publishing your app, please <u><a href=\"https://github.com/sxux/apps/issues/new?title=publish+fail&body="+encodeURIComponent(info.err)+"\" target=_blank>file an issue</a></u>, thanks!");
                    }
                    $("#submitPublish").removeAttr("disabled");
                } else {
                    $("#publishedInfo p").text("You successfully submitted \"<%=app.repository.title%>\" to be published.  You can track its status on Github.");
                    $("#publishedInfo").show();
                    $("#publishedInfo a").show();
                    $("#publishedInfo .issueLink").attr("href", info.issue.html_url).text(info.issue.html_url).attr("target", "_blank");
                }
            }).error(function() {
                $("#submitPublish").removeAttr("disabled");
                $("#publishedInfo p").addClass("error").text("There was an error publishing your app.  Please try again.");
            });
        });
      });
    </script>
  </head>
  <body>
    <div id='publishOverlay'>
      <div id='publishedInfo'>
        <p>You successfully submitted "<%=app.repository.title%>" to be published.  You can track its status on Github.</p>
        <a class='issueLink' href='#'>...</a>
        <a class='action-button' href='#'>GOT IT!</a>
      </div>
    </div>
    <content class="body">
      <p class='publishInfo'>
        Confirm the information in the fields below, then click "Publish to registry" to submit the app.  Once you've submitted your app, we create an issue on Github to track it.
        <p><br />
          This copy of the app was last modified at <%
// this is gooey, sorry :(
var at;
if(github && github.config && github.config.cached) for(var k in github.config.cached) {
  if(k.toLowerCase().replace("/","-") == id.toLowerCase()) at = github.config.cached[k];
}
function ago(time){
    diff = (((new Date()).getTime() - time) / 1000),
    day_diff = Math.floor(diff / 86400);

  if ( isNaN(day_diff) || day_diff < 0)
    return;

  return day_diff == 0 && (
      diff < 60 && "just now" ||
      diff < 120 && "1 minute ago" ||
      diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
      diff < 7200 && "1 hour ago" ||
      diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
    day_diff == 1 && "Yesterday" ||
    day_diff < 7 && day_diff + " days ago" ||
    day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"
    || Math.floor( day_diff / 30 ) + " months ago";
}
if(at)
{
  at = ago((new Date(Date.parse(at.substr(0,at.indexOf("Z"))))).getTime());
}else{
  at = "unknown";
}
// I no good w/ css!
%><span style="font-weight:bold"><%= at %></span>, <a href="#" id="forceSync" style="text-decoration:underline">check for updates</a>.
        </p>
      </p>
        <ul class="publish-list">
          <li><p>1. App Name
          <% if (app.repository && app.repository.title) { %>
            <p class="indented">
            <%= app.repository.title %>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You must supply a title for your application.  Please refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
            </p>
          </li>
          <li><p>2. App Description</p>
          <% if (app && app.description) { %>
            <p class="indented">
            <%= app.description%>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You must supply a description for your application.  Please refer
              to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
            </p>
          </li>
          <li><p>3. Types Used (optional)</p>
            <% if (app && app.repository && app.repository.uses && app.repository.uses.types && app.repository.uses.types.length > 0) { %>
              <p class="indented">
              <%=: app.repository.uses.types | capitalAll | join:', ' %>
            <% } else { %>
            <p class="indented warning">
              You did not supply the types that your application uses in your
              package.json.  While not required it is highly recommended that
              you set your primary types.  Refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
            </p>
          </li>
          <li><p>4. Services Used (optional)</p>
            <% if (app && app.repository && app.repository.uses && app.repository.uses.services && app.repository.uses.services.length > 0) { %>
              <p class="indented">
              <% for (var i = 0; i < app.repository.uses.services.length; ++i) { console.log(i);%>
                 <img src='img/icons/32px/<%= app.repository.uses.services[i] %>.png' />
              <% } %>
            <% } else { %>
            <p class="indented warning">
              You did not supply the services that your application uses in your
              package.json.  While not required it is highly recommended that
              you set your primary services.  Refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
            </p>
          </li>
          <li><p>5. Screenshot or other appropriate imagery</p>
          <% if (app.screenshot) { %>
          <div class="preview"><img src="<%= app.srcdir %>/screenshot.png"/></div>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You must include a screenshot.png, please refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
          </li>
        </ul>
        <div class="actions">
        <p><button id='submitPublish' type="submit" <%=(hasError ? "disabled='disabled'" : "")%> class="publish">PUBLISH</button></p>
    </content>
  </body>
</html>


