﻿jQuery.fn.contenteditable = function(options) {

    //Set the default values, use comma to separate the settings, example:
    var defaults = {
        postbackurl: 'asfasfd.ashx'
    }

    var options = $.extend(defaults, options);

    //public properties
    this.disable = function() {
        alert(this.length);
        this.each(function() {
            var $this = $(this);
            $this.removeAttr('contenteditable');
        });
    };

    this.save = function() {
        this.each(function() {
            var $this = $(this);
            if ($this.metadata().isdirty) {
                var position = $this.position();
                var savediv = $('body').prepend('<div>Saving</div>').find("div:first").css({ 'position': 'absolute', 'z-index': '2000', 'left': position.left, 'top': position.top, 'background-color': 'red' });
				$.post(options.postbackurl, { propertyname: $this.metadata().propname, path:$this.metadata().path, content: $this.html() },function(data){$(savediv).remove();} );                
            }
            $this.metadata().isdirty = false;

            //alert('saving ' + $this.metadata().propname + ': ' + $this.html() + ' to path: ' + $this.metadata().path);
            $this.removeAttr('contenteditable');
        });

    };

    // iterate and reformat each matched element
    return this.each(function() {
        var $this = $(this);
        $this.attr('contenteditable', 'true');
        $this.metadata().isdirty = false;
        $this.blur(function() {
            $this.metadata().isdirty = true;
        });
        //alert($this.metadata().path);
    });

};