window['$'] = function(el) {
  return (typeof el == 'string' ? document.getElementById(el) : el);
};

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
};

String.prototype.camelize = function() {
  return this.replace(/[-_]\D/g, function(match) {
    return match.charAt(1).toUpperCase();
  });
};

var Browser = {
  IE:     !!(window.attachEvent && !window.opera),
  Opera:  !!window.opera,
  WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
  Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
};

var ScreenUtils = {
  getClientHeight: function() {
    return (document.documentElement && Browser.Opera && document.documentElement.clientHeight) || document.body.clientHeight; // FIXME later!
  },
  getHeight: function() {
    return window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight;
  },
  getScrollTop: function() {
    return window.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
  }
};

function getXY(element) {
  var element = $(element);
  var top = 0, left = 0;
  do {
    top += element.offsetTop  || 0;
    left += element.offsetLeft || 0;
    element = element.offsetParent;
  } while (element);
  return {x: left, y: top};
}

function getX(element) {
  return getXY(element).x;
}

function getY(element) {
  return getXY(element).y;
}

function addTableRow(params) {
  var table = $(params.table);
  var row = table.insertRow(-1);
  var cells = params.cells;

  // setting row's attributes
  for (var k in params.attr) {
    row.setAttribute(k, params.attr[k]);
  }

  for (var k = 0; k < cells.length; k++) {
    var cell = row.insertCell(-1);
    cell.className = 'td_list_row';
    cell.innerHTML = cells[k].html;
    // setting cell's attributes
    for (var i in cells[k].attr) {
      cell.setAttribute(i, cells[k].attr[i]);
    }
  }
  row.onclick = (function (x) {
    return function() {
      selRow(x);
      return true;
    }
  })(row);
  selRow(row);
  table.style.display = '';
  return true;
}

function getStyle(object, property) {
  property = property == 'float' ? 'cssFloat' : property.camelize();
  var value = object.style[property];
  if (!value) {
    if (document.defaultView && document.defaultView.getComputedStyle) {
      value = document.defaultView.getComputedStyle(object, null)[property] || null;
    } else if (window.getComputedStyle) {
      value = window.getComputedStyle(object, null)[property] || null;
    } else if (object.currentStyle) {
      value = object.currentStyle[property] || null;
    }
  }
  if (property == 'opacity') {
    return value ? parseFloat(value) : 1.0;
  }
  return value == 'auto' ? null : value;
}

Array.prototype.toString =
Object.prototype.toString = function() {
  var cont = [];
  var addslashes = function(s) {
    return
      s.split('\\').join('\\\\').split('"').join('\\"');
  }
  for (var k in this) {
    var v = this[k];
    var vs = '';
    if (typeof v == 'undefined') {
      cont[cont.length] = 'undefined';
    } else if (typeof v == 'object' && !v) {
      cont[cont.length] = 'null';
    } else /* if (v) */ {
      if (cont.length) cont[cont.length-1] += ",";
      if (v.constructor == Function) {
        vs= 'function';
      } else if (v.constructor == String) {
        vs = '"' + v.split('\\').join('\\\\').split('"').join('\\"') + '"';
      } else {
        vs = v.toString();
      }
      if (this.constructor == Array || this.constructor == Function) {
        cont[cont.length] = vs;
      } else {
        cont[cont.length] = k + ": " + vs;
      }
    }
  }
  cont = "  " + cont.join("\n").split("\n").join("\n  ");
  //cont = "  " + cont.join(" ");
  if (this.constructor == Object) {
    return  "{\n"+cont+"\n}";
  } else if (this.constructor == Array) {
    return  "[\n"+cont+"\n]";
  }
}

function jfGetInt(n){
  return parseInt(n, 10);
}
