function getDOMObjectPosition(obj) {
  // get absolute coordinates for dom element
  var info = {
    left: 0, 
    top: 0, 
    width: obj.width ? obj.width : obj.offsetWidth, 
    height: obj.height ? obj.height : obj.offsetHeight
  };

  while (obj) {
    info.left += obj.offsetLeft;
    info.top += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return info;
}

function wopen(url,name,w,h,r,s,st) {
   var w=window.open(url,name,"width="+w+",height="+h+",resizable="+r+",toolbar=0,location=0,status="+st+",menubar=0,directories=0,scrollbars="+s);
}

function ondel(url) {
 if (confirm('Вы точно хотите удалить?')) location.href=url;
}

function openerHref(url) {
    window.opener.location = url;
    window.close();
}

function heightEdit100pers() {
  var iframe = document.getElementById('content___Frame');
  iframe.height = (document.body.clientHeight - 60) + "px";
}

function heightEditNew100pers() {
  var editor_area = document.getElementById('editor_area');
  editor_area.style.height = (document.body.clientHeight - 60) + "px";
}

function reload(path) {
  document.location.href = path;
}

function hideNotification(id) {
  if (typeof id == "undefined") {
    id = 'notification_item';
  }
  document.getElementById(id).style.display = "none";
}

function pushNotification(num, id) {
  if (typeof id == "undefined") {
    id = 'notification_item';
  }
  if (num < 5) {
    num++;
    document.getElementById(id).style.top = "-" + (num * 10) + "px";
    setTimeout('pushNotification(' + num + ', \'' + id + '\')', 200);
  } else {
    setTimeout('hideNotification(\'' + id + '\')', 200);
  }
}

function showNotification(id) {
  if (typeof id == "undefined") {
    id = 'notification_item';
  }
  document.getElementById(id).style.display = "block";
  setTimeout('pushNotification(0, \''+id+'\')', 5000);
}

function showMessage(message, type) {
  if (!type) {
    type = 'ok';
  }
  document.getElementById('js_notification_item_text').setAttribute("class", "notification_pattern notify_"+type);
  document.getElementById('js_notification_item_text').innerHTML = message;
  document.getElementById('js_notification_item').style.top = '0px';
  showNotification('js_notification_item');
}

function showDescrForm(image_id) {
  document.getElementById('descr_' + image_id).style.display = 'none';
  document.getElementById('change_descr_' + image_id).style.display = 'block';
}

function updateDescr(image_id) {
    var resultDIV = document.getElementById("descr_" + image_id);
    var descr = document.getElementById('change_descr_text_' + image_id).value;
    var req = new Subsys_JsHttpRequest_Js();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            resultDIV.innerHTML = descr;
            document.getElementById('change_descr_' + image_id).style.display = 'none';
            document.getElementById('descr_' + image_id).style.display = 'block';
        } else if (req.readyState == 0) {
            resultDIV.innerHTML='<span class="error">Connection failed</span>';
            document.getElementById('change_descr_' + image_id).style.display = 'none';
            document.getElementById('descr_' + image_id).style.display = 'block';
        }
    }
    req.caching = false;
    req.open('POST', catalog_path + 'updatedescr.php', true);
    req.send({"id": image_id,"descr": descr});
}

function checkUploadForm(gallery_id, is_limited) {
  if (is_limited) {
    parent.document.getElementById('upload_form_' + gallery_id).style.display = 'none';
  } else {
    parent.document.getElementById('upload_form_' + gallery_id).style.display = 'block';
  }
}

function reloadGallery(gallery_id) {
  var resultDIV = parent.document.getElementById('gallery_' + gallery_id);
  var req = new Subsys_JsHttpRequest_Js();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      resultDIV.innerHTML = req.responseJS.result;
      checkUploadForm(gallery_id, req.responseJS.is_limited);
    } else if (req.readyState == 0) {
      resultDIV.innerHTML = '<span class="error">Connection failed</span>';
    }
  }
  req.caching = false;
  req.open('POST', catalog_path + 'get.php', true);
  req.send({"id": gallery_id});
}

function dropImage(image_id, gallery_id) {
  var req = new Subsys_JsHttpRequest_Js();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      reloadGallery(gallery_id, catalog_path + 'get.php');
    } else if (req.readyState == 0) {
      resultDIV.innerHTML = '<span class="error">Connection failed</span>';
    }
  }
  req.caching = false;
  req.open('POST', catalog_path + 'dropimage.php', true);
  req.send({"id": image_id});
}


function showUpload(gallery_id) {
  parent.document.getElementById('gallery_upload_' + gallery_id).style.display='block';
}

function updateTextField(update_url, field_name, id) {
  var req = new Subsys_JsHttpRequest_Js();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      showMessage('Значение поля изменено!', 'ok');
    } else if (req.readyState == 0) {
      alert('Connection failed');
    }
  }
  req.open('POST', update_url, true);
  req.send({'value': document.getElementById(id).value, 'field_name': field_name});
}

function openSubmenu(id) {
  document.getElementById('submenu_' + id).style.display = 'block';
}

function closeSubmenu(id) {
  document.getElementById('submenu_' + id).style.display = 'none';  
}

function openMenuActions(id) {
  link_obj = getDOMObjectPosition(document.getElementById('values_editmenu_' + id + '_name'));
  document.getElementById('div_menu_actions_' + id).style.top = (link_obj.top-3) + 'px';
  document.getElementById('div_menu_actions_' + id).style.marginLeft = (link_obj.width-1) + 'px';
  document.getElementById('div_menu_actions_' + id).style.display = 'block';
}

function closeMenuActions(id) {
  document.getElementById('div_menu_actions_' + id).style.display = 'none';
}

function openActionActions(menu_id, id) {
  link_obj = getDOMObjectPosition(document.getElementById('action_item_' + menu_id + '_' + id));
  document.getElementById('div_item_actions_' + menu_id + '_' + id).style.top = (link_obj.top-5) + 'px';
  document.getElementById('div_item_actions_' + menu_id + '_' + id).style.marginLeft = (link_obj.width-1) + 'px';
  document.getElementById('div_item_actions_' + menu_id + '_' + id).style.display = 'block';
}

function closeActionActions(menu_id, id) {
  document.getElementById('div_item_actions_' + menu_id + '_' + id).style.display = 'none';
}

function updateSession(field, value, action) {
  new Request.JSON({
    url: main_url + admin_inc + 'updatemenu.php',
    method: 'get',
    data: 'field=' + field + '&value=' + value + '&action=' + action
  }).send();
}

function pleaseWait(id) {
  $(id).set('html', '<img src="'+core_path+'images/ajax-loader.gif" title="Загружается..." alt="Загружается..." />')
}

function pleaseWaitImage(id) {
  $(id).set('src', core_path+'images/circle-loader.gif');
  $(id).set('height', '16');
}


function toggleMenu(id) {
  var elem_id = 'div_menu_' + id;
  var elem_image_id = 'item_image_' + id;

  var elem = $(elem_id);  

  if (elem.style.display == 'block') {
    elem.style.display = 'none';
    $(elem_image_id).set('src' , 'img/closednode.gif');
    updateSession('div_menu', id, 0);
  } else {
    if($(elem_id).get('html')) {
      updateSession('div_menu', id, 1);
      $(elem_id).setStyle('display' , 'block');
      $(elem_image_id).set('src', 'img/openednode.gif');
      $(elem_image_id).set('height', '22');
    } else {
      core_menu_openChildrenBlock(id, elem_id, elem_image_id);
    }
  }
  return false;
}