

var rAccordion = new Class({
	
	initialize: function(container, toggleClass, elementClass, options){
		this.container = container;
		this.tClass = toggleClass;
		this.eClass = elementClass;
		this.options = options;
		this.selector = '#' + this.container + ' > .';
		this.makeAccordion();
	},
	
	makeAccordion: function(){
		new Accordion(
			$$(this.selector+this.tClass),
			$$(this.selector+this.eClass),
			this.options
		).addEvents({
			// The onActive and onComplete events added to the stack here to
			// attempt to address some of the css issues.
			'onActive': function(toggle,element){
				if(toggle.getParent().getStyle('height') != 0)
					toggle.getParent().setStyle('height', '');
         			toggle.setStyle('font-weight', 'bold');
         			toggle.setStyle('color', '#fff799');	
         			
         			
				if(toggle.getParent().getParent().getStyle('height') != 0)
					toggle.getParent().getParent().setStyle('height', '');
         			toggle.setStyle('font-weight', 'bold');
         			toggle.setStyle('color', '#fff799');	
         			
         			
				if(toggle.getParent().getParent().getParent().getStyle('height') != 0)
					toggle.getParent().getParent().getParent().setStyle('height', '');
         			toggle.setStyle('font-weight', 'bold');
         			toggle.setStyle('color', '#fff799');	
         			
         			if(toggle.getParent().getParent().getParent().getParent().getStyle('height') != 0)
					toggle.getParent().getParent().getParent().getParent().setStyle('height', '');
         			toggle.setStyle('font-weight', 'bold');
         			toggle.setStyle('color', '#fff799');
         			
         			
			},
			'onBackground': function(toggle,element) {
				if(toggle.getStyle('font-style') != 'italic') {
         			toggle.setStyle('font-weight', 'normal');
         			}
         			toggle.setStyle('color', '#f2ebc7');
			},
			'onComplete': function(a){
				if ($defined(a)) {
					var height = 0;
					a.getParent().getParent().getChildren().each(function(e){
						height = height + e.offsetHeight;
					});
					if(height != a.getParent().offsetHeight && 			a.getParent().offsetHeight != 0)
					a.getParent().setStyle('height','');
						
					if(height != a.getParent().getParent().offsetHeight && a.getParent().getParent().offsetHeight != 0)
					a.getParent().getParent().setStyle('height','');
						
					if(height != a.getParent().getParent().getParent().offsetHeight && a.getParent().getParent().getParent().offsetHeight != 0)
					a.getParent().getParent().getParent().setStyle('height','');
				}
			}
			
		});
		this.selector += this.eClass + ' > .';
		if($defined($$(this.selector)[0]))
			this.makeAccordion();
	}
});


window.addEvent('domready', function(){
		if(openThis == undefined) {
		openThis = -1;
		}
		if(openThisSub == undefined) {
		openThisSub = -1;
		}
		if(openThisSub2 == undefined) {
		openThisSub2 = -1;
		}
	new rAccordion('container', 'toggle', 'element', {
		alwaysHide: true,
		//start:'all-closed',
		show: openThis,
		opacity: false,
		duration: 300,
		onActive: function(toggle){
			toggle.addClass('active');
		},
		onBackground: function(toggle,element){
			toggle.removeClass('active');
			element.setStyle('height', element.offsetHeight);
			element.setStyle('height','0');
		}
	});
	
	new rAccordion('container2', 'toggle2', 'element2', {
		alwaysHide: true,
		//start:'all-closed',
		show: openThisSub,
		opacity: false,
		duration: 300,
		onActive: function(toggle2){
			toggle2.addClass('active');
		},
		onBackground: function(toggle2,element2){
			toggle2.removeClass('active');
			element2.setStyle('height', element2.offsetHeight);
			element2.setStyle('height','0');
		}
	});
	
	new rAccordion('container3', 'toggle3', 'element3', {
		alwaysHide: true,
		//start:'all-closed',
		show: openThisSub2,
		opacity: false,
		duration: 300,
		onActive: function(toggle3){
			toggle3.addClass('active');
		},
		onBackground: function(toggle3,element3){
			toggle3.removeClass('active');
			element3.setStyle('height', element3.offsetHeight);
			element3.setStyle('height','0');
		}
	});
});

