// todo; create copy of tanto to use on this site.

if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fun /*, thisp*/) {
        if (typeof fun != "function") {
            throw new TypeError();
        }
        var len = this.length,
            thisp = arguments[1],
            i;

        for (i = 0; i < len; i++) {
            if (i in this) {
                fun.call(thisp, this[i], i, this);
            }
        }
    };
}


if (!Array.prototype.map) {
  Array.prototype.map = function(fun /*, thisp*/) {
    if (typeof fun != "function") {
      throw new TypeError();
    }

    var len = this.length,
        res = new Array(len),
        thisp = arguments[1],
        i;

    for (i = 0; i < len; i++) {
        if (i in this) {
            res[i] = fun.call(thisp, this[i], i, this);
        }
    }
    return res;
  };
}


(function() {
    var contentsMap = function(elem) {
            return elem.innerHTML;
        },
        heightsMap = function(elem) {
            return elem.offsetHeight + 16 + "px";
        };

    window.onload = function() {
        var isMSIE = /*@cc_on!@*/false,

            isLowRes = false,
            width = function() {
                if (window.innerWidth) {
                    return window.innerWidth;
                } else if (document.documentElement && document.documentElement.clientWidth !== 0) {
                    return document.documentElement.clientWidth;
                } else if (document.body) {
                    return document.body.clientWidth;
                }  
                return 0;
            },
            layout = function() {
                if (!isLowRes && width() < 850) {
                    isLowRes = true;
                    document.body.className += " low-res";
                } else if (isLowRes && width() > 870) {
                    isLowRes = false;
                    document.body.className = document.body.className.replace(/low-res/gi, "");
                }
        
            };
        
        window.onresize = function() {
            layout();
        };

        if (isMSIE) { return false; }       
        var pres = Array.prototype.slice.call(document.getElementsByTagName("pre"), 0),
            contents = pres.map(contentsMap),
            heights = pres.map(heightsMap),
            i = 0;
        
        pres.forEach(function(el) {
            if (el.className.indexOf("not-js") < 0) {
                var editor = new CodeMirror(CodeMirror.replace(el), {
                    parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
                    path: "http://static.boundvariable.com/j/",
                    stylesheet: "http://static.boundvariable.com/c/jscolors.css",
                    content: contents[i],
                    height: heights[i],
                    readOnly: true,
                    initCallback: function() {editor.reindent();}
               });
            }
           i = i + 1;
        });
        layout();
    };

}());

