var BrandNav = Class.create({

    isAnimating: false,

    nextItem: null,

    overThreshold: false,

    initialize: function(config) {

      if(config.id === undefined) throw new Error("Must provide an element id to create nav.");
      this.nav = $(config.id);
      if(config.contentId === undefined) throw new Error("Must provide an element id for content div.");
      this.contentElement = $(config.contentId);

      this.navItems = this.nav.select("li");

      var count = this.navItems.length;
      for(var i=0;i<count;i++) {
        this.navItems[i].observe("mouseover", this._handleMouseOver.bind(this));
      }

      this.nav.observe("mousemove", this._trackMotion.bind(this));

    },

    _trackMotion: function(event) {
      if(this._oldPX == null ||
         this._oldPY == null) {
        this._oldPX = event.pointerX();
        this._oldPY = event.pointerY();
      } else {
        var diffX = this._oldPX - event.pointerX();
        var diffY = this._oldPY - event.pointerY();

        this.overThreshold = false;
        if(diffX > 11 ||
           diffX < -11) this.overThreshold = true;

        this._oldPX = event.pointerX();
        this._oldPY = event.pointerY();
      }

      var target = event.element();
      if(!target.hasClassName("selected") &&
         !this.overThreshold &&
         !this.isAnimating) {
        this._transitionNextItem();
      }
    },

    /** @private */
    _handleMouseOver: function(event) {

      var target = event.element();
      var li = target.parentNode;

      try {
        this.nextItem = li.className.match(/brand[1-9]+/).first();
      } catch(e) {
        this.nextItem = null;
      }

      if(this.isAnimating || 
         li.hasClassName("selected") ||
         this.overThreshold) return;

      var count = this.navItems.length;
      for(var i=0;i<count;i++) {
        if(this.navItems[i].hasClassName("selected")) {
          this.navItems[i].removeClassName("selected");
        }
      }

      li.addClassName("selected");

      this.transitionInfo(li.className.match(/brand[1-9]+/).first());
    },

    _transitionNextItem: function() {
      if(this.nextItem != null) {

        var selected = this.nav.select("li.selected").first();

        if(selected.className.indexOf(this.nextItem) >= 0) {
          this.nextItem = null;
          return;
        }

        var count = this.navItems.length;
        for(var i=0;i<count;i++) {
          if(this.navItems[i].hasClassName("selected")) {
            this.navItems[i].removeClassName("selected");
          }
        }
        
        var li = $$("li."+this.nextItem).first();
        li.addClassName("selected");

        this.transitionInfo(this.nextItem);
        this.nextItem = null;
      }
    },

    transitionInfo: function(brand) {

      this.isAnimating = true;

      function done() {
        this.isAnimating = false;
        this._transitionNextItem();
      };

      function show() {

        this.swapContent(brand);

        var effect = new Effect.Move(this.contentElement, {
          afterFinish: done.bind(this),
          duration: .0,
          transition: Effect.Transitions.sinoidal,
          x: 0, y: 100 });

      };

      var effect = new Effect.Move(this.contentElement, {
          afterFinish: show.bind(this),
          duration: .0,
          transition: Effect.Transitions.sinoidal,
          x: 0, y: -100 });
    },

    swapContent: function(brand) {
      this.contentElement.innerHTML = $(brand+"-info").innerHTML;
    }

  });