//-- copyright
// Copyright (C)2013-2014 the OpenProject Foundation (OPF)
// Copyright (C)2011 Stephan Eckardt, Tim Felgentreff, Marnen Laibow-Koser, Sandro Munda
// Copyright (C)2010-2011 friflaj
// Copyright (C)2010 Maxime Guilbot, Andrew Vit, Joakim Kolsjö, ibussieres, Daniel Passos, Jason Vasquez, jpic, Emiliano Heyns
// Copyright (C)2009-2010 Mark Maglana
// Copyright (C)2009 Joe Heck, Nate Lowrie
// The copyright follows:
// Copyright (C) 2010-2011 - Emiliano Heyns, Mark Maglana, friflaj
// Copyright (C) 2011 - Jens Ulferts, Gregor Schmidt - Finn GmbH - Berlin, Germany
// See doc/COPYRIGHT.rdoc for more details.
RB.Model=function(e){return RB.Object.create({initialize:function(t){this.$=e(t),this.el=t},afterCreate:function(){},afterSave:function(e,t,i){var n,s;n=this.isNew(),s=RB.Factory.initialize(RB.Model,e),this.unmarkSaving(),this.refresh(s),n?(this.$.attr("id",s.$.attr("id")),this.afterCreate(e,t,i)):this.afterUpdate(e,t,i)},afterUpdate:function(){},beforeSave:function(){},cancelEdit:function(){this.endEdit(),this.isNew()&&this.$.hide("blind")},close:function(){this.$.addClass("closed")},copyFromDialog:function(){var t;t=0===this.$.find(".editors").length?e("<div class='editors'></div>").appendTo(this.$):this.$.find(".editors").first(),t.html(""),t.append(e("#"+this.getType().toLowerCase()+"_editor").children(".editor")),this.saveEdits()},displayEditor:function(t){var i=this.$.offset(),n=this;t.dialog({buttons:{OK:function(){n.copyFromDialog(),e(this).dialog("close")},Cancel:function(){n.cancelEdit(),e(this).dialog("close")}},close:function(e){27===e.which&&n.cancelEdit()},dialogClass:this.getType().toLowerCase()+"_editor_dialog",modal:!0,position:[i.left-e(document).scrollLeft(),i.top-e(document).scrollTop()],resizable:!1,title:this.isNew()?this.newDialogTitle():this.editDialogTitle()}),t.find(".editor").first().focus()},edit:function(){var t=this.getEditor(),i=this,n=0;return e(".stories .editors .editor").each(function(){var t;t=parseInt(e(this).attr("tabindex"),10),t>n&&(n=t)}),this.$.find(".editable").each(function(){var s,o,r,a,l,u,c,h,d;s=e(this),fieldId=s.attr("field_id"),a=s.attr("fieldname"),r=s.attr("fieldlabel"),l=parseInt(s.attr("fieldorder"),10),o=s.attr("fieldtype")||"input",r||(r=a.replace(/_/gi," ").replace(/ id$/gi,"")),"select"===o?"status_id"===a?(h=e.trim(i.$.find(".type_id .v").html()),""==h&&(h=e("#type_id_options").val()),d=e.trim(i.$.find(".status_id .v").html()),u=i.findFactory(h,d,a)):"type_id"===a?(u=e("#"+a+"_options").clone(!0),u.change(function(){h=e(this).val(),d=e.trim(i.$.find(".status_id .v").html()),c=i.findFactory(h,d,"status_id"),c=i.prepareInputFromFactory(c,fieldId,"status_id",l,n),c=i.replaceStatusForNewType(u,c,e(this).parent().find(".status_id").val(),t)})):u=e("#"+a+"_options").clone(!0):u=e(document.createElement(o)),u=i.prepareInputFromFactory(u,fieldId,a,l,n),u.val("select"===o?s.children(".v").first().text():s.text()),s.hasClass("date")&&(u.datepicker({changeMonth:!0,changeYear:!0,closeText:"Close",dateFormat:"yy-mm-dd",firstDay:1,showOn:"button",onClose:function(){e(this).focus()},selectOtherMonths:!0,showAnim:"",showButtonPanel:!0,showOtherMonths:!0}),s.unbind("click"),s.click(function(){u.datepicker("show")}),u.mouseup(function(){e(this).datepicker("show")})),u.focus(function(){i.$.data("focus",e(this).attr("name"))}),u.blur(function(){i.$.data("focus","")}),e("<label />").attr({"for":u.attr("id")}).text(r).appendTo(t),u.appendTo(t)}),this.displayEditor(t),this.editorDisplayed(t),t},findFactory:function(t,i,n){return newInput=e("#"+n+"_options_"+t+"_"+i),0===newInput.length&&(newInput=e("#status_id_options_default_"+i)),newInput=newInput.clone(!0)},prepareInputFromFactory:function(e,t,i,n,s){return e.attr("id",i+"_"+t),e.attr("name",i),e.attr("tabindex",n+s),e.addClass(i),e.addClass("editor"),e.removeClass("template"),e.removeClass("helper"),e},replaceStatusForNewType:function(t,i,n,s){i.val(n),i.val()!==n&&(i.append(new Option("","")),i.val("")),i.focus(function(){self.$.data("focus",e(this).attr("name"))}),i.blur(function(){self.$.data("focus","")}),i.appendTo(s),t.parent().find(".status_id").replaceWith(i)},editDialogTitle:function(){return"Edit "+this.getType()},editorDisplayed:function(){},endEdit:function(){this.$.removeClass("editing")},error:function(t,i,n){this.markError(),RB.Dialog.msg(e(t.responseText).find(".errors").html()),this.processError(t,i,n)},getEditor:function(){var t,i;return t=this.getType().toLowerCase()+"_editor",i=e("#"+t).html(""),0===i.length&&(i=e("<div id='"+t+"'></div>").appendTo("body")),i},getID:function(){return this.$.children(".id").children(".v").text()},getType:function(){throw"Child objects must override getType()"},handleClick:function(t){var i,n,s,o;i=e(this),n=i.parents(".model").first().data("this"),s=n.$,s.hasClass("editing")||s.hasClass("dragging")||s.hasClass("prevent_edit")||e(t.target).hasClass("prevent_edit")||(o=n.edit(),o.find("."+e(t.currentTarget).attr("fieldname")+".editor").focus())},handleSelect:function(t){var i=e(this),n=i.data("this");e(t.target).hasClass("editable")||e(t.target).hasClass("checkbox")||i.hasClass("editing")||"A"===t.target.tagName||i.hasClass("dragging")||n.setSelection(!n.isSelected())},isClosed:function(){return this.$.hasClass("closed")},isNew:function(){return""===this.getID()},markError:function(){this.$.addClass("error")},markIfClosed:function(){throw"Child objects must override markIfClosed()"},markSaving:function(){this.$.addClass("saving")},newDialogTitle:function(){return"New "+this.getType()},open:function(){this.$.removeClass("closed")},processError:function(){},refresh:function(t){this.$.html(t.$.html()),t.$.length>1&&t.$.filter("script").each(function(){try{e.globalEval(e(this).html())}catch(t){}}),t.isClosed()?this.close():this.open(),this.refreshed()},refreshed:function(){},saveDirectives:function(){throw"Child object must implement saveDirectives()"},saveEdits:function(){var t,i=this.$,n=this,s=i.find(".editor");s.each(function(){var t,n;t=e(this),n=t.attr("name"),this.type.match(/select/)?((""!==t.val()||"status_id"!==n)&&i.children("div."+n).children(".v").text(t.val()),i.children("div."+n).children(".t").text(t.children(":selected").text())):i.children("div."+n).text(t.val())}),n.markIfClosed(),t=n.saveDirectives(),n.beforeSave(),n.unmarkError(),n.markSaving(),RB.ajax({type:"POST",url:t.url,data:t.data,success:function(e,t,i){n.afterSave(e,t,i)},error:function(e,t,i){n.error(e,t,i)}}),n.endEdit()},unmarkError:function(){this.$.removeClass("error")},unmarkSaving:function(){this.$.removeClass("saving")}})}(jQuery);