// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var query_status_interval_id = 0;

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept","text/javascript")}
})


function show_query_dialog(query_string, query_key) 
{
  var delay_func = function() {get_query_status(query_key,query_string);};
  // $("#dialog_query_string").html(query_string + ": " + query_key);
  $("#dialog_query_string").html(query_string);
  $("#dialog").dialog('open');
  query_status_interval_id = setInterval(delay_func, 2500); 
}

function show_about_dialog()
{
  window.scroll(0,0);
  $("#about_dialog").dialog('open');
}

function clean_up_query_dialog()
{
  clearInterval(query_status_interval_id);
  // $("#progressbar").progressbar('value', 0);
}

function get_query_status(query_key,query_string)
{
  $.post("/brain/get_query_status","query_key=" + query_key + "&query_string=" + escape(query_string), null, "script");
}

$(function() { 
  $("#flash").hide();
  var flash_message = $.trim($("#flash").html());
  if(flash_message != "") {
    notify(flash_message);
  }
});

function notify(flash_message) {
  var flash_div = $("#flash");
  flash_div.html(flash_message);
  flash_div.fadeIn(250);
  setTimeout(function(){
    flash_div.fadeOut(500,
      function(){
        flash_div.html("");
        flash_div.hide()
      })
    },
    7000);
};

$(document).ready(function() {
  $("#new_query").submit(function(){
    $.post($(this).attr("action"), $(this).serialize(), null, "script");
    return false;
  });
  
  $(function() {
    $("#progressbar").progressbar({
      value: 0
    });
  });
  
  $(function() {
    $("#dialog").dialog({
      bgiframe: true,
      height: 150,
      width: 450,
      autoOpen: false,
      modal: true,
      draggable: false,
      resizable: false,
      buttons: { "Cancel Search": function() { $(this).dialog("close"); } },
      beforeclose: function(event, ui) { clean_up_query_dialog(); $("#progressbar").progressbar('value', 0); return true; }

    });
    $("#about_dialog").dialog({
      bgiframe: true,
//      height: 350,
      position: 'center',
      width: 450,
      autoOpen: false,
      modal: true,
      draggable: false,
      resizable: false      
    });
    $(".accordion").accordion({
      fillSpace: true
    });
  });
  
  if ($("#auto_submit").val() == 'true') {
    $("#new_query").submit();
  };
  // $("#text_container").resizable({ handles: 's' });
  

})
