// accordion.js v2.0
//
// Copyright (c) 2007 stickmanlabs | http://www.stickmanlabs.com
//  - Kevin P Miller
// 
// Accordion is freely distributable under the terms of the MIT-style license.
//

/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined') { 
	throw("accordion.js requires including script.aculo.us' effects.js library!");
}

var Accordion = Class.create({
  
  duration: null,
  animating: false,
  container: null,
  pairs: [],
  
	initialize: function(container, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed: 8,
			selectors: {
				toggle: '.toggle',
				content: '.content'
			},
			order: null,
			show: null,
			size: {
			  width: null,
			  height: null
			},
			fixes: {
			  opacity: true
			},
			vertical: true,
			horizontal: false
		}, options || {});
		
		this.container = $(container);
		
		if (this.options.fixes.opacity) {
		  this.container.setStyle({
		    opacity: 0.99999
		  });
		}
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

    var toggles = $$('#'+container+' '+this.options.selectors.toggle);
    var contents = $$('#'+container+' '+this.options.selectors.content);
    for (var x = 0; x < toggles.length; x++) {
      if (this.options.order && this.options.order[x]-1 <= x) {
        pair = {
          toggle: $(toggles[x]), 
          content: $(contents[this.options.order[x]-1])
        };
      } else {    
        pair = {
          toggle: $(toggles[x]),
          content: $(contents[x])
        };
      }
      
      Event.observe(pair.toggle, 'mouseover', this.onMouseover.bindAsEventListener(this, x, pair), true);
      Event.observe(pair.toggle, 'mouseout', this._onMouseout.bindAsEventListener(this, x, pair), true);
      Event.observe(pair.toggle, 'click', this.onClick.bindAsEventListener(this, x, pair), true);

      
      this.pairs.push(pair);
    }
    
    this.onCreate();
	},
	
	onCreate: function() {
	  return false;
	},
	
  onMouseover: function(event, index, pair) {
	  return false;
  },
  
  onMouseout: function(event, index, pair) {
	  return false;
  },
	
  onClick: function(event, index, pair) {
    this.activate(event, index, pair);
  },
  
  onOpen: function(event, index, pair) {
	  return false;
  },
  
  onClose: function(event, index, pair) {
	  return false;  
  },
  
	activate: function(event, index, pair) {
    this.onOpen(event, index, pair, true);
	},

	deactivate : function(event, index, pair) {
    this.onClose(event, index, pair);
	},
	
  // Private Methods
  
  _isChildOf: function(parent, child) {
    if( child != null ) {     
      while( child.parentNode ) {
        if( (child = child.parentNode) == parent ) {
          return true;
        }
      }
    }
    return false;
  },  
  
  _onMouseout: function (event, index, pair) {

    var element = Event.element(event);
    
    if(event.toElement) {       
      var target = event.toElement;
    } else if(event.relatedTarget) {        
      var target = event.relatedTarget;
    }
    
    if(!this._isChildOf(element, target) && element != target) {
      this.onMouseout(event, index, pair);
    }  
  }
	
});

var TabAccordion = Class.create(Accordion, {
  
  counter: 0,
  active: false,
  
  initialize: function($super, container, options) {
    $super(container, options);
    
    this.pairs.reverse();

    this.pairs.each(function(pair){
      
      pair.content.setStyle({
        display: 'none'
      });      
          
      pair.toggle.makePositioned();
      pair.content.makePositioned();

      if (this.counter == this.options.default) {
        this.onOpen(null, null, pair, false);
        this.active = pair;
      }
      
      this.counter = this.counter+1;
    }.bind(this));

		$$('.filter-checkbox').each(function(checkbox) {
		  checkbox.checked = true;
		  Event.observe(checkbox, 'change', this.filter.bindAsEventListener(this), true);
		}.bind(this));


  },


	filter: function() {

    new Effect.ScrollTo('feature-focus', {
		  duration: 0.65,
		  transition: Effect.Transitions.sinoidal
	  });
	  
    new Effect.Appear('feature-overlay', {
      duration: 0.6, 
      to: 0.999999,
      queue: {
        position: 'end',
        scope: 'tabFilter_animation'
      },
      afterFinish: function(effect) {
       
        var queryString = $('filter-form').serialize();

        this.active = 0;
        this.x = 0;
        this.pairs.each(function(pair){
          if ($(pair.content).getStyle('display') == 'block') {
            this.active = this.x;
          }
          this.x = this.x + 1;
        }.bind(this));

        new Ajax.Request('/wp/wp-content/themes/twbiz/features.php', {
          method: 'get',
          parameters: queryString,
          onComplete: function (t) {

            this.pairs[this.active].content.innerHTML = t.responseText;

            new Effect.Fade('feature-overlay', {
              duration: 0.6,
              queue: {
                position: 'end',
                scope: 'tabFilter_animation'
              }
            });
            
          }.bind(this)
        });
        
      }.bind(this)
    });
    
    var queryString = $('filter-form').serialize();

    this.active = 0;
    this.x = 0;
    this.pairs.each(function(pair){
      if ($(pair.content).getStyle('display') == 'block') {
        this.active = this.x;
      }
      this.x = this.x + 1;
    }.bind(this));
  },
  
  onOpen: function(event, index, pair, scroll) {
    
    if (scroll) {
      new Effect.ScrollTo('feature-focus', {
			  duration: 0.65,
			  transition: Effect.Transitions.sinoidal
		  });
		}

    var effects = [];
    
    if (this.active) {
      this.active.toggle.removeClassName('feature-tab-on').addClassName('feature-tab-off');
      effects.push(
        new Effect.Fade(this.active.content, {
          sync: true,
          to: 0.0
        })
      );
    }
    
    pair.toggle.removeClassName('feature-tab-off').addClassName('feature-tab-on');
    effects.push(
      new Effect.Appear(pair.content, {
        sync: true,
        from: 0.0,
        to: 0.99999
      })
    );

    new Effect.Parallel(effects, {
      duration: this.duration,
      queue: {
        position: 'end',
        scope: 'tab_accordion'
      }
    });    
    
    this.active = pair;
  }
  
});
