Event.observe(document, 'dom:loaded', function() {
  new Interface();
});

Interface = Class.create({
  initialize: function() {
    // tell css what we can do
    if (!Utility.addHTMLClassName('with-javascript')) return;

    // draw the page
    this.drawPage();
  },

  /* Sets up the page by creating any objects that should be created, and other
   * misc display tasks.
   */
  drawPage: function() {
    this.gallery = new Gallery('gallery');

    this.navigation = new Navigation('navigation');
    $('yield').observe('mouseover', function() {
      $('yield').fire('navigation:hide');
    });

    this.makeAnchorsSmoothScroll('body');
    this.makeHelperInputs('body');
  },

  makeAnchorsSmoothScroll: function(element) {
    element = $(element);

    var anchors = [];
    if (element) anchors = element.select('a.smooth-scroll');
    else helpers = $$('a.smooth-scroll');

    anchors.each(function(anchor) {
      if (!anchor.href.include('#')) return;
      var urlParts = Utility.getUrlParts(anchor.href);
      if (!urlParts.hash) return;

      var targetElement = $(urlParts.hash);
      if (targetElement) {
        anchor.observe('click', function(e) {
          e.stop();
          new Effect.ScrollTo(targetElement, {
            duration: 1,
            transition: Effect.Transitions.swingTo
          });
        });
      }
    });

  },

  /* Makes input fields with the class name "helper" have a default value that
   * will clear itself for user input, and fills the helper text back when the
   * input loses focus while empty.
   */
  makeHelperInputs: function(element) {
    element = $(element);

    var helpers = [];
    if (element) helpers = element.select('input.helper');
    else helpers = $$('input.helper');

    helpers.each(function(input) {
      if (!input.title || input._helper) return;
      input._helper = true;

      var form = input.up('form');
      if (form) form.observe('submit', function() { if (input.value == input.title) input.value = ''; });

      if (!input.value) input.value = input.title;
      else input.removeClassName('helper');

      input.observe('focus', function() {
        if (input.value == input.title) {
          input.value = '';
          input.removeClassName('helper');
        }
      });

      input.observe('blur', function() {
        if (!input.value) {
          input.value = input.title;
          input.addClassName('helper');
        }
      });
    });
  }

});

// add handling for the back/forward button
window.currentLocation = window.location.href;
window.intervalTest = setInterval(function() {
  if (window.currentLocation != window.location.href) {
    var urlParts = Utility.getUrlParts(window.location.href);
    Event.fire(document, 'interface:transition', {containerElement: '', url: urlParts.hash ? urlParts.hash : urlParts.path});
  }
}, 5);

// add handling for the refresh button
var urlParts = Utility.getUrlParts(window.location.href);
if (urlParts.hash) {
  Event.observe(document, 'dom:loaded', function() {
    if (document.body) document.body.setStyle('visibility:hidden');
  });
  window.location.href = urlParts.hash
}

