﻿(function($) {

    $.placeholder = function($element, options) {
        var config = this.settings = $.extend({}, this.defaults(), options);
        this.$element = $element;
        this.setup();
    };

    var $placeholder = $.placeholder;

    $placeholder.fn = $placeholder.prototype = {
        placeholder: '1.0.0'
    };

    $placeholder.fn.extend = $placeholder.extend = $.extend;

    $placeholder.fn.extend({
        defaults: function() {
            var variable = null;
            return {
                1: 1,
                2: 2
            }
        },
        setup: function() {
            var $this = this.$element,
				$parent,
				$placeholder = $("<div>");
            $placeholder.addClass("placeholder").text($this.data("placeholder"));
            //$this.addClass("placeholder")
            $this.wrap("<div>");
            $parent = $this.parent();
            $parent.addClass("placeholder-parent");
            $parent.prepend($placeholder);

            if ($.trim($this.val()).length > 0) {
                $placeholder.addClass("nonempty");
            }

            $placeholder.bind("click", function(event) {
                $this.focus();
            });

            $this.bind("focus", function(event) {
                $placeholder.addClass("focused").removeClass("nonempty");
            });

            $this.bind("blur", function(event) {
                $placeholder.removeClass("focused");
            });

            $this.bind("blur keyup change", function(event) {
                if ($.trim($this.val()).length > 0) {
                    $placeholder.addClass("nonempty");
                } else {
                    $placeholder.removeClass("nonempty");
                }
            });
        }
    });

    $.fn.placeholder = function(settings, argument1) {
        var returnObject, $this = $(this);

        if (typeof settings === "string") {
            for (var i = 0; i < $this.length; i++) {
                var $this = $this.eq(i);
                var placeholder = $this.data("place-holder");
                if (placeholder) {
                    var property = placeholder[settings];
                    if ($.isFunction(property)) {
                        returnObject = property.apply(placeholder, [argument1]);
                    }
                    else if (typeof argument1 == "undefined") {
                        return property;
                    }
                    else {
                        return property = argument1;
                    }
                }
            }
            return this;
        }
        else {
            return $this.filter("input[type=text], input[type=password], textarea").each(function(i, e) {
                var $this = $(this);
                if (typeof $this.data("placeholder") === "string") {
                    $this.data("place-holder", new $placeholder($this, settings));
                }
            });
        }
    };

})(jQuery);

(function($) {
    $(document).ready(function(event) {
        $(":input").placeholder();
        setTimeout(function() {
            $(":input").each(function(index, element) {
                var $this = $(this);
                if (typeof $this.data("placeholder") === "string") {
                    if ($.trim($this.val()).length > 0) {
                        $this.parent().find("div.placeholder").addClass("nonempty");
                    }
                }
            });
        }, 1000);
    });
})(jQuery);
