// --------- AJAX ---------
//  target_div = document.getElementById('info_div');
var check_status, status_hide;
// Images

function get_images(path){
  target_div = document.getElementById('images_div');
  ajax_onready_start = show_images;
  var get_query = 'mod=images/get&folder=' + path;
  get_request = get_query;

  ajax_get_data(dirname + '/ajax.php?' + get_query);
}

function show_images(){
  if(req.readyState == 4){

//    target_div.innerHTML = '';
    if(req.status == 200){
      target_div.innerHTML = req.responseText;
    }else{
      target_div.innerHTML += '<pre style="color:red;font-size:120%;">Ошибка!\nНе удалось получить данные:\n' + req.statusText + '</pre>';
    }
  }
}

// Search forms requests
function requestdata(get_query){
  target_div = document.getElementById(element_id);
  ajax_onready_start = show_requestdata;
  get_request = get_query;
  ajax_get_data(dirname + '/ajax.php?' + get_query);
}

function show_requestdata() {
  if (req.readyState == 4) {
    
    // only if "OK"
    element = document.getElementById(element_id);
    if (req.status == 200) {
      element.innerHTML  = req.responseText;
      if(element.style.display == 'none') element.style.display = '';
    } else {
      element.innerHTML += '<br>Ошибка!<br>Не удалось получить данные:\n' + req.statusText;
    }
  }
}
// ----------

function ajax_get_data(url) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  }else if (window.ActiveXObject){
    req = new ActiveXObject('Microsoft.XMLHTTP');
//    oServerXMLHTTPRequest.setOption(SXH_OPTION_URL_CODEPAGE, 'UTF-8');
  }

  if(req){
    req.onreadystatechange = ajax_onready_start;
    req.open('GET', url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    req.send(null);
//    alert(req.readyState());
  }

  request_abort = window.setTimeout('req.abort();', 10000);
  if(is_admin){
    clearInterval(status_hide);
    ajax_status.style.display = 'inline';
    ajax_status.innerHTML = 'Sending data...';
  }
  ajax_check_status.counter = 0;
  ajax_check_status();
}

function ajax_check_status(){

  if(ajax_check_status.counter == 0) clearInterval(status_hide);
  clearInterval(check_status);

  if(!req){
    ajax_status.innerHTML = '<font color=red>Unknown error!</font>';
    status_hide = setInterval('ajax_status_hide()', 3000);
    ajax_check_status.counter = 0;
    return;
  }

  if(req.readyState == 4){
    ajax_status.innerHTML = '<font color=green>Data received!</font>';// + '<pre>' + req.getAllResponseHeaders();
    status_hide = setInterval('ajax_status_hide()', 2000);
    ajax_check_status.counter = 0;
    return;
  }else{
    ajax_status.innerHTML += ' ' + req.readyState;
  }

  if(ajax_check_status.counter >= 50){
//  alert(1); 
    ajax_status.innerHTML += '<font color=red>Error receiving data: timeout.</font>';
//    ajax_status.innerHTML += '<pre>Request status: ' + req.statusText + ' (' + req.status + ')';
    ajax_check_status.counter = 0;
    status_hide = setInterval('ajax_status_hide()', 4000);
    return;
  }
  check_status = setInterval('ajax_check_status();', 50);
  ajax_check_status.counter ++;
}

function ajax_status_hide(){
  ajax_check_status.counter = 0;
  clearInterval(status_hide);
  ajax_status.style.display = 'none';
}

// Popup info box
function showInfo(get_query){
  target_div = document.getElementById('info_div');
  ajax_onready_start = printInfo;
  get_request = get_query;
  if(!notmove){
    if(mouseCoords(ev).x + 350 > window_width()){
      target_div.style.left = (window_width() - 500) + 'px';// mouseCoords(ev).x-200;
      target_div.style.top = (mouseCoords(ev).y + 15) + 'px';
      target_div.style.width = null;
    }else{
      target_div.style.left = (mouseCoords(ev).x + 15) + 'px';
      target_div.style.top = mouseCoords(ev).y + 'px';
      target_div.style.width = null;
    }
  }
  ajax_get_data(dirname + '/ajax.php?' + get_query);
  if(!nothide){
    clearInterval(timerID);
    timerID = setInterval('hideInfo()', 5000);
  }
}

function show_ajax_filter(module, column, id){
  element_id = id;
  target_div = document.getElementById(element_id);
  if(target_div.style.display == '') target_div.style.display = 'none';

  var obj = getElementsByClass('ajax_filter', null, 'div');
    for(i=0;i<obj.length;i++){
      obj[i].style.display = 'none';
    }
//  alert(obj.length + ids);
//  if(target_div.style.display == 'none'){
    target_div.innerHTML = wait_caption;
    ajax_onready_start = set_ajax_filter;
    get_request = 'mod=ajax_filter&from=' + module + '&column=' + column;
    ajax_get_data(dirname + '/ajax.php?' + get_request);
//    alert(column + target_div.style.display);
    target_div.style.display = 'block';
//  }

//  on_off_element(element_id, false);
}

function set_ajax_filter() {
  if (req.readyState == 4) {
    
    // only if "OK"
    element = document.getElementById(element_id);
    if (req.status == 200) {
      element.innerHTML  = req.responseText;
    } else {
      element.innerHTML += '<br>Ошибка!<br>Не удалось получить данные:\n' + req.statusText;
    }
  }
}

function hideInfo(){
  target_div = document.getElementById('info_div');
  target_div.style.display = 'none';
  clearInterval(timerID);
}

function startInfoTimer(){
  if(!nothide){
    clearInterval(timerID);
    timerID = setInterval('hideInfo()', 5000);
  }
}

function pushpin(img){
  nothide = !nothide;
  setCookie('info_div_nothide', nothide ? 1 : 0, 0, dirname);
  img.src = dirname + '/images/pushpin_' + (nothide ? 'on' : 'off') + '.gif';
}

function printInfo(){
  if (req.readyState == 4) {

    var info_start = '<table style="overflow:visible; " ' + (target_div.style.width ? 'width=100%' : '') + 
      ' border=0 cellpadding=0 cellspacing=0 onMouseOver="clearInterval(timerID);" onMouseOut="startInfoTimer();">' +
      '<tr height="1px"><td>' +
      '<table STYLE="background:#1290E9;border-bottom:1px solid gray;" width=100% border=0 cellpadding=0 cellspacing=0><tr>' +
        // Push-pin button
        '<td width=1px><img style="cursor:pointer;" src="' + dirname + '/images/pushpin_' + (nothide ? 'on' : 'off') + '.gif' + '" onclick=pushpin(this);>' +
        '<td onMouseDown="mouseDown();" style="cursor:move;"><img src="' + dirname + '/images/e.gif" width=200px height=1>' +
        // Close window button
        '<td width=1px><img border=1 src="' + dirname + '/images/close_16.png" style="cursor:pointer;" onClick="hideInfo();">' +
      '</table>' +
//      '</table><div>';
      '<tr valign=top><td style="padding:3px;">';
    var info_end = '</td></tr></table>';
//    var info_end = '</div>';

    target_div.innerHTML = '';
    if (req.status == 200) {
      target_div.innerHTML = info_start + req.responseText + info_end;
      target_div.style.display = 'inline';
    } else {
      target_div.innerHTML = info_start + '<pre style="color:red;font-size:120%;">Ошибка!\nНе удалось получить данные:\n' + req.statusText + '</pre>' + info_end;
      target_div.innerHTML += '<button onclick="notmove=true;showInfo(get_request);notmove=false;">Повторить</button>';
    }
  }
}
// ----------

