//Start ajax by calling this function: send_query()

//*****************************
//AJAX Specific Functions
//*****************************

function process_ajax()
{
  if (ajax_request.readyState == 4)
    {
      // only if "OK"
      if (ajax_request.status == 200)
        {
          if(ajax_request.responseText != "")
            {
              show_image_information(ajax_request.responseText);
            }
        }
      else {
        //alert("There was a problem retrieving data:<br />"+ajax_request.statusText);
      }
    }
}

//Start ajax by calling this function
function send_query(pattern_id, obj, event)
{
  initialize_ajax();
  coordinates = get_coordinates(obj, event);

  var url="ancillary/ajax_return_image_info.php?pattern_id="+pattern_id;
  if(ajax_request!=null) {
    ajax_request.onreadystatechange = process_ajax;
    ajax_request.open("GET", url, true);
    ajax_request.send(null);
  }
}

//*****************************
//Topic specific functions
//*****************************

function show_image_information(topic_data)
{
  var left = 0;
  var top = 0;
  var left_offset = 400;
  var top_offset = -200;

  var width_max = 550;

  image_data_space_ref = get_ref_to_id("image_data_space");

  var temp = coordinates.split(":"); //global variable
  var x_coordinate = temp[0];
  var y_coordinate = temp[1];

  //Convert to numbers
  x_coordinate = x_coordinate * 1;
  y_coordinate = y_coordinate * 1;
  while (x_coordinate > width_max)
    {
      x_coordinate = x_coordinate-1;
    }
  image_data_space_ref.innerHTML = topic_data;

  if( image_data_space_ref ) {
    if( image_data_space_ref.style ) { image_data_space_ref = image_data_space_ref.style; }
    image_data_space_ref.display = "block";
    left = x_coordinate + left_offset;
    image_data_space_ref.left = left + 'px';
    top = y_coordinate + top_offset + click_top; //click_top is a global variable
    image_data_space_ref.top = top + 'px';
  }
}

function hide_image_div()
{
  image_data_space_ref = get_ref_to_id("image_data_space");

  if( image_data_space_ref ) {
    if( image_data_space_ref.style ) { image_data_space_ref = image_data_space_ref.style; }
    image_data_space_ref.display="none";
  }
  hide_rate_div();
}

//*****************************
//Keybord handling
//*****************************

function check_keypress(e) {
  if (e) {
    keyCode = e.which
      esc_code = 0;
  }
  else if (event) {
    keyCode=event.keyCode
      esc_code = 27;
  }

  if (keyCode == esc_code) {
    hide_image_div();
  }
}

if (navigator.appName == 'Netscape') {
  window.captureEvents(Event.KEYPRESS);
  window.onKeyPress = check_keypress;
}

//check for the escape key to hide the image_data_space
document.onkeypress = check_keypress;
