﻿(function ($) {
    $.fn.makeSearchBox = function (defaultText, postBackControlId) {
        return this.each(function () {

            // init
            var me = $(this);
            me.val(defaultText);
            me.addClass('searchboxempty');

            // functions
            me.focus(function () {
                if (me.val() == defaultText) {
                    me.removeClass('searchboxempty');
                    me.addClass('searchbox');
                    me.val('');
                }
            });

            me.focusout(function () {
                if (me.val().length == 0) {
                    me.removeClass('searchbox');
                    me.addClass('searchboxempty');
                    me.val(defaultText);
                }
            });

            me.keydown(function (event) {
                if (event.keyCode == 13) {                      // [Ret]
                    if (postBackControlId.length > 1 && me.val().length > 0)
                        __doPostBack(postBackControlId, '');    // Performs a postback
                }

                if (event.keyCode == 27) {                      // [Esc]
                    me.val('');
                }
            });

        });
    };

})(jQuery);
