(function($){

    var activeTabId = '';
    var activeTabName = '';
    var changedPrintEvent = false;

    $(document).ready(function() {
        activeTabId = $("div.faq-category:first").attr("id");
        activeTabName = activeTabId.substring(activeTabId.indexOf("-") + 1, activeTabId.length);
    });

    $(function() {

		$("div.faq-category").hover(
			function() {
				$(this).addClass("active");
                var bottomSrc = $(this).next().attr('src');
                $(this).next().attr('src', bottomSrc.replace('-off', '-on'));
            }, function() {
                var tabId = $(this).attr("id");
                if (tabId != activeTabId) {
                    $(this).removeClass("active");
                    var bottomSrc = $(this).next().attr('src');
                    $(this).next().attr('src', bottomSrc.replace('-on', '-off'));
                }
			}
		);

		$("div.faq-category").click(
			function() {
				activeTabId = $(this).attr("id");
                activeTabName = activeTabId.substring(activeTabId.indexOf("-") + 1, activeTabId.length);
				$("div.faq-category").removeClass("active");
				$(this).toggleClass("active");

                if (!changedPrintEvent) {
                    $('.printLink').each(function() {
                        $(this)[0].onclick = null;
                        $(this).click(function() {
                            popFAQSectionForPrinting();
                            return false;
                        });
                        changedPrintEvent = true;
                    });
                }

                $('#faq-categories img').each(function() {
                    var bottomSrc = $(this).attr('src');
                    $(this).attr('src', bottomSrc.replace('-on', '-off'));
                });
                var bottomSrc = $(this).next().attr('src');
                $(this).next().attr('src', bottomSrc.replace('-off', '-on'));

                $("div.faqContent-categoryContent").css("display","none");
				$("#faqContent-" + activeTabName).css("display","block");
            }
		);

		$("div.faq-questionAnswer li.link a").click(
			function() {
				if($(this).parents("div.faq-questionAnswer").children("div:first").hasClass("expand")) {
					$(this).parents("div.faq-questionAnswer").find("div.expand").slideDown("normal");
					$(this).parents("div.faq-questionAnswer").find("div.expand").addClass("collapse");
					$(this).parents("div.faq-questionAnswer").find("div.expand").removeClass("expand");
                    $(this).parents("li.link:first").addClass("arrowDown");
                } else {
					$(this).parents("div.faq-questionAnswer").find("div.collapse").slideUp("normal");
					$(this).parents("div.faq-questionAnswer").find("div.collapse").addClass("expand");
					$(this).parents("div.faq-questionAnswer").find("div.collapse").removeClass("collapse");
                    $(this).parents("li.link:first").removeClass("arrowDown");
                }
                return false;
            }
		);
    });

    function popFAQSectionForPrinting() {
        window.open('/tips-and-know-how/faqs?print=true&section=' + $("#" + activeTabId).text().replace(" ", ""), 'print', 'width=670,scrollbars=yes');
    }

})(jQuery);

