// Вспомогательные DOM функции
//   getLastDirectChild(obj, tag_name)
//   getFirstDirectChild(obj, tag_name)
//   getPrevSibling(obj, tag_name)
//   getNextSibling(obj, tag_name)
//   toggle_visibility(id)
//   getStyle(elem, name)
//   getPageYScroll()
//   getPageSize()

// Функция получения последнего прямого потомка заданного типа для данного узла.
function getLastDirectChild(obj, tag_name) {
  var all_children = obj.getElementsByTagName(tag_name);
  var last_direct_child = null;
  for (var i = 0; i < all_children.length; i++) {
    if (all_children[i].parentNode == obj)
      last_direct_child = all_children[i];
  }
  return last_direct_child;
}

// Функция получения первого прямого потомка заданного типа для данного узла.
function getFirstDirectChild(obj, tag_name) {
  var all_children = obj.getElementsByTagName(tag_name);
  var first_direct_child = null;
  for (var i = 0; i < all_children.length; i++) {
    if (all_children[i].parentNode == obj) {
      first_direct_child = all_children[i];
      break;
    }
  }
  return first_direct_child;
}

// Функция получения предыдущего sibling заданного типа для данного узла.
// Возвращает siblibg типа tag_name предшествующий obj, или null в случае
// если obj не содержит впереди себя тэгов типа tag_name.
function getPrevSibling(obj, tag_name) {
  var prev_sibling;

  while (true) {
    prev_sibling = obj.previousSibling;
    if (!prev_sibling || prev_sibling.nodeName.toLowerCase() == tag_name.toLowerCase())
      return prev_sibling;
    obj = prev_sibling;
  }
}

// Функция получения следующего sibling заданного типа для данного узла.
// Возвращает siblibg типа tag_name следующий за obj, или null в случае
// если obj не содержит после себя тэгов типа tag_name.
function getNextSibling(obj, tag_name) {
  var next_sibling;

  while (true) {
    next_sibling = obj.nextSibling;
    if (!next_sibling || next_sibling.nodeName.toLowerCase() == tag_name.toLowerCase())
      return next_sibling;
    obj = next_sibling;
  }
}

// Показать/скрыть элемент
function toggle_visibility(id) {
  var e = document.getElementById(id);

  if (e.style.display != "none")
    e.style.display = "none";
  else
    e.style.display = "";
}

// Функция для получения свойства стиля (name) заданного элемента (elem)
function getStyle(elem, name) {
  if (elem.style[name])             // Если свойство существует в style[]
    return elem.style[name];
  else if (elem.currentStyle) {     // Попробуем использовать метод IE
    return elem.currentStyle[name];
  // Или метод W3C, если он существует
  } else if (document.defaultView && document.defaultView.getComputedStyle) {
    // Он использует традиционный стиль написания правил,
    // 'text-align' вместо textAlign
    name = name.replace(/([A-Z])/g,"-$1");
    name = name.toLowerCase();

    // Get the style object and get the value of the property (if it exists)
    var s = document.defaultView.getComputedStyle(elem,"");
    return s && s.getPropertyValue(name);
  } else // Иначе, мы используем некоторый другой браузер
    return null;
}

// getPageYScroll()
// Return y page scroll values.
//
function getPageYScroll() {

  var yScroll;

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }

  return yScroll;
}

// getPageXScroll()
// Return x page scroll values.
//
function getPageXScroll() {

  var xScroll;

  if (self.pageXOffset) {
    xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollLeft) { // Explorer 6 Strict
    xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    xScroll = document.body.scrollLeft;
  }

  return xScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize() {

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {	
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  //  console.log(self.innerWidth);
  //  console.log(document.documentElement.clientWidth);

  if (self.innerHeight) {  // all except Explorer
    if(document.documentElement.clientWidth) {
      windowWidth = document.documentElement.clientWidth; 
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }	

  // for small pages with total height less then height of the viewport
  if (yScroll < windowHeight) {
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // console.log("xScroll " + xScroll)
  // console.log("windowWidth " + windowWidth)

  // for small pages with total width less then width of the viewport
  if (xScroll < windowWidth) {
    pageWidth = xScroll;		
  } else {
    pageWidth = windowWidth;
  }
  // console.log("pageWidth " + pageWidth)

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}
																												