

var SlideShow = {
	show:function(url){
		var x = '<img src="'+url+'" />';
		Window.blur();
		$('slideshow_zoom').show();
		$('slideshow_zoom_img').update(x);
		
	},
	hide:function(){
		Window.focus();
		$('slideshow_zoom').hide();
	}
}



var WindowShade = {
  lh:null,
  loc:false,
  collapse:function(o){
    if(this.loc){
      return false;
    }
    this.loc = true;
    setTimeout(function(){WindowShade.loc=false;},810);
    var x = $$('.t_std_layout_outer')[0];
    WindowShade.lh = x.getDimensions().height;
    
    // new Effect.SlideUp(x);
    new Effect.Morph(x, {
      style: 'height:12px;', // CSS Properties
      duration: 0.8 // Core Effect properties
    });
    o=$(o);
    o.src = '/images/public/bullet_arrow_bottom.png';
    o.onclick = function(){WindowShade.expand(this);}
  },
  expand:function(o){
    if(this.loc){
      return false;
    }
    this.loc = true;
    setTimeout(function(){WindowShade.loc=false;},810);
    
    var x = $$('.t_std_layout_outer')[0];
    new Effect.Morph(x, {
      style: 'height:'+WindowShade.lh+'px;', // CSS Properties
      duration: 0.8 // Core Effect properties
    });
    this.lh=null;
    o=$(o);
    o.src = '/images/public/bullet_arrow_top.png';
   o.onclick = function(){WindowShade.collapse(this);}
  }
}


var SearchSite = {
	keyup:function(){
		
	},
	on_enter:function(){
		var s = $F('search_site');
		if(s.length==0){
			new Effect.Shake('search_site');
			return;
		}
		SearchSite.search(s);
	},
	search:function(s){
		window.open('/search?s='+s, '_self');
	}
}




/*only supports nav one level deep. will do dynamic hovers for all first
tier nav nodes, but only show deeper nodes for the selected node*/
var Navigation = {
	default_node:2,
	hover_timer:null,
	re_show_menus:false,
	show_hide_sub_child_items_with_menus:false,
	adjust_menus:function(){
		//moves menu to relevant place on page on page load if you want
		Navigation.hover_node(this.default_node, $('nav_link_'+this.default_node)); 
	},
	adjust_page_padding:function(){
		var padding_offset = 0;
		$$('.nav_sub_child_items').each(function(t){
			if(t.visible()){
				padding_offset += 1;
			}
		});
		//compensate for ie7 making 1st subnav large
		if(BrowserDetect.ie7){
			padding_offset = padding_offset * 22;
			padding_offset = padding_offset + 30;
		}else if(BrowserDetect.safari){
			padding_offset = padding_offset * 20;
		}else{
			padding_offset = padding_offset * 22;
		}
		$('content').style.paddingTop = (padding_offset)+'px';
	},
	hover_home_node:function(o){
		this.clear_and_hide_selected_root(true, -1);
	},
	hover_node:function(id,o){
		if(this.hover_timer != null) clearTimeout(this.hover_timer);
		
		this.clear_and_hide_selected_root(true, id);
		
		//for application link there are no child items
		try{
			var cnd = $('nav_child_items_'+id);
			cnd.show();
			
			var xoffset = $$('.nav_root_items')[0].getDimensions().width - o.positionedOffset()[0] - o.getDimensions().width - (cnd.getDimensions().width - o.getDimensions().width);
			//-3 for shadow
			
			cnd.style.right = (xoffset-15)+'px';
			
			$(o).addClassName('open');
		}catch(ex){}
	},
	clear_hover_node:function(){
		this.hover_timer = setTimeout(function(){
			this.clear_and_hide_selected_root(false, -1);
		}.bind(this), 100);
	},
	suspend_clear_timer:function(){
		if(this.hover_timer != null) clearTimeout(this.hover_timer);
	},
	resume_clear_timer:function(){
		this.clear_hover_node();
	},
	clear_and_hide_selected_root:function(force_switch, sel_id){
		
		$$('.nav_child_items').each(function(t){
			t.hide();
		});
		$$('.open').each(function(t){
			t.removeClassName('open');
		});
		//restore to default menu, not empty. if nothing set this will skip and
		//restore to empty.
		if(!force_switch && this.default_node > 0){
			//for application link there are no child items
			try{
				//$('nav_link_'+this.default_node).addClassName('open');
				if(this.re_show_menus){
					$('nav_child_items_'+this.default_node).show();
					if(this.show_hide_sub_child_items_with_menus){
						$$('.nav_sub_child_items').each(function(t){t.show();});
					}
				}
			}catch(ex){}
		}else{
			if(this.show_hide_sub_child_items_with_menus){
				if(sel_id == this.default_node){
					$$('.nav_sub_child_items').each(function(t){t.show();});
				}else{
					$$('.nav_sub_child_items').each(function(t){t.hide();});
				}
			}
		}
	}
}




var PhotoZoom = {
	show:function(url){
		var x = '<img src="'+url+'" />';
		Window.blur();
		$('slideshow_zoom').show();
		$('slideshow_zoom_img').update(x);
		$('page_dim').on('click',function(){PhotoZoom.hide();});
	},
	hide:function(){
		Window.focus();
		$('slideshow_zoom').hide();
	}
}





var ImageSlider = {
	c_ndx:0,
	width:500,
	slow:false,
	count:function(){
		return $('slideshow-inner').select('.thumb').length;
	},

	next:function(){
		this.move(1);
	},
	prev:function(){
		this.move(-1);
	},
	move:function(dir){
		var max = this.count();
		var min = 0;

		var ndx = this.c_ndx + dir;

		if(ndx >= max){
			this.scroll(0);
		}else if(ndx < 0){
			this.scroll(max-1);
		}else{
			this.scroll(ndx);
		}
	},
	scroll:function(ndx){
		var dur = 0.5;
		if(this.slow){
			dur = 3.0;
		}
		new Effect.Move($('slideshow-inner'), { x: -(ndx-this.c_ndx)*ImageSlider.width, y: 0, mode: 'relative', duration:dur, queue: 'end' });

		this.c_ndx = ndx;
	}

}









var _PeoplePublic = Class.create(PosterController, {
  initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'last_name asc';
		this.name_singular = 'person';
		this.allow_multi_tags = false;
		this.limit = 10;
  },
	
	alphabet_letter:null,
	
	choose_letter:function(l){
		
		this.alphabet_letter = l;
		this.refresh_list();
	},
	refresh_list_args:function(){
		return {alphabet_letter:this.alphabet_letter,text:$F('peoplex_list_search'),tags:this.tags};
	},
	refresh_list_target_list:function(){
		return 'list_peoplex';
	},
	
	asdf:function(){
		//stub
	}

});
var PeoplePublic = new _PeoplePublic('peoplex', '/people');











var _Impact = Class.create(PosterController, {
  initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'title asc';
		this.name_singular = 'impact';
		this.limit = 2;
		this.allow_multi_tags = false;
  },
	
	
	
	asdf:function(){
		//stub
	}

});
var Impact = new _Impact('impact', '/impact');











var _Careers = Class.create(PosterController, {
	doc_mode:'documents',
	
  initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'id desc';
		this.name_singular = 'career';
		this.limit = 50;
  },
	
	switch_to_mode:function(o,n){
		Careers.mode = n;
		Careers.clear_all_tags.apply(Careers,[o]);
	},
	
	start_up_resume_for_careers:function(){
		var n = Card.show(this,'start_upload_resume_careers',function(){
			
		}.bind(this));
	},
	resume_post_upload:function(){
		Card.hide(this,'start_upload_resume_careers');
		var n = Card.show(this,'thx_res_up_careers',function(){
			
		}.bind(this));
	},
	close_thx_res_up:function(){
		Card.hide(this,'thx_res_up_careers');
	},
	
	
	
	have_acct:function(){
		hide('car-pop-start');
		show('login');
		Card.set_sub_title('Please log in before posting a career opportunity');
	},
	no_have_acct:function(){
		hide('car-pop-start');
		show('signup');
		Card.set_sub_title('Sign up to post');
	},
	
	
	
	
	start_email:function(){
		var n = Card.show(this,'start_email_career',function(){
			
		}.bind(this));
		
	},
	try_email:function(){
		
		this.do_email();
		
	},
	do_email:function(){
		var from_n = $F('new_cemail_from_n');
		var from_e = $F('new_cemail_from_e');
		var to_e = $F('new_cemail_to_e');
		
		
		
		Request(this.controller_path+'/email', {from_n:from_n,from_e:from_e,to_e:to_e,id:this.id}, function(r){
			if(r.ok){
				Card.hide(this,'start_email_career');
			}else{
				
			}
		}.bind(this));
		
	},
	get_start_email_career_args:function(){
		return {id:this.id};
	},
	
	
	start_un_approve:function(){
		Dialogue.show({
			html:["Unapprove and hide","The public will not be able to see this."],
			img:'/images/edit/remove_64.png',
			ok_label:"Un-approve",
			on_ok:function(){
				this.do_un_approve();
			}.bind(this)
		});
	},
	do_un_approve:function(){
		Request(this.controller_path+'/unapprove', {id:this.id}, function(r){
			if(r.ok){
				window.location.reload();
			}
		}.bind(this));
	},
	after_delete:function(){
		window.location.reload();
	},
	after_restore:function(){
		window.location.reload();
	},

	begin_edit:function(){
		$('edit_career').show();
		$('read_only').hide();
		$('top-toolbar-car').hide();
	},
	save:function(){
		var args = this.get_save_args();
		args.save_tags = this.save_tags;
		
		Request(this.controller_path+'/save', args, function(r){
			if(r.ok){
				window.location.reload();
			}
		}.bind(this));
	},
	cancel_save:function(){
		$('edit_career').hide();
		$('read_only').show();
		$('top-toolbar-car').show();
	},

	start_approve:function(){
		Dialogue.show({
			html:["Approve & publish","Approving this will make it public."],
			img:'/images/edit/restore_64.png',
			ok_label:"Approve",
			on_ok:function(){
				this.do_approve();
			}.bind(this)
		});
	},
	do_approve:function(){
		Request(this.controller_path+'/approve', {id:this.id}, function(r){
			if(r.ok){
				window.location.reload();
			}
		}.bind(this));
	},

	start_request_approve:function(){
		Dialogue.show({
			html:["Request approval and publishing","Once we approve this it will be visible to the public."],
			img:'/images/edit/restore_64.png',
			ok_label:"Request approval",
			on_ok:function(){
				this.do_request_approve();
			}.bind(this)
		});
	},
	do_request_approve:function(){
		Request(this.controller_path+'/request_approve', {id:this.id}, function(r){
			if(r.ok){
				window.location.reload();
			}
		}.bind(this));
	},
	
	save_tags:[],
	save_consume_tag:function(o,tid){
		if(o.hasClassName('selected')){
      o.removeClassName('selected');
      this.save_tags = this.save_tags.without(tid);
    }else{
      o.addClassName('selected');
      this.save_tags.push(tid);
    }
	},
	
	create_tags:[],
	create_consume_tag:function(o,tid){
		if(o.hasClassName('selected')){
      o.removeClassName('selected');
      this.create_tags = this.create_tags.without(tid);
    }else{
      o.addClassName('selected');
      this.create_tags.push(tid);
    }
	},
	create_clear_all_tags:function(o){
  	$(o).up().up().up().select('.tag .selected').each(function(t){t.removeClassName('selected');});
  	this.create_tags = [];
  },
	
	before_start_create:function(){
		this.create_tags = [];
		return true;
	},
	after_start_create_always:function(r){
		try{
			Calendar.setup({dateField:'new_career_valid_from'});
			Calendar.setup({dateField:'new_career_valid_to'});
		}catch(ex){}
	},
	extra_create_args:function(){
		var args={};
		
		args.create_tags=this.create_tags;
		
		return args;
	},
	create_args_string:'title company ext_url city state valid_from valid_to contact_name contact_phone contact_email contact_address contact_address_2 contact_city contact_state contact_zip contact_fax description',
	before_create:function(args){
		// var cont = true;
		// 		var errors = [];
		// 		if(args['title']==null || args['title']==''){
		// 			cont = false;
		// 			errors.push("You need a name.");
		// 		}else{
		// 			cont = true;
		// 		}
		// 		
		// 		if(errors.length>0){
		// 			Warn(errors.join("<br>"));
		// 		}
		
		return true;
	},
	after_create_ok:function(r){
		window.open(r.url,'_self');
	},
	
	save_args_string:'title company ext_url city state valid_from valid_to contact_name contact_phone contact_email contact_address contact_address_2 contact_city contact_state contact_zip contact_fax description',
	
	extra_save_args:function(){
		// if(this.rich_text_id != null){
		// 			this.text_editor.saveHTML();
		// 			var ct = this.text_editor.get('element').value;
		// 			return {content:ct, editor_field:this.editor_field};
		// 		}
	},
	
	rich_text_id:null,
	editor_field:null,
	text_editor:null,
	reset_editors:function(){
		try{
				this.text_editor.destroy();
		}catch(ex){}
		this.rich_text_id = null;
		this.text_editor = null;
	},
	
	add_editor:function(str){
		this.reset_editors();
		this.rich_text_id = str;
		
		var text_editor = new YAHOO.widget.Editor(this.rich_text_id, {
		    height: '200px',
		    width: '590px',
		    dompath: true, //Turns on the bar at the bottom
		    animate: false
		});
		
		text_editor.on('afterRender', function(){
			// Poster.fit_poster_to_content('careers');
		}, text_editor, true);
		
		text_editor.render();
		this.text_editor = text_editor;
	},
	
	load_bio:function(){
		//this.add_editor('edit_person_biography');
		this.editor_field = 'biography';
	},
	load_quote:function(){
		//this.add_editor('edit_person_quote');
		this.editor_field = 'quote';
	},
	load_profile:function(){
		//this.add_editor('edit_person_profile');
		this.editor_field = 'profile';
	},
	
	after_close_inspect:function(){
		this.reset_editors();
		this.sel_doc_id = null;
	},
	
	
	
	
	add_doc_by_id:function(did){
		Request(this.controller_path+'/add_doc', {id:this.id,document_id:did,mode:this.doc_mode}, function(r){
			this.refresh_docs();
		}.bind(this));
	},
	sel_doc_id:null,
	inspect_doc:function(did){
		this.sel_doc_id = did;
		if(this.doc_mode != 'photos'){
			this.start_remove_doc();
		}else{
			Poster.show('person_document_options');
			showif(this.doc_mode=='photos','person_document_options_primary');
		}
		
	},
	start_remove_doc:function(){
		var did = this.sel_doc_id;
		Dialogue.show({
			html:['Are you sure you want to remove this document?'],
			img:'/images/edit/remove_64.png',
			ok_label:'Remove document',
			on_ok:function(){
				Request(this.controller_path+'/remove_doc', {id:this.id,document_id:did,mode:this.doc_mode}, function(r){
					this.refresh_docs();
					Poster.hide('person_document_options');
				}.bind(this));
			}.bind(this)
		});
	},
	start_set_primary_doc:function(){
		Request(this.controller_path+'/set_primary_photo', {id:this.id,document_id:this.sel_doc_id}, function(r){
			Poster.hide('person_document_options');
			this.refresh_docs();
		}.bind(this));
	},
	refresh_docs:function(){
		// Loading(tgt);
		var tgt = 'careers_edit_'+this.doc_mode;
		Request(this.controller_path+'/doc_list', {id:this.id,mode:this.doc_mode}, function(r){
			
			// Loaded(tgt);
			// $(tgt).update(r.html);
			this.sel_doc_id = null;
		}.bind(this), tgt);
	},
	normalize_poster:function(){
		var ins = $('poster_'+this.name).select('.inspector')[0];
		ins.removeClassName("wider");
		ins.removeClassName("variable");
		ins.addClassName("wider");
		// Poster.fit_poster_to_content('careers');
	},
	
	switch_edit_tab:function(o,n){
		Editor.open(null,'careers',n);
		$('body').addClassName('editing-other');
		$(o).up().select('.selected').each(function(t){t.removeClassName('selected');});
		$(o).addClassName('selected');
		$('editor-menu-area-careers').show();
		$('editor-menu-area-page').hide();
		eval("if(this.switch_edit_tab_"+n+" != undefined){this.switch_edit_tab_"+n+"();}");
	},
	exit_lists:function(){
		$('editor-menu-area-careers').hide();
		$('editor-menu-area-page').show();
		$('body').removeClassName('editing-other');
	},
	
	
	
	after_refresh_card_tab_details_documents:function(){
		Careers.doc_mode='documents';
		Careers.refresh_docs();
	},
	after_refresh_card_tab_details_photos:function(){
		Careers.doc_mode='photos';
		Careers.refresh_docs();
	},
	
	
	
	
	after_signup:function(){
		Card.hide(this,'start_create');
		var n = Card.show(this,'after_signup_careers',function(){
			
		}.bind(this));
	},
	
	
	signup:function(){
		var args = {};
		if($F('signup_password') != $F('signup_password_2')){
			Warn("Your passwords do not match");
			blank('signup_password signup_password_2');
			new Effect.Shake('signup');
			return;
		}
		Request(this.controller_path+'/signup', {email:$F('signup_email'),password:$F('signup_password')}, function(r){
			if(!r.ok){
				new Effect.Shake('signup');
			}else{
				this.after_signup();
			}
		}.bind(this));
	},
	
	login:function(){
		Request(this.controller_path+'/login', {email:$F('login_email'),password:$F('login_password')}, function(r){
			if(!r.ok){
				try{
					new Effect.Shake('login');
				}catch(ex){}
				try{
					new Effect.Shake('global_login');
				}catch(ex){}
			}else{
				Success("Logged in... thanks");
				this.start_create();
			}
		}.bind(this));
	},
	
	start_forgot_password:function(){
		$('login').hide();
		$('forgot_password').show();
		$('forgot_email').value = $F('login_email');
	},
	cancel_forgot_password:function(){
		$('login').show();
		$('forgot_password').hide();
		$('login_email').value = $F('forgot_email');
	},
	forgot_password:function(){
		Request(this.controller_path+'/reset_password', {email:$F('forgot_email')}, function(r){
			if(!r.ok){
				new Effect.Shake('forgot_password');
			}else{
				$('forgot_password').hide();
				$('forgot_password_thanks').show();
			}
		}.bind(this));
	},
	
	send_new_password:function(k){
		if($F('password') != $F('password_2')){
			Warn("Your passwords do not match");
			blank('password password_2');
			new Effect.Shake('new_password');
			return;
		}
		Request(this.controller_path+'/do_reset_password', {key:k,password:$F('password')}, function(r){
			if(!r.ok){
				new Effect.Shake('new_password');
			}else{
				window.open('/careers','_self');
			}
		}.bind(this));
	},
	

	
	kjsfd:function(){}
});
var Careers = new _Careers('careers', '/careers');























var Apply = {
	controller_path:'/apply',
	advocate_key:null,
	
	signup:function(){
		var args = {};
		if($F('signup_password') != $F('signup_password_2')){
			Warn("Your passwords do not match");
			blank('signup_password signup_password_2');
			new Effect.Shake('signup');
			return;
		}
		Request(this.controller_path+'/signup', {email:$F('signup_email'),password:$F('signup_password')}, function(r){
			if(!r.ok){
				new Effect.Shake('signup');
			}else{
				window.location.reload();
			}
		}.bind(this));
	},
	
	login:function(){
		Request(this.controller_path+'/login', {email:$F('login_email'),password:$F('login_password')}, function(r){
			if(!r.ok){
				try{
					new Effect.Shake('login');
				}catch(ex){}
				try{
					new Effect.Shake('global_login');
				}catch(ex){}
			}else{
				window.location.reload();
			}
		}.bind(this));
	},
	exit:function(){
		window.open('/apply/logout','_self');
	},
	focus:function(o){
		if($F(o)=='Email' || $F(o)=='password'){
			o.value = '';
		}
	},
	show_inline:function(){
		$('login-tagg').hide();
		$('real-login').show();
		$('global_login').addClassName('highlighted');
	},
	start_forgot_password:function(){
		$('login').hide();
		$('forgot_password').show();
		$('forgot_email').value = $F('login_email');
	},
	cancel_forgot_password:function(){
		$('login').show();
		$('forgot_password').hide();
		$('login_email').value = $F('forgot_email');
	},
	forgot_password:function(){
		Request(this.controller_path+'/reset_password', {email:$F('forgot_email')}, function(r){
			if(!r.ok){
				new Effect.Shake('forgot_password');
			}else{
				$('forgot_password').hide();
				$('forgot_password_thanks').show();
			}
		}.bind(this));
	},
	forgot_password_inline:function(){
		Request(this.controller_path+'/reset_password', {email:$F('login_email_i')}, function(r){
			if(!r.ok){
				new Effect.Shake('global_login');
			}else{
				$('real-login').update("<div style='color:#fff;padding:4px;'>"+r.html+"</div>");
			}
		}.bind(this));
	},
	login_inline:function(){
		Request(this.controller_path+'/login', {email:$F('login_email_i'),password:$F('login_password_i')}, function(r){
			if(!r.ok){
				try{
					new Effect.Shake('login');
				}catch(ex){}
				try{
					new Effect.Shake('global_login');
				}catch(ex){}
			}else{
				window.location.reload();
			}
		}.bind(this));
	},
	send_new_password:function(k){
		if($F('password') != $F('password_2')){
			Warn("Your passwords do not match");
			blank('password password_2');
			new Effect.Shake('new_password');
			return;
		}
		Request(this.controller_path+'/do_reset_password', {key:k,password:$F('password')}, function(r){
			if(!r.ok){
				new Effect.Shake('new_password');
			}else{
				window.open('/apply','_self');
			}
		}.bind(this));
	},
	
	
	switch_tab:function(o,nm){
		o=$(o);
	  var outer = $('apply');
	  
    outer.select('.tabs a').each(function(t){t.removeClassName('selected');});
    o.addClassName('selected');
		
    outer.select('.poster_tab_area').each(function(t){
      if(t.hasClassName('p_tab_area_'+nm)){
        t.show();
      }else{
        t.hide();
      }
    });
	},
	
	save_info:function(){
		var iargs = {};
    prefix = 'edit_user_';
    $w('name address address_2 email city state zip phone organization title country is_permanent_resident citizenship dob').each(function(t){
      iargs[t.toString()] = $F(prefix+t);
    });
		prefix = 'edit_petition_';
    $w('advocate_name advocate_email advocate_institution ccus_experience').each(function(t){
      iargs[t.toString()] = $F(prefix+t);
    });
		
		Request(this.controller_path+'/save_info', iargs, function(r){
			if(r.ok){
				Notice.flash_ok('content');
			}else{
				Notice.flash_error('content');
			}
			showif('advocate_broadcast',r.allow_advocate_broadcast);
		}.bind(this));
		
	},
	
	send_advocate_email:function(o){
		Loading('content');
		Request(this.controller_path+'/get_advocate_name', {}, function(r){
			Loaded('content');
			Dialogue.show({html:['We will email '+r.name+', at '+r.email+', asking for a letter.', r.name+' will be able to upload a PDF or type one in manually.'],
				img:'/images/icons/64/ok.png',
				on_ok:function(){
					Request(Apply.controller_path+'/send_advocate_email', {}, function(r){
						if(r.ok){
							Notice.flash_ok('content');
							$(o).hide();
						}else{
							Notice.flash_error('content');
						}
					}.bind(this));
				},
				ok_label:'Send link'
			});
		}.bind(this));
	},
	
	start_submit:function(){
		Dialogue.show({html:['Are you sure you want to submit this application?', 'You will not be able to make any changes once you submit.'],
			img:'/images/icons/64/ok.png',
			on_ok:function(){
				Request(Apply.controller_path+'/submit_app', {}, function(r){
					if(r.ok){
						Notice.flash_ok('content');
						window.location.reload();
					}else{
						Notice.flash_error('content');
					}
				}.bind(this));
			},
			ok_label:'Submit now'
		});
	},
	
	save_advocate_text:function(key){
		Request(this.controller_path+'/save_advocate_text', {key:this.advocate_key,letter:$F('letter_text')}, function(r){
			if(r.ok){
				window.open("/apply/provide_a_reference?key="+key,'_self');
			}else{
				Notice.flash_error('content');
			}
		}.bind(this));
	},
	
	save_advocate_file:function(){
		
	},
	
	post_upload:function(mode,id){
		window.location.reload();
	},
	post_advocate_upload:function(key,id){
		window.open("/apply/provide_a_reference?key="+key,'_self');
	},
	
	remove_doc:function(mode){
		Loading('body');
		Request(this.controller_path+'/remove_doc', {mode:mode}, function(r){
			window.location.reload();
		}.bind(this));
	},
	
	submit_advocation:function(key){
		Request(this.controller_path+'/finalize_advocation', {key:key}, function(r){
			window.location.reload();
		}.bind(this));
	},
	change_advocation:function(key){
		window.open("/apply/provide_a_reference?key="+key+"&force_edit=1",'_self');
	}
	
	
	
	
}






var Program = {
	change:function(o){
		var id = $F(o);
		$$('#the_program .one').each(function(t){t.hide();});
		$('schedule_'+id).show();
		$('program-print-headline').update("Agenda for "+id);
	}
}







var Survey = {
	controller_path:'/survey',
	save_args:function(){
		args = [];
		i=0;
		$('survey-form').select('.question').each(function(t){
			var style = t.className.gsub("question","");
			style = style.gsub("pending","");
			style = style.gsub("required-q","");
			style = style.strip();
			var qid = t.id.gsub("question_","");
			args[i] = {style:style, question_id:qid};
			
			if(style=="radio" || style=="ranking"){
				var sel = null;
				t.select('input[type="radio"]').each(function(ig){
					if(ig.checked){
						sel = ig.value;
					}
				});
				args[i].selected_option_id = sel;
			}else if(style=="check"){
				var opts = [];
				var k = 0;
				t.select('input[type="checkbox"]').each(function(chk){
					opts[k] = [chk.value,chk.checked];
					k+=1;
				});
				args[i].options = opts;
			}else if(style=="text"){
				args[i].text = t.select('textarea')[0].value;
			}
			i+=1;
		});
		return {survey_type_id:this.survey_type_id,data:args};
	},
	save:function(cbk){
		args = this.save_args();
		
		Request(this.controller_path+'/save', args, function(r){
			if(cbk!=null && cbk!=undefined){
				cbk.apply(this);
			}
		}.bind(this));
	},
	
	
	
	start_add_new_question:function(){
		Poster.show('new_question');
	},
	add_new_question:function(){
		args = {question:$F('new_question_question'),style:$F('new_question_style'),description:$F('new_question_description'),required:$('new_question_required').checked,survey_type_id:this.survey_type_id};
		Request(this.controller_path+'/save_new_question', args, function(r){
			if(r.ok){
				$('all-questions').update(r.html);
				Poster.hide('new_question');
			}else{
				Notice.flash_error($('poster_new_question').select('.poster_content')[0]);
			}
		}.bind(this));
	},
	load_new_question:function(callback, target_area){
		var inspect_args = {};

		Request(this.controller_path+'/add_new_question', inspect_args, function(r){
			Loaded(target_area);
			if(r.ok){
				$(target_area).update(r.html);
				callback.apply(this, [true, r, null]);
			}else{
				$(target_area).update(r.msg);
				callback.apply(this, [false, r, null]);
			}
		}.bind(this));
	},
	
	cancel_edit:function(){
		this.id=-1;
		Poster.hide('edit_question');
	},
	start_edit_question:function(o,id){
		this.id=id;
		Poster.show('edit_question');
	},
	save_question:function(){
		args = {id:this.id, question:$F('edit_question_question'),style:$F('edit_question_style'),description:$F('edit_question_description'),required:$('edit_question_required').checked};
		
		var opts = [];
		try{
			$('options').select("input").each(function(t){
				opts.push([t.id.gsub('option_',''),t.value]);
			});
		}catch(ex){
			
		}
		args.survey_type_id = this.survey_type_id;
		args.options = opts;
		
		Request(this.controller_path+'/save_edit_question', args, function(r){
			if(r.ok){
				$('all-questions').update(r.html);
				$('all-questions').select('.just-changed').each(function(t){
					Notice.flash_ok(t);
				});
				Poster.hide('edit_question');
			}else{
				Notice.flash_error('all-questions');
			}
		}.bind(this));
	},
	load_edit_question:function(callback, target_area){
		Request(this.controller_path+'/view_edit_question', {id:this.id}, function(r){
			Loaded(target_area);
			if(r.ok){
				$(target_area).update(r.html);
				callback.apply(this, [true, r, null]);
			}else{
				$(target_area).update(r.msg);
				callback.apply(this, [false, r, null]);
			}
		}.bind(this));
	},
	start_delete_question:function(id){
		this.id = id;
		Dialogue.show({img:'/images/edit/remove_64.png',html:["Are you sure you want to remove this question?"],
			on_ok:function(){
				Request(this.controller_path+'/delete_question', {id:id}, function(r){
					window.location.reload();
				}.bind(this));
			}.bind(this),
			ok_label:"Delete"
		});
	},
	
	
	start_add_new_type:function(){
		Dialogue.show({img:'/images/icons/64/info.png',html:["A new survey type lets you add more questions that will show up only under this new type."],
			on_ok:function(){
				var name = prompt("Name of new survey type");
				Request(this.controller_path+'/add_new_type', {text:name}, function(r){
					if(r.ok){
						window.location.reload();
					}
				}.bind(this));
			}.bind(this),
			ok_label:"Add one"
		});
	},
	remove_option:function(o,oid){
		this.oid = oid;
		Dialogue.show({img:'/images/edit/remove_64.png',html:["Are you sure you want to remove this option?","People will no longer be able to select '" + o.up().select('input')[0].value+"'"],
			on_ok:function(){
				Request(this.controller_path+'/delete_option', {id:this.oid}, function(r){
					var ox = o.up();
					ox.innerHTML = '';
					ox.hide();
				}.bind(this));
			}.bind(this),
			ok_label:"Delete"
		})
	},
	make_new_option:function(){
		var name = prompt("Text for new option");
		if(name != null && name != undefined && name !=''){
			Request(this.controller_path+'/add_new_option', {id:this.id,text:name}, function(r){
				if(r.ok){
					$('options').insert({after:r.html});
				}
			}.bind(this));
		}
	},
	type_id:null,
	switch_type:function(o){
		Loading('body');
		this.save(function(){
			Survey.survey_type_id = $F(o);
			Survey.refresh_survey();
		});
	},
	refresh_survey:function(){
		Request(this.controller_path+'/inspect', {survey_type_id:this.survey_type_id}, function(r){
			Loaded('body');
			if(r.ok){
				$('all-questions').update(r.html);
				$('all-questions').select('.just-changed').each(function(t){
					Notice.flash_ok(t);
				});
			}else{
				Notice.flash_error('all-questions');
			}
		}.bind(this));
	}
	
}
















































































