﻿/*!
*  jQueryExtension.js
* Copyright MAGEX TECHNOLOGIES INC. All Rights Reserved.
*/
(function ($) {

    $.fn.delayedMouseOver = function (callback, options) {
        var options = $.extend({ delay: 1000 }, options);
        return this.each(function () {
            var obj = $(this);
            var timer = false;
            obj.mouseover(function (event) {
                if (timer) clearTimeout(timer);
                timer = setTimeout(function () { callback.apply(obj[0], [event]); }, options.delay);
            });
            obj.mouseout(function (event) {
                if (timer) clearTimeout(timer);
                timer = false;
            });
        });
    };

    $.fn.delayedMouseOut = function (callback, options) {
        var options = $.extend({ delay: 1000 }, options);
        return this.each(function () {
            var obj = $(this);
            var timer = false;
            obj.mouseout(function (event) {
                if (timer) clearTimeout(timer);
                timer = setTimeout(function () { callback.apply(obj[0], [event]); }, options.delay);
            });
            obj.mouseover(function (event) {
                if (timer) clearTimeout(timer);
                timer = false;
            });
        });
    };

    $.fn.equalHeight = function () {
        // Set: Find Height
        var maxHeight = 0;
        this.each(function () {
            var height = $(this).height();
            if (height > maxHeight)
                maxHeight = height;
        });

        // Set: Max Height
        this.each(function () {
            $(this).height(maxHeight);
        });
    };

    $.fn.verticalAlign = function () {
        return this.not("tr, td").each(function (i) {
            var verticalAlign = $(this).css('vertical-align');
            var eh = $(this).outerHeight(true);
            var ph = $(this).parent().height();
            var h = (ph - eh);
            if (verticalAlign == 'middle') {
                $(this).css('margin-top', h / 2);
                $(this).css('margin-bottom', h / 2);
            }
            else if (verticalAlign == 'top') {
                $(this).css('margin-bottom', h);
            }
            else {
                $(this).css('margin-top', h);
            }
        });
    };

    $.fn.minOffset = function (options) {
        var options = $.extend({ top: 0, left: 0 }, options);
        return this.each(function () {
            var offset = $(this).offset();
            if (offset.top < options.top) {
                $(this).offset({ top: options.top });
            }
            if (offset.left < options.left) {
                $(this).offset({ left: options.left });
            }
        });
    };

    $.fn.draggableDialog = function (options) {
        var options = $.extend({ handle: this }, options);
        var draggable;
        var prevpageY;
        var prevPageX;
        $(options.handle).bind('mousedown', { draggable: this }, mousedown_handler);

        function mousedown_handler(event) {
            draggable = event.data.draggable;
            prevpageY = event.pageY;
            prevPageX = event.pageX;
            $(document).bind('mouseup', mouseup_handler);
            $(document).bind('mousemove', mousemove_handler);
            return false;
        };

        function mouseup_handler(event) {
            draggable = null;
            $(document).unbind('mouseup', mouseup_handler);
            $(document).unbind('mousemove', mousemove_handler);
        };

        function mousemove_handler(event) {
            if (draggable) {
                if (prevpageY != event.pageY || prevPageX != event.pageX) {
                    var offset = draggable.offset();
                    // Set: left
                    var left = offset.left + (event.pageX - prevPageX);
                    if (left + draggable.outerWidth() > $(window).width()) {
                        left = $(window).width() - draggable.outerWidth();
                    }
                    if (left < 0) {
                        left = 0;
                    }
                    // Set: top
                    var top = offset.top + (event.pageY - prevpageY);
                    if (top + draggable.outerHeight() > $(window).height()) {
                        top = $(window).height() - draggable.outerHeight();
                    }
                    if (top < 0) {
                        top = 0;
                    }
                    // Set: offset
                    if (left != offset.left || top != offset.top) {
                        draggable.offset({ left: left, top: top });
                    }
                    // Set: Previous position
                    prevpageY = event.pageY;
                    prevPageX = event.pageX;
                }
            }
        };
    };

    $.fn.watermark = function (options) {
        // Set: Default Class
        if (options.className == undefined) {
            var watermarkRequired = $(this).data("watermark-required");
            if (watermarkRequired) {
                options.className = 'watermarkRequired';
            }
            else {
                options.className = 'watermark';
            }
        }
        var options = $.extend({ text: '', className: 'watermark' }, options);
        return this.each(function () {
            // Validate: blur
            $(this).blur(function () {
                if ($(this).val().replace(/\s/g, '').length == 0 || $(this).val() == options.text) {
                    $(this).val(options.text);
                    $(this).addClass(options.className);
                }
            });
            // Validate: focus
            $(this).focus(function () {
                if ($(this).val() == options.text) {
                    $(this).val('');
                    $(this).removeClass(options.className);
                }
            });
            // Init: Watermark
            if ($(this).val().replace(/\s/g, '').length == 0 || $(this).val() == options.text) {
                $(this).val(options.text);
                $(this).addClass(options.className);
            }
        });
    };

    $.fn.watermarkValidate = function (options) {
        var options = $.extend({ cancelEvent: true }, options);
        return this.each(function () {
            // Validate: click
            $(this).click(function () {
                // Validate: Watermark
                var isValid = true;
                $("input, textarea").each(function () {
                    var watermarkRequired = $(this).data("watermark-required");
                    if (watermarkRequired) {
                        var watermarkText = $(this).data("watermark-text");
                        if (watermarkText == undefined) {
                            watermarkText = '';
                        }
                        if ($(this).val().replace(/\s/g, '').length == 0 || $(this).val() == watermarkText) {
                            isValid = false;
                            return false;
                        }
                    }
                });
                // Clear: Watermark Before PostBack
                if (isValid || !options.cancelEvent) {
                    $("input, textarea").each(function () {
                        var watermarkText = $(this).data("watermark-text");
                        var watermarkClass = $(this).data("watermark-class");
                        if (watermarkText || watermarkClass) {
                            if ($(this).val() == watermarkText) {
                                $(this).val('');
                                if (watermarkClass) {
                                    $(this).removeClass(watermarkClass);
                                }
                            }
                        }
                    });
                }
                // Cancel: Event
                if (!isValid && options.cancelEvent) {
                    return false;
                }
            });
        });
    };


})(jQuery);

// Bind: Document Functions
$(document).ready(function () {
    $("input, textarea").each(function (e) {
        var watermarkText = $(this).data("watermark-text");
        var watermarkClass = $(this).data("watermark-class");
        if (watermarkText != undefined || watermarkClass != undefined) {
            $(this).watermark({ text: watermarkText, className: watermarkClass });
        }
    });
    $("input, a").each(function (e) {
        var watermarkValidate = $(this).data("watermark-validate");
        if (watermarkValidate != undefined) {
            $(this).watermarkValidate({ cancelEvent: watermarkValidate.cancelEvent });
        }
    });
});
