/**
 * jQuery Placeholder Plugin (
 * http://rozhdestvenskiy.ru/projects/jquery-placeholder/ ) Copyright (c)
 * Roman Rozhdestvenskiy ( sbmaxx@gmail.com )
 */
"use strict";
/*global jQuery, console*/
/*jslint white: true, devel: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true, indent: 4 */

(function ($) {
    $.fn.placeholder = function (settings) {
        var config = {};

        if (settings) {
            $.extend(config, settings);
        }

        function Placeholder($el, config) {
            this.$el = $el;
            this.config = config;
        }

        Placeholder.prototype = {
            init: function () {
                if (! this.isDefaultValueSet()) {
                    this.bind();
                    this.setDefaultValue(this.getValue());
                }
            },
            bind: function () {
                var t = this;
                this.$el.bind('placeholder.reset', function () {
                    t.setValue(t.getDefaultValue());
                });
                this.$el.bind('placeholder.resetDefault', function () {
                    t.setDefaultValue(t.getValue());
                });
            },
            setDefaultValue: function (value) {
                this.$el.data('defaultValue', value);
            },
            getDefaultValue: function () {
                return this.$el.data('defaultValue');
            },
            setValue: function (value) {
                if (this.$el.attr('type') !== 'checkbox') {
                    this.$el.val(value);
                } else {
                    this.$el.attr('checked', value);
                }
            },
            getValue: function () {
                if (this.$el.attr('type') !== 'checkbox') {
                    return this.$el.val();
                } else {
                    return this.$el.attr('checked');
                }
            },
            isDefaultValueSet: function () {
                var val = this.$el.data('defaultValue');
                return typeof(val) !== 'undefined' && val !== null; 
            }
        };

        this.each(function () {
            var placeholder = new Placeholder($(this), config);
            placeholder.init();
        });

        return this;
    };
}(jQuery));
