// 1) Set a device-width viewport
// 2) Set a border or outline on the body
// 3) get document.body.clientWidth
// 4) Give me a goddamn prize
(function(window, $) {

var absolutify = document.createElement('a')
    // A regex for detecting http(s) URLs
  , protocolMatcher = /^http(s)?/

  , hosts = [
        '//ir0.mobify.com'
      , '//ir1.mobify.com'
      , '//ir2.mobify.com'
      , '//ir3.mobify.com'
    ]

    /**
     * Hash `url` into a well distributed int.
     */
  , URLHash = Mobify.URLHash = function(url) {
        var hc, len = url.length;

        // Let's hash on 8 different character codes, chosen 
        // progresively back from the end of the URL, and xor 'em
        hc = url.charCodeAt(len - 2 % len) ^ url.charCodeAt(len - 3 % len)
           ^ url.charCodeAt(len - 5 % len) ^ url.charCodeAt(len - 7 % len)
           ^ url.charCodeAt(len - 11 % len) ^ url.charCodeAt(len - 13 % len)
           ^ url.charCodeAt(len - 17 % len) ^ url.charCodeAt(len - 19 % len)

        // A little linear congruential generator action to shuffle 
        // things up, inspired by libc's random number generator
        hc = (((hc * 1103515245) % 4294967296 + 12345) % 4294967296);
        hc = (hc < 0) ? hc + 4294967296: hc;
        return hc;
    }
          
    /**
     * Returns a URL suitable for use with the 'ir' service.
     *  :host/:format:quality/:width/:height/:url
     */ 
  , getImageURL = Mobify.getImageURL = function(url, options) {
        options = options || {}

        var host = hosts[URLHash(url) % hosts.length]
          , bits = [host];

        // If projectName is set on defaults and truthy, put it in resized image urls
        if (defaults.projectName) {
            var projectIdeintifier = "project-" + defaults.projectName;
            bits.push(projectIdeintifier);
        }

        if (options.format) {
            bits.push(options.format + (options.quality || ''));
        }

        if (options.maxWidth) {
            bits.push(options.maxWidth)

            if (options.maxHeight) {
                bits.push(options.maxHeight);
            }
        }

        bits.push(url);
        return bits.join('/');
    }

    /**
     * Searches the collection for imgs and modifies them to use the `ir` service.
     * Pass `options` to modify how the images are serviced.
     */
  , resizeImages = $.fn.resizeImages = function(options) {
        var opts = $.extend(defaults, typeof options == 'object' && options)
          , dpr = window.devicePixelRatio
          , $imgs = this.filter(opts.selector).add(this.find(opts.selector))
          , attr;

        if (typeof options == 'number') {
            opts.maxWidth = options;
        }

        if (dpr) {
            if (opts.maxWidth) {
                opts.maxWidth = Math.ceil(opts.maxWidth * dpr);
            }

            if (opts.maxHeight) {
                opts.maxHeight = Math.ceil(opts.maxHeight * dpr);
            }
        }

        return $imgs.each(function() {
            if (attr = this.getAttribute(opts.attribute)) {
                absolutify.href = attr;
                var url = absolutify.href;
                // Produce an image resize url only for matched protocols
                if(protocolMatcher.exec(url)) {
                    this.setAttribute('x-src', getImageURL(url, opts));
                }
            }
        });
    }
  , defaults = resizeImages.defaults = {
        selector: 'img[x-src]'
      , attribute: 'x-src'
      , projectName: Mobify.config.projectName || ''
    }

})(this, Mobify.$);

@@ -1,146 +1,4 @@
-// 1) Set a device-width viewport%0A// 2) Set a border or outline on the body%0A// 3) get document.body.clientWidth%0A// 4) Give me a goddamn prize%0A
 (fun
@@ -145,986 +145,105 @@
 %0A%0A  
-, hosts = %5B%0A        '//ir0.mobify.com'%0A      , '//ir1.mobify.com'%0A      , '//ir2.mobify.com'%0A      , '//ir3.mobify.com'%0A    %5D%0A%0A    /**%0A     * Hash %60url%60 into a well distributed int.%0A     */%0A  , URLHash = Mobify.URLHash = function(url) %7B%0A        var hc, len = url.length;%0A%0A        // Let's hash on 8 different character codes, chosen %0A        // progresively back from the end of the URL, and xor 'em%0A        hc = url.charCodeAt(len - 2 %25 len) %5E url.charCodeAt(len - 3 %25 len)%0A           %5E url.charCodeAt(len - 5 %25 len) %5E url.charCodeAt(len - 7 %25 len)%0A           %5E url.charCodeAt(len - 11 %25 len) %5E url.charCodeAt(len - 13 %25 len)%0A           %5E url.charCodeAt(len - 17 %25 len) %5E url.charCodeAt(len - 19 %25 len)%0A%0A        // A little linear congruential generator action to shuffle %0A        // things up, inspired by libc's random number generator%0A        hc = (((hc * 1103515245) %25 4294967296 + 12345) %25 4294967296);%0A        hc = (hc %3C 0) ? hc + 4294967296: hc;%0A        return hc;%0A    %7D
+// A protocol relative URL for the host ir0.mobify.com%0A  , PROTOCOL_AND_HOST = '//ir0.mobify.com'
 %0A   
@@ -491,74 +491,33 @@
 var 
-host = hosts%5BURLHash(url) %25 hosts.length%5D%0A          , bits = %5Bhost
+bits = %5BPROTOCOL_AND_HOST
 %5D;%0A%0A
@@ -1547,16 +1547,41 @@
  attr;%0A%0A
+        // integer width%0A
         
@@ -1642,23 +1642,35 @@
 Width = 
+Math.floor(
 options
+)
 ;%0A      
@@ -2515,16 +2515,236 @@
 %0A    %7D%0A%0A
+/**%0A* TODO: Implement automatic maximum non-zoomed displayable size detection:%0A* 1) Set a device-width viewport%0A* 2) Set a border or outline on the body%0A* 3) get document.body.clientWidth%0A* 4) Give me a goddamn prize%0A*/%0A
 %7D)(this,

remove sharding from image resize client code