(function($){GroupTemplateUtil={showLoader:function(){var htmlCode="<center><img src='/images/loading.gif' /><br/>Loading...</center>";CustomPopup.createPopUpUsingLayout1("<div id='popup_confirm' class='confirm'>"+htmlCode+"</div>",100,100,'loaderDimmerID','loaderPromptID');},hideLoader:function(){$('#loaderDimmerID, #loaderPromptID').remove();}};GroupTemplate=function(){if(this instanceof GroupTemplate){this.id=0;this.name="";this.description="";}else{return new GroupTemplate();}}
GroupTemplateManager={showTemplateForm:function(obj){$('#template_form_dimmer, #template_form_pop_up_container').remove();var group_id=(obj.group_id)?obj.group_id:0;var template_id=(obj.id)?obj.id:0;var template_name=(obj.name)?obj.name:'';var template_description=(obj.description)?obj.description:'';var template_modules=(obj.modules instanceof Array)?obj.modules:new Array();var header_name=(0<template_id)?"Update Template":"Create a template from this SubGroup";var action_label=(0<template_id)?"Save Changes":"Save as Template";var height=320+(75*template_modules.length);var action_url=(obj.action_url)?obj.action_url:"/group_template.php?action=save";var module_content='';if(0<template_modules.length){jQuery.each(template_modules,function(i,val){module_content+=''+'<tr>'+'<td class="first">'+'<input type="checkbox" value="'+val.type+'" checked="checked" name="group_template_modules[]"/>'+'</td>'+'<td class="second">'+'<strong>'+val.name+'</strong>'+'<p>'+val.description+'</p>'+'</td>'+'</tr>';});module_content=''+'<label class="basic">Contents to be included <span class="required">*</span></label>'+'<p class="supplement">All the contents you select here will be copied to all the new subgroups that will be created based on this template.</p>'+'<table id="include_modules">'+module_content+'</table>';}
var html=''+'<h4 class="header" id="popup_header"><strong>'+header_name+'</strong></h4>'+'<form id="template_form" action="'+action_url+'" method="post" onsubmit="GroupTemplateManager.submitForm(this); return false;">'+'<div id="popup_message" class = "confirm">'+'<div class="errors" style="display:none;"></div>'+'<ul class="form">'+'<li class="form_set">'+'<label class="basic" for="">Template Name <span class="required">*</span></label>'+'<input type="text" value="'+template_name+'" name="group_template_name" class="textBox_small"/>'+'</li>'+'<li class="form_set">'+'<label class="basic" for="">Description <span class="required">*</span></label>'+'<p class="supplement">A small text describing this template.</p>'+'<textarea name="group_template_description" class="small">'+template_description+'</textarea>'+'</li>'+'<li class="form_set">'+module_content+'</li>'+'</ul>'+'<input type="hidden" name="group_template_id" value="'+template_id+'"/>'+'<input type="hidden" name="group_id" value="'+group_id+'"/>'+'</div>'+'<div class="buttons_box" id="popup_buttons">'+'<p>'+'<input type="submit" value="'+action_label+'" class="prompt_button"/>'+'<input type="button" onclick="jQuery(\'#template_form_dimmer, #template_form_pop_up_container\').remove();" value="Cancel" class="prompt_button"/>'+'</p>'+'</div>'+'</form>';CustomPopup.createPopUpUsingLayout1(html,450,height,'template_form_dimmer','template_form_pop_up_container');GroupTemplateManager.submitForm=function(form){var errors=new Array();if(""==$.trim($('input[name=group_template_name]').val())){errors.push("Template name must not be empty.");}
if(""==$.trim($('textarea[name=group_template_description]').val())){errors.push("Description must not be empty.");}
if(0<template_modules.length&&0==$('#template_form :checkbox:checked').length){errors.push("Choose at least one content section.");}
if(0==errors.length){if(template_id>0){$.post('/group_template.php?action=save',$(form).serialize(),function(obj){CustomPopup.initPrompt(obj.message,"Message","OK");CustomPopup.createPopup();if(obj.is_successful){$('#group_template_name_'+obj.group_template.ID).html(obj.group_template.name);$('#group_template_description_'+obj.group_template.ID).html(obj.group_template.description);GroupTemplateUtil.hideLoader();}},'json');$('#template_form_dimmer, #template_form_pop_up_container').remove();GroupTemplateUtil.showLoader();}
else{$('#template_form_dimmer, #template_form_pop_up_container').hide();GroupTemplateUtil.showLoader();form.submit();}}
else{$('.errors').html(errors.join('<br />')).fadeIn('slow');}}},showCreateForm:function(group_id,action_url){GroupTemplateUtil.showLoader();$.post('/group_template.php?action=fetch_template_modules','',function(modules){GroupTemplateUtil.hideLoader();GroupTemplateManager.showTemplateForm({'group_id':group_id,'modules':modules,'action_url':action_url});},'json');}};GroupForm={chosen_template:null,setTemplateShowablesVisibility:function(visibilility){if(visibilility){$('.template_showables').css('display','none');if(this.chosen_template){$('#megaDrop').css('display','none');var tpl=''+'<strong>'+this.chosen_template.name+'</strong>'+' <a title="" href="javascript:GroupForm.toggleSelectTemplate();">Change</a>'+'<br/>'+this.chosen_template.description;$('#showtemplateInfo').html(tpl);$('#showtemplateInfo').css('display','block');$('.box_overlay').css('display','none');}else{$('#megaDrop').css('display','block');$('.box_overlay').css('display','none');$('.action_emphasize').attr('class','action_emphasize');}}else{$('.template_showables').css('display','block');$('#megaDrop').css('display','none');$('.action_emphasize').attr('class','action_emphasize');$('.box_overlay').css('display','none');$('#showtemplateInfo').css('display','none');}},createGroupTemplate:function(id,name,description){this.chosen_template=new GroupTemplate();this.chosen_template.id=id;this.chosen_template.name=name;this.chosen_template.description=description;},toggleSelectTemplate:function(){if('none'==$('.box_overlay').css('display')){$('#megaDrop').css('display','block');$('.box_overlay').css('display','block');$('.action_emphasize').attr('class','action_emphasize pressed');$('#showtemplateInfo').css('display','none');}else{GroupForm.setTemplateShowablesVisibility(true);}},submitForm:function(form){if(this.chosen_template&&0<$('#use_template_1:checked').length){form.action=form.action+"&group_template_id="+this.chosen_template.id;}},showForm:function(group_template_id,parent_group_id){var content='<h4 class="header" id="popup_header"><strong>Create new group from template</strong></h4>'+'<form onsubmit="GroupForm.submitPopUpGroupForm(this); return false;" method="post">'+'<div id="popup_message" class = "confirm"><div class="errors" style="display:none;"></div>'+'<ul class="form">'+'<li class="form_set">'+'<label class="basic" for="name">Group name: <span class="required">*</span></label>'+'<p class="supplement"> Please use only letters, numbers, dash or apostrophe. Use only single space between words. </p>'+'<input type="text" name="name" id="name" value="" class="textBox_small" /> '+'</li>'+'</ul>'+'<input type="hidden" name="parent_group_id" value="'+parent_group_id+'"/>'+'<input type="hidden" name="group_template_id" value="'+group_template_id+'" />'+'</div>'+'<div class="buttons_box" id="popup_buttons">'+'<p>'+'<input type="submit" value="Create group" class="prompt_button"/>'+'<input type="button" onclick="jQuery(\'#dimmerID2, #promptID2\').remove();" value="Cancel" class="prompt_button"/>'+'</p>'+'</div>'+'</form>';var height=(0==group_template_id)?700:300;var width=(0==group_template_id)?550:430;CustomPopup.createPopUpUsingLayout1(content,width,height,'dimmerID2','promptID2');GroupForm.submitPopUpGroupForm=function(form){if(""==$.trim($('input[name=name]').val())){$('.errors').html("Group Name is required").fadeIn('slow');}
else{$('#dimmerID2, #promptID2').css('display','none');GroupTemplateUtil.showLoader();$.post('/group.php?action=save',$(form).serialize(),function(obj){GroupTemplateUtil.hideLoader();if(!obj.is_successful){$('.errors').html(obj.message).fadeIn('slow');$('#dimmerID2, #promptID2').css('display','block');}
else{window.location.href=obj.redirect_url;}},'json');}}}};GroupTemplateTags={showForm:function(parentID,group_template_id){var content='<h4 class="header" id="popup_header"><strong>Assign Tags</strong></h4>'+'<form onsubmit="GroupTemplateTags.submitPopUpGroupForm(this); return false;" method="post">'+'<div id="popup_confirm" class = "confirm">'+'<div id="add_tag_loading_indicator" style="display: none;">'+'<img id="loading_search" style="vertical-align: text-bottom;" src="/images/loading_small.gif" /> Validating...'+'</div>'+'<div class="errors" style="display:none;"></div>'+'<p>'+'<label>Tags: <span class="required">*</span></label>'+'<input type="text" value="" style="width: 240px;" id="tagNames" name="tagNames" autocomplete="off" class="ac_input"/>'+'</p>'+'<p>'+'You can add <span class="strong">multiple tags</span> by separating them with an empty space e.g. '+'<span class="em">Spring <span class="em">2009</span> <span class="em">France</span> <span class="em">Volunteer</span></span>'+'</p>'+'<input type="hidden" name="groupID" id="groupID" value="'+parentID+'" />'+'<input type="hidden" value="RECENT" id="category" name="category"/>'+'</div>'+'<div class="buttons_box" id="popup_buttons">'+'<input type="submit" value="Assign" class="prompt_button" id="a_submit"/>'+'<input type="button" value="Cancel" onclick="jQuery(\'#dimmerID2, #promptID2\').remove();" class="prompt_button" id="a_cancel"/>'+'</div>'+'</form>';var height=(0==group_template_id)?700:300;var width=(0==group_template_id)?550:430;CustomPopup.createPopUpUsingLayout1(content,width,height,'dimmerID2','promptID2');$('#tagNames').autocomplete(GroupTagLabels,{multiple:true});$('#tagNames').focus();GroupTemplateTags.submitPopUpGroupForm=function(form){$('.errors').hide();$('#add_tag_loading_indicator').show();if(""==$.trim($('input[name=tagNames]').val())){$('#add_tag_loading_indicator').hide();$('.errors').html("Please add at least one tag name").fadeIn('slow');}
else{var tags=$('#tagNames').val();jQuery.ajax({type:'POST',cache:false,url:'/group_template.php?action=assign_tag&group_template_id='+group_template_id+'&tags='+tags,success:function(html){$('#add_tag_loading_indicator').hide();if(html=='success'){window.location.reload();}
else if(html=='error'){$('#promptID2').hide();CustomPopup.initPrompt("You have to be logged in as the advisor of this group to be able to do this! You will be redirected to the login page.","Error","Okay");CustomPopup.setCustomCancel("Okay",function(){$.ajax({type:"GET",success:function(){var page='login.php?redirect=/group_template.php?action=view_template_manager&group_id='+parentID;window.location.href='/'+page;}})});CustomPopup.setJS(true);CustomPopup.createPopup();}
else{$('#dimmerID2, #promptID2').css('display','block');$('.errors').html(html).fadeIn('slow');}}});}}}};})(jQuery);
