$(function() {

  $("#gsfn_search").submit(function () {

  });

  var search_timeout = undefined;
  $("textarea#gsfn_search_topic").keyup(function () {

    if(search_timeout != undefined) {
      clearTimeout(search_timeout);
    };

    //Reference for Use in TimeOut Function
    var elem = $(this);

    search_timeout = setTimeout(
                      function() {

                        search_timeout = undefined;

                        var query = elem.val();
                        var url =  'http://getsatisfaction.com/wego/searches?format=json&limit=5&query='+query;

                        $("div.status").html("Searching for related topics..");

                        $.ajax({url: url,
                                dataType: 'jsonp',
                                success: function (response, status) {

                                  if (response.length > 0) {

                                    $("div.results").css("display","block");

                                    results_html = "<ol>";

                                    for(i=0; i < response.length; i++) {

                                      var style = response[i].style;
                                      var title = response[i].subject;
                                      var url   = response[i].url;

                                      //Start Tag
                                      results_html += "<li class='" + style + "'>";

                                      results_html += "<a href='" + url + "'>" + title + "</a>";

                                      //End Tag
                                      results_html += "</li>";

                                    } //End While

                                    results_html += "</ol>";
                                    $("div.results").append(results_html);

                                    $("div.status").html("Here's what our search squirrels found (if you rephrase, they'll look again).");
                                    $("span#gsfn_search_directions").show();

                                    //Plural vs. Singular
                                    if (response.length == 1) {
                                      $("div.seemore").html("Our search squirrels found " + response.length + " result.").show();
                                    } else {
                                      $("div.seemore").html("Our search squirrels found " + response.length + " results.").show();
                                    }

                                  } else {

                                    $("div.status").html("Our search squirrels didn't find anything that matches.");
                                    $("span#gsfn_search_directions").show();

                                  } //End If

                                  //Scroll to Bottom
                                  var objDiv = document.getElementById("results");
                                  objDiv.scrollTop = objDiv.scrollHeight;

                                }
                               });

                      }, 1000);

  });

  $("a#gsfn_q_tab").click(function () {

    $("div#gsfn").attr("class", "question");
    $("input#gsfn_style").val("question");
    $(" div.status").html("Ask a question. We'll look for answers.");

    return false;

  });

  $("a#gsfn_i_tab").click(function () {

    $("div#gsfn").attr("class", "idea");
    $("input#gsfn_style").val("idea");
    $("div.status").html("Share an idea. We'll see if other people have this idea, too.");

    return false;

  });

  $("a#gsfn_p_tab").click(function () {

    $("div#gsfn").attr("class", "problem");
    $("input#gsfn_style").val("problem");
    $("div.status").html("Report a problem, We'll look for solutions.");

    return false;

  });

  $("a#gsfn_t_tab").click(function () {

    $("div#gsfn").attr("class", "praise");
    $("input#gsfn_style").val("praise");
    $("div.status").html("What makes you happy about Wego?");

    return false;

  });

});