

/*------------faqs.js----------*/

reg.postSetup(function() {
	$('a[href$="/pop-privacy"]').fancybox({
		'overlayShow':true,
		'frameWidth':500,
		'frameHeight':350
	});
	
	// define easing
	//jQuery.easing.def = 'easeOutBounce';
});

// client login form in header
reg.click('a#alllink', function(e) {
	toggleAll();
	return false;
});

// cats
reg.click("a@id^='catlink'", function(e) {
	id = this.id.split('catlink');
	toggleCategory(id[1]);
	return false;
});

// qs
reg.click("a@id^='toggle'", function(e) {
	id = this.id.split('toggle');
	toggleFAQ(id[1]);
	return false;
});

function toggleFAQ(id) {
	// open/close 
	if ($("#faq" + id + " .answer").is(":hidden")){
	  //fx
   	$("#faq" + id + " .answer").slideDown("slow");
		
		// add to view count
		$.post("/ajax/trackFaqView", {id: id });
  } else {
		//fx
		$("#faq" + id + " .answer").slideUp("slow");
	}
	
	//+- icons
	$("#faq" + id).toggleClass('opened');
}

function toggleCategory(id) {
	// update class
	if ($("#cat" + id).attr('class') == 'opened') {
		$("#cat" + id).removeClass('opened');
    $("#cat" + id).addClass('closed');
	} else { 
		$("#cat" + id).removeClass('closed');
    $("#cat" + id).addClass('opened');
	}
	
	// toggle text
	$("#catlink" + id).text($("#catlink" + id).text() == "close all" ? "open all" : "close all");
	
	// open/close each
	$("#cat" + id).find(".answer").each(function(i) {
	  // slide down if class is open, else slide up
	  if ($("#cat" + id).attr('class') == 'opened'){
   		$(this).slideDown("slow");
	  } else {
	 		$(this).slideUp("slow");
		}
	});
	
	// +/- icons
	$("#cat" + id).find("li").each(function(i) {
		 if ($("#cat" + id).attr('class') == 'opened'){
			$(this).removeClass('opened');
	    $(this).addClass('opened');
		} else {
			$(this).removeClass('opened');
		}
	});
}

function toggleAll() {
  // change text
	$("#alllink").text($("#alllink").text() == "close all faqs" ? "open all faqs" : "close all faqs");
	// switch class
	$("#alllink").toggleClass('opened');
	
	// open/close each
	$("#faqs").find(".answer").each(function(i) {
	  // slide down if class is open, else slide up
	  if ($("#alllink").attr('class') == 'opened'){
   		$(this).slideDown("slow");
	  } else {
	 		$(this).slideUp("slow");
		}
	});
	
	// +/- icons for faqs
	$("#faqs").find("li").each(function(i) {
	   if ($("#alllink").attr('class') == 'opened'){
			$(this).removeClass('opened');
	    $(this).addClass('opened');
		} else {
			$(this).removeClass('opened');
		}
	});
	
	// category text
	$("#faqs").find("h4 a").each(function(i) {
	  if ($("#alllink").attr('class') == 'opened'){
	  	$(this).text("close all");
		} else {
			$(this).text("open all");
		}
	});
	
	// category class	
	$('div[id^=cat]').each(function(i) {
	 if ($("#alllink").attr('class') == 'opened'){  		
			$(this).removeClass('opened');
    	$(this).addClass('closed');
	  } else {
	 		$(this).removeClass('closed');
    	$(this).addClass('opened');
		}
	});
	
	// open/close each faq
	$("#faqs").find("div").each(function(i) {
	  if ($(this).attr('class') == 'opened') {
			$(this).removeClass('opened');
	    $(this).addClass('closed');
		} else { 
			$(this).removeClass('closed');
	    $(this).addClass('opened');
		}
	});
}
