(function(A){if(!window.TeamCalendars){window.TeamCalendars={}
}if(!window.TeamCalendars.subcalendar){window.TeamCalendars.subcalendar={}
}if(!window.TeamCalendars.subcalendar.jira){var B=function(J,I,H,F,K){var E=A("form",H.getCurrentPanel().body);
var G=A("select[name='project']",E).empty().attr("disabled","disabled").append(A(document.createElement("option")).text(J.getText(I,"calendar3.loading")));
var D=A(".submit",E).attr("disabled","disabled");
A.ajax({cache:false,dataType:"json",error:function(M,P,O){if(J.isRequireOauth(M)){var L=A.parseJSON(M.responseText);
var N=A(".project-oauth",E).empty().removeClass("hidden");
A(document.createElement("div")).addClass("oauth-approve").html(AJS.format(J.getText(I,"calendar3.oauth.loginapprovetoseeprojects"),A(document.createElement("span")).text(J.getText(I,"calendar3.oauth.loginandauthorize")).html())).appendTo(N).find(".approve-dance").click(function(){AppLinks.authenticateRemoteCredentials(L.oAuthUrl,function(){N.addClass("hidden");
B(J,I,H,null,function(){C(J,I,H)
})
},function(){alert(J.getText(I,"calendar3.oauth.trydancingagain"))
});
return false
});
J.showDialogInUpdatedHeight(H)
}else{if(F&&A.isFunction(F)){F(M,P,O)
}}},success:function(L){G.empty().removeAttr("disabled","disabled");
if(A.isArray(L)&&L.length){A.each(L,function(M,N){A(document.createElement("option")).attr("value",N.key).text(N.name).appendTo(G)
});
D.removeAttr("disabled")
}else{A(document.createElement("option")).attr("value","").text(J.getText(I,"calendar3.error.noprojectsfound")).appendTo(G)
}J.showDialogInUpdatedHeight(H);
if(K&&A.isFunction(K)){K(L)
}},url:J.getParameter(I,"baseUrl")+"/rest/calendar-services/1.0/jira/"+encodeURIComponent(A("select[name='server'] option:selected",E).val())+"/projects.json"})
};
var C=function(L,K,G,E){var I=A("form",G.getCurrentPanel().body);
var J=A("select[name='project']",I);
var H=A(".field-group-jira-show",I);
A("div.date-fields, div.error",H).empty();
var D=A(".date-fields",H);
var F=A(document.createElement("img")).attr("src",L.getParameter(K,"baseUrl")+"/images/icons/wait.gif").addClass("date-fields-spinner").appendTo(D);
A.ajax({cache:false,dataType:"json",error:function(){F.remove();
A(".dateFieldName-error",H).text(L.getText(K,"calendar3.jira.error.datefields.unabletoshow"));
L.showDialogInUpdatedHeight(G)
},success:function(N){F.remove();
var M=L.getParameter(K,"elementIdSuffix");
A.each(N,function(Q,P){var O="option-"+P.key+"-"+M;
A(document.createElement("div")).addClass("checkbox").append(A(document.createElement("input")).attr({type:"checkbox",id:O,name:"dateFieldName",value:P.key})).append(A(document.createElement("label")).attr({"for":O}).text(P.name)).appendTo(D.appendTo(H))
});
if(E&&A.isFunction(E)){E(N)
}},url:L.getParameter(K,"baseUrl")+"/rest/calendar-services/1.0/jira/"+encodeURIComponent(A("select[name='server'] option:selected",I).val())+"/"+encodeURIComponent(J.val())+"/date-fields.json"})
};
window.TeamCalendars.subcalendar.jira={initializeForm:function(G,F,E){E.addHeader(G.getText(F,"calendar3.createjiracalendar"));
E.addPanel("",AJS.template.load("jira-form-"+G.getParameter(F,"elementIdSuffix")).toString(),"calendar-dialog-panel");
var D=A("form.sub-calendar-edit-form",E.getCurrentPanel().body);
A("select[name='server']",D).change(function(){var H=A("select[name='server'] option:selected",D);
if(H.length&&H.val()){B(G,F,E,null,function(){C(G,F,E)
})
}});
A("input[name='spaceKeyAutocomplete']",D).bind("selected.autocomplete-content",function(I,H){A("input[name='spaceKey']",D).val(H.content.key);
A(this).val(H.content.name);
A(".spaceKeyAutocomplete-error",D).empty();
G.showDialogInUpdatedHeight(E)
});
A("select[name='project']",D).change(function(){C(G,F,E)
});
A(".submit",D).click(function(){D.submit();
return false
});
A(".subcalendar-cancel",D).click(function(){E.hide();
return false
});
return D
},getFormDataAsAjaxData:function(G,F,E){var D=A("form.sub-calendar-edit-form",E.getCurrentPanel().body);
var H="";
A(".field-group-jira-show input:checked",D).each(function(){if(H){H+="&"
}H+="dateFieldName="+encodeURIComponent(A(this).val())
});
return{type:"jira",subCalendarId:A("input[name='subCalendarId']",D).val(),name:A("input[name='name']",D).val(),description:A("textarea[name='description']",D).val(),color:A("input[name='color']",D).val(),spaceKey:A("input[name='spaceKeyAutocomplete']",D).val()?A("input[name='spaceKey']",D).val():"",location:"jira://"+encodeURIComponent(A("select[name='server'] option:selected",D).val()||"")+"?projectKey="+encodeURIComponent(A("select[name='project'] option:selected",D).val()||"")+(H?"&"+H:"")}
},populateForm:function(P,O,H,J){var E=H.getCurrentPanel();
var I=A("form.sub-calendar-edit-form",E.body);
P.setFieldErrors(E.body,null);
var G=A(".form-mode",I);
var M=A("select[name='server']",I);
var L=A("select[name='project']",I);
var N=A("input[name='options']",I).removeAttr("checked");
var K=A(".timeZoneIdDisplay",I);
if(J){E.page.header.empty().text(J.name);
G.removeClass().addClass("form-mode edit");
A("input[name='subCalendarId']",I).val(J.id);
A("input[name='name']",I).val(J.name);
A("textarea[name='description']",I).val(J.description);
A("input[name='color']",I).val(J.color);
A("input[name='spaceKey']",I).val(J.spaceKey);
A("input[name='spaceKeyAutocomplete']",I).val(J.spaceName);
var D=false;
A("option",M).removeAttr("selected").each(function(){var Q=A(this);
if(Q.val()===J.applicationId){Q.attr("selected","selected");
D=true;
return false
}});
if(!D){A(document.createElement("option")).attr("value",J.applicationId).attr("selected","selected").text(J.applicationName).appendTo(M)
}B(P,O,H,null,function(Q){var S=false;
if(Q&&A.isArray(Q)){A.each(Q,function(T,U){if(U.key===J.projectKey){S=true;
return false
}})
}var R=A("select[name='project']",I);
if(!S){A("option:selected",R).remoteAttr("selected");
R.append(A(document.createElement("option")).attr("value",J.projectKey).text(J.projectName).attr("selected","selected"))
}else{A("option",R).removeAttr("selected").each(function(){var T=A(this);
if(T.val()===J.projectKey){T.attr("selected","selected")
}})
}C(P,O,H,function(){A.each(J.dateFieldNames,function(U,T){A("input[value='"+T+"']",I).attr("checked","checked")
})
})
});
K.text(J.timeZoneId)
}else{E.page.header.empty().text(P.getText(O,"calendar3.createjiracalendar"));
G.removeClass().addClass("form-mode create");
A("input[name='subCalendarId']",I).val("");
A("input[name='name']",I).val("");
A("textarea[name='description']",I).val("");
A("input[name='color']",I).val(P.getNextUnusedSubCalendarColor(O));
A("input[name='spaceKey']",I).val("");
A("input[name='spaceKeyAutocomplete']",I).val("");
K.text("");
B(P,O,H,null,function(){C(P,O,H)
})
}var F=A("input[name='name'], textarea[name='description'], input[name='spaceKeyAutocomplete']",I);
if(P.isCalendarInEditMode(O)){F.removeAttr("readonly");
M.removeAttr("disabled");
L.removeAttr("disabled");
N.removeAttr("disabled")
}else{G.removeClass().addClass("form-mode read-only");
F.attr("readonly","readonly");
M.attr("disabled","disabled");
L.attr("disabled","disabled");
N.attr("disabled","disabled")
}},onSubmitStart:function(F,E,D){A(".submit",D.getCurrentPanel().body).attr("disabled","disabled")
},onSubmitEnd:function(F,E,D){A(".submit",D.getCurrentPanel().body).removeAttr("disabled")
},getWeight:function(){return 4
},getTypeName:function(E,D){return E.getText(D,"calendar3.subcalendar.type.jira")
}}
}})(jQuery);

