 import UIKit
 
 enum CKTextFieldValidationResult: Int {
-    case CKTextFieldValidationUnknown = 0, CKTextFieldValidationPassed, CKTextFieldValidationFailed
+    case Unknown = 0, Passed, Failed
 }
 
-@objc protocol CKTextFieldValidationDelegate: NSObjectProtocol {
-    optional func textField(textField: CKTextField, validationResult:Int, forText:String)
+protocol CKTextFieldValidationDelegate: NSObjectProtocol {
+    func textField(textField: CKTextField, validationResult:CKTextFieldValidationResult, forText:String)
 }
 
+/**
+ * TODO override didSet for textAlignment -> create leftView on the fly
+ * TODO override didSet for placeholder -> create placeholderLabel on the fly
+ */
 class CKTextField: UITextField, UITextFieldDelegate {
 
     // IB fields for attribute inspector

            }
            
            self.placeholderHideInProgress = false

        }
    }
    
    override func layoutSubviews() {

            }
        }
    }

    override var text: String! {
        get {
            return super.text
        }
        set(newText) {
            if (self.performValidationOnInput(newText)) {
                super.text = newText
                self.sendActionsForControlEvents(UIControlEvents.EditingChanged)
                
                if let placeholderLabel = self.placeholderLabel {
                    if (count(self.text) == 0 && placeholderLabel.hidden) {
                        if (self.originalTextAlignment == NSTextAlignment.Center) {
                            self.leftView?.frame = CGRectMake(0.0, 0.0, self.bounds.width / 2 - 8, self.bounds.height)
                            self.textAlignment = NSTextAlignment.Left
                        }
                        placeholderLabel.alpha = 0.0
                        placeholderLabel.hidden = false
                        UIView.animateWithDuration(0.3, animations: { () -> Void in
                            placeholderLabel.alpha = 1.0
                        })
                    } else if (count(self.text) > 0 && !placeholderLabel.hidden) {
                        if (self.originalTextAlignment == NSTextAlignment.Center) {
                            self.leftView?.frame = CGRectMake(0.0, 0.0, 0.0, self.bounds.height)
                            self.textAlignment = NSTextAlignment.Center
                        }
                        
                        if (!self.placeholderHideInProgress) {
                            self.placeholderHideInProgress = true
                            UIView.animateWithDuration(0.3, animations: { () -> Void in
                                placeholderLabel.alpha = 0.0
                                placeholderLabel.transform = CGAffineTransformMakeScale(1.1, 1.1)
                            }, completion: { (finished) -> Void in
                                placeholderLabel.hidden = true
                                placeholderLabel.transform = CGAffineTransformIdentity
                                self.placeholderHideInProgress = false
                            })
                        }
                    }
                }
            }
        }
    }
    
    func performValidationOnInput(text: String) -> Bool {
        if let type = self.validationType {
            if (type == CKTextField.VALIDATION_TYPE_TEXT) {
                let minLength: Int
                let maxLength: Int
                if let pattern = self.pattern {
                    minLength = count(pattern)
                    maxLength = count(pattern)
                } else {
                    if let min = self.minLength {
                        minLength = min.toInt()!
                    } else {
                        minLength = 0
                    }
                    if let max = self.maxLength {
                        maxLength = max.toInt()!
                    } else {
                        maxLength = Int.max
                    }
                }
                
                if (count(text) < minLength) {
                    if let delegate = self.validationDelegate {
                        delegate.textField(self, validationResult: CKTextFieldValidationResult.Unknown, forText: text)
                    }
                    return true
                }
                
                if (count(text) > maxLength) {
                    if let delegate = self.validationDelegate {
                        delegate.textField(self, validationResult: CKTextFieldValidationResult.Failed, forText: text)
                    }
                    return false
                }
                
                if let delegate = self.validationDelegate {
                    delegate.textField(self, validationResult: CKTextFieldValidationResult.Passed, forText: text)
                }
                return true
            }
        }
        return true
    }
    
    // MARK: visual effect methods
    
    func shake() {
        self.layer.transform = CATransform3DMakeTranslation(10.0, 0.0, 0.0)
