     }
     
 }
-
-// MARK: - Extensions
-extension Array {
-    
-    subscript (safe index: Int) -> T? {
-        return indices(self) ~= index ? self[index] : nil
-    }
-    
-}

    
    var items = [String]()
    
    var isShown = false
    
    // configuration
    var animationDuration: NSTimeInterval = 0.5
    var maskBackgroundColor: UIColor = .blackColor()

    private let tableView = UITableView()
    private let separatorView = UIView()
    
    private var tableViewTopConstraint = NSLayoutConstraint()
    
    private let tableHeaderViewHeight = CGFloat(300)
    
    private var tableViewHeight: CGFloat {
        return tableHeaderViewHeight + self.frame.height
    }
    
    convenience init(items: [String]) {
        self.init(frame: CGRect())
        
        self.items = items
        
        initialize()
    }
    
    func initialize() {
        
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        
        if let navigationController = navigationController {
            var frame = navigationController.view.bounds
            frame.origin.y = navigationController.navigationBar.frame.maxY
            frame.size.height -= frame.origin.y
            self.frame = frame
        }
        
        self.clipsToBounds = true
        self.hidden = true
        
        backgroundView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.addSubview(backgroundView)
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: tableHeaderViewHeight))
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = .clearColor()
        tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.addSubview(tableView)
        
        separatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.addSubview(separatorView)
        
        let views = ["backgroundView": backgroundView, "tableView": tableView, "separatorView": separatorView]
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[separatorView]|", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[backgroundView]|", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[tableView]|", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[separatorView(==0.5)]", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[backgroundView]|", options: nil, metrics: nil, views: views))
        self.addConstraint(NSLayoutConstraint(item: tableView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: tableViewHeight))
        tableViewTopConstraint = NSLayoutConstraint(item: tableView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: -tableViewHeight)
        self.addConstraint(tableViewTopConstraint)
    }
    
    func showMenu() {
        backgroundView.backgroundColor = self.maskBackgroundColor
        tableView.tableHeaderView?.backgroundColor = self.cellBackgroundColor
        separatorView.backgroundColor = cellSeparatorColor
        tableView.frame.origin.y = -self.frame.height
        backgroundView.alpha = 0
        self.hidden = false
        tableViewTopConstraint.constant = -tableHeaderViewHeight
        self.setNeedsUpdateConstraints()
        UIView.animateWithDuration(animationDuration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: nil, animations: {
            self.layoutIfNeeded()
            self.backgroundView.alpha = self.maskBackgroundOpacity
        }, completion: nil)
    }
    
    func hideMenu() {
        tableViewTopConstraint.constant = -tableViewHeight
        self.setNeedsUpdateConstraints()
        UIView.animateWithDuration(animationDuration, animations: {
            self.layoutIfNeeded()
            self.backgroundView.alpha = 0
        }, completion: { _ in
            self.hidden = true
