﻿//------------------------------------
// (c) Copyright Totaljobs Group Ltd.
//------------------------------------
/// <reference path="../jquery-1.3.2.js" />
(function ($) {
    $.fn.watermark = function (options) {
        options = $.extend({}, $.fn.watermark.defaults, options);
        return this.each(function () {
            var watermarkText = options.text || this.title;
            // only create watermark if we have some text
            if (watermarkText) {
                var $input = $(this);
                var $context = $("<span/>").css({
                    position: "relative"
                }).addClass(options.cssClass).insertBefore(this);
                var $label = $("<label/>")
                    .text(watermarkText)
                    .attr("for", this.id)
                    .css({
                        position: "absolute",
                        left: 0,
                        top: 0
                    })
                    .hide()
                    .appendTo($context)
                    .data("watermark", true);    // mark this label as a watermark, used by validatorCallout
                $input
                    .focus(function (ev) {
                        $label.hide();
                    })
                    .blur(function (ev) {
                        if (!$(this).val()) {
                            $label.fadeIn("fast");
                        }
                    })
                    // HE: 8/7/2010: IE remembers what was already in the textbox when you come back to the
                    //  page by hitting the back button. That text doesn't get loaded until document.ready() and you
                    //  get two lots of text in the input if you run the watermark logic first.
                    .ready(function (ev) {
                        // set initial state
                        if (!$input.val()) {
                            $label.trigger("position").fadeIn("fast");
                        }
                    });
            }
        });
    };
    $.fn.watermark.defaults = {
        cssClass: "watermark",
        text: ""
    };
    $.watermark = {
        attachAll: function (options) {
            $("input:text[title!=''],input:password[title!=''],textarea[title!='']").watermark(options);
        }
    };
})(jQuery);