!function(e){var r=new function(){var s=this;s.width=ko.observable(e("#main").outerWidth());var o=function(){var s=this;s.oldHostname="",s.serialize=function(){return{id:s.id(),hostname:s.hostname(),port:s.port(),username:s.username(),password:s.password()}},s.update=function(){r.jabberservers.loading||(s.hostname.isValid()?(s.editing(!1),s.port.isValid()&&s.username.isValid()&&s.password.isValid()?(s.id()?e.ajax({url:"../../admin/jabberservers/update",data:s.serialize(),type:"POST",timeout:3e4,error:MMONIT.error.json}):(e.ajax({url:"../../admin/jabberservers/create",data:s.serialize(),type:"POST",dataType:"json",timeout:3e4,success:function(e,r,o){s.id(e.id)},error:MMONIT.error.json}),r.jabberservers.createInProgress(!1)),r.jabberservers.expandedRow()&&s.test()):(r.jabberservers.expandedRow(s),s.port.isValid()||e("#jabberserver"+s.id()+" .jabberPort").focus())):s.editing(!0))},s.test=function(){if(s.hostname().length>0&&s.port()>0){s.testInProgress&&4!=s.testInProgress.readyState&&s.testInProgress.abort(),s.showTestResult(!0);var r=e("#jabberserver"+s.id()+" .testResult .accessory");r.html("<img src='../../img/spinner_small.gif' height='16' width='16' />"),s.testInProgress=e.ajax({url:"../../admin/jabberservers/test",data:s.serialize(),type:"POST",dataType:"json",timeout:3e4,success:function(e,s,o){r.css("color",0==e.status?"red":"green"),r.text(e.description)},error:function(e,s,o){"timeout"===s?(r.css("color","red"),r.text("Test timed out")):"abort"!==s&&MMONIT.error.json(e,s,o)}})}},s.edit=function(e,r){r.stopPropagation(),s.oldHostname=s.hostname(),s.editing(!0)},s.stopEdit=function(){s.hostname().trim().length>0&&(s.oldHostname!==s.hostname()?(s.oldHostname=s.hostname(),s.update()):s.editing(!1))},s.id=ko.observable(null),s.hostname=ko.observable("").extend({required:!0,maxLength:255}),s.port=ko.observable().extend({required:!0,number:!0,min:1,max:65535,step:1}),s.username=ko.observable("").extend({required:!0,maxLength:255}),s.password=ko.observable("").extend({required:!0,maxLength:255}),s.editing=ko.observable(!1),s.showTestResult=ko.observable(!1),s.mouseover=ko.observable(ISTABLET),s.portSubscription=s.port.subscribe(s.update),s.usernameSubscription=s.username.subscribe(s.update),s.passwordSubscription=s.password.subscribe(s.update)};s.jabberservers={loading:!0,list:ko.observableArray(),jabberserver:ko.observable(),expandedRow:ko.observable(),createInProgress:ko.observable(!1),toggleExpand:function(e,r){r.stopPropagation(),s.jabberservers.expandedRow(s.jabberservers.expandedRow()!==e?e:null),s.jabberservers.expandedRow()&&e.test()},isExpanded:function(e){return e===s.jabberservers.expandedRow()},enableMouseover:function(e,r){r.stopPropagation(),ISTABLET||e.mouseover(!0)},disableMouseover:function(e,r){r.stopPropagation(),ISTABLET||e.mouseover(!1)},confirmDelete:function(r,o){o.stopPropagation(),s.jabberservers.jabberserver(r),e("#confirmJabberserverDelete").modal("show")},add:function(e,r){if(r.stopPropagation(),!s.jabberservers.createInProgress()){s.jabberservers.createInProgress(!0),s.jabberservers.expandedRow(null);var t=new o;t.editing(!0),s.jabberservers.list.push(t)}},remove:function(r,o){o.stopPropagation(),r.id()>0?e.ajax({url:"../../admin/jabberservers/delete",data:{id:r.id()},type:"POST",timeout:3e4,success:function(o,t,a){e("#confirmJabberserverDelete").modal("hide"),s.jabberservers.list.remove(r)},error:function(r,s,o){e("#confirmJabberserverDelete").modal("hide"),MMONIT.error.json(r,s,o)}}):(e("#confirmJabberserverDelete").modal("hide"),s.jabberservers.createInProgress(!1),s.jabberservers.list.remove(r))}},e.getJSON("../../admin/jabberservers/list",(function(e){for(var r=0;r<e.length;r++){var t=new o;t.id(e[r].id),t.hostname(e[r].hostname),t.port(e[r].port),t.username(e[r].username),t.password(e[r].password),s.jabberservers.list.push(t)}s.jabberservers.loading=!1})).fail(MMONIT.error.json)};ko.validation.init({insertMessages:!1,decorateElement:!0,errorElementClass:"error"},!0),ko.applyBindings(r,document.getElementById("jabberTab")),e(window).on("resize orientationchange",(function(s){r.width(e("#main").outerWidth())}))}(jQuery);