var faq = {
    init:$(function() {
        
        //cache all of the information
        $("div.item").each(function(i,v) {
            var item = $(v);
            var question = item.find(".q");
            var answer = item.find(".a");
            faq.data.push({
                q:question.html(),
                a:answer.html(),
                all:question.text() + " " + answer.text()
                });
        });
        
        //set the handler for the filter
        setTimeout(function() {
            $(".filter").slideDown();
            faq.filter.select();
            }, 500);
            
        faq.filter = $("#filter")
            .keyup(function() {                
                clearTimeout(faq._filterTimeout);
                faq._filterTimeout = setTimeout(
                    function() { faq._checkFilter(faq.filter.val().length); },
                    250                    
                    )})
            
        faq.matchCheck = $("#allMatch")
            .click(faq._filter);
            
        faq.results = $("#results");
        faq.all = $("#all")
        
        //setup the help box
        faq.noResults = $("#noResults");
        faq.help = $("#help");        
        faq.help.find("ul > li > a")            
            .attr("href","javascript:void(0);")
            .click(function() {
                var link = $(this);
                faq.filter.val(link.text());
                faq._filter();
            });
        $("#searchAgain")
            .attr("href","javascript:void(0);")
            .click(function() {
                faq.filter.select();
            });
            
        $(".helpLink > a")
            .attr("href","javascript:void(0);")
            .click(function() {
                faq.noResults.hide();
                faq.help.slideDown();
            });
                

    }),
    
    data:[],
    filter:{},
    results:{},
    all:{},
    words:[],
    matchCheck:{},
    help:{},
    noResults:{},
    allMatch:false,
    _filterTimeout:null,
        
    _checkFilter:function(lastCount) {
        if (faq.filter.val().length != lastCount) { return; }
        clearTimeout(faq._filterTimeout);  
        faq._filter();
    },
    
    _filter:function() {

        //check the filter
        var phrase = $.trim(faq.filter.val().replace(/[^a-z0-9\s]/gi, ""));
        if (phrase.length < 2) {
            faq.results.hide();
            faq.all.show();
            return;
        }
        
        //get the query information        
        faq.allMatch = faq.matchCheck.is(":checked");
        faq.words = phrase.split(/\s{1,}/g);
        
        //run a query to search for this
        var query = jLinq.from(faq.data).ignoreCase();
        if (faq.allMatch) {
            //match a word
            query = query.where(function(rec) {
                for(var i = 0; i < faq.words.length; i++) {
                    var search = new RegExp(faq.words[i]+"+","i");
                    if (rec.all.search(search) == -1) { 
                        return false; 
                    }
                }
                return true;
            });
        } 
        else {
            //match any word
            query = query.match("all", faq.words.join("|"));
        }

        //select the actual records
        var results = query
            .select(function(rec) {
                return {
                    q:faq._format(rec.q),
                    a:faq._format(rec.a)
                };
            });
    
        //regenerate the results
        faq._rebuild(results)

    },
    
    _format:function(content) {
		return content;
    
        //filter single characters        
        var allow = [];
        for(var i = 0; i < faq.words.length; i++) {
            if (faq.words[i].length > 1) { allow.push(faq.words[i]); }
        }
           
        //run the expression as needed
        content = content.replace(
            new RegExp("("+allow.join("|")+")","gi"),
            function(match) { 
                return "<span class=\"highlight\" >" + match + "</span>";
            });
 
        return content;
    },
    
    _rebuild:function(data,words) {

        //create the results container
        var results = $("<div/>")
            .addClass("set"); 

        //show the filtered results
        faq.all.hide();
        faq.results.empty();
        
        //make sure there are results
        if (data.length > 0) {
            faq.help.hide();
            faq.noResults.hide();
            faq.results.append($("<h3/>").text(
                    "Results " + 
                    (faq.matchAny?"that contain":"for") +
                    " '" + 
                    faq.words.join(" ") + 
                    "'"))
                .append(results)            
                .show(); 
        }
        else {
            faq.help.show();
            faq.noResults
                .show()
                .text(
                    "No results found for" + 
                    " '" + 
                    faq.words.join(" ") + 
                    "' were found."
                );
                
        }
            
        //append each result                    
        $.each(data, function(i,result) {
            $("<div/>").addClass("item")
                .append($("<div/>").addClass("q").html(result.q))
                .append($("<div/>").addClass("a").html(result.a))
                .appendTo(results);
        });
        

    }
};