var faqFadeDuration = 400;
var faqCurrentItem = null;
var faqQuestions = null;
var faqAnswers = null;
var faqTransitioning = false;
var faqHeights = new Array();
 
function faqInit() {
	$$('.ab-content-wrapper .page-content .faq').addEvent('click', function(e) { faqToggle (e, this); } );
	faqQuestions = $$('.ab-content-wrapper .page-content .faq');
	faqAnswers = $$('.ab-content-wrapper .page-content .faqanswer');
	
	for ( var i = 0; i < faqAnswers.length; i++ ) {
		faqAnswers[i].style.display = 'block';
		faqHeights[i] = faqAnswers[i].measure( function() { return this.getSize() } ).y;
		faqAnswers[i].style.height = '0px';
	}
}

function faqGetItem ( sender ) {
	for ( var i = 0; i < faqQuestions.length; i++ ) {
		if ( faqQuestions[i] == sender ) return i;
	}
}

function faqGetCurrentItem () {
	return faqGetItem(faqCurrentItem);
}

function faqToggle(e, sender) {
	e = new Event(e).stop();
	
	if(faqTransitioning) return;
	 faqTransitioning = true;
	
	var nextItem = faqGetItem(sender);
	
	var completeHandler = function() { };
	if ( faqAnswers[nextItem] == faqCurrentItem ) {
		completeHandler = function () { 
			faqCurrentItem = null; faqTransitioning = false;
			
			$('element-' + currentIdx).style.height = $$('#element-' + currentIdx + ' .ab-content-wrapper')[0].getSize().y + 'px'; 
		}
	} else {
		completeHandler = function () {
			faqCurrentItem = null;
			faqOpen( faqAnswers[nextItem], 
				function() { 
					faqCurrentItem = faqAnswers[nextItem];
					faqTransitioning = false;
					$('element-' + currentIdx).style.height = $$('#element-' + currentIdx + ' .ab-content-wrapper')[0].getSize().y + 'px'
			 	},
			 	faqHeights[nextItem]
			); 
		}
	} 
	
	if ( faqCurrentItem != null )  { 
		faqClose( faqCurrentItem, completeHandler );
	} else {
		faqOpen( faqAnswers[nextItem], function() { 
			faqCurrentItem = faqAnswers[nextItem]; faqTransitioning = false; 
			$('element-' + currentIdx).style.height = $$('#element-' + currentIdx + ' .ab-content-wrapper')[0].getSize().y + 'px' 
		}, faqHeights[nextItem] );
	}
	  
}

function faqOpen ( item, closeEvent, height ) {
	item.set('morph', { duration: faqFadeDuration, onComplete: closeEvent, transitions: Fx.Transitions.Sine.easeInOut } );
	item.morph( { opacity: [0, 0.85], height: [0, height ], marginTop: [0, 20], marginBottom: [0, 20] } ) ;
}

function faqClose ( item, closeEvent  ) {
	item.set('morph', { duration: faqFadeDuration, onComplete: closeEvent, transitions: Fx.Transitions.Sine.easeInOut } );
	item.morph( { opacity: 0, height: 0, marginTop: 0, marginBottom: 0 } ) ;
}