(function($) {



    function in_array(needle, haystack, argStrict) {
        var key = '',
            strict = !! argStrict;
        if (strict) {
            for (key in haystack) {
                if (haystack[key] === needle) {
                    return true;
                }
            }
        } else {
            for (key in haystack) {
                if (haystack[key] == needle) {
                    return true;
                }
            }
        }
        return false;
    }
    var settings = {
        'duration': 3000,
        'animationTime': 400,
        'text': 'Put some fun text',
        'classe': 'mensagem',
        'callback': function() {},
        'version': '0.4'
    },
        methods = {
            init: function() {
                return this.each(function() {
                    var $this = $(this),
                        data = $this.data('Mensagem');
                    if (!data) {
                        $this.data('Mensagem', {
                            'start': false,
                            'text': $this.html(),
                            'classe': $this.attr('class').split(' ')
                        });
                        $this.addClass('mensagem');
                    }
                    $this.hide();
                    $this.bind("click",function (){
                        $(this).stop(true,true);
                        $(this).slideUp(50,function (){
                            var $this = $(this),
                                data= $this.data('Mensagem');
                                data.start = false;
                                settings.callback.call(this);
                        });
                    });
                });
            },
            close: function (){
            	return this.each(function() {
                    var $this = $(this),
                        data = $this.data('Mensagem');
                        data.start = false;
                        $this.slideUp(400);
                        
                   
                });
            },
            showText: function(option) {
                return this.each(function() {
                    var $this = $(this),
                        data = $this.data('Mensagem'),
                        txt = typeof option.text == 'undefined' ? settings.text : option.text,
                        classe = typeof option.classe == 'undefined' ? settings.classe : option.classe;
                    
                    if (!data) {
                        $.error('Not have Mensage init yet!');
                        return false;
                    }
                     $this.removeClass().addClass(classe);
                    if (data.start == true) {
                        return false;
                    } else {
                        data.start = true;
                    }
                    if (typeof option.callback == 'function') {
                        settings.callback = option.callback;
                    }
					
                    $this.
                    html('<p>' + txt + '</p>').
                    slideDown(settings.animationTime,'easeOutQuint').
                    delay(settings.duration).
                    slideUp(settings.animationTime,'easeOutQuint', function() {
                        data.start = false;
                        settings.callback.call(this);
                        $this.removeClass().addClass(data.classe[0]);
                        return;
                    });
                    
                    window.setTimeout(function (){
                    	if( $this.data().start == false ){
                    		$this.Mensagem("close");
                    	}
                    },settings.duration+100);
                    
                    
                });
            },
            destroy: function() {
                return this.each(function() {
                    var $this = $(this),
                        data = $this.data('Mensagem');

                    if (data) {
                        if (data.start == false) {
                            $this.removeData();
                            $this.removeClass('mensagem');
                        }
                    }




                });
            }
        };
    $.fn.Mensagem = function(method) {



        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.Mensagem');
        }



    }

})(jQuery);
