;(function($,_,undefined){"use strict";ips.controller.register('forms.front.index.selector',{categoryHtml:'',categoryTitle:'',currentType:null,initialize:function(){this.on('click','[data-action="formType"]',this.chooseFormType);this.on('click','[data-action="backToCategories"]',this.backToCategories);this.on('click','[data-action="form"]',this.chooseForm);this.on(document,'contentItemAdded',this.calendarAutocomplete);History.Adapter.bind(window,'statechange',_.bind(this.stateChange,this));this.setup();},setup:function(){this.categoryHtml=this.scope.find('[data-role="wrapperContent"]').html();this.categoryTitle=this.scope.find('[data-role="wrapperTitle"]').text();this.initialUrl=window.location.href.replace('#','');Debug.log(this.initialUrl);},stateChange:function(e){var state=History.getState();if(!_.isUndefined(state.data.controller)&&state.data.controller==='legoForms'){switch(state.data.action){case"chooseForm":this.doChooseForm(state.data);break;case"chooseFormType":this.doChooseFormType(state.data);break;default:this.resetPage();break;}}else if(state.cleanUrl===this.initialUrl){this.resetPage();}},calendarAutocomplete:function(e,data){try{var eventID=parseInt(data.itemList[0]);var self=this;ips.getAjax()(ips.getSetting('baseURL')+'index.php?app=forms&module=forms&controller=forms&do=getDates&id='+eventID,{dataType:'json'}).done(function(response){if(response.start_date){var startDate=response.start_date.split(' ');self.scope.find('input[name="forms_activity_start"]').val(startDate[0]);}
if(response.end_date){var endDate=response.end_date.split(' ');self.scope.find('input[name="forms_activity_end"]').val(endDate[0]);}});}catch(err){Debug.log("Error populating dates: "+err);}},chooseForm:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');var elemId=$(e.currentTarget).identify().attr('id');var state=History.getState();History.pushState({action:'chooseForm',type:state.data.type,elemId:elemId,url:url,controller:'legoForms'},document.title,url);},chooseFormType:function(e){e.preventDefault();var url=$(e.currentTarget).attr('href');var type=$(e.currentTarget).attr('data-type');var elemId=$(e.currentTarget).identify().attr('id');History.pushState({action:'chooseFormType',type:type,url:url,elemId:elemId,controller:'legoForms',},document.title,url);},doChooseForm:function(data){var container=this.scope.find('[data-role="wrapperContent"]');var title="Finally, complete the form";if(data.type=='activity'){title="Finally, tell us about your activity";}else if(data.type=='application'){title="Finally, complete your application";}
this.scope.removeClass('legoForms_initialState');this.scope.find('[data-role="wrapperTitle"]').text(title);this.scope.find('#'+data.elemId).addClass('ipsLoading ipsLoading_small');ips.getAjax()(data.url).done(function(response){container.html(response);$(document).trigger('contentChange',[container]);});},backToCategories:function(e){if(e){e.preventDefault();}
History.replaceState({action:'index',controller:'legoForms'},document.title,this.initialUrl);},resetPage:function(){this.scope.addClass('legoForms_initialState');this.scope.find('[data-role="wrapperContent"]').html(this.categoryHtml);this.scope.find('[data-role="wrapperTitle"]').text(this.categoryTitle);$('#submittedForms').slideDown();},doChooseFormType:function(data){var container=this.scope.find('[data-role="wrapperContent"]');var title='Choose a form';if(data.type=='activity'){title='What kind of activity did you complete?';}else if(data.type=='application'){title='What kind of application are you making?';}
this.scope.removeClass('legoForms_initialState');this.scope.find('[data-role="wrapperTitle"]').text(title);this.scope.find('#'+data.elemId+' .legoForms_categoryIcon').addClass('ipsLoading').find('img').hide();ips.getAjax()(data.url).done(function(response){container.html(response);$('#submittedForms').hide();});}});}(jQuery,_));;