var moment = require('moment'),
  references = require('../services/references'),
  pane = require('../services/pane'),
  edit = require('../services/edit'),
  rules = require('../validators'),
  validation = require('../services/publish-validation'),
  progress = require('../services/progress'),
  dom = require('../services/dom'),
  state = require('../services/page-state');

module.exports = function () {
  function constructor(el) {
    this.el = el;
    this.form = dom.find(el, '.schedule');
    this.pageUri = document.documentElement.getAttribute(references.referenceAttribute);
  }

  constructor.prototype = {
    events: {
      '.publish-now click': 'onPublishNow',
      '.unpublish click': 'onUnpublish',
      '.schedule submit': 'onSchedule',
      '.unschedule click': 'onUnschedule'
    },

    onPublishNow: function () {
      var pageUri = this.pageUri;

      pane.close();
      progress.start('publish');

      return validation.validate(rules).then(function (errors) {
        if (errors.length) {
          progress.done('error');
          pane.openValidationErrors(errors);
        } else {
          return edit.unschedulePublish(pageUri).then(function () {
            return edit.publishPage()
              .then(function (url) {
                progress.done();
                progress.open('publish', `Published! <a href="${url}" target="_blank">View Page</a>`);
                state.toggleScheduled(false);
              })
              .catch(function () {
                // note: the Error passed into this doesn't have a message, so we use a custom one
                progress.done('error');
                progress.open('error', `Server errored when publishing, please try again.`, true);
              });
          });
        }
      });
    },

    onUnpublish: function () {
      var pageUri = this.pageUri;

      pane.close();
      progress.start('publish');

      return edit.unschedulePublish(pageUri).then(function () {
        return edit.unpublishPage()
        .then(function () {
          progress.done();
          progress.open('publish', `Unpublished!`, true);
          state.toggleScheduled(false);
        })
        .catch(function () {
          // note: the Error passed into this doesn't have a message, so we use a custom one
          progress.done('error');
          progress.open('error', `Server errored when unpublishing, please try again.`, true);
        });
      });
    },

    onSchedule: function (e) {
      var form = this.form,
        pageUri = this.pageUri,
        date = dom.find(form, 'input[type=date]').value,
        time = dom.find(form, 'input[type=time]').value,
        datetime = moment(date + ' ' + time, 'YYYY-MM-DD HH:mm'),
        timestamp = datetime.valueOf();

      // close publish pane and start progress bar
      pane.close();
      progress.start('schedule');

      // stop form from submitting normally
      e.preventDefault();

      // only schedule one thing at a time
      return edit.unschedulePublish(pageUri).then(function () {
        return edit.schedulePublish({
          at: timestamp,
          publish: pageUri
        })
        .then(function () {
          progress.done();
          progress.open('schedule', `Publishing scheduled ` + state.formatTime(timestamp, true), true);
          state.toggleScheduled(true);
        })
        .catch(function () {
          // note: the Error passed into this doesn't have a message, so we use a custom one
          progress.done('error');
          progress.open('error', `Server errored when scheduling, please try again.`, true);
        });
      });
    },

    onUnschedule: function () {
      var pageUri = this.pageUri;

      pane.close();
      progress.start('schedule');

      return edit.unschedulePublish(pageUri)
      .then(function () {
        progress.done();
        progress.open('schedule', `Unscheduled!`, true);
        state.toggleScheduled(false);
      })
      .catch(function () {
        // note: the Error passed into this doesn't have a message, so we use a custom one
        progress.done('error');
        progress.open('error', `Server errored when unscheduling, please try again.`, true);
      });
    }
  };
  return constructor;
};

@@ -29,58 +29,8 @@
 '),%0A
-  references = require('../services/references'),%0A
   pa
@@ -63,16 +63,16 @@
 pane'),%0A
+
   edit =
@@ -448,97 +448,8 @@
 ');%0A
-    this.pageUri = document.documentElement.getAttribute(references.referenceAttribute);%0A
   %7D%0A
@@ -714,36 +714,35 @@
 r pageUri = 
-this
+dom
 .pageUri
 ;%0A%0A      pan
@@ -721,32 +721,34 @@
 ri = dom.pageUri
+()
 ;%0A%0A      pane.cl
@@ -1700,36 +1700,35 @@
 r pageUri = 
-this
+dom
 .pageUri
 ;%0A%0A      pan
@@ -1707,32 +1707,34 @@
 ri = dom.pageUri
+()
 ;%0A%0A      pane.cl
@@ -2396,28 +2396,27 @@
 geUri = 
-this
+dom
 .pageUri
 ,%0A      
@@ -2407,16 +2407,18 @@
 .pageUri
+()
 ,%0A      
@@ -3561,20 +3561,19 @@
 i = 
-this
+dom
 .pageUri
 ;%0A%0A 
@@ -3568,16 +3568,18 @@
 .pageUri
+()
 ;%0A%0A     

use dom.pageUri when scheduling