var min_w = 530;
var min_h = 70;
var max_w = 530;
var max_h = 130;

var width = new Array();
var height = new Array();
var i = new Array();
var state = new Array();

var tid;

var A = new Array();
var C = new Array();

var new_search = false;

var hide = true;

var search_timeout = 2;

var search_str = '';

function SearchTimeout() {
  if (search_timeout > 0) {
//    document.getElementById('main_results').innerHTML = '<b>К сожалению, ответ не найден.</b>';
    loadXMLDoc('search', 1, search_str);
//    search_timeout = 1;
  }
}

function ZoomIn(id) {

  if (state[id] < 2) {

    i[id]++;
    width[id] = min_w + i[id]*0;
    height[id] = min_h + i[id]*3;
    table_id = 'a_table' + id;
    a_id = 'a' + id;
    zin_id = 'zin' + id;
    zout_id = 'zout' + id;
    zings_id = 'zin-gs' + id;
    zoutgs_id = 'zout-gs' + id;
    window.document.getElementById(table_id).style.border = '1px solid #666699';
    window.document.getElementById(table_id).style.width = width[id] + 'px';
    window.document.getElementById(table_id).style.height = height[id] + 'px';
    if ( (i[id] > 19) && (state[id] < 2) ) {
      state[id] = 2;
      window.document.getElementById(a_id).innerHTML = C[id];
      window.document.getElementById(zin_id).style.display = "none";
      window.document.getElementById(zings_id).style.display = "inline";
      window.document.getElementById(zout_id).style.display = "inline";
      window.document.getElementById(zoutgs_id).style.display = "none";
      clearInterval(tid);
    }
  } else {
    clearInterval(tid);
  }
}

function ZoomOut(id) {

  if (state[id] > 0) {

    i[id]--;
    width[id] = max_w - ((20-i[id])*0);
    height[id] = max_h - ((20-i[id])*3);
    table_id = 'a_table' + id;
    a_id = 'a' + id;
    zin_id = 'zin' + id;
    zout_id = 'zout' + id;
    zings_id = 'zin-gs' + id;
    zoutgs_id = 'zout-gs' + id;
    window.document.getElementById(table_id).style.width = width[id] + 'px';
    window.document.getElementById(table_id).style.height = height[id] + 'px';
    if (i[id] == 19) {
      window.document.getElementById(a_id).innerHTML = A[id];
    }
    if ( (i[id] < 1) && (state[id] >0) ) {
      state[id] = 0;
      window.document.getElementById(a_id).innerHTML = A[id];
      window.document.getElementById(zout_id).style.display = "none";
      window.document.getElementById(zoutgs_id).style.display = "inline";
      window.document.getElementById(zin_id).style.display = "inline";
      window.document.getElementById(zings_id).style.display = "none";
      window.document.getElementById(table_id).style.border = '0px solid #666699';
      clearInterval(tid);
    }
  } else {
    clearInterval(tid);
  }

}

function Stars(show, id, n) {
  for (var i=1; i<n+1; i++) {
    var s_id = "s" + i + "-" + id;
    if (show) {
      window.document.getElementById(s_id).style.backgroundColor = "RED";
    } else {
      window.document.getElementById(s_id).style.backgroundColor = "WHITE";
    }
  }
  return false;
}

function loadXMLDoc(what, id, param) {

  var data;

  if (what == "vote") {
    data = 'id=' + id + "&vote=" + param;
    url = "ajax_vote.php";
  }
  if (what == "spam") {
    data = 'id=' + id;
    url = "ajax_spam.php";
  }
  if (what == "edit") {
    hide = false;
    data = 'id=' + id;
    data += "&e_a=" + window.document.dedit.e_a.value;
    data += "&e_a_full=" + window.document.dedit.e_a_full.value;
    data += "&e_url=" + window.document.dedit.e_url.value;
    if (param == 1) {
      data += "&e_q=" + window.document.dedit.e_q.value;
      data += "&new=1";
    }
    if (param == -1) {
      data += "&e_q=" + window.document.dedit.e_q.value;
      data += "&new=-1";
    }
    if (param == 0) {
      data += "&e_q=" + window.document.dedit.e_q.value;
      data += "&new=0";
    }
    url = "ajax_edit.php";
    document.getElementById('res').style.display = "block";
    document.getElementById('result').innerHTML = "<br><center><img src=images/pb.gif></center>";
  }
  if (what == "search") {
//    alert(id);
    new_search = true;
    search_str = param;
    data = 'req=' + param + '&nosearch=' + id;
    url = "ajax_new_search.php";

    if (id != 1) {
      if (Jlng == 'ru') {
        Jsearch = 'Идет поиск';
      } else {
        Jsearch = 'Searching';
      }
      document.getElementById('main_results').innerHTML += '<br><br><center><b>' + Jsearch + '...</b><br><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/ shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="500" height="193"><param name="movie" value="/images/02_ok.' + Jlng + '.swf"><param name="quality" value="high"><embed src="/images/02_ok.' + Jlng + '.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="500" height="193"></embed></object></center>';
    }

    setTimeout("SearchTimeout()", 60 * 1000);

  }

  var dataL = data.length;

  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open('POST', url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-Length", dataL);
    req.send(data);
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = processReqChange;
      req.open('POST', url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-Length", dataL);
      req.send(data);
    }
  }
}

function loadXMLPic(what, id, param) {

  var data;

  if (what == "pics") {
    data = 'req=' + param;
    url = "ajax_pics.php";
  }

  var dataL = data.length;

  if (window.XMLHttpRequest) {
    reqPic = new XMLHttpRequest();
    reqPic.onreadystatechange = processReqChangePic;
    reqPic.open('POST', url, true);
    reqPic.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    reqPic.setRequestHeader("Content-Length", dataL);
    reqPic.send(data);
  } else if (window.ActiveXObject) {
    reqPic = new ActiveXObject("Microsoft.XMLHTTP");
    if (reqPic) {
      reqPic.onreadystatechange = processReqChangePic;
      reqPic.open('POST', url, true);
      reqPic.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      reqPic.setRequestHeader("Content-Length", dataL);
      reqPic.send(data);
    }
  }
}

function processReqChange() {
  var response;
  if (search_timeout != 1) {
  if (req.readyState == 4) {
    if (req.status == 200) {

      search_timeout = 0;
      search_timeout_display_result = false;

      response = req.responseText;
      if (new_search) {

        document.getElementById('main_results').innerHTML = response;
        new_search = false;

        if (/!-- js/.test(response)) {
          subres = response.split("!-- js");
          if (subres.length > 1) {
            strings = subres[1].split("\n");
            if (! /\n/.test(subres[1])) {
              strings = subres[1].split("\r");
            }
            for (var j=0; j<strings.length; j++) {
              if (/^[0-9]+#Dialogus#/.test(strings[j])) {
                dvals = strings[j].split("#Dialogus#");
                var q_id = dvals[0];
                i[q_id]=0;
                state[q_id]=0;
                width[q_id]=0;
                height[q_id]=0;
                A[q_id]=dvals[1];
                C[q_id]=dvals[2];
              }
            }
          }
        }
      } else {
        document.getElementById('res').style.display = "block";
        document.getElementById('result').innerHTML = response;

        if (hide) {
          setTimeout("document.getElementById('res').style.display = 'none'", 1000);
        } else {
          hide = true;
        }
      }
    } else {
      alert("There was a problem retrieving the data:\n" + req.statusText);
      if (new_search) {
        document.getElementById('main_results').innerHTML = response;
        new_search = false;
      } else {
        document.getElementById('res').style.display = "block";
        document.getElementById('result').innerHTML = response;
        if (hide) {
          setTimeout("document.getElementById('res').style.display = 'none'", 1000);
        } else {
          hide = true;
        }
      }
    }
  }
  }
}

function processReqChangePic() {
  var response;
  if (reqPic.readyState == 4) {
    if (reqPic.status == 200) {
      response = reqPic.responseText;
      document.getElementById('pics').innerHTML = response;
    } else {
      alert("There was a problem retrieving the data:\n" + reqPic.statusText);
      document.getElementById('pics').innerHTML = response;
    }
  }
}
