// If type is "width" returns browser width else returns browser height
function getSize(type) {
  var browserWidth = 0;
  var browserHeight = 0;
  if(typeof(window.innerWidth) == "number" ) {
    // All browsers except MSIE
    browserWidth  = window.innerWidth;
    browserHeight = window.innerHeight;
  } else if(document.documentElement 
         && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    // MSIE 6+ in 'standards compliant mode'
    browserWidth  = document.documentElement.clientWidth;
    browserHeight = document.documentElement.clientHeight;
  } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    // MSIE 4 compatible - old browsers
    browserWidth  = document.body.clientWidth;
    browserHeight = document.body.clientHeight;
  }
  // Return value according type
  if(type == "width") return browserWidth;
  else return browserHeight;
}

// Returns browser width via getSize()
function getBrowserWidth() {
  return getSize("width");
}

// Returns browser width via getSize()
function getBrowserHeight() {
  return getSize("height");
}

// Count and return top right corner of element - left
function getElementPositionLeft(id) {
  var e = document.getElementById(id);
  var eWidth = removePx(document.getElementById(id).style.width);
  return (Math.round((getBrowserWidth() - eWidth) / 2.1));
}

// Count and return top right corner of element - top
function getElementPositionTop(id) {
  var e = document.getElementById(id);
  var eHeight = removePx(document.getElementById(id).style.height);
  return Math.round((getBrowserHeight() - eHeight) / 3.3);
}

// Set element position - left
function setElementPositionLeft(id) {
  checkAbsolutePosition(id);
  document.getElementById(id).style.left = 0 + "px";
  return;
}

// Set element position - top
function setElementPositionTop(id) {
  checkAbsolutePosition(id);
  document.getElementById(id).style.top = 0 + "px";
  return;
}

// Set element position
function setElementPosition(id) {
  setElementPositionLeft(id);
  setElementPositionTop(id);
  setZIndex(id);
  setVisibility(id);
  return;
}

/* ************************************************************************** */
/*                            Support functions                               */ 
/* ************************************************************************** */

// Check absolute position setting
function checkAbsolutePosition(id) {
  if(document.getElementById(id).style.position != "absolute")
    document.getElementById(id).style.position = "absolute";
  return;
}

// Remove string 'px' from CSS style
function removePx(text) {
  return text.replace(/px/, "");
}

// Set z-index - great number for its layer
function setZIndex(id) {
  document.getElementById(id).style.zIndex = 999999;
  return;
}

// Set visibility
function setVisibility(id) {
  document.getElementById(id).style.display = "inline";
  return;
}

// Hide element
function hideElement(id) {
  document.getElementById(id).style.display = "none";
  return;
}

// If type is "left" returns scrollbar left position else returns scrollbar top position
function getScrollbars(type) {
  var scrollLeft = 0;
  var scrollTop = 0;
  if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    scrollLeft  = document.documentElement.scrollLeft;
    scrollTop = document.documentElement.scrollTop;
  } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    scrollLeft  = document.body.scrollLeft;
    scrollTop = document.body.scrollTop;
  } else {
    scrollLeft = window.scrollLeft;
    scrollTop  = window.scrollTop;  
  }
  // Return value according type
  if(type == "left") return scrollLeft;
  else return scrollTop;
}

// Returns scrollbar left position via getScrollbars() 
function getScrollbarLeft() {
  return getScrollbars("left");
}

// Returns scrollbar top position via getScrollbars()
function getScrollbarTop() {
  return getScrollbars("top");
}

