// accordion.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

var contentHeight = 105;

if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

	//
	//  Setup the Variables
	//
	showAccordion : null,
	currentAccordion : null,
	duration : null,
	effects : [],
	animating : false,
	
	//  
	//  Initialize the accordions
	//
	initialize: function(container, defaultAccordion, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				content : 'accordion_content'
			},
			defaultSize : {
				height : contentHeight,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		var accordionsCount = 0;
		accordions.each(function(accordion) {
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			if (this.options.onEvent == 'click') {
			  accordion.onclick = function() {return false;};
			}
			
			this.currentAccordion = $(accordion.next(0));
			
			var options;
			var targetHeight = this.options.defaultSize.height ? ((this.options.defaultSize.height < this.currentAccordion.scrollHeight) ? this.options.defaultSize.height : this.currentAccordion.scrollHeight) : this.currentAccordion.scrollHeight;            
            
            if (accordionsCount != defaultAccordion) {
		        if (this.options.direction == 'horizontal') {
			        options = $H({width: '0px'});
		        } else {
			        options = $H({height: '0px'});
		        }
		        options.merge({display: 'none'});
		    } else {
		        if (this.options.direction == 'horizontal') {
			        options = $H({width: '0px'});
		        } else {
			        options = $H({height: targetHeight+'px'});
		        }
		        options.merge({overflow: 'hidden'});
		        this.showAccordion = $(accordion.next(0));
		    }		    
			
			this.currentAccordion.setStyle(options);
			accordionsCount++;
		}.bind(this));
	},
	
	//
	//  Activate an accordion
	//
	activate : function(accordion) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));
		this.currentAccordion.setStyle({
			display: 'block'
		});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		var toggleHeadImage = this.currentAccordion.previous(0).childNodes[0].src;
		this.currentAccordion.previous(0).childNodes[0].onmouseout = "";
		if (this.currentAccordion == this.showAccordion) {
		    this.deactivate();
		} else {
		    this._handleAccordion();
		}
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {
	    //var targetHeight = this.options.defaultSize.height ? ((this.options.defaultSize.height < this.currentAccordion.scrollHeight) ? this.options.defaultSize.height : this.currentAccordion.scrollHeight) : this.currentAccordion.scrollHeight;
	    var currentScrollHeight = this.currentAccordion.childElements()[0].scrollHeight;
	    var targetHeight = this.options.defaultSize.height ? ((this.options.defaultSize.height < currentScrollHeight) ? this.options.defaultSize.height : currentScrollHeight) : currentScrollHeight;
		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			scaleMode: { 
			    originalHeight: targetHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			},
			afterFinish: function() {
				this.showAccordion.setStyle({
                    height: 'auto',
					display: 'none'
				});				
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});    
        options.merge(this.scaling);

        this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
        
        
        var toggleHeadImage = this.showAccordion.previous(0).childNodes[0].src;
		var toggleHeadImageNew = toggleHeadImage.substring(0, toggleHeadImage.length-6) + toggleHeadImage.substr(toggleHeadImage.length-4);
	    function mouseOut(e) {
            var targ;
            if (!e) var e = window.event;
            if (e.target) targ = e.target;
            else if (e.srcElement) targ = e.srcElement;
            if (targ.nodeType == 3) // defeat Safari bug
	            targ = targ.parentNode;
	        targ.src = toggleHeadImageNew;
        }
	    this.showAccordion.previous(0).childNodes[0].onmouseout = mouseOut;

        new Effect.Scale(this.showAccordion, 0, options);
	},

  //
  // Handle the open/close actions of the accordion
  //
	_handleAccordion : function() {
	    var currentScrollHeight = this.currentAccordion.childElements()[0].scrollHeight;
	    var targetHeight = this.options.defaultSize.height ? ((this.options.defaultSize.height < currentScrollHeight) ? this.options.defaultSize.height : currentScrollHeight) : currentScrollHeight;
	    var options = $H({
		    sync: true,
		    scaleFrom: 0,
		    scaleContent: false,
		    transition: Effect.Transitions.sinoidal,
		    scaleMode: { 
			    originalHeight: targetHeight,
			    originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
		    }
	    });
	    options.merge(this.scaling);
	    
	    this.effects.push(
		    new Effect.Scale(this.currentAccordion, 100, options)
	    );
		

		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			var toggleHeadImage = this.showAccordion.previous(0).childNodes[0].src;
			var toggleHeadImageNew = toggleHeadImage.substring(0, toggleHeadImage.length-6) + toggleHeadImage.substr(toggleHeadImage.length-4);
		    this.showAccordion.previous(0).childNodes[0].src = toggleHeadImageNew;
		    function mouseOut(e) {
	            var targ;
	            if (!e) var e = window.event;
	            if (e.target) targ = e.target;
	            else if (e.srcElement) targ = e.srcElement;
	            if (targ.nodeType == 3) // defeat Safari bug
		            targ = targ.parentNode;
		        targ.src = toggleHeadImageNew;
            }
		    this.showAccordion.previous(0).childNodes[0].onmouseout = mouseOut;
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);				
		}
		
    new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {
					this.showAccordion.setStyle({
						display: 'none',
						overflow: 'hidden'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: targetHeight+'px',
				  overflow: 'hidden'
				});
				
				Scroller.updateAll();
				
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
}
	