/**
 * Script: class.mainNavigation.js
 *	Compatible with:
 *		mootools-1.2.4-core.js
 *		mootools-1.2.4.2-more.js
 *
 *	Description: spice up simple navigations
 */
var mainNavigation = new Class({
	Implements: [Events, Options],

	options: {
    containerId: 'container',
    containerFocusOpacity: 1,
    containerNoFocusOpacity: 1
	},

	initialize: function(options) {
		this.setOptions(options);
    this.container = document.id(this.options.containerId);
    this.container.store('linkClicked', false);
    this.buttonsDimmed = false;

		this.bound = {
		  click: this.handleClick.bindWithEvent(this),
		  open: this.open,
		  close: this.close,
		  closeFast: this.closeFast
		};

    this.buttons = this.container.getElements('li');
	  this.buttons.each(function(btn) {
	    var link = btn.getElement('a');
	    btn.set({
	      opacity: this.options.containerFocusOpacity,
        morph: {
          duration: (Browser.Engine.trident ? 100 : 250),
          transition: (Browser.Engine.trident ? 'sine:in' : 'back:out')
        }
      }).store('href', link.get('href'));
	    link.set({
	      title: '',
        morph: {
          duration: 100,
          transition: (Browser.Engine.trident ? 'sine:in' : 'expo:out')
        }
      });
      this.attach(btn);
    }.bind(this));

    //this.timer = null;
	},

  attach: function(element, attach) {
    var method = $pick(attach, true) ? 'addEvents' : 'removeEvents';
    element[method]({
      click: this.bound.click,
      mouseenter: this.bound.open,
      mouseleave: this.bound.close,
      close: this.bound.close,
      closeFast: this.bound.closeFast,
      open: this.bound.open
    });
    return this;
  },

  detach: function(element) {
    this.attach(element, false);
  },

  handleClick: function(e) {
    e.stop();
    var btn = e.target;
    if(btn.get('tag') !== 'li') btn = btn.getParent('li');
    btn.store('active', true);
    var otherBtns = this.buttons.filter(function(element) {
      if(element != btn) return true;
    });
    $each(otherBtns, function(otherBtn) {
      otherBtn.store('active', false).fireEvent('close');
    });
    this.container.store('linkClicked', true);
		this.fireEvent('linkClicked', btn.retrieve('href'));
  },

  open: function() {
    //$clear(this.timer);
    this.morph('.mainnavigation-li-active');
    this.getElement('a').morph('.mainnavigation-li-a-active');
  },

  close: function() {
    //$clear(this.timer);
    if(!this.retrieve('active')) {
      this.morph('.mainnavigation-li-inactive');
      this.getElement('a').morph('.mainnavigation-li-a-inactive');
    }
  },

  closeFast: function() {
    if(!this.retrieve('active')) {
      new Fx.Morph(this).set('.mainnavigation-li-inactive');
      new Fx.Morph(this.getElement('a')).set('.mainnavigation-li-a-inactive');
    }
  },

  dim: function(state) {
    if(!$defined(state)) state = true;
    this.buttonsDimmed = state;
    var activeBtns = this.buttons.filter(function(element) {
      if(element.retrieve('active')) return true;
    });
    var otherBtns = this.buttons.filter(function(element) {
      if(!activeBtns.contains(element)) return true;
    });
    if(!state) {
      otherBtns.store('dimmed', false).set('opacity', this.options.containerFocusOpacity);
    } else {
      otherBtns.store('dimmed', true);
      var obj = {};
  		otherBtns.each(function(otherBtn, i) {
  			obj[i] = {
  			  'opacity': this.options.containerNoFocusOpacity
  			};
  		}.bind(this));
      new Fx.Elements(otherBtns, {
        link: 'cancel',
        duration: 600,
        transition: 'pow:in:out'
      }).start(obj);
    }
  },

	setActiveByLink: function(link) {
    var btn = this.buttons.filter(function(element) {
      if(element.retrieve('href') === link) return true;
    });
		btn = btn[0];
    btn.store('active', true);
    var otherBtns = this.buttons.filter(function(element) {
      if(element != btn && element.retrieve('active')) return true;
    });
    $each(otherBtns, function(otherBtn) {
      otherBtn.store('active', false);
      if(this.buttonsDimmed) {
        otherBtn.fireEvent('closeFast');
        otherBtn.set('opacity', this.options.containerNoFocusOpacity)
      } else {
        otherBtn.fireEvent('close');
      }
    }.bind(this));
    btn.fireEvent('open');
    this.container.store('linkClicked', true);
	}
});