 //  Wishlist
 //
 //  Created by Christian Deckert on 15.11.16.
-//  Copyright © 2016 Christian Deckert
+//  Copyright © 2016 x-root Software GmbH. All rights reserved.
 //
 
 import Foundation
 import UIKit
 
-class CDAnimatedBarButtonItem: UIBarButtonItem {
+public struct CDAnimatedBarButtonItemOptions {
+    var rippleColor: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.10)
+    var contentMode: UIViewContentMode = .ScaleAspectFit
+    var font: UIFont?
+    var animationDuration: CGFloat = 0.4
+    var animationScaleFactor: CGFloat = 0.65
+    var textColor: UIColor?
+    var frame: CGRect?
+    var title: String?
+    var icon: UIImage?
+    var callback: ((animatedBarButtonItem: CDAnimatedBarButtonItem?) -> Void)? = nil
+    
+    static var defaults: CDAnimatedBarButtonItemOptions {
+        return CDAnimatedBarButtonItemOptions()
+    }
+}
+
+public class CDAnimatedBarButtonItem: UIBarButtonItem {
+    
+    class NotifyingButton: UIButton {
+        
+        var onDidMoveToSuperView: ((notifyingButton: NotifyingButton, superview: UIView?) -> Void)?
+        
+        override func didMoveToSuperview() {
+            super.didMoveToSuperview()
+            
+            if let superview = self.superview {
+                onDidMoveToSuperView?(notifyingButton: self, superview: superview)
+            } else {
+                onDidMoveToSuperView?(notifyingButton: self, superview: nil)
+                onDidMoveToSuperView = nil
+            }
+        }
+        
+        override func alignmentRectInsets() -> UIEdgeInsets {
+            return UIEdgeInsetsZero
+        }
+        
+    }
+    
+    public var options: CDAnimatedBarButtonItemOptions = CDAnimatedBarButtonItemOptions.defaults {
+        
+        didSet {
+            updateUI()
+        }
+    }
     
     public var onTapCallback: ((animatedBarButtonItem: CDAnimatedBarButtonItem?) -> Void)?
-    public var rippleColor: UIColor = UIColor.blackColor()
-    public var font: UIFont?
-    public var contentMode: UIViewContentMode?
+    public var onDidMoveToSuperViewCallback: ((animatedBarButtonItem: CDAnimatedBarButtonItem) -> Void)?
+    
+    @IBInspectable public var font: UIFont? {
+        set {
+            self.options.font = newValue
+            updateUI()
+        }
+        
+        get {
+            return self.options.font
+        }
+    }
+    
+    @IBInspectable public var textColor: UIColor? {
+        set {
+            self.options.textColor = newValue
+            updateUI()
+        }
+        
+        get {
+            return self.options.textColor
+        }
+    }
+    
+    @IBInspectable public var rippleColor: UIColor? {
+        set {
+            self.options.rippleColor = newValue ?? CDAnimatedBarButtonItemOptions.defaults.rippleColor
+            updateUI()
+        }
+        
+        get {
+            return self.options.rippleColor
+        }
+    }
+    
+    public var contentMode: UIViewContentMode? {
+        set {
+            self.options.contentMode = newValue ?? CDAnimatedBarButtonItemOptions.defaults.contentMode
+            updateUI()
+        }
+        
+        get {
+            return self.options.contentMode
+        }
+    }
     
     @IBInspectable public var frame: CGRect? {
-        didSet {
-            updateUI()
+        set {
+            self.options.frame = newValue
+            updateUI()
+        }
+        
+        get {
+            return self.options.frame
         }
     }
     
     @IBInspectable public var icon: UIImage? {
-        didSet {
-            updateUI()
+        set {
+            self.options.icon = newValue
+            updateUI()
+        }
+        
+        get {
+            return self.options.icon
         }
     }
     
     @IBInspectable public var text: String? {
-        didSet {
-            updateUI()
+        set {
+            self.options.title = newValue
+            updateUI()
+        }
+        
+        get {
+            return self.options.title
         }
     }
     
     public override var title: String? {
         get {
-            return self.text
-        }
-        
-        set {
-            self.text = newValue
-            updateUI()
-        }
-    }
+            return self.options.title
+        }
+        
+        set {
+            self.options.title = newValue
+            updateUI()
+        }
+    }
+    
+    private weak var label: UILabel?
+    private weak var imageView: UIImageView?
     
     public required init?(coder aDecoder: NSCoder) {
         super.init(coder: aDecoder)
-        commonInit()
+        
     }
     
     public override init() {

        self.icon = icon
        commonInit()
        
    }
    
    public func commonInit() {

            return
        }
        
        let halfDuration = Double(self.options.animationDuration/2)
        UIView.animateWithDuration(halfDuration, animations: {
            subview.transform = CGAffineTransformMakeScale(self.options.animationScaleFactor, self.options.animationScaleFactor)
            button.backgroundColor = self.options.rippleColor
        }) { complete in
            
            self.onTapCallback?(animatedBarButtonItem: self)
            
            UIView.animateWithDuration(halfDuration, animations: {
                button.backgroundColor = UIColor.clearColor()
                subview.transform = CGAffineTransformIdentity
            }) { complete in
