// run jQuery in noConflict mode to avoid problems with prototype (caused by the powermail extension)
jQuery.noConflict();

/**
 * Main 
 * 
 * @author Fabrizio Branca, punkt.de
 * @since 2008-10-13
 * @version $Id: faq.js,v 1.1 2008/10/16 10:11:10 ry36 Exp $
 */
jQuery(document).ready(function(){
	
	// hide all faqitem-content element
	jQuery('.faq-list-container .faqitem-content').hide();
	
	// generate idsfor all faq-list-item elements
	jQuery.generateId.prefix = 'jq$faq' + jQuery('body').attr('id');
	jQuery.generateId.count = 0;
	jQuery('.faq-list-container .faq-list-item').generateId();
	
	// restore cookie State
	var faqCookie = jQuery.cookie('faq') || '';
	jQuery('.faq-list-container .faq-list-item').each(function() {
		if (faqCookie.indexOf(jQuery(this).attr('id') + ',') > -1) {
			expandFaqItem(jQuery(this));
		}	
	});
	
	// expand on click on the "right icon"
	jQuery('.faq-list-container .faqitem-right').click(function() {
		expandFaqItem(jQuery(this).parents('.faq-list-item'));
		return false;
	});
	
	// collapse on click on the "down icon" or the "close button"
	jQuery('.faq-list-container .faqitem-down, .faq-list-container .faqClose').click(function() {
		collapseFaqItem(jQuery(this).parents('.faq-list-item'));
		return false;
	});
});



/**
 * Collapses a faq item
 * 
 * @param {Object} div.faq-list-item element
 * @author Fabrizio Branca, punkt.de
 * @since 2008-10-13
 */
function collapseFaqItem(item) {
	
	// update status in cookie
	var faqCookie = jQuery.cookie('faq') || '';
	faqCookie = faqCookie.replace(item.attr('id') + ',', '');
	jQuery.cookie('faq', faqCookie, {path: '/'});
	
	// hide element and swap arrow icon
	item.children('.faqitem-content').hide();
	item.find('.faqitem-down').addClass('inactive');
	item.find('.faqitem-right').removeClass('inactive');	
}



/**
 * Expands a faq item
 * 
 * @param {Object} div.faq-list-item element
 * @author Fabrizio Branca, punkt.de
 * @since 2008-10-13
 */
function expandFaqItem(item) {
	
	// update status in cookie
	var faqCookie = jQuery.cookie('faq') || '';
	if (faqCookie.indexOf(item.attr('id') + ',') == -1) {
		faqCookie += item.attr('id') + ',';
		jQuery.cookie('faq', faqCookie, {path: '/'});
	}
	
	// show element and swap arrow icon
	item.children('.faqitem-content').show();
	item.find('.faqitem-right').addClass('inactive');
	item.find('.faqitem-down').removeClass('inactive');
}

/**
 * Expand all faq items
 */
function toggleAllFAQs() {
	if (jQuery('#toggleAllFAQs').attr('class') == 'hide') {
		jQuery('.faq-list-container .faq-list-item').each(function() {
			expandFaqItem(jQuery(this));
			jQuery('#toggleAllFAQs').attr('class', 'show');
		});
	} else {
		jQuery('.faq-list-container .faq-list-item').each(function() {
			collapseFaqItem(jQuery(this));
			jQuery('#toggleAllFAQs').attr('class', 'hide');
		});
	}
	return false;
}
