window.gj = window.gj || {};
window.gj.canvas = window.gj.canvas || {};

(function (ns) {

    /*
     * Utility function for drawing dashed lines in Canvas
     *
     * Copied from http://davidowens.wordpress.com/2010/09/07/html-5-canvas-and-dashed-lines/
     * and changed to a function rather than method on the canvas object itself.
     */
     ns.dashedLine = function (ctx, fromX, fromY, toX, toY, pattern) {
         // Our growth rate for our line can be one of the following:
         //   (+,+), (+,-), (-,+), (-,-)
         // Because of this, our algorithm needs to understand if the x-coord and
         // y-coord should be getting smaller or larger and properly cap the values
         // based on (x,y).
         var lt = function (a, b) { return a <= b; };
         var gt = function (a, b) { return a >= b; };
         var capmin = function (a, b) { return Math.min(a, b); };
         var capmax = function (a, b) { return Math.max(a, b); };

         var checkX = { thereYet: gt, cap: capmin };
         var checkY = { thereYet: gt, cap: capmin };

         if (fromY - toY > 0) {
             checkY.thereYet = lt;
             checkY.cap = capmax;
         }
         if (fromX - toX > 0) {
             checkX.thereYet = lt;
             checkX.cap = capmax;
         }

         ctx.moveTo(fromX, fromY);
         var offsetX = fromX;
         var offsetY = fromY;
         var idx = 0, dash = true;
         while (!(checkX.thereYet(offsetX, toX) && checkY.thereYet(offsetY, toY))) {
             var ang = Math.atan2(toY - fromY, toX - fromX);
             var len = pattern[idx];

             offsetX = checkX.cap(toX, offsetX + (Math.cos(ang) * len));
             offsetY = checkY.cap(toY, offsetY + (Math.sin(ang) * len));

             if (dash) ctx.lineTo(offsetX, offsetY);
             else ctx.moveTo(offsetX, offsetY);

             idx = (idx + 1) % pattern.length;
             dash = !dash;
         }
     };

})(window.gj.canvas);

