/*
 * Jeditable - jQuery in place edit plugin
 *
 * Copyright (c) 2006-2013 Mika Tuupola, Dylan Verheul
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   http://www.appelsiini.net/projects/jeditable
 *
 * Based on editable by Dylan Verheul <dylan_at_dyve.net>:
 *    http://www.dyve.net/jquery/?editable
 *
 */
!function($){$.fn.editable=function(a,b){if("disable"==a)return $(this).data("disabled.editable",!0),void 0;if("enable"==a)return $(this).data("disabled.editable",!1),void 0;if("destroy"==a)return $(this).unbind($(this).data("event.editable")).removeData("disabled.editable").removeData("event.editable"),void 0;var c=$.extend({},$.fn.editable.defaults,{target:a},b),d=$.editable.types[c.type].plugin||function(){},e=$.editable.types[c.type].submit||function(){},f=$.editable.types[c.type].buttons||$.editable.types.defaults.buttons,g=$.editable.types[c.type].content||$.editable.types.defaults.content,h=$.editable.types[c.type].element||$.editable.types.defaults.element,i=$.editable.types[c.type].reset||$.editable.types.defaults.reset,j=c.callback||function(){},k=c.onedit||function(){},l=c.onsubmit||function(){},m=c.onreset||function(){},n=c.onerror||i;return c.tooltip&&$(this).attr("title",c.tooltip),c.autowidth="auto"==c.width,c.autoheight="auto"==c.height,this.each(function(){var a=this,b=$(a).width(),o=$(a).height();$(this).data("event.editable",c.event),$.trim($(this).html())||$(this).html(c.placeholder),$(this).bind(c.event,function(m){if(!0!==$(this).data("disabled.editable")&&!a.editing&&!1!==k.apply(this,[c,a])){m.preventDefault(),m.stopPropagation(),c.tooltip&&$(a).removeAttr("title"),0==$(a).width()?(c.width=b,c.height=o):("none"!=c.width&&(c.width=c.autowidth?$(a).width():c.width),"none"!=c.height&&(c.height=c.autoheight?$(a).height():c.height)),$(this).html().toLowerCase().replace(/(;|"|\/)/g,"")==c.placeholder.toLowerCase().replace(/(;|"|\/)/g,"")&&$(this).html(""),a.editing=!0,a.revert=$(a).html(),$(a).html("");var p=$("<form />");c.cssclass&&("inherit"==c.cssclass?p.attr("class",$(a).attr("class")):p.attr("class",c.cssclass)),c.style&&("inherit"==c.style?(p.attr("style",$(a).attr("style")),p.css("display",$(a).css("display"))):p.attr("style",c.style));var r,q=h.apply(p,[c,a]);if(c.loadurl){var s=setTimeout(function(){q.disabled=!0,g.apply(p,[c.loadtext,c,a])},100),t={};t[c.id]=a.id,$.isFunction(c.loaddata)?$.extend(t,c.loaddata.apply(a,[a.revert,c])):$.extend(t,c.loaddata),$.ajax({type:c.loadtype,url:c.loadurl,data:t,async:!1,success:function(a){window.clearTimeout(s),r=a,q.disabled=!1}})}else c.data?(r=c.data,$.isFunction(c.data)&&(r=c.data.apply(a,[a.revert,c]))):r=a.revert;g.apply(p,[r,c,a]),q.attr("name",c.name),f.apply(p,[c,a]),$(a).append(p),d.apply(p,[c,a]),$(":input:visible:enabled:first",p).focus(),c.select&&q.select(),q.keydown(function(b){27==b.keyCode&&(b.preventDefault(),i.apply(p,[c,a]))});var s;"cancel"==c.onblur?q.blur(function(){s=setTimeout(function(){i.apply(p,[c,a])},500)}):"submit"==c.onblur?q.blur(function(){s=setTimeout(function(){p.submit()},200)}):$.isFunction(c.onblur)?q.blur(function(){c.onblur.apply(a,[q.val(),c])}):q.blur(function(){}),p.submit(function(b){if(s&&clearTimeout(s),b.preventDefault(),!1!==l.apply(p,[c,a])&&!1!==e.apply(p,[c,a]))if($.isFunction(c.target)){var d=c.target.apply(a,[q.val(),c]);$(a).html(d),a.editing=!1,j.apply(a,[a.innerHTML,c]),$.trim($(a).html())||$(a).html(c.placeholder)}else{var f={};f[c.name]=q.val(),f[c.id]=a.id,$.isFunction(c.submitdata)?$.extend(f,c.submitdata.apply(a,[a.revert,c])):$.extend(f,c.submitdata),"PUT"==c.method&&(f._method="put"),$(a).html(c.indicator);var g={type:"POST",data:f,dataType:"html",url:c.target,success:function(b){"html"==g.dataType&&$(a).html(b),a.editing=!1,j.apply(a,[b,c]),$.trim($(a).html())||$(a).html(c.placeholder)},error:function(b){n.apply(p,[c,a,b])}};$.extend(g,c.ajaxoptions),$.ajax(g)}return $(a).attr("title",c.tooltip),!1})}}),this.reset=function(b){this.editing&&!1!==m.apply(b,[c,a])&&($(a).html(a.revert),a.editing=!1,$.trim($(a).html())||$(a).html(c.placeholder),c.tooltip&&$(a).attr("title",c.tooltip))}})},$.editable={types:{defaults:{element:function(){var c=$('<input type="hidden"></input>');return $(this).append(c),c},content:function(a){$(":input:first",this).val(a)},reset:function(a,b){b.reset(this)},buttons:function(a,b){var c=this;if(a.submit){if(a.submit.match(/>$/))var d=$(a.submit).click(function(){"submit"!=d.attr("type")&&c.submit()});else{var d=$('<button type="submit" />');d.html(a.submit)}$(this).append(d)}if(a.cancel){if(a.cancel.match(/>$/))var e=$(a.cancel);else{var e=$('<button type="cancel" />');e.html(a.cancel)}$(this).append(e),$(e).click(function(){if($.isFunction($.editable.types[a.type].reset))var e=$.editable.types[a.type].reset;else var e=$.editable.types.defaults.reset;return e.apply(c,[a,b]),!1})}}},text:{element:function(a){var c=$("<input />");return"none"!=a.width&&c.attr("width",a.width),"none"!=a.height&&c.attr("height",a.height),c.attr("autocomplete","off"),$(this).append(c),c}},textarea:{element:function(a){var c=$("<textarea />");return a.rows?c.attr("rows",a.rows):"none"!=a.height&&c.height(a.height),a.cols?c.attr("cols",a.cols):"none"!=a.width&&c.width(a.width),$(this).append(c),c}},select:{element:function(){var c=$("<select />");return $(this).append(c),c},content:function(data,settings,original){if(String==data.constructor)eval("var json = "+data);else var json=data;for(var key in json)if(json.hasOwnProperty(key)&&"selected"!=key){var option=$("<option />").val(key).append(json[key]);$("select",this).append(option)}if($("select",this).children().each(function(){($(this).val()==json.selected||$(this).text()==$.trim(original.revert))&&$(this).attr("selected","selected")}),!settings.submit){var form=this;$("select",this).change(function(){form.submit()})}}}},addInputType:function(a,b){$.editable.types[a]=b}},$.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);