var math = (function() {
  var normaliseRadians = function(radians){
      radians=radians % (2*Math.PI);
      if(radians<0) {
          radians+=(2*Math.PI);
      }
      return radians;
  };

  var radians = function(degrees) {
    return degrees*(Math.PI/180);
  };

  return {
    normaliseRadians: normaliseRadians,
    radians:          radians
  };
})();

var vectors = (function() {
   var rotate = function(v, angle){
     angle=math.normaliseRadians(angle);
     return [v[0]* Math.cos(angle)-v[1]*Math.sin(angle),
             v[0]* Math.sin(angle)+v[1]*Math.cos(angle)];
  };

  var dot = function(v1, v2){
    return (v1[0] * v2[0]) + (v1[1] * v2[1]);
  };

  var len = function(v) {
    return Math.sqrt(v[0]*v[0] + v[1]*v[1]);
  };

  var unit = function(v) {
    var l = len(v);
    if(l) {
      return [v[0] / l, v[1] / l];
    }
    return [0, 0];
  };

  return {
    rotate: rotate,
    dot:    dot,
    len:    len,
    unit:   unit
  };
})();

var indexOf = function(needle) {
    if(typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                if(this[i] === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle);
};