// 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: false
			},
			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);
    }  
  }
	
});
