/*********************************************************
Modified fx.Accordion, added the open / close feature and
the image replace (arrow down / arrow up). You need to 
pass the arrow images url when you create this accordion.
--Johnny 7.25.2008
*********************************************************/

fx.AccordionRev = Class.create();
fx.AccordionRev.prototype = {
	setOptions: function(options) {
		this.options = {
			delay: 100,
			opacity: false
		}
		Object.extend(this.options, options || {});
	},

	initialize: function(togglers, elements, options, open, close) {
		this.elements = elements;
		this.setOptions(options);
		var options = options || '';
		this.open = open;
		this.close = close;
		this.togglers = togglers;
		
		elements.each(function(el, i){
			options.onComplete = function(){
				if (el.offsetHeight > 0) el.style.height = '1%';
			}
			el.fx = new fx.Combo(el, options);
			el.fx.hide();
		});

		togglers.each(function(tog, i){
			tog.onclick = function(){
				this.show(elements[i]);
			}.bind(this);
		}.bind(this));
	},

	showThisHideOpen: function(toShow){
		if (toShow.offsetHeight == 0) setTimeout(function(){this.clearAndToggle(toShow);}.bind(this), this.options.delay);
		this.elements.each(function(el, i){
			if (el.offsetHeight > 0 && el != toShow){
			    this.clearAndToggle(el);
			    var img = this.getChildImage(this.togglers[i]);
			    img[0].src = this.open;
			}
			if(el == toShow){
			    var img = this.getChildImage(this.togglers[i]);
			    img[0].src = this.close;
			}
		}.bind(this));
	},

	clearAndToggle: function(el){
		el.fx.clearTimer();
		el.fx.toggle();
		
		var tog = this.getToggler(el);
		var img = this.getChildImage(tog);
		if(el.offsetHeight > 0){
			img[0].src = this.open;
		}
		else
		{
		  img[0].src = this.close; 
		}
		
	},
	show: function(toShow){
	    if(toShow.offsetHeight == 0){
	         this.showThisHideOpen(toShow);
	    }
	    else
	    {
	        this.clearAndToggle(toShow);
	     
	    }
	    
	},
	getChildImage: function(tog){
	    var childCollection;
	    if(tog){
	        childCollection = tog.childNodes;
	     }
	     return childCollection;
	},
	getToggler: function(el){
	          var tog;
	          this.elements.each( function(e, i){
	               if(e == el) tog = this.togglers[i];
	               }.bind(this));
	               
	          return tog;
	 }                   
	    
}