﻿String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

function $id(id){ if (typeof id == 'string') return document.getElementById(id); return id }

// get window size
function windowHeight() { return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0 }
function windowWidth() { return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0 }

// get pixel position of an object
function getY(o){ var y = 0
	if (o.offsetParent) while (o.offsetParent) { y += o.offsetTop; o = o.offsetParent }
	return y
}
function getX(o){ var x = 0
	if (o.offsetParent) while (o.offsetParent) { x += o.offsetLeft; o = o.offsetParent }
	return x
}
function setX(o, n){ o.style.left = n + 'px' }
function setY(o, n){ o.style.top = n + 'px' }

function footer() {
    var dif = 0;
	var h = windowHeight()
    var footer = $id('footer')
    var bottom = getY($id('bottom'))
    if ($id('sidebarbottom')) {
      var sidebar = getY($id('sidebarbottom'))
    } else {
      var sidebar = 0;
    }
	var fh = footer.clientHeight || footer.offsetHeight // todo: why does IE report 0 for clientHeight?
    if (sidebar > bottom) {
      if ((h - fh) < sidebar) {
        var dif = sidebar - bottom
      }
    }
	if (bottom != 0) {
		if (bottom < h - fh) {
        if (dif == 0) {
          footer.style.marginTop = ((h - fh)-bottom-19)+'px'
        } else {
          footer.style.marginTop = (dif)+'px'
        }
//          alert(bottom + ' - ' + sidebar + ' - ' + footer.style.marginTop)
        } else {
//          footer.style.marginTop = 0
          footer.style.marginTop = (dif)+'px'
//          alert(bottom + ' - ' + sidebar + ' - ' + footer.style.marginTop)
        }
	}
	footer.parentNode.style.visibility = 'visible'
}

function fillSocials() {
  var bookmarks = document.getElementById('bookmarks');
  var links = bookmarks.getElementsByTagName('a');
  var url = encodeURIComponent(window.location.href);
  if (!url) url = "";
  var title = encodeURIComponent(document.title);
  if (!title) title = "";
  var description = encodeURIComponent(document.description);
  if (!description) description = "";
  for (var i=0; i<links.length; i++) {
    var link = links[i].href;
    link = link.replace(/PERMALINK/g, url);
    link = link.replace(/TITLE/g, title);
    link = link.replace(/DESCRIPTION/g, description);
    link = link.replace(/undefined/g, '');
    links[i].href = link;
  }
}


