﻿/*!
* jQuery textyle plugin: placing textbox labels INTO textbox
* version 0.1 (28/02/2011)
* Requires jQuery v1.4 or later
* Authors: Geert Van Geendertaelen
*/


(function ($) {

    // default settings
    var settings = {
        'defaultClass': 'boxtext',
        'unprocessedClass': 'unprocessed',
        'clearOnValue': 'false'
    };

    // assigned methods
    var methods = {

        init: function (options) {

            return this.each(function () {

                if (options) {
                    $.extend(settings, options);
                }

                var $this = $(this);
                var $form = $(this.form);
                var $win = $(window);
                var title = $this.attr('title');
                var initialValue = $this.val();
                var hasDefaultClass = $this.hasClass(settings['defaultClass']);

                if ($this.attr('title')) {

                    //$this.bind('focus.textyle', methods.focus);
                    //$this.bind('blur.textyle', methods.blur);

                    if (!hasDefaultClass) { $this.attr('class', settings.defaultClass); }

                    if (!initialValue || settings['clearOnValue'] === 'true') {
                        Initialize();
                    }

                    $this.focus(function () {
                        Process();
                    });

                    $this.blur(function () {
                        if (!$this.val()) {
                            Initialize();
                        }
                    });

                }




                function Initialize() {
                    $this.val(title);
                    $this.addClass(settings['unprocessedClass']);
                }

                function Process() {
                    if ($this.hasClass(settings['unprocessedClass'])) {
                        $this.removeClass(settings['unprocessedClass']);
                        $this.val('');
                    }
                }

                $form.submit(Process);
                $win.unload(Process); // handles Firefox's autocomplete

            });

        },

        reset: function (options) {

            return this.each(function () {

                if (options) {
                    $.extend(settings, options);
                }

                var $this = $(this);
                var title = $this.attr('title');
                var initialValue = $this.val();

                if ($this.hasClass(settings['unprocessedClass'])) {
                    $this.removeClass(settings['unprocessedClass']);
                    $this.val('');
                }
                if (!initialValue || settings['clearOnValue'] === 'true') {
                    $this.val(title);
                    $this.addClass(settings['unprocessedClass']);
                }
            });
        },

        destroy: function () {

            return this.each(function () {
                var $this = $(this);

                if ($this.hasClass(settings['unprocessedClass'])) {
                    $this.removeClass(settings['unprocessedClass']);
                    $this.val('');
                }

                $(this).unbind('.textyle');
            });

        }

    };

    // method invocation function
    $.fn.textyle = 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.textyle');
        }

    };

})(jQuery);
