 
 // this extension dynamically change the size of the fonts, so text can fit
 extension UILabel {
-    func adjustFontSizeToHeight(var font: UIFont, max:CGFloat, min:CGFloat)
+    func adjustFontSizeToHeight(var font: UIFont, max: CGFloat, min: CGFloat)
     {
         // Initial size is max and the condition the min.
         for var size = max ; size >= min ; size -= 0.1
         {
-            
+
             font = font.fontWithSize(size)
-            let attrString = NSAttributedString(string: self.text!, attributes: [NSFontAttributeName : font])
+            let attrString = NSAttributedString(string: self.text!, attributes: [NSFontAttributeName: font])
             let rectSize = attrString.boundingRectWithSize(CGSizeMake(self.bounds.width, CGFloat.max), options: .UsesLineFragmentOrigin, context: nil)
-            
+
             if rectSize.size.height <= self.bounds.height
             {
                 self.font = font
                 break
             }
-
         }
         // in case, it is better to have the smallest possible font
         self.font = font

                    completion: nil
                )
            }
        }else {
            isSearchBarVisible = false
            decksCollectionView.contentInset = UIEdgeInsets(top: -topItemOffset, left: 0, bottom: 0, right: 0)
            
            UIView.animateWithDuration(softAnimationDuration, delay: 0, options: .CurveEaseOut,
                animations: {
                    self.searchBar?.frame.origin.y = 0

        cell.deckNameLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.deckNameLabel.preferredMaxLayoutWidth = cell.bounds.size.width
        cell.contentView.backgroundColor = UIColor.sb_Graphite()

        return cell
    }

    // When cell tapped, change to test
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if let decks = searchDecks {
            DecksViewController.selectedDeckForTesting = decks[indexPath.row]
        } else if let notNilDecksArray = decksArray {
            DecksViewController.selectedDeckForTesting = notNilDecksArray[indexPath.row]
        }
        if let test = storyboard?.instantiateViewControllerWithIdentifier(Utils.UIIds.TestViewControllerID) {

            if let bar = searchBar {
                cancelSearchReposition(bar, animated: false)
            }
            navigationController?.viewControllers = [test]
        }
    }
}

// MARK: UISearchBar implementaton
extension DecksViewController: UISearchBarDelegate {

    func setupSearchBar() {

        let bar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: searchBarHeight))
        bar.searchBarStyle = .Default
        bar.tintColor = UIColor.whiteColor()
        bar.placeholder = "Talie"
        bar.delegate = self
        view.insertSubview(bar, aboveSubview: decksCollectionView)
        searchBar = bar
        
    }
   
    func cancelSearchReposition(searchBar: UISearchBar, animated: Bool) {
        searchDecks = nil
        decksCollectionView.reloadData()

        searchBar.text = nil
        searchBar.resignFirstResponder()
        searchBar.setShowsCancelButton(false, animated: animated)

        if animated {
            UIView.beginAnimations("cancel", context: nil)
            UIView.setAnimationDelay(0)
            UIView.setAnimationDuration(accurateAnimationDuration)
            UIView.setAnimationCurve(.EaseOut)
        }
        
        UIView.animateWithDuration(animated ? accurateAnimationDuration : 0,
            animations: {
                if let navigationBar = self.navigationController?.navigationBar {
                    
                    let top = self.topItemOffset - self.searchBarHeight
                    
                    self.decksCollectionView.contentInset = UIEdgeInsets(top: -top, left: 0, bottom: 0, right: 0)
                    self.decksCollectionView.contentOffset.y = top
                    
                    navigationBar.frame.origin.y = self.statusBarHeight
                    
                    searchBar.frame = CGRect(x: 0, y: -self.topItemOffset, width: searchBar.frame.width, height: self.searchBarHeight)
                }
            }, completion: nil
        )
    }

    func startSearchReposition(searchBar: UISearchBar, animated: Bool) {

        searchBar.setShowsCancelButton(true, animated: true)
        
        UIView.animateWithDuration(animated ? accurateAnimationDuration : 0,
            animations: {
                if let navigationBar = self.navigationController?.navigationBar {
                    
                    navigationBar.frame.origin.y = -navigationBar.frame.height
                    
                    searchBar.frame.origin.y = 0
                    searchBar.frame.size.height = self.searchBarHeight + self.statusBarHeight
                    
                    self.decksCollectionView.contentInset = UIEdgeInsets(top: self.searchBarHeight + self.statusBarHeight + self.marginValue, left: 0, bottom: 0, right: 0)
                }
            },
            completion: nil
        )

    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.characters.count > 0 {
            searchDecks = decksArray?.filter {
                return $0.name.containsString(searchText)
            }.sort { a, b in
                return a.name < b.name
            }
        } else {
            searchDecks = nil
        }

        decksCollectionView.reloadData()
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        cancelSearchReposition(searchBar, animated: true)
    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

        startSearchReposition(searchBar, animated: true)
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        if (scrollView.contentOffset.y < topItemOffset) {

            if (!isSearchBarVisible) {
                isSearchBarVisible = true
                decksCollectionView.contentInset = UIEdgeInsets(top: searchBarHeight - topItemOffset, left: 0, bottom: 0, right: 0)

                UIView.animateWithDuration(softAnimationDuration, delay: 0, options: .CurveEaseOut,
                    animations: {
                        self.searchBar?.frame.origin.y = -self.topItemOffset
