// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Element._insertionTranslations.replace = Element.replace;

Application = {
  Helpers: {
    Nav: {
      initialize: function() {
        //this.initializeHoverEffects();
        this.initializeSelection();
      },

      initializeSelection: function() {
        var selectedNav = $('nav').down('.selected');
        if (!selectedNav) return;
        var selection = $('selection');
        selection.style.left = selectedNav.parentNode.offsetLeft + selectedNav.offsetWidth / 2 - 10 + 'px';
        selection.visualEffect('appear', {duration: 0.5});
      },

      initializeHoverEffects: function() {
        this.registerNavEvent('mouseover', function(element) {
          element.morph({'font-size': '20px'}, {duration: 0.2, queue: 'front'});
        });

        this.registerNavEvent('mouseout', function(element) {
          element.morph({'font-size': '16px'}, {duration: 0.1, queue: 'end'});
        });
      },

      registerNavEvent: function(type, callback) {
        $('nav').observe(type, function(event) {
          var element = Event.element(event);
          if (element.tagName == 'A') {
            callback(element);
          }
        });
      }
    },

    Products: {
      initialize: function() {
        Element.observe(document, 'mouseover', function(event) {
          var element = event.element();
          if (element.match('.product img.product_thumbnail')) {
            element.fx && element.fx.cancel();
            element.fx = new Effect.Morph(element.up('.product').down('.price_tag'), {
              style: 'padding-left: 20px',
              duration: 0.2
            });
          }
        });

        Element.observe(document, 'mouseout', function(event) {
          var element = event.element();
          if (element.match('.product img.product_thumbnail')) {
            element.fx && element.fx.cancel();
            element.fx = new Effect.Morph(element.up('.product').down('.price_tag'), {
              style: 'padding-left: 10px',
              duration: 0.2,
              queue: 'end'
            });
          }
        });
      }
    },

    Pagination: {
      initialize: function() {
        Element.observe(document, 'mouseover', function(event) {
          var element = event.element();
          if (element.match('.pagination a')) {
            element.fx && element.fx.cancel();
            element.fx = new Effect.Morph(element, {
              style: 'margin-left: 4px; margin-right: 4px',
              duration: 0.2,
              fps: 30
            });
          }
        });

        Element.observe(document, 'mouseout', function(event) {
          var element = event.element();
          if (element.match('.pagination a')) {
            element.fx && element.fx.cancel();
            element.fx = new Effect.Morph(element, {
              style: 'margin-left: 0px; margin-right: 0px',
              duration: 0.2,
              fps: 30
            });
          }
        });
      }
    }
  },

  DomExtensions: {
    Form: {
      clear: function(element) {
        element = $(element);
        element.getElements().each(function(element) {
          if (element.tagName == 'INPUT' && element.type == 'hidden') return;
          element.clear();
        });
        return element;
      }
    }
  },

  initialize: function() {
    this.initializeHelpers();
    this.initializeDomExtensions();
  },

  initializeHelpers: function() {
    for (helper in Application.Helpers) {
      if (Application.Helpers[helper].initialize) {
        Application.Helpers[helper].initialize();
      }
    }
  },

  initializeDomExtensions: function() {
    for (extension in Application.DomExtensions) {
      if (extension != 'Element' && window[extension]) {
        Object.extend(window[extension], Application.DomExtensions[extension]);
      }
      Element.addMethods(Application.DomExtensions[extension]);
    }
  }
}
