

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

reg.postSetup(function() {
	// submit cat change on select
	$('#selected_category').change(function(obj) {
		selected = $('#selected_category option[@selected]');
		selected_cat = 'cat' + selected.val();
		
		// category class	
		$('div[id^=cat]').each(function(i) {
			if (selected_cat == 'cat') {
				$(this).removeClass('hidden');
			} else if ( $(this).attr('id') == selected_cat) {
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	})
});

// disable/enable form actions
function disableFaqAction() {
	//$('form#form-contact #contact_send').blur().attr({'disabled':'disabled'}).hide();
	//$('form#form-contact #contact-send-button').append('<img id="contact-processing" src="/images/lion_running_onwhite.gif" width="47" height="20" alt="processing..." title="processing..." />');
}
function enableFaqAction(container) {
	//$('form#form-contact #contact-processing').remove();
	//$('form#form-contact #contact_send').attr({'disabled':''}).show();
}

// clear error styled fields for a given form
function clearFaqFormErrors(id) {
	var list = $('#' + id + ' ul > li[id]');
	list.each(function() {
		$(this).removeClass('error');
	});
}

// submit form via Ajax call to controller method
function submitFaqFormAjax(obj) {
	// set values
	var form      = $(obj).parents('form');
	var form_id   = form.attr('id');
	var container = $(obj).parent();
	
	// disable action
	disableFaqAction(container);
	
	// post form
	form.ajaxSubmit({
		dataType: 'json',
		success: function(response) {
			// re-enable actions
			enableFaqAction(container);
			
			// errors?
			if (response.errors) {
				// clear previous errors
				clearFaqFormErrors(form_id);
				
			  displayErrorMessage(response.errors)
				
				// scroll to the top of the page
				// so we can see the notice
				$.scrollTo('#user-notice', 800, {offset:-15});
			} else if (response.notice) {
				// clear errors
				clearFaqFormErrors(form_id);
				
				// reset form
				form.resetForm();
				
				// display notice
				setNotice(escape(response.notice));
				
				// scroll to the top of the page
				// so we can see the notice
				$.scrollTo('#user-notice', 800, {offset:-15});
			} else if (response.success) {
	      // reset and hide
				clearNotice();
				form.resetForm();
				

 				// add success message
				setNotice(escape(response.success), 'message');               
			} else if (response.redirect) {
				// redirect
			  location.href = response.redirect;
			} else {
				// display notice
				setNotice(escape('There was an error processing your request. Please try again.'), 'error');
			}
		}
	});
}

// all
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("/faqs/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 this category" ? "expand this category" : "close this category");
	
	// 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 this category");
		} else {
			$(this).text("expand this category");
		}
	});
	
	// 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');
		}
	});
}
