﻿    jQuery.fn.outerHtml = function(replacement) {
        if (replacement) {
            return this.each(function() { $(this).replaceWith(replacement); });
        }

        var tmp_node = $("<div></div>").append($(this).clone());
        var markup = tmp_node.html(); 
        
        tmp_node.remove();
        return markup;
    };


    jQuery.fn.styleAttr = function(name, newVal) {
        var style = this.attr("style");

        if (style == null && newVal) {
            this.attr("style", (name.toLowerCase() + ":" + newVal + ";"));
        }
        else if (style == null) return "";

        style = style.toLowerCase();

        var styles = style.split(";");

        for (var si = 0; si < styles.length; si++) {
            var keyVal = styles[si].split(":");

            if (jQuery.trim(keyVal[0]) == name) {
                if (newVal) {
                    style = style.replace(styles[si], keyVal[0] + ":" + newVal);
                    this.attr("style", style);
                    return;
                }
                else
                    return keyVal[1];
            }
        }

        if (newVal) {
            this.attr("style", style + ";" + (name.toLowerCase() + ":" + newVal + ";"));
        }

        return "";
    };
    
    jQuery.fn.documentHeight = function () {
        return Math.max(
            $(document).height(),
            $(window).height(),
            /* For opera: */
            document.documentElement.clientHeight
        );
    };

    jQuery.fn.selectedValue = function(newVal) {
        var si = this[0].selectedIndex;

        if (newVal) {
            var op = this.children("[value='" + newVal + "']");

            if (op.length == 1) {
                var idx = this.children().index(op);
                this[0].selectedIndex = idx;
                return true;
            }
        }
        else {
            return this.eq(si).text();
        }

        return false;
    }

    jQuery.fn.defaultValue = function(defaultText) {
        this.each(function() {
            $(this).focus(function() { if (this.value == defaultText) this.value = ""; });
            $(this).blur(function() { if (jQuery.trim(this.value) == "") this.value = defaultText; });
        });

        var jqs = this;
        $("form").submit(function() { jqs.each(function() { if (this.value == defaultText) this.value = ""; }) });
    }
    
    jQuery.fn.disable = function() {
        this.attr("disabled", true);
    }

    jQuery.fn.enable = function() {
        this.attr("disabled", false);
        this.removeAttr("disabled");
    }


    jQuery.fn.onEnterKey = function(toCall) {
        this.keydown(function(e){    
            if (e.keyCode == 13) {
                e.preventDefault();
                toCall();
            }
        });
    }

    $(function() {
        try {
            var __oldDoPostBack = __doPostBack;
            __doPostBack = function(eventTarget, eventArgument) {
                $("form").triggerHandler("submit");
                __oldDoPostBack(eventTarget, eventArgument);
            }
        }
        catch (ex) { }
    })

    jQuery.fn.parseValueAsJSON = function() {
        var obj = ParseJSON(this.val());

        return obj;
    }

    jQuery.fn.textNodes = function() {
    var retval = this.contents().filter(function() { return this.nodeType === 3 }); //equals Node.TEXT_NODE

        return retval;
    }

    
/*
jQuery.fn.outerHTML = function() {
    return $('<div>').append(this.eq(0).clone()).html();
};
*/
/*
(function($) {
    $.fn.outerHTML = function(s) {
        var p = document.createElement('p');
        var c = this.eq(0).clone();
        p.appendChild(c[0]);
        return (s) 
            ? this.before(s).remove() 
            : p.innerHTML;
    }

})(jQuery);
*/