(function(a){a.fn.editable=function(c,e){if("disable"==c)a(this).data("disabled.editable",!0);else if("enable"==c)a(this).data("disabled.editable",!1);else if("destroy"==c)a(this).unbind(a(this).data("event.editable")).removeData("disabled.editable").removeData("event.editable");else{var b=a.extend({},a.fn.editable.defaults,{target:c},e),h=a.editable.types[b.type].plugin||function(){},j=a.editable.types[b.type].submit||function(){},r=a.editable.types[b.type].buttons||a.editable.types.defaults.buttons,
p=a.editable.types[b.type].content||a.editable.types.defaults.content,s=a.editable.types[b.type].element||a.editable.types.defaults.element,n=a.editable.types[b.type].reset||a.editable.types.defaults.reset,q=b.callback||function(){},t=b.onedit||function(){},u=b.onsubmit||function(){},v=b.onreset||function(){},w=b.onerror||n;b.tooltip&&a(this).attr("title",b.tooltip);b.autowidth="auto"==b.width;b.autoheight="auto"==b.height;return this.each(function(){var d=this,c=a(d).width(),e=a(d).height();a(this).data("event.editable",
b.event);a.trim(a(this).html())||a(this).html(b.placeholder);a(this).bind(b.event,function(k){if(!0!==a(this).data("disabled.editable")&&!d.editing&&!1!==t.apply(this,[b,d])){k.preventDefault();k.stopPropagation();b.tooltip&&a(d).removeAttr("title");0==a(d).width()?(b.width=c,b.height=e):("none"!=b.width&&(b.width=b.autowidth?a(d).width():b.width),"none"!=b.height&&(b.height=b.autoheight?a(d).height():b.height));a(this).html().toLowerCase().replace(/(;|")/g,"")==b.placeholder.toLowerCase().replace(/(;|")/g,
"")&&a(this).html("");d.editing=!0;d.revert=a(d).html();a(d).html("");var f=a("<form />");b.cssclass&&("inherit"==b.cssclass?f.attr("class",a(d).attr("class")):f.attr("class",b.cssclass));b.style&&("inherit"==b.style?(f.attr("style",a(d).attr("style")),f.css("display",a(d).css("display"))):f.attr("style",b.style));var g=s.apply(f,[b,d]),l;if(b.loadurl){var m=setTimeout(function(){g.disabled=!0;p.apply(f,[b.loadtext,b,d])},100);k={};k[b.id]=d.id;a.isFunction(b.loaddata)?a.extend(k,b.loaddata.apply(d,
[d.revert,b])):a.extend(k,b.loaddata);a.ajax({type:b.loadtype,url:b.loadurl,data:k,async:!1,success:function(a){window.clearTimeout(m);l=a;g.disabled=!1}})}else b.data?(l=b.data,a.isFunction(b.data)&&(l=b.data.apply(d,[d.revert,b]))):l=d.revert;p.apply(f,[l,b,d]);g.attr("name",b.name);r.apply(f,[b,d]);a(d).append(f);h.apply(f,[b,d]);a(":input:visible:enabled:first",f).focus();b.select&&g.select();g.keydown(function(a){27==a.keyCode&&(a.preventDefault(),n.apply(f,[b,d]))});"cancel"==b.onblur?g.blur(function(){m=
setTimeout(function(){n.apply(f,[b,d])},500)}):"submit"==b.onblur?g.blur(function(){m=setTimeout(function(){f.submit()},100)}):a.isFunction(b.onblur)?g.blur(function(){b.onblur.apply(d,[g.val(),b])}):g.blur(function(){});f.submit(function(c){m&&clearTimeout(m);c.preventDefault();if(!1!==u.apply(f,[b,d])&&!1!==j.apply(f,[b,d]))if(a.isFunction(b.target))c=b.target.apply(d,[g.val(),b]),a(d).html(c),d.editing=!1,q.apply(d,[d.innerHTML,b]),a.trim(a(d).html())||a(d).html(b.placeholder);else{c={};c[b.name]=
g.val();c[b.id]=d.id;a.isFunction(b.submitdata)?a.extend(c,b.submitdata.apply(d,[d.revert,b])):a.extend(c,b.submitdata);"PUT"==b.method&&(c._method="put");a(d).html(b.indicator);var e={type:"POST",data:c,dataType:"html",url:b.target,success:function(c){"html"==e.dataType&&a(d).html(c);d.editing=!1;q.apply(d,[c,b]);a.trim(a(d).html())||a(d).html(b.placeholder)},error:function(a){w.apply(f,[b,d,a])}};a.extend(e,b.ajaxoptions);a.ajax(e)}a(d).attr("title",b.tooltip);return!1})}});this.reset=function(c){this.editing&&
!1!==v.apply(c,[b,d])&&(a(d).html(d.revert),d.editing=!1,a.trim(a(d).html())||a(d).html(b.placeholder),b.tooltip&&a(d).attr("title",b.tooltip))}})}};a.editable={types:{defaults:{element:function(){var c=a('<input type="hidden"></input>');a(this).append(c);return c},content:function(c){a(":input:first",this).val(c)},reset:function(a,e){e.reset(this)},buttons:function(c,e){var b=this;if(c.submit){if(c.submit.match(/>$/))var h=a(c.submit).click(function(){"submit"!=h.attr("type")&&b.submit()});else h=
a('<button type="submit" />'),h.html(c.submit);a(this).append(h)}if(c.cancel){if(c.cancel.match(/>$/))var j=a(c.cancel);else j=a('<button type="cancel" />'),j.html(c.cancel);a(this).append(j);a(j).click(function(){(a.isFunction(a.editable.types[c.type].reset)?a.editable.types[c.type].reset:a.editable.types.defaults.reset).apply(b,[c,e]);return!1})}}},text:{element:function(c){var e=a("<input />");"none"!=c.width&&e.width(c.width);"none"!=c.height&&e.height(c.height);e.attr("autocomplete","off");a(this).append(e);
return e}},textarea:{element:function(c){var e=a("<textarea />");c.rows?e.attr("rows",c.rows):"none"!=c.height&&e.height(c.height);c.cols?e.attr("cols",c.cols):"none"!=c.width&&e.width(c.width);a(this).append(e);return e}},select:{element:function(){var c=a("<select />");a(this).append(c);return c},content:function(c,e,b){if(String==c.constructor)eval("var json = "+c);else var h=c;for(var j in h)h.hasOwnProperty(j)&&"selected"!=j&&(c=a("<option />").val(j).append(h[j]),a("select",this).append(c));
a("select",this).children().each(function(){(a(this).val()==h.selected||a(this).text()==a.trim(b.revert))&&a(this).attr("selected","selected")})}}},addInputType:function(c,e){a.editable.types[c]=e}};a.fn.editable.defaults={name:"value",id:"id",type:"text",width:"auto",height:"auto",event:"click.editable",onblur:"cancel",loadtype:"GET",loadtext:"Loading...",placeholder:"Click to edit",loaddata:{},submitdata:{},ajaxoptions:{}}})(jQuery);
