$(document).ready(function()
{
    //hide all faq answers
    $(".faqs li span p, .faqs li span ul").hide();

    $("#faqs-form").submit(function(event) {
        event.preventDefault();
    });

    $("#category-select").bind('change keyup', function()
    {
        if ( $(this).find('option:selected').html() == 'All categories' )
        {
            //alert('no cat');
            location.href = "all";
        }
        else
        {
            var cat_id = $(this).find('option:selected').val();

            //check if coming from the hiv-aids faqs
            if( $("#faqs-form").find("#faq-type").length ) {
                location.href = theDomain+"hiv-aids/faqs/"+$("#faq-type").val()+"/"+cat_id;
            }else{
                location.href = cat_id;
            }

        }
    });

    //search input when given focus - slidedown results and clear default value
    $("#faq-search").bind("focus", function(event) {
        if( $(this).val() == 'Enter a search term') $(this).val('');
        $("#faqs-results").slideDown();
    });

    //when search input loses focus clear out results and div
    $("#faq-search").bind("blur", function(event) {
        if( $(this).val() == '')
        {
            $("#faqs-results").children("ol").html('');
            $("#faqs-results").slideUp();
            $(this).val('Enter a search term');
        }
    });

    //on key typing, ajax call database to find any questions with a match to keyword
    var results = '';
    $("#faq-search").bind("keyup", function(event) {
        if( $(this).val() != '') {

            var type = "";
            var path = "../inc/faqs/";

            //check to see if it is the faqs running from the hiv-aids section and change path accordingly
            if( $("#faqs-form").find("#faq-type").length ) {
                type = $("#faqs-form").find("#faq-type").val();
                path = "../../inc/hiv-aids/";
            }

            $.post(path+'search-faqs.php', {term:$(this).val(),type:type},
            function(data) {
                var values = $.parseJSON(data);
                $.each(values, function(){
                    results += '<li><p id="'+this.id+'" class="open">'+this.question+'</p><span><p>'+this.answer+'</p></span></li>';
                });
                if(results == '') results += '<li class="noresults">There were no results found that match your search term.</li>';
                $("#faqs-results").children("ol").html(results);
                $("#faqs-results li span p").hide();
				$("#faqs-results li span ul").hide();
                results = '';
            });
        }else{
            $("#faqs-results").children("ol").html('');
        }
    });

    //faqs accordion
    $('.faqs li p.open').live("click",function()
    {
        //record view count for chosen question
        if( $(this).parent().children("span").children("p").is(":hidden") && $("#faqs-form").find("#faq-type").length == 0 ) {
            $.post('../inc/faqs/record-faqs-view.php', {id:$(this).attr("id")}, function(data) { });
        }
        //close any open questions
        $('.faqs li span p:visible').each(function(){ $(this).slideToggle(); });
		$('.faqs li span ul:visible').each(function(){ $(this).slideToggle(); });
		$('.faqs li span ul li ul:visible').each(function(){ $(this).slideToggle(); });
        $(this).parent().children("span").children("p:hidden").slideToggle('fast');
		$(this).parent().children("span").children("ul:hidden").slideToggle('fast');
		$(this).parent().children("span").children("ul").children("li").children("ul:hidden").slideToggle('fast');

        return false;
    });

    //submit a question form
    $("#faqs-suggest").submit(function(event) {
        event.preventDefault();

		var name = $('#name').val();
		var email = $('#email').val();
		var question =  $('#question').val();

		//Validation
		if(name == "")
		{
			var message = "<h3 class=\"error\">There is something wrong&hellip;</h3><p>You have not entered a name</p>";
			var field_name = "name";
		}
		else if(email == "")
		{
			var message = "<h3 class=\"error\">There is something wrong&hellip;</h3><p>You have not entered an email address</p>";
			var field_name = "email";
		}
        else if(!isEmail(email))
        {
            var message = "<h3 class=\"error\">There is something wrong&hellip;</h3><p>You have not entered a valid email address</p>";
            var field_name = "email";
        }
		else if(question == "")
		{
			var message = "<h3 class=\"error\">There is something wrong&hellip;</h3><p>You have not entered a question</p>";
			var field_name = "question";
		}

		$('input, select, textarea').removeClass('form-field-error');
		$('#'+field_name).addClass('form-field-error');

		//Outputs message, adds to database & sends an email
		if(message === undefined)
		{
			$.post('../inc/faqs/submit-faqs-question.php', {name:name, email:email, question:question},
            function(data){
                lbdialog({content: '<h3 class="success">Question submitted</h3><p>Your question has been sent to us and will be answered soon.  Thank you.</p>'});
            });
		}
		else
		{
			lbdialog({content: message});
		}
    });
});
