var post_refs = null;
var post_refs2 = null;
var query_box = null;
var row_style_visible = null;
var informer_options = new Array();
var visible_messages = null;

function init(error_pos)
{
  query_box = el_enrich(el_by_id("se"));
  set_focus_next_link(error_pos);
}

function message_visible(id)
{
  return el_visible(document.getElementById("mt_" + id), 0.1) || 
         el_visible(document.getElementById("ma_" + id), 0.1) ||
         el_visible(document.getElementById("mm_" + id), 0.1) ||
         el_visible(document.getElementById("mf_" + id), 1.0);
}

function report_message_impression()
{
  if(message_impression == null || message_impression.length == 0)
  {
    return;
  }

  if(visible_messages)
  {
    var stat_url = "";
    var shift = 0;

    for(var i = 0; i < visible_messages.length; ++i)
    {
      var j = visible_messages[i] - shift;
      var id = message_impression[j];

      if(message_visible(id))
      {
        if(stat_url == "")
        {
          stat_url = impression_stat_url_prefix + "&m=";
        }
        else
        {
          stat_url += "+";
        }

        stat_url += el_mime_url_encode(id);
        message_impression.splice(j, 1);
        ++shift; 
      }
    }

    if(stat_url != "")
    {
      var request;

      try
      {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
        request = new XMLHttpRequest();
      }

      request.open("GET", stat_url, true);
      request.send("");
    }
  }

  visible_messages = new Array();

  for(var i = 0; i < message_impression.length; ++i)
  {
    var id = message_impression[i];

    if(message_visible(id))
    {
      visible_messages.push(i);
    }
  }

  window.setTimeout("report_message_impression();", 1000);
}

function create_informer_script(url)
{
  var scr = el_by_id("informer");

  if(scr !== undefined && scr !== null)
  {
    scr.parentNode.removeChild(scr);
  }

  if(url.length > max_get_length)
  {
    el_by_id("ir_placeholder").innerHTML = "Informer request URI is too large. Try to simplify search criteria.";
    return false;
  }
  else
  {
    var element = document.createElement("script");
    element.type = "text/javascript";
    element.id = "informer";
    element.src = url;

    document.getElementsByTagName('head')[0].appendChild(element);
    return true;
  }
}

function add_informer_option(param_name, element_id, def_value, just_stl)
{
  informer_options.push( { param: param_name, 
                           id: element_id, 
                           def: def_value,
                           just_style: just_stl
                         } );
}

function set_informer_option(elem_id, value)
{
  var obj = el_by_id(elem_id);

  if(obj == null)
  {
    return false;
  }

  if(value == null)
  {
    for(var i = 0; i < informer_options.length; ++i)
    {
      var io = informer_options[i];

      if(io.id == elem_id)
      {
        value = io.def;
        break;
      }
    }
  }

  if(obj.tagName == "SELECT")
  {
    for(var i = 0; i < obj.length; ++i)
    {
      if(obj.options[i].value == value)
      {
        obj.selectedIndex = i;
        return true;
      }
    }

    return false;
  }

  if(obj.type == "checkbox")
  {
    if(value != "1" && value == "0")
    {
      return false;
    }

    obj.checked = value == "1";
    return true;
  }

  obj.value = value;
  return true;
}

function informer_option(elem_id)
{
  var obj = el_by_id(elem_id);

  if(obj == null)
  {
    alert("Not found " + elem_id);
  }

  return value = obj.tagName == "SELECT" ? 
    obj.options[obj.selectedIndex].value : 
   (obj.type == "checkbox" ? (obj.checked ? "1" : "0") : obj.value);
}

function script_changed()
{
  var script = el_by_id("ir_script").value;
  var b = script.indexOf("/search?t=j&");
  var lang;

  if(b >= 3)
  {
    lang = script.substring(b - 3, b);
    b += 8;
  }
  else
  {
    b = -1;
  }

  var query = "";

  if(b >= 0)
  {
    script = script.substring(b);
    var e = script.indexOf("\">");

    if(e > 0)
    {
      query = script.substring(0, e);
    }
  }

  if(query == "")
  {
    alert("Invalid informer code");
  }
  else
  {
    if(query.indexOf("&amp;") >= 0 || query.indexOf("&#") >= 0)
    {
      query = el_xml_decode(query);
    }

    query = extract_param("t=", query).query;
    query = extract_param("iv=", query).query;

    var b = script.indexOf("<!--IR");
    var e = script.indexOf("IR-->");

    if(b >= 0)
    {
      b += 6;
    }

    if(b >= 0 && e > b)
    {
      query += el_xml_decode(script.substring(b, e));
    }

    el_post_url(search_path + "?lang=" + lang + "&ir=1&" + query);

    return; 
  }

  informer_changed();
}

function informer_changed()
{ 
  var params = informer_params() + "&" + 
               extract_param("r=", search_query).query;

  var msg_count = informer_option("ir_msg_count");

  if(msg_count != results_per_page)
  {
    el_post_url(search_path + "?" + params);
    return; 
  }

  var external_style = informer_option("ir_style_placement") == 1;

  var obj = el_by_id("ir_class_prefix_row");

  if(row_style_visible == null) 
  {
    row_style_visible = obj.style.display;
  }

  obj.style.display = external_style ? row_style_visible : "none";

  obj = el_by_id("ir_style_intro");
  obj.style.display = external_style ? row_style_visible : "none";

  obj = el_by_id("ir_title_underline_color_row");

  obj.style.display = 
    parseInt(informer_option("ir_img_pos")) < 2 ? row_style_visible : "none";

  obj = el_by_id("ir_style");
  obj.style.width = el_by_id("ir_script").offsetWidth;
  obj.style.display = external_style ? "inline" : "none";

  var meta_controls_visible = 
    el_by_id("ir_metainfo_columns").selectedIndex > 0;

  var rows = document.getElementsByTagName("tr");

  for(var i = 0; i < rows.length; ++i)
  {
    var obj = rows[i];

    if(obj.className == "ir_meta_control")
    {
      obj.style.display = meta_controls_visible ? row_style_visible : "none";
    }
  }

  var proto_params = "t=j&iv=2&";
  params = proto_params + informer_params() + "&" + ir_search_query;

  var script_code = "";
  
  if(create_informer_script(search_path + "?" + params))
  { 
    var extras = "";

    if(external_style)
    {
      params = proto_params + informer_params(false) + "&" + ir_search_query;
      extras = el_xml_encode(informer_params(true))
    }
    else
    {
      params = extract_param("cp=", params).query;
      params = extract_param("ir=", params).query;
    }

    var hostname = search_path.split('/')[2];

    var div_style = "";
    var a_style = "";
    var div_class = "";

    if(informer_option("ir_style_placement") == 0)
    {
      div_style += ' style="font-weight: 400; text-align: left; ' + 
                   'padding: 0.2em;';

      var option = informer_option("ir_catchword_background_color");
      if(option != "") div_style += ' background-color: ' + option + ';'

      var option = informer_option("ir_catchword_color");
      if(option != "") div_style += ' color: ' + option + ';'

      var option = informer_option("ir_catchword_size");
      if(option != "") div_style += ' font-size: ' + option + ';'

      var option = informer_option("ir_catchword_font");
      if(option != "") div_style += ' font-family: ' + option + ';'

      div_style += '"';

      a_style += ' style="text-decoration: none;';

      var option = informer_option("ir_catchword_link_color");
      if(option != "") a_style += ' color: ' + option + ';'

      a_style += '"';
    }
    else
    {
      div_class += ' class="' + informer_option("ir_class_prefix") + 'footer"';
    }

    script_code = '<script charset="utf-8" src="' + search_path + "?" + 
                  params + '"></script>'

    if(extras != "")
    {
      script_code += "<!--IR&amp;" + extras + "IR-->";
    }

    var id="_dsnnginf_634080201465X579_";

    script_code += '<div name="' + id + '" id="' + id + '"' +
                   div_style + div_class +'>';

    var cwt = el_trim(informer_option("ir_catchword_link_text"));
//    set_informer_option("ir_catchword_link_text", cwt == "" ? null : cwt);

    if(cwt != "")
    {
      script_code += '<a' + a_style + ' href="' + search_path + "?" + 
                     ir_search_query + '" target="_blank">' + 
                     el_xml_encode(cwt, true) + 
                     '</a> at ';
    }

    script_code += '<a' + a_style + ' href="http://' + hostname +
                   '" target="_blank">' + hostname + '</a></div>';
  }

  set_informer_option("ir_script", script_code);

}

function set_focus_next_link(error_pos)
{
  var elems = document.getElementsByName("next_link");

  if(error_pos == 0 && elems !== undefined && elems.length > 0)
  {
    elems[0].tabIndex = "1"
    query_box.tabIndex = "2";

    elems[0].focus();
  }
  else
  {
    var sel = error_pos > 0 ? {start:error_pos-1, end:error_pos-1} : 
      {start:0, end:query_box.value.length};

    query_box.el_set_selection(sel, true)
  }
}

function search_key_press(event)
{
  if(event.keyCode == 13)
  {
    search();
    return false;
  }

  return true;
}

function search(create_informer_mode)
{
  query = extract_param("q=", search_query).query;

  var res = extract_param("v=C", query);
  var c1 = res.param;
  query = res.query;
  
  res = extract_param("g=", query);
  var c2 = res.param;
  query = res.query;

  res = extract_param("v=A", query);
  query = res.query;

  res = extract_param("v=S", query);
  var s1 = res.param;
  query = res.query;

  res = extract_param("f=", query);
  var s2 = res.param;
  query = res.query;

  res = extract_param("v=E", query);
  var e1 = res.param;
  query = res.query;

  res = extract_param("h=", query);
  var e2 = res.param;
  query = res.query;

  var q = el_trim(el_by_id("se").value);

  var c = c1 == "" ? c2 : c1;
  var s = s1 == "" ? s2 : s1;
  var e = e1 == "" ? e2 : e1;

  var url = search_path + "?";

  if(create_informer_mode)
  {
    url += informer_params() + "&";
  }

  url += "q=" + el_mime_url_encode(q);

  if(s != "")
  {
    url += (q == "" ? "&v=S" : "&f=") + s;
  }
  else if(e != "")
  {
    url += (q == "" ? "&v=E" : "&h=") + e;
  }
 
  if(c != "")
  {
    if(q == "" || c != "%2F")
    {
      url += (q == "" && s == "" && e == "" ? "&v=C" : "&g=") + c;
    }
  }

  if(q == "" && c == "" && s == "" && e == "")
  {
    url += "&v=A";
  }

  if(query != "")
  {
    url += "&" + query;
  }

  navigate(url);
}

function show_settings_dialog_wrapper()
{
  show_settings_dialog(informer_params());
}

function show_feed_paging_dialog()
{
  show_paging_dialog('feed', informer_params())
}

function show_spaces_dialog_wrapper()
{
  show_spaces_dialog(informer_params());
}

