﻿(function ($) {

    // Textbox Watermarks
    // Master Watermark function, that applies both focus and blur events
    $.fn.applyWatermark = function (watermarkText, options) {

        // Providing parameters if no values have been passed
        if (watermarkText === undefined)
            watermarkText = "";

        // Options:

        // watermarkClass: Css class to apply when the watermark is active

        // bindFocusEvent: allows you to determine whether the focus event should be added (Default: True)
        // alwaysClearOnFocus: will always clear the text/content on focus (Default: False)

        // bindBlurEvent: allows you to determine whether the blur event should be added (Default: True)
        // alwaysClearOnBlur: will always clear the text/content when the element loses focus (Default: False)

        if (options === undefined || options === null)
            options = {};

        options.watermarkClass = options.watermarkClass === undefined ? "" : options.watermarkClass;

        options.bindFocusEvent = options.bindFocusEvent === undefined ? true : options.bindFocusEvent;
        options.alwaysClearOnFocus = options.alwaysClearOnFocus === undefined ? false : options.alwaysClearOnFocus;

        options.bindBlurEvent = options.bindBlurEvent === undefined ? true : options.bindBlurEvent;
        options.alwaysClearOnBlur = options.alwaysClearOnBlur === undefined ? false : options.alwaysClearOnBlur;

        // If you've provided watermark text, add it to the text box
        if (watermarkText != "")
            $(this).val(watermarkText);

        if (options.watermarkClass != "")
            $(this).addClass(options.watermarkClass);

        // Applies events to the textbox
        if (options.bindFocusEvent)
            $(this).bindWatermarkFocusEvent(options.watermarkClass, watermarkText, options.alwaysClearOnFocus);

        if (options.bindBlurEvent)
            $(this).bindWatermarkBlurEvent(options.watermarkClass, watermarkText, options.alwaysClearOnFocus);
    }

    // Function that adds Focus event to handle the watermark
    $.fn.bindWatermarkFocusEvent = function (watermarkClass, watermarkText, alwaysClearOnFocus) {

        $(this).off("focus");

        // Define what happens when the textbox comes under focus
        // Remove the watermark class and clear the box
        $(this).on("focus", function () {

            $(this).filter(function () {

                // We only want this to apply if there's not
                // something actually entered
                return $(this).val() == "" || $(this).val() == watermarkText || alwaysClearOnFocus

            }).removeClass(watermarkClass).val("");

        });
    }

    // Function that adds Blur event to handle the watermark
    $.fn.bindWatermarkBlurEvent = function (watermarkClass, watermarkText, alwaysClearOnBlur) {

        $(this).off("blur");

        // Define what happens when the textbox loses focus
        // Add the watermark class and default text
        $(this).on("blur", function () {

            $(this).filter(function () {

                // We only want this to apply if there's not
                // something actually entered
                return $(this).val() == "" || alwaysClearOnBlur

            }).addClass(watermarkClass).val(watermarkText);

        });
    }

    //Detect Left Main Container Height.
    $.fn.detectLeftPanelHeight = function (offset) {
        var value = $("#leftMainContainer").height() - offset;

        return value;
    }

})(jQuery);

