(function ($) {

    /**
    @class searchBox
    @namespace $
    **/
    $.fn.searchBox = function (options) {

        var 

        PRIVATE = {

            //---------------------------
            //--- CONSTRUCTOR
            //---------------------------

            init: function (options) {

                PRIVATE.options = $.extend({
                    submitWhenEmpty: false
                }, options);

                PRIVATE.get_input().attr("maxlength", "50");
                PRIVATE.get_input().wrap("<span class=\"app-searchbox\"></span>");
                PRIVATE.get_input().after("<button class=\"search\"><div class=\"search-icon\"></div></button>");
                PRIVATE.get_input().watermark({
                    text: options.watermark ? options.watermark : "Search..."
                });

                PRIVATE.get_input().on("keypress", $.proxy(PRIVATE.input_onKeyPress, PRIVATE));
                PRIVATE.get_button().on("click", $.proxy(PRIVATE.button_onClick, PRIVATE));

                if (PRIVATE.options.wrapperCss !== undefined) {
                    PRIVATE.get_wrapper().css(PRIVATE.options.wrapperCss);
                }

            },

            //---------------------------
            //--- PROPERTIES
            //---------------------------

            get_wrapper: function () {
                return PRIVATE.get_input().closest(".app-searchbox");
            },

            get_input: function () {
                return PRIVATE.options.input;
            },

            get_button: function () {
                return PRIVATE.get_wrapper().find("BUTTON");
            },

            get_value: function () {
                return PRIVATE.get_input().data("watermark").value();
            },

            get_watermark: function () {
                return PRIVATE.get_input().data("watermark");
            },

            //---------------------------
            //--- EVENT HANDLERS
            //---------------------------

            input_onKeyPress: function (e) {

                var active = PRIVATE.get_button().hasClass("cancel"),
                    value = PRIVATE.get_value();

                if (e.keyCode === 13) {

                    if ((value !== "" || PRIVATE.options.submitWhenEmpty) && !active) {

                        PRIVATE.toggle("cancel");

                        PRIVATE.fireEvent("search", {
                            term: PRIVATE.get_value()
                        });

                    }
                    else if (active && value !== "") {

                        PRIVATE.toggle("cancel");

                        PRIVATE.fireEvent("search", {
                            term: PRIVATE.get_value()
                        });

                    }
                    else if (active && value === "") {

                        PRIVATE.toggle("search");

                        PRIVATE.fireEvent("search", {
                            term: PRIVATE.get_value()
                        });

                    }

                    e.preventDefault();

                }

            },

            button_onClick: function (e) {

                if (PRIVATE.get_button().hasClass("cancel")) {

                    PRIVATE.get_watermark().reset();
                    PRIVATE.toggle("search");

                    PRIVATE.fireEvent("search", {
                        term: PRIVATE.get_value()
                    });

                }
                else {

                    if (PRIVATE.get_value() !== "") {

                        PRIVATE.toggle("cancel");

                        PRIVATE.fireEvent("search", {
                            term: PRIVATE.get_value()
                        });

                    }

                }

            },

            //---------------------------
            //--- METHODS
            //---------------------------

            fireEvent: function (eventName, args) {

                if (PRIVATE.options.hasOwnProperty(eventName)) {
                    PRIVATE.options[eventName](PRIVATE.options.input, args);
                }

            },

            toggle: function (action) {

                switch (action) {

                    case "search":

                        PRIVATE.get_button().addClass("search");
                        PRIVATE.get_button().removeClass("cancel");
                        break;

                    case "cancel":

                        PRIVATE.get_button().addClass("cancel");
                        PRIVATE.get_button().removeClass("search");
                        break;

                }

            }

        },

        PUBIC = {
            value: PRIVATE.get_value
        };

        PRIVATE.init($.extend({
            input: this
        }, options));

        this.data("searchBox", PUBIC);

    };

})(jQuery);