(function(a){a.fn.extend({editableReport:function(o){var j;var c={};var f={fields:{ticket:{editable:false},description:{multiline:true}},delay:5000,rpcurl:"/login/xmlrpc",timerurl:"",debug:false};var o=a.extend(f,o);function h(){if(o.debug&&typeof console!="undefined"){console.log.apply("",arguments)}}function d(){function s(v,u){function t(){a.fn.colorbox.close();return false}window.popup_context={cancel:t,setup:function(w){},done:function(w){return t()}};a.fn.colorbox({href:"./../popup/"+u+"/"+v,open:true})}function q(t){s(t,"ticket")}function p(t){s(t,"comment/ticket")}function r(t){document.location="./../ticket/"+t}this.showQuickEditor=q;this.showCommentEditor=p;this.showFullEditor=r}function g(){var p={type:"text",editable:true,options:{}};this.isEditable=function(q){var r=o.fields[q];if(typeof(r)=="undefined"){return false}return r.editable||p.editable};this.getFieldType=function(q){var r=o.fields[q]||p;return r.type||p.type};this.getFieldOptions=function(q){var r=[""];var s=o.fields[q]||p;var t=s.options||p.options;return r.concat(t)};this.setFieldValue=function(u,w,r){r=r||"";var t=o.fields[w];if(typeof(t)!="undefined"){var v=t.options;if(typeof(v)!="undefined"&&typeof(u[t.field_name])!="undefined"){try{var q=parseInt(r,10);if(!isNaN(q)){r=v[q-1];h("selection value for index="+q+" is "+r)}}catch(s){h("failed to convert to number")}}u[t.field_name]=r;return r}};this.getFieldValue=function(r,s){var q=o.fields[s];if(typeof(q)!="undefined"){return r[q.field_name]}}}function e(){var p={};var q={};this.dequeue=function(s){var r=p[s];if(typeof r!="undefined"){h("dequeue-ticket",s);delete p[s];clearTimeout(r);return true}return false};this.enqueue=function(r){h("enqueue-ticket",r);var s=this;delete q[r];this.dequeue(r);p[r]=setTimeout(function(){s.dequeue(r);m(r)},o.delay)};this.postpone=function(r){if(this.dequeue(r)){h("postpone-ticket",r);q[r]=true}};this.requeue=function(r){if(q[r]){h("requeue-ticket",r);delete q[r];this.enqueue(r)}}}function n(p){j=a.rpc(p,"xml",function(q){if(!q||!q.system){alert("Could not get the rpc object ..");return}h("rpc initialized")})}function b(p){if(typeof p!="string"){return p}return p.replace(/^\s+|\s+$/,"")}function m(q){var p=c[q];h("posting ticket content",p);j.ticket.update(function(r){if(typeof(r.result)!="undefined"){h("ticket "+q+" saved")}else{h("saving failed. error string =",r.error.faultString)}},q,"",p)}n(o.rpcurl);var l=new g();var i=new e();var k=new d();return this.each(function(){var p;a("table.tickets tbody tr",this).each(function(){$row=a(this);var t=a("td.ticket a",$row);if(t.length==1){var r=t.text().match(/#\d+/);var q=parseInt(r[0].substr(1),10);h("found-ticket-number",q);var s={};a("td",$row).each(function(){var y=a(this),z=a("a",y);var w=y.html(),u=z.outerHTML();var A=y.attr("class").split(" ");var v=A[0];var B=b(b(y.text()).replace("\n",""));if(l.isEditable(v)){l.setFieldValue(s,v,B);var x=l.getFieldType(v);y.editable(function(D,C){p=y;h("setting-ticket-field ",v," to ",D," for ticket",s);D=l.setFieldValue(s,v,D);i.enqueue(q);if(z.length>0){h("returning html as we have anchor");return w.replace(u,z.text(D).outerHTML())}return D},{data:function(){if(x=="select"){return l.getFieldOptions(v)}return l.getFieldValue(s,v)},placeholder:"&lt;n/a&gt;",type:x,submit:'<a class="s-icon s-icon-inline-save t-inline-edit-button" title="Save" href="#">Save</a>',cancel:'<a class="s-icon s-icon-inline-cancel t-inline-edit-button" title="Cancel" href="#">Cancel</a>',onedit:function(){if(p==y){return false}i.postpone(q)},onreset:function(){i.requeue(q)}})}if(y.is(":last-child")){y.addClass("t-menu");y.html("<span>"+y.html()+'</span>\n<ul class="s-icon-bar" style="display: none;"><li><a title="Quick view/edit" class="s-icon-button s-icon s-icon-edit-quick" href="#">View</a></li><li><a title="Full view/edit" class="s-icon-button s-icon s-icon-edit" href="#">Edit</a></li><li><a title="Comments" class="s-icon-button s-icon s-icon-comments" href="#">Comments</a></li>'+(o.timerurl!=""?'<li><a href="'+o.timerurl+"?title="+encodeURIComponent("#"+s.id+" "+s.summary)+'">Time Track</a></li>':"")+"</ul>");y.find(".s-icon-edit-quick").click(function(){k.showQuickEditor(q)});y.find(".s-icon-edit").click(function(){k.showFullEditor(q)});y.find(".s-icon-comments").click(function(){k.showCommentEditor(q)})}});h("ticket-object",s);c[q]=s;$row.hover(function(){var u=a(this);u.addClass("hover");setTimeout(function(){if(u.hasClass("hover")){var v=u.find(".s-icon-bar");v.fadeIn();v.parent().find("span").fadeOut("fast")}},500)},function(){var u=a(this);u.removeClass("hover");var v=u.find(".s-icon-bar");v.fadeOut("fast");v.parent().find("span").fadeIn("fast")})}});h("all-found-tickets",c)})},outerHTML:function(){return a("<div>").append(this.eq(0).clone()).html()}})})(jQuery);