1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
$axure.internal(function($ax) {
    $ax.public.fn.matrixMultiply = function(matrix, vector) {
        if(!matrix.tx) matrix.tx = 0;
        if(!matrix.ty) matrix.ty = 0;
        var outX = matrix.m11 * vector.x + matrix.m12 * vector.y + matrix.tx;
        var outY = matrix.m21 * vector.x + matrix.m22 * vector.y + matrix.ty;
        return { x: outX, y: outY };
    }

    $ax.public.fn.matrixInverse = function(matrix) {
        if(!matrix.tx) matrix.tx = 0;
        if(!matrix.ty) matrix.ty = 0;

        var determinant = matrix.m11*matrix.m22 - matrix.m12*matrix.m21;
        //var threshold = (M11 * M11 + M22 *M22 + M12 *M12+ M21 *M21) / 100000;
        //if(determinant.DeltaEquals(0, threshold) && determinant < 0.01) {
        //    return Invalid;
        //}
        return  {
            m11 : matrix.m22/determinant,
            m12 : -matrix.m12/determinant,
            tx : (matrix.ty*matrix.m12 - matrix.tx*matrix.m22)/determinant,
            m21: -matrix.m21 / determinant,
            m22: matrix.m11 / determinant,
            ty: (matrix.tx * matrix.m21 - matrix.ty * matrix.m11) / determinant
        };
    }


    $ax.public.fn.matrixMultiplyMatrix = function (matrix1, matrix2) {
        if (!matrix1.tx) matrix1.tx = 0;
        if (!matrix1.ty) matrix1.ty = 0;
        if (!matrix2.tx) matrix2.tx = 0;
        if (!matrix2.ty) matrix2.ty = 0;

        return {
            m11: matrix1.m12*matrix2.m21 + matrix1.m11*matrix2.m11,
            m12: matrix1.m12*matrix2.m22 + matrix1.m11*matrix2.m12,
            tx: matrix1.m12 * matrix2.ty + matrix1.m11 * matrix2.tx + matrix1.tx,
            m21: matrix1.m22 * matrix2.m21 + matrix1.m21 * matrix2.m11,
            m22: matrix1.m22 * matrix2.m22 + matrix1.m21 * matrix2.m12,
            ty: matrix1.m22 * matrix2.ty + matrix1.m21 * matrix2.tx + matrix1.ty,
        };
    }


    $ax.public.fn.transformFromElement = function (element) {
        var st = window.getComputedStyle(element, null);

        var tr = st.getPropertyValue("-webkit-transform") ||
            st.getPropertyValue("-moz-transform") ||
            st.getPropertyValue("-ms-transform") ||
            st.getPropertyValue("-o-transform") ||
            st.getPropertyValue("transform");

        if (tr.indexOf('none') < 0) {
            var matrix = tr.split('(')[1];
            matrix = matrix.split(')')[0];
            matrix = matrix.split(',');
            for (var l = 0; l < matrix.length; l++) {
                matrix[l] = Number(matrix[l]);
            }

        } else { matrix = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; }

        return matrix;
        // matrix[0] = cosine, matrix[1] = sine. 
        // Assuming the element is still orthogonal.
    }

    $ax.public.fn.vectorMinus = function(vector1, vector2) { return { x: vector1.x - vector2.x, y: vector1.y - vector2.y }; }

    $ax.public.fn.vectorPlus = function (vector1, vector2) { return { x: vector1.x + vector2.x, y: vector1.y + vector2.y }; }

    $ax.public.fn.vectorMidpoint = function (vector1, vector2) { return { x: (vector1.x + vector2.x) / 2.0, y: (vector1.y + vector2.y) / 2.0 }; }

    $ax.public.fn.fourCornersToBasis = function (fourCorners) {
        return {
            widthVector: $ax.public.fn.vectorMinus(fourCorners.widgetTopRight, fourCorners.widgetTopLeft),
            heightVector: $ax.public.fn.vectorMinus(fourCorners.widgetBottomLeft, fourCorners.widgetTopLeft)
        };
    }

    $ax.public.fn.matrixString = function(m11, m21, m12, m22, tx, ty) {
        return "Matrix(" + m11 + "," + m21 + "," + m12 + "," + m22 + ", " + tx + ", " + ty + ")";
    }
    
    //$ax.public.fn.getWidgetBoundingRect = function (widgetId) {
    //    var emptyRect = { left: 0, top: 0, centerPoint: { x: 0, y: 0 }, width: 0, height: 0 };
    //    var element = document.getElementById(widgetId);
    //    if (!element) return emptyRect;

    //    var object = $obj(widgetId);
    //    if (object && object.type && $ax.public.fn.IsLayer(object.type)) {
    //        var layerChildren = _getLayerChildrenDeep(widgetId);
    //        if (!layerChildren) return emptyRect;
    //        else return _getBoundingRectForMultipleWidgets(layerChildren);
    //    }
    //    return _getBoundingRectForSingleWidget(widgetId);
    //};

    var _getLayerChildrenDeep = $ax.public.fn.getLayerChildrenDeep = function (layerId, includeLayers, includeHidden) {
        var deep = [];
        var children = $ax('#' + layerId).getChildren()[0].children;
        for (var index = 0; index < children.length; index++) {
            var childId = children[index];
            if(!includeHidden && !$ax.visibility.IsIdVisible(childId)) continue;
            if ($ax.public.fn.IsLayer($obj(childId).type)) {
                if (includeLayers) deep.push(childId);
                var recursiveChildren = _getLayerChildrenDeep(childId, includeLayers, includeHidden);
                for (var j = 0; j < recursiveChildren.length; j++) deep.push(recursiveChildren[j]);
            } else deep.push(childId);
        }
        return deep;
    };

    //var _getBoundingRectForMultipleWidgets = function (widgetsIdArray, relativeToPage) {
    //    if (!widgetsIdArray || widgetsIdArray.constructor !== Array) return undefined;
    //    if (widgetsIdArray.length == 0) return { left: 0, top: 0, centerPoint: { x: 0, y: 0 }, width: 0, height: 0 };
    //    var widgetRect = _getBoundingRectForSingleWidget(widgetsIdArray[0], relativeToPage, true);
    //    var boundingRect = { left: widgetRect.left, right: widgetRect.right, top: widgetRect.top, bottom: widgetRect.bottom };

    //    for (var index = 1; index < widgetsIdArray.length; index++) {
    //        widgetRect = _getBoundingRectForSingleWidget(widgetsIdArray[index], relativeToPage);
    //        boundingRect.left = Math.min(boundingRect.left, widgetRect.left);
    //        boundingRect.top = Math.min(boundingRect.top, widgetRect.top);
    //        boundingRect.right = Math.max(boundingRect.right, widgetRect.right);
    //        boundingRect.bottom = Math.max(boundingRect.bottom, widgetRect.bottom);
    //    }

    //    boundingRect.centerPoint = { x: (boundingRect.right + boundingRect.left) / 2.0, y: (boundingRect.bottom + boundingRect.top) / 2.0 };
    //    boundingRect.width = boundingRect.right - boundingRect.left;
    //    boundingRect.height = boundingRect.bottom - boundingRect.top;
    //    return boundingRect;
    //};

    //var _getBoundingRectForSingleWidget = function (widgetId, relativeToPage, justSides) {
    //    var element = document.getElementById(widgetId);
    //    var boundingRect, tempBoundingRect, position;
    //    var displayChanged = _displayHackStart(element);

    //    if (_isCompoundVectorHtml(element)) {
    //        //tempBoundingRect =  _getCompoundImageBoundingClientSize(widgetId);
    //        //position = { left: tempBoundingRect.left, top: tempBoundingRect.top };
    //        position = $(element).position();
    //        tempBoundingRect = {};
    //        tempBoundingRect.left = position.left; //= _getCompoundImageBoundingClientSize(widgetId);
    //        tempBoundingRect.top = position.top;
    //        tempBoundingRect.width = Number(element.getAttribute('data-width'));
    //        tempBoundingRect.height = Number(element.getAttribute('data-height'));
    //    } else {
    //        var boundingElement = element;
    //        if($ax.dynamicPanelManager.isIdFitToContent(widgetId)) {
    //            var stateId = $ax.visibility.GetPanelState(widgetId);
    //            if(stateId != '') boundingElement = document.getElementById(stateId);
    //        }
    //        tempBoundingRect = boundingElement.getBoundingClientRect();

    //        var jElement = $(element);
    //        position = jElement.position();
    //        if(jElement.css('position') == 'fixed') {
    //            position.left += Number(jElement.css('margin-left').replace("px", ""));
    //            position.top += Number(jElement.css('margin-top').replace("px", ""));
    //        }
    //    }

    //    var layers = $ax('#' + widgetId).getParents(true, ['layer'])[0];
    //    var flip = '';
    //    var mirrorWidth = 0;
    //    var mirrorHeight = 0;
    //    for (var i = 0; i < layers.length; i++) {

    //        //should always be 0,0
    //        var layerPos = $jobj(layers[i]).position();
    //        position.left += layerPos.left;
    //        position.top += layerPos.top;

    //        var outer = $ax.visibility.applyWidgetContainer(layers[i], true, true);
    //        if (outer.length) {
    //            var outerPos = outer.position();
    //            position.left += outerPos.left;
    //            position.top += outerPos.top;
    //        }

    //        //when a group is flipped we find the unflipped position
    //        var inner = $jobj(layers[i] + '_container_inner');
    //        var taggedFlip = inner.data('flip');
    //        if (inner.length && taggedFlip) {
    //            //only account for flip if transform is applied
    //            var matrix = taggedFlip && (inner.css("-webkit-transform") || inner.css("-moz-transform") ||
    //                        inner.css("-ms-transform") || inner.css("-o-transform") || inner.css("transform"));
    //            if (matrix !== 'none') {
    //                flip = taggedFlip;
    //                mirrorWidth = $ax.getNumFromPx(inner.css('width'));
    //                mirrorHeight = $ax.getNumFromPx(inner.css('height'));
    //            }
    //        }
    //    }
    //     //Now account for flip
    //    if (flip == 'x') position.top = mirrorHeight - position.top - element.getBoundingClientRect().height;
    //    else if (flip == 'y') position.left = mirrorWidth - position.left - element.getBoundingClientRect().width;

    //    boundingRect = {
    //        left: position.left,
    //        right: position.left + tempBoundingRect.width,
    //        top: position.top,
    //        bottom: position.top + tempBoundingRect.height
    //    };

    //    _displayHackEnd(displayChanged);
    //    if (justSides) return boundingRect;

    //    boundingRect.width = boundingRect.right - boundingRect.left;
    //    boundingRect.height = boundingRect.bottom - boundingRect.top;

    //    boundingRect.centerPoint = {
    //        x: boundingRect.width / 2 + boundingRect.left,
    //        y: boundingRect.height / 2 + boundingRect.top
    //    };

    //    return boundingRect;
    //};

    var _getPointAfterRotate = $ax.public.fn.getPointAfterRotate = function (angleInDegrees, pointToRotate, centerPoint) {
        var displacement = $ax.public.fn.vectorMinus(pointToRotate, centerPoint);
        var rotationMatrix = $ax.public.fn.rotationMatrix(angleInDegrees);
        rotationMatrix.tx = centerPoint.x;
        rotationMatrix.ty = centerPoint.y;
        return $ax.public.fn.matrixMultiply(rotationMatrix, displacement);
    };

    $ax.public.fn.getBoundingSizeForRotate = function(width, height, rotation) {
        // point to rotate around doesn't matter since we just care about size, if location matter we need more args and location matters.

        var origin = { x: 0, y: 0 };

        var corner1 = { x: width, y: 0 };
        var corner2 = { x: 0, y: height };
        var corner3 = { x: width, y: height };

        corner1 = _getPointAfterRotate(rotation, corner1, origin);
        corner2 = _getPointAfterRotate(rotation, corner2, origin);
        corner3 = _getPointAfterRotate(rotation, corner3, origin);

        var left = Math.min(0, corner1.x, corner2.x, corner3.x);
        var right = Math.max(0, corner1.x, corner2.x, corner3.x);
        var top = Math.min(0, corner1.y, corner2.y, corner3.y);
        var bottom = Math.max(0, corner1.y, corner2.y, corner3.y);

        return { width: right - left, height: bottom - top };
    }

    $ax.public.fn.getBoundingRectForRotate = function (boundingRect, rotation) {
        var centerPoint = boundingRect.centerPoint;
        var corner1 = { x: boundingRect.left, y: boundingRect.top };
        var corner2 = { x: boundingRect.right, y: boundingRect.top };
        var corner3 = { x: boundingRect.right, y: boundingRect.bottom };
        var corner4 = { x: boundingRect.left, y: boundingRect.bottom };
        corner1 = _getPointAfterRotate(rotation, corner1, centerPoint);
        corner2 = _getPointAfterRotate(rotation, corner2, centerPoint);
        corner3 = _getPointAfterRotate(rotation, corner3, centerPoint);
        corner4 = _getPointAfterRotate(rotation, corner4, centerPoint);

        var left = Math.min(corner1.x, corner2.x, corner3.x, corner4.x);
        var right = Math.max(corner1.x, corner2.x, corner3.x, corner4.x);
        var top = Math.min(corner1.y, corner2.y, corner3.y, corner4.y);
        var bottom = Math.max(corner1.y, corner2.y, corner3.y, corner4.y);

        return { left: left, top: top, width: right - left, height: bottom - top };
    }


    //$ax.public.fn.getPositionRelativeToParent = function (elementId) {
    //    var element = document.getElementById(elementId);
    //    var list = _displayHackStart(element);
    //    var position = $(element).position();
    //    _displayHackEnd(list);
    //    return position;
    //};

    //var _displayHackStart = $ax.public.fn.displayHackStart = function (element) {
    //    // TODO: Options: 1) stop setting display none. Big change for this late in the game. 2) Implement our own bounding.
    //    // TODO:  3) Current method is look for any parents that are set to none, and and temporarily unblock. Don't like it, but it works.
    //    var parent = element;
    //    var displays = [];
    //    while (parent) {
    //        if (parent.style.display == 'none') {
    //            displays.push(parent);
    //            //use block to overwrites default hidden objects' display
    //            parent.style.display = 'block';
    //        }
    //        parent = parent.parentElement;
    //    }

    //    return displays;
    //};

    //var _displayHackEnd = $ax.public.fn.displayHackEnd = function (displayChangedList) {
    //    for (var i = 0; i < displayChangedList.length; i++) displayChangedList[i].style.display = 'none';
    //};


    var _isCompoundVectorHtml = $ax.public.fn.isCompoundVectorHtml = function(hElement) {
        return hElement.hasAttribute('compoundmode') && hElement.getAttribute('compoundmode') == "true";
    }

    $ax.public.fn.removeCompound = function (jobj) { if(_isCompoundVectorHtml(jobj[0])) jobj.removeClass('compound'); }
    $ax.public.fn.restoreCompound = function (jobj) { if (_isCompoundVectorHtml(jobj[0])) jobj.addClass('compound'); }

    $ax.public.fn.compoundIdFromComponent = function(id) {

        var pPos = id.indexOf('p');
        var dashPos = id.indexOf('-');
        if (pPos < 1) return id;
        else if (dashPos < 0) return id.substring(0, pPos);
        else return id.substring(0, pPos) + id.substring(dashPos);
    }

    $ax.public.fn.l2 = function (x, y) { return Math.sqrt(x * x + y * y); }

    $ax.public.fn.convertToSingleImage = function (jobj) {
        if(!jobj[0]) return;

        var widgetId = jobj[0].id;
        var object = $obj(widgetId);

        if ($ax.public.fn.IsLayer(object.type)) {
            var recursiveChildren = _getLayerChildrenDeep(widgetId, true);
            for (var j = 0; j < recursiveChildren.length; j++)
                $ax.public.fn.convertToSingleImage($jobj(recursiveChildren[j]));
            return;
        }

        //var layer = 

        if(!_isCompoundVectorHtml(jobj[0])) return;


        $('#' + widgetId).removeClass("compound");
        $('#' + widgetId + '_img').removeClass("singleImg");
        jobj[0].setAttribute('compoundmode', 'false');
        
        var components = object.compoundChildren;
        delete object.generateCompound;
        for (var i = 0; i < components.length; i++) {
            var componentJobj = $jobj($ax.public.fn.getComponentId(widgetId, components[i]));
            componentJobj.css('display', 'none');
            componentJobj.css('visibility', 'hidden');
        }
    }


    $ax.public.fn.getContainerDimensions = function(query) {
        // returns undefined if no containers found.
        var containerDimensions;
        for (var i = 0; i < query[0].children.length; i++) {
            var node = query[0].children[i];
            if (node.id.indexOf(query[0].id) >= 0 && node.id.indexOf('container') >= 0) {
                containerDimensions = node.style;
            }
        }
        return containerDimensions;
    }


    $ax.public.fn.rotationMatrix = function (angleInDegrees) {
        var angleInRadians = angleInDegrees * (Math.PI / 180);
        var cosTheta = Math.cos(angleInRadians);
        var sinTheta = Math.sin(angleInRadians);

        return { m11: cosTheta, m12: -sinTheta, m21: sinTheta, m22: cosTheta, tx: 0.0, ty: 0.0 };
    }

    $ax.public.fn.GetFieldFromStyle = function (query, field) {
        var raw = query[0].style[field];
        if (!raw) raw = query.css(field);
        return Number(raw.replace('px', ''));
        }


    $ax.public.fn.setTransformHowever = function (transformString) {
        return {
            '-webkit-transform': transformString,
            '-moz-transform': transformString,
            '-ms-transform': transformString,
            '-o-transform': transformString,
            'transform': transformString
        };
    }

    $ax.public.fn.getCornersFromComponent = function (id) {
        var element = document.getElementById(id);
        var matrix = element ? $ax.public.fn.transformFromElement(element) : [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        var currentMatrix = { m11: matrix[0], m21: matrix[1], m12: matrix[2], m22: matrix[3], tx: matrix[4], ty: matrix[5] };
        var dimensions = {};
        var axObj = $ax('#' + id);
        var viewportLocation = axObj.offsetLocation();
        dimensions.left = viewportLocation.left;
        dimensions.top = viewportLocation.top;
        //dimensions.left = axObj.left(true);
        //dimensions.top = axObj.top(true);
        var size = axObj.size();
        dimensions.width = size.width;
        dimensions.height = size.height;
        //var transformMatrix1 = { m11: 1, m12: 0, m21: 0, m22: 1, tx: -invariant.x, ty: -invariant.y };
        //var transformMatrix2 = { m11: 1, m12: 0, m21: 0, m22: 1, tx: 500, ty: 500 };

        var halfWidth = dimensions.width * 0.5;
        var halfHeight = dimensions.height * 0.5;
        //var preTransformTopLeft = { x: -halfWidth, y: -halfHeight };
        //var preTransformBottomLeft = { x: -halfWidth, y: halfHeight };
        var preTransformTopRight = { x: halfWidth, y: -halfHeight };
        var preTransformBottomRight = { x: halfWidth, y: halfHeight };

        return {
            //relativeTopLeft: $ax.public.fn.matrixMultiply(currentMatrix, preTransformTopLeft),
            //relativeBottomLeft: $ax.public.fn.matrixMultiply(currentMatrix, preTransformBottomLeft),
            relativeTopRight: $ax.public.fn.matrixMultiply(currentMatrix, preTransformTopRight),
            relativeBottomRight: $ax.public.fn.matrixMultiply(currentMatrix, preTransformBottomRight),
            centerPoint: { x: dimensions.left + halfWidth, y: dimensions.top + halfHeight }
            //originalDimensions: dimensions,
            //transformShift: { x: matrix[4], y: matrix[5] }
        }
    }



    $ax.public.fn.inversePathLengthFunction = function (pathFunction) {
        // these are for computing the inverse functions of path integrals.

        var makeDivisionNode = function(node1, node2) {
            var param = 0.5 * (node1.Param + node2.Param);
            var inBetweenNode = {
                LowerStop: node1,
                HigherStop: node2,
                Param: param,
                Position: pathFunction(param),
                Cumulative: 0.0
            };
            var lowerDisplacement = $ax.public.fn.vectorMinus(node1.Position, inBetweenNode.Position);
            inBetweenNode.LowerInterval = {
                Length: $ax.public.fn.l2(lowerDisplacement.x, lowerDisplacement.y),
                Node: inBetweenNode,
                IsHigher: false
            };
            var higherDisplacement = $ax.public.fn.vectorMinus(node2.Position, inBetweenNode.Position);
            inBetweenNode.HigherInterval = {
                Length: $ax.public.fn.l2(higherDisplacement.x, higherDisplacement.y),
                Node: inBetweenNode,
                IsHigher: true
            };
            return inBetweenNode;
        };

        var expandLower = function(node) {
            node.LowerChild = makeDivisionNode(node.LowerStop, node);
            node.LowerChild.Parent = node;
        };

        var expandHigher = function(node) {
            node.HigherChild = makeDivisionNode(node, node.HigherStop);
            node.HigherChild.Parent = node;
        };

        // for this function, cumulative is a global variable
        var cumulative = 0.0;
        var labelCumulativeLength = function(node) {
            if(!node.LowerChild) {
                node.LowerStop.Cumulative = cumulative;
                cumulative += node.LowerInterval.Length;
                node.Cumulative = cumulative;
            } else labelCumulativeLength(node.LowerChild);

            if(!node.HigherChild) {
                node.Cumulative = cumulative;
                cumulative += node.HigherInterval.Length;
                node.HigherStop.Cumulative = cumulative;
            } else labelCumulativeLength(node.HigherChild);
        };

        var getIntervalFromPathLength = function(node, length) {
            if(length < node.Cumulative) {
                return node.LowerChild ? getIntervalFromPathLength(node.LowerChild, length) : node.LowerInterval;
            } else return node.HigherChild ? getIntervalFromPathLength(node.HigherChild, length) : node.HigherInterval;
        };

        var intervalLowerEnd = function(interval) {
            return interval.IsHigher ? interval.Node : interval.Node.LowerStop;
        };

        var intervalHigherEnd = function(interval) {
            return interval.IsHigher ? interval.Node.HigherStop : interval.Node;
        };

        var getParameterFromPathLength = function (node, length) {
            var interval = getIntervalFromPathLength(node, length);
            var lowerNode = intervalLowerEnd(interval);
            var higherNode = intervalHigherEnd(interval);
            return lowerNode.Param + (higherNode.Param - lowerNode.Param) * (length - lowerNode.Cumulative) / (higherNode.Cumulative - lowerNode.Cumulative);
        };

        var insertIntoSortedList = function (longer, shorter, toInsert) {
            while (true) {
                if (!longer) {
                    longer = shorter;
                    shorter = shorter.NextLongest;
                    continue;
                } else if (!shorter) longer.NextLongest = toInsert;
                else {
                    if (longer.Length >= toInsert.Length && shorter.Length <= toInsert.Length) {
                        longer.NextLongest = toInsert;
                        toInsert.NextLongest = shorter;
                    } else {
                        longer = shorter;
                        shorter = shorter.NextLongest;
                        continue;
                    }
                }
                break;
            }
        }
        var head = {Param: 0.0, Position: pathFunction(0.0) };
        var tail = { Param: 1.0, Position: pathFunction(1.0) };
        var root = makeDivisionNode(head, tail);
        var currentCurveLength = root.LowerInterval.Length + root.HigherInterval.Length;
        var longestInterval;
        if (root.LowerInterval.Length < root.HigherInterval.Length) {
            longestInterval = root.HigherInterval;
            longestInterval.NextLongest = root.LowerInterval;
        } else {
            longestInterval = root.LowerInterval;
            longestInterval.NextLongest = root.HigherInterval;
        }
        while (longestInterval.Length * 100.0 > currentCurveLength) {
            var newNode;
            if (longestInterval.IsHigher) {
                expandHigher(longestInterval.Node);
                newNode = longestInterval.Node.HigherChild;
            } else {
                expandLower(longestInterval.Node);
                newNode = longestInterval.Node.LowerChild;
            }
            currentCurveLength += (newNode.LowerInterval.Length + newNode.HigherInterval.Length - longestInterval.Length);
            insertIntoSortedList(null, longestInterval, newNode.LowerInterval);
            insertIntoSortedList(null, longestInterval, newNode.HigherInterval);
            longestInterval = longestInterval.NextLongest;
        }
        labelCumulativeLength(root);

        return function(lengthParam) {
            return getParameterFromPathLength(root, lengthParam * cumulative);
        };
    }
});