     
     static func findThreshhold(elements: [CGFloat], threshholds: [CGFloat], measurement: CGFloat) -> CGFloat {
         assert(elements.count == threshholds.count + 1, "elements and threshholds do not match")
+        return elements[self.findThreshholdIndex(threshholds, measurement: measurement)]
+    }
+    static func findThreshholdIndex(threshholds: [CGFloat], measurement: CGFloat) -> Int {
         for (i, threshhold) in enumerate(reverse(threshholds)) {
             if measurement >= threshhold {
                 let actualIndex = threshholds.count - i
-                return elements[actualIndex]
-            }
-        }
-        return elements[0]
+                return actualIndex
+            }
+        }
+        return 0
     }
 }
 


import UIKit

var DEBUG_SHOW_SPACERS = false

// TODO: create class from layout dictionary?

struct layoutConstants {
    static let landscapeRatio: CGFloat = 2
    
    static let sideEdgesPortraitArray: [CGFloat] = [3, 4]
    static let sideEdgesPortraitWidthThreshholds: [CGFloat] = [700]
    static let sideEdgesLandscape: CGFloat = 3
    static let topEdgePortraitArray: [CGFloat] = [12, 10, 8]
    static let topEdgePortraitWidthThreshholds: [CGFloat] = [350, 400]
    static let topEdgeLandscape: CGFloat = 6
    
    static let keyboardShrunkSizeArray: [CGFloat] = [522, 524]
    static let keyboardShrunkSizeWidthThreshholds: [CGFloat] = [700]
    static let keyboardShrunkSizeBaseWidthThreshhold: CGFloat = 600
    
    static let rowGapsPortrait: CGFloat = 15
    static let rowGapsLandscape: CGFloat = 7
    
    static let keyGaps: CGFloat = 6
    static let keyGapsSmall: CGFloat = 5

            
            // measurement
            let keyHeight: CGFloat = {
                let totalGaps = bottomEdge + topEdge + rowGapTotal
                return (bounds.height - totalGaps) / CGFloat(numRows)
            }()
            
