;(function( $ ){
    
    var _wrapper = '<div class="toast-wrapper"></div>';
    
    /**
     * @class Toaster
     *
     * @param element 
     * @param configuration
     */
    function Toaster( element, configuration ) {
    
        this.element = $( element );

        this.configuration = $.extend({
        
            html: '<div/>', // Enables us to create empty toasts for other components to use.
        
            duration: 5000,
        
            sticky: false
        
        }, configuration || {});        
                
        this.init();
        
    }
    
    Toaster.prototype = {
  
        init: function(){
        
          this.element.delegate('.toast-close', 'click', _close);

        },
        
        toast: function( configuration ) {
        
            var toast, config = $.extend({}, 
                
                this.configuration, 
            
                configuration || {});

            toast = new Toast( config );
        
            this.element[ config.sticky ? 'append' : 'prepend' ]( toast.element );
            
            return toast;

        },
        
        configure: function(key, value) {

            typeof key === 'object' ? $.extend(this.configuration, key) : (this.configuration[key] = value);

        }        
    };

    /**
     * @class Toast
     *
     * @param configuration
     */    
    function Toast( configuration ){
    
        this.element = configuration.element || $( configuration.html ).data('toast', this);

        this.configuration = configuration;
        
        this.init();

    }
    
    Toast.prototype = {
      
        init: function(){
            
            var cssClass = 'toast', config = this.configuration;
        
            if (!config.sticky)
            {                    
                this.timer = setTimeout( $.proxy(_timeout, this), config.duration );          
            }
            
            if (config.cssClass)
            {
                cssClass = 'toast ' + config.cssClass;
            }
            
            this.element.attr('class', cssClass);
        
        },

        close: function(){
        
          this.timer && clearTimeout(this.timer);
		  
          this.element.remove();
        
        }

    };

    function _close(){
        
		$(this).closest('.toast').data('toast').close();

        return false;
        
    }

    function _timeout(){
    
        var parent = this.element.wrap(_wrapper).parent();
        
            parent.queue('animation', [
        
                function( next ){ parent.animate({ opacity:0 }, 500, next); },
                
                function( next ){ parent.animate({ height: 0 }, 750, next); },
                
                function( next ){ parent.remove(); }
            
            ]);
            
            parent.dequeue('animation');
    };

    $.fn.toaster = function( configuration ){
        
        if (!this.length){ return; }
        
        if (this.data('toaster'))
        {            
            $.error('A Toaster instance has already been attached to an element in this selection.');
        }

        return this.each(function(){

            $(this).data('toaster', new Toaster( this, configuration ));

        });

    };

    /* NOTE: Toast object needs to be publicly available because the view for a toast may be rendered on the server instead of the client; When this happens
     * a new Toast object must be instatiated and attached to the DOM element representing the toast.
     */
    Chemistry.include({ Toast:Toast });

})( jQuery );

if(typeof logc !== 'function'){
	function logc(inp){
		if (window.console){
			console.log(inp);
		}
		return inp;
	}
}

