


var SDNavigation = {
	open:function(n,o){
		try{
			o=$(o);
		}catch(ex){}
		$('sd_links_outer').select('a').each(function(t){
			t.removeClassName('nav_selected_child_item');
		});
		try{
			o.addClassName('nav_selected_child_item');
		}catch(ex){}
		$$('.app_main_pane_inner').each(function(t){t.hide();});
		try{
			$('app_'+n).show();
		}catch(ex){}
	},
	svc_home:function(){
		this.open('home');
	},
	gift_home:function(){
		this.open('gift_home');
	}
}

var SignUp = {
	controller_path:'/sound_delivery/security',
	
	mode:'',
	$code:'',
	$pcode:'',
	$email:'',
	$name:'',
	promo_code:null,
	take_code:function(){
		var code = $F('sign_up_code');
		var pcode = $F('sign_up_promocode');
		this.mode = 'owner';
		
		if(code==''){
			this.code_error('You must enter a code to continue!');
			return;
		}else{
			this.code_clear_error();
			Request(this.controller_path+'/take_code', {code:code, pcode:pcode}, function(r){
				if(r.ok){
					if(r.valid_pcode){
						Dialogue.show({html:['30 free minutes applied to your account!','Thanks for reading Well Being, and enjoy your new Sound Beginnings!'],
							ok_label:'Continue',
							on_ok:function(){
								this.code_clear_error();
								$('signup_minute_name_teaser').show();
								this.show_name_email_input();
								this.$code = code;
								this.$pcode = pcode;
							}.bind(this),
							suppress_cancel_button:true
						});
					}else if(r.invalid_pcode){
						Dialogue.show({html:['Unknown promo code','Please try again, or continue without a promo code'],
							ok_label:'Continue without promo code',
							on_ok:function(){
								this.code_clear_error();
								$('signup_minute_name_teaser').show();
								this.show_name_email_input();
								this.$code = code;
							}.bind(this),
							cancel_label:'Try another code'
						});
					}else{
						this.code_clear_error();
						$('signup_minute_name_teaser').show();
						this.show_name_email_input();
						this.$code = code;
					}
				}else{
					this.code_error(r.msg);
				}
			}.bind(this));
		}
	},
	code_error:function(msg){
		Notice.shake('signup_code_first_entry');
		Notice.flash_error('sign_up_code');
		$('invalid_code_notice').update(msg);
		$('invalid_code_notice').show();
	},
	code_clear_error:function(){
		$('invalid_code_notice').hide();
		$('invalid_code_notice').update('');
	},
	
	
	
	
	show_name_email_input:function(){
		$('welcome_desc_outer').hide();
		$('app_login_or_sign_up').removeClassName('app_login_or_sign_up_padded');
		$('signup_code_first_entry').hide();
		$('signup_other_first_steps').hide();
		$('signup_name_email_entry').show();
	},
	save_name_email:function(){
		
		var fname = $F('signup_fname');
		var lname = $F('signup_lname');
		
		var name = fname + ' ' + lname;
		
		var email = $F('signup_email');
		var email2 = $F('signup_email_2');
		var password = $F('signup_password');
		var password2 = $F('signup_password_2');
		
		var passed = true;
		var err = '';
		
		this.$email = email;
		this.$name = name;
		
		
		if($('signup_tos_agree').checked != true){
			passed = false;
			err = 'You must read and accept Sound Delivery\'s terms of service.';
		}
		
		if(fname=='' || lname==''){
			passed = false;
			err = 'You must enter your first and last name.';
		}
		
		if(email==''){
			passed = false;
			err = 'You must enter an email.';
		}
		
		if(password==''){
			passed = false;
			err = 'You must enter a password.';
		}
		
		if(password != password2){
			passed = false;
			err = 'Your passwords do not match, please try again.';
			$('signup_password').value = '';
			$('signup_password_2').value = '';
		}
		
		if(email != email2){
			passed = false;
			err = 'Your emails do not match, please try again.';
			$('signup_email').value = '';
			$('signup_email_2').value = '';
		}
		
		var promo_code = this.promo_code;
		
		if(passed){
			Request(this.controller_path+'/sign_up', {
				code:this.$code,
				pcode:this.$pcode,
				mode:this.mode,
				name:name,
				email:email,
				password:password,
				promo_code:promo_code
			}, function(r){
				if(r.ok){
					this.hide_error_name_email();
					this.hide_name_email();
					this.show_registry_signup();
					$('sign_up_main_headline').update('Signing up ' + r.name);
				}else{
					this.show_error_name_email(r.msg);
				}
			}.bind(this));
		}else{
			this.show_error_name_email(err);
		}
	},
	hide_name_email:function(){
		$('signup_name_email_entry').hide();
	},
	show_error_name_email:function(err){
		Notice.shake('signup_name_email_entry');
		$('signup_form_error').update(err);
		$('signup_form_error').show();
	},
	hide_error_name_email:function(){
		$('signup_form_error').update('');
		$('signup_form_error').hide();
	},
	
	
	
	_get_reg_fields:function(){
		var ctr = $('signup_registry');
		var fields = {};
		
		ctr.select('input[type="text"]').each(function(t){
			fields[t.id.replace('reg_','')] = $F(t);
		});
		
		ctr.select('select').each(function(t){
			fields[t.id.replace('reg_','')] = $F(t);
		});
		
		return fields;
	},
	
	show_registry_signup:function(){
		if(this.mode=='gift'){
			this.show_thanks('giftuser@domain.com');
		}else{
			$('signup_registry').show();
		}
	},
	save_registry:function(){
		
		var reg_data = this._get_reg_fields();
		
		Request(this.controller_path+'/save_registry', reg_data, function(r){
			if(r.ok){
				this.hide_error_registry();
				this.hide_registry();
				this.show_thanks(this.$email);
			}else{
				var failed_ids = r.invalid_ids;
				
				//flash all problems
				$A(failed_ids).each(function(t){
					if($('reg_'+t) != null){
						var problem_element = $('reg_'+t);
						Notice.flash_error(problem_element);
					}
				});
				
				this.show_error_registry(r.msg);
			}
		}.bind(this));
		
		
	},
	skip_registry:function(){
		Dialogue.show({html:['Are you sure you want to skip adding yourself to the registry?', 'Friends and family will not be able to send you gift minutes if you are not in the registry.','You can always add yourself later if you choose to skip this step now.'],
			img:'/images/sound_delivery/vcard_x_64.png',
			ok_label:'Skip registry sign up',
			on_ok:function(){
				this.hide_error_registry();
				this.hide_registry();
				this.show_thanks(this.$email, this.$name);
			}.bind(this),
			cancel_label:'Join the registry now'
		});
	},
	hide_registry:function(){
		$('signup_registry').hide();
	},
	show_error_registry:function(err){
		Notice.shake('signup_registry');
		$('signup_reg_form_error').update(err);
		$('signup_reg_form_error').show();
	},
	hide_error_registry:function(){
		$('signup_reg_form_error').update('');
		$('signup_reg_form_error').hide();
	},
	
	
	show_thanks:function(email, name){
		$('app_login_or_sign_up').style.background = 'none';
		$('signup_thanks').show();
		$('sign_up_main_headline').hide();
		$('log_in_menu').hide();
		
		window.location.reload();
	},
	
	
	start_gift_signup:function(){
		this.mode = 'gift';
		this.show_name_email_input();
	},
	start_service_signup:function(){
		this.mode = 'service';
    this.promo_code = $F('sign_up_promocode_2');
    
    if(this.promo_code==null || this.promo_code == ''){
      $('signup_minute_name_teaser').hide();
  		this.show_name_email_input();
    }else{
      Request(this.controller_path+'/take_promo_code_no_prod', {key:this.promo_code}, function(r){
  			if(r.ok){
  				Dialogue.show({html:[r.msg],
    				ok_label:'Continue',
    				on_ok:function(){
    					$('signup_minute_name_teaser').hide();
          		this.show_name_email_input();
    				}.bind(this),
    				suppress_cancel_button:true
    			});
  			}else{
  				Dialogue.show({html:['Unknown promo code','Please try again, or continue without a promo code'],
    				ok_label:'Continue without promo code',
    				on_ok:function(){
    					$('signup_minute_name_teaser').hide();
    					this.promo_code = null;
          		this.show_name_email_input();
    				}.bind(this),
    				cancel_label:'Try another code'
    			});
  			}
  		}.bind(this));
    }
    
		
	}
}





var LogIn = {
	controller_path:'/sound_delivery/security',
	
	login:function(){
		Request(this.controller_path+'/login', {email:$F('log_in_email'), password:$F('log_in_password')}, function(r){
			if(r.ok){
				window.location.reload();
			}else{
				Notice.respond(r);
				Notice.shake('login_menu_inner');
			}
		}.bind(this));
		
	},
	start_forgot_password:function(){
		$('reset_password_menu').show();
		$('log_in_menu').hide();
		$('reset_password_email').value = $F('log_in_email');
	},
	send_reset_password:function(){
		var em = $F('reset_password_email');
		
		Request(this.controller_path+'/request_reset_password', {email:em}, function(r){
			if(r.ok){
				$('reset_password_menu').update('<h2>Check ' + em + ' for a link to reset your password.</h2>');
				setTimeout(LogIn.cancel_reset_password, 3000);
			}else{
				Dialogue.show({html:['We could not find an account for ' + em + '!', 'You may have registered with another email address. Please check your email for typos, or try again with another email.'], suppress_cancel_button:true});
			}
		}.bind(this));
	},
	cancel_reset_password:function(){
		$('reset_password_menu').hide();
		$('log_in_menu').show();
	}
}




var _Registry = Class.create(PosterController, {
  initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'id asc';
  },

	//start custom
	search:function(){
		var text = $F('registry_search');
		$('reg_search_results').show();
		
		if(text==''){
			this.error_reg_search('You must enter a name, address, phone number, or email to search.');
		}else{
			Page.start_spinning('reg_search_results');
			Request(this.controller_path+'/list', {search:text}, function(r){
				Page.stop_spinning('reg_search_results');
				if(r.ok){
					$('reg_search_results').removeClassName('no_results_found');
					$('reg_search_results').update(r.html);
					Notice.flash_text_ok('reg_search_results');
				}else{
					this.error_reg_search(r.msg);
				}
			}.bind(this));
			
		}
	},
	error_reg_search:function(msg){
		Notice.shake('reg_srch_box_outer');
		Notice.flash_error('registry_search');
		$('reg_search_results').addClassName('no_results_found');
		$('reg_search_results').update('<h1>'+msg+'</h1>');
	},
	send_gift:function(){
		Poster.hide('registry');
		Pay.open_gift_buy(this.$id);
	},
	after_inspect:function(inspect_r){
		if(inspect_r.ok){
			$('reg_search_inspect_send_gift').show();
			$('minutes_send_a_gift_to_title').update(inspect_r.giftee_name);
			$('minutes_send_a_gift_to_title_2').update(inspect_r.giftee_name);
			$('minutes_send_a_gift_to_title_3').update(inspect_r.giftee_name);
		}else{
			$('reg_search_inspect_send_gift').hide();
		}
	}
});


var Registry = new _Registry('registry', '/sound_delivery/registry');










var _Transactions = Class.create(PosterController, {
	initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'created_at desc';
  },
	_refresh_list_extra_data:function(){
		var srch = $F('recording_list_home_search');
		if(srch==this.default_search_box_text){
			srch = '';
		}
		return {show_deleted:$('recordings_show_deleted').checked, search:srch};
	}
});
var Transactions = new _Transactions('transactions', '/sound_delivery/transactions');





var Pay = {
	controller_path:'/sound_delivery/pay',
	$reg_id:-1,
	
	open_gift_buy:function(re_id){
		this.$reg_id = re_id;
		SDNavigation.open('buy_minutes', null);
	},
	open_buy:function(re_id){
		SDNavigation.open('buy_minutes', null);
	},
	
	cancel_buy:function(){
		SDNavigation.open('minutes', $('top_tab_minutes'));
		Transactions.refresh_list();
	},
	change_amount:function(o){
		var amount = $F(o).split('|')[0];
		var cost = $F(o).split('|')[1];
		
		$('buy_minutes_cost_preview').update('$'+cost);
		$('buy_min_total_cost').update('$'+cost);
	},
	
	
	
	
	show_payment_processing:function(){
		Hide('app_buy_minutes_desc app_buy_minutes_normal_headline minutes_payment_buttons_2 minutes_payment_buttons minutes_payment_options minute_buy_table_outer');
		$('buy_min_spinner').show();
	},
	hide_payment_processing:function(){
		Show('minute_buy_table_outer minutes_payment_options minutes_payment_options minutes_payment_buttons minutes_payment_buttons_2 buy_mins_paypal_buttons app_buy_minutes_normal_headline app_buy_minutes_desc');
		$('buy_min_spinner').hide();
	},
	handle_payment_response:function(r){	//called from express and direct
		//in case its not a gift
		try{
			$('minutes_send_a_gift_to_title_2').update(r.reg_name);
			$('minutes_send_a_gift_to_title').update(r.reg_name);
		}catch(ex){}
		if(!r.ok){
			Notice.shake('app_buy_minutes');
			$('buy_minutes_error_top').update(r.msg);
			Show('buy_minutes_error_top buy_mins_paypal_buttons app_buy_minutes_desc app_buy_minutes_normal_headline');
		}else{
			$('buy_minutes_error').hide();
			$('buy_minutes_error_top').hide();
			$('buy_minutes_error').update('');
			$('app_buy_minutes').hide();
			$('app_buy_minutes_receipt').show();
			$('min_thanks_receipt_outer').update(r.receipt);
		}
	},
	
	
	start_express:function(){
		var amount = $F('buy_minutes_number').split('|')[0];
		var cost = $F('buy_minutes_number').split('|')[1];
		var min_bucket_id = $F('buy_minutes_number').split('|')[2];
		
		Dialogue.show({
			html:['You will be charged $' + cost + ' for ' + amount + ' minutes.', 'Next you will be sent to PayPal\'s web site to complete your purchase.'],
			on_ok:function(){
				if($('buy_email') != null){
					var buy_email = $F('buy_email');
				}else{
					var buy_email = '';
				}
				Request(this.controller_path+'/start_express', {min_bucket_id:min_bucket_id, registry_entry_id:this.$reg_id, email:buy_email}, function(r){
					if(r.ok){
						window.open(r.url, '_self');
					}else{
						this.show_buy_error(r.msg);
					}
				}.bind(this));
			}.bind(this),
			ok_label:'Buy now',
			img:'/images/sound_delivery/card_machine.png'
		});
	},
	do_express_async:function(token, payer_id){
		this.show_payment_processing();
		Request(this.controller_path+'/finalize_express_payment', {token:token, payer_id:payer_id}, function(r){
			if(r.gift){
				$('sd_links_outer').hide();
			}
			
			this.hide_payment_processing();
			this.handle_payment_response(r);
		}.bind(this));
	},
	
	
	show_cancelled:function(){
		
	},
	
	
	show_buy_error:function(msg){
		Notice.shake('app_buy_minutes');
		$('buy_minutes_error').show();
		$('buy_minutes_error').update(msg);
	},
	clear_buy_error:function(){
		$('buy_minutes_error').hide();
		$('buy_minutes_error').update('');
	},
	
	
	
	
	show_direct:function(){
		var mid = $F('buy_minutes_number').split('|')[2];
		Request(this.controller_path+'/switch_to_ssl', {mid:mid, gift:this.$reg_id}, function(r){
			window.open(r.url, '_self');
		}.bind(this));
	},
	
	start_direct:function(){
		var amount = $F('buy_minutes_number').split('|')[0];
		var cost = $F('buy_minutes_number').split('|')[1];
		var min_bucket_id = $F('buy_minutes_number').split('|')[2];
		
		Dialogue.show({
			html:['You will be charged $' + cost + ' for ' + amount + ' minutes.', 'Your card will be charged as soon as you click the Buy now button below.'],
			on_ok:function(){
				
				//since paypal might take a while hide and wait
				Hide('app_buy_minutes_desc app_buy_minutes_normal_headline minutes_payment_buttons_2 minutes_payment_buttons minutes_payment_options minute_buy_table_outer')
				
				$('buy_min_spinner').show();
				
				var args = this._get_pay_fields();
				args['min_bucket_id'] = min_bucket_id;
				
				args['registry_entry_id'] = this.$reg_id;

				Request(this.controller_path+'/start_direct', args, function(r){
					//put back page elements
					Show('app_buy_minutes_desc app_buy_minutes_normal_headline minute_buy_table_outer minutes_payment_options minutes_payment_buttons minutes_payment_buttons_2 minutes_payment_options');
					
					$('buy_min_spinner').hide();
					
					if(!r.ok){
						this.show_buy_error(r.msg);
					}else{
						this.clear_buy_error();
						$('app_buy_minutes').hide();
						$('app_buy_minutes_receipt').show();
						$('min_thanks_receipt_outer').update(r.receipt);
						//hide tabs to get them to click out to the http site
						$('sd_links_outer').hide();
					}
				}.bind(this));
				
				
			}.bind(this),
			ok_label:'Buy now',
			img:'/images/sound_delivery/card_machine.png'
		});
		
	},
	
	
	
	_get_pay_fields:function(){
		var ctr = $('minutes_payment_options');
		var fields = {};
		
		ctr.select('input[type="text"]').each(function(t){
			fields[t.id.replace('buy_','')] = $F(t);
		});
		
		ctr.select('select').each(function(t){
			fields[t.id.replace('buy_','')] = $F(t);
		});
		
		return fields;
	}
}
























































var _Recordings = Class.create(PosterController, {
  
	message_title:'',
	lock:false,
	default_search_box_text:'Search by title/caller/notes',
	
	initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'created_at desc';
  },
	
	
	
	
	
	
	after_inspect:function(inspect_r){
		if(inspect_r.ok){
			this.message_title = inspect_r.title;
			if(this.message_title==null) this.message_title = 'this';
			
			if(inspect_r.deleted){
				this.mark_poster_deleted();
				$('recordings_save_changes_button').hide();
			}else{
				this.unmark_poster_deleted();
				$('recordings_save_changes_button').show();
			}
		}else{
			
		}
	},
	_refresh_list_extra_data:function(){
		var srch = $F('recording_list_home_search');
		if(srch==this.default_search_box_text){
			srch = '';
		}
		
		return {show_deleted:$('recordings_show_deleted').checked, search:srch};
		
	},
	
	
	
	
	load_preview:function(callback, the_content_area){
		var inspect_args = {};
		inspect_args['id'] = this.$id;
		
		Request(this.controller_path+'/inspect_preview', inspect_args, function(r){
			Notice.respond(r);
			Page.stop_spinning(the_content_area);
			if(r.ok){
				$(the_content_area).update(r.html);
				callback.apply(this,[true, r, this._after_load_preview]);
			}else{
				$(the_content_area).update(r.msg);
				callback.apply(this, [false, r, this._after_load_preview]);
			}
		}.bind(this));
	},
	_after_load_preview:function(){
		Poster.fit_poster_to_content('poster_dynamic_play_audio');
	},
	stop_preview:function(){
		//for IE we have to clear the movie object
		$('preview_audio_objects').update('');
	},
	
	preview:function(id){
		this.get_temp_lock();
		this.$id = id;
		Poster.show('dynamic_play_audio');
	},
	
	
	
	
	
	download:function(id){
		this.get_temp_lock();
		window.open('/sound_delivery/recordings/download?id='+id, '_self');
	},
	download_new:function(){
		Request(this.controller_path+'/allow_downloads', {kind:'new'}, function(r){
			Notice.respond(r);
			if(r.ok){
				window.open('/sound_delivery/recordings/download_new', '_self');
			}
		}.bind(this));
	},
	download_all:function(){
		Request(this.controller_path+'/allow_downloads', {kind:'all'}, function(r){
			Notice.respond(r);
			if(r.ok){
				window.open('/sound_delivery/recordings/download_all', '_self');
			}
		}.bind(this));
	},
	
	
	
	
	
	
	get_temp_lock:function(){
		this.lock = true;
		setTimeout(function(){this.lock = false;}.bind(this), 200);
	},
	inspect:function($super, o, id){
		if(this.lock) return;//locked bc they clicked on the dl link or the preview link
		$super(o, id);
	},
	get_editing_args:function(){
		return {title:$F('message_title'), message:$F('message_notes')};
	},
	after_save:function(r){
		if(r.ok){
			this.flash_inspector_ok();
			this.refresh_list();
		}else{
			this.flash_inspector_error();
		}
	},
	
	
	
	
	
	start_delete:function(){
		Dialogue.show({
			html:['Are you sure you want to delete ' + this.message_title + '?','You will not be able to download this message again.'],
			on_ok:function(){
				//delete it
				Request(this.controller_path+'/delete', {id:this.$id}, function(r){
					this.close_inspect();
					this.refresh_list();
				}.bind(this));
			}.bind(this),
			ok_label:'Delete',
			img:'/images/sound_delivery/delete_64.png'
		});
	},
	start_restore:function(){
		Dialogue.show({
			html:['Are you sure you want to restore ' + this.message_title + '?','You will then be able to download this recording again.'],
			on_ok:function(){
				//delete it
				Request(this.controller_path+'/restore', {id:this.$id}, function(r){
					this.close_inspect();
					this.refresh_list();
				}.bind(this));
			}.bind(this),
			ok_label:'Restore',
			img:'/images/sound_delivery/restore_64.png'
		});
	},
	
	
	
	
	
	
	
	//if nothing was saved then we do it as-is; if there is something saved we load the html
	//from the server
	inspect_number:function(o, recording_id){
		this.get_temp_lock();
		Callers.inspect(o, recording_id)
	}
	
});


var Recordings = new _Recordings('recordings', '/sound_delivery/recordings');































var _Callers = Class.create(PosterController, {
  
	initialize: function($super, name, controller_path) {
    $super(name, controller_path);
		this.order_by = 'id asc';
  },
	
	
	//fake this out since the list the user sees with this info is the one
	//with recordings
	refresh_list:function(){
		Recordings.refresh_list();
	},
	after_inspect:function(r){
		if(r.assigned){
			if(BrowserDetect.ie6){
				$('caller_save_changes_button').value = 'Save changes';
			}else{
				$('caller_save_changes_button').down().down().update('Save changes');
			}
		}else{
			if(BrowserDetect.ie6){
				$('caller_save_changes_button').value = 'Assign number';
			}else{
				$('caller_save_changes_button').down().down().update('Assign number');
			}
		}
	},
	
	
	
	get_editing_args:function(){
		return {manual_name:$F('assign_number_name_entry'), pb_name:$F('assign_number_name_drop')};
	},
	after_save:function(r){
		if(r.ok){
			this.close_inspect();
			this.refresh_list();
		}else{
			this.flash_inspector_error();
		}
	},
	
	
	handle_number_assign_change:function(o){
		o=$(o);
		if($F(o)=='' && $F('assign_number_name_entry')==''){
			// $('assign_number_manual_text_controls').setOpacity(1.0);
			// $('assign_number_auto_controls').setOpacity(1.0);
		}else if($F(o)==''){
			this.focus_manual_assign_text();
		}else{
			this.focus_auto_assign_drop();
		}
	},
	close_assign_number:function(){
		Sheet.hide_this_one('sheet_assign_number');
		$('assign_number_name_drop').reset();
		$('assign_number_name_entry').reset();
		// $('assign_number_manual_text_controls').setOpacity(1.0);
	},
	start_assign_number:function(){
		var name_text = $F('assign_number_name_entry');
		var name_choice_id = $F('assign_number_name_drop');
		
		if(name_text=='' && name_choice_id==''){
			Notice.flash_error('assign_number_inner_area');
			Notice.warn_text('You must type a name or select a person from the list.');
			return;
		}
		
		this.do_assign_number();
	},
	do_assign_number:function(){
		if(false){
			Notice.flash_error('assign_number_inner_area');
			Notice.warn_text('server error');
		}else{
			this.close_assign_number();
			Notice.text('You cannot save changes yet.');
		}
	},
	handle_focus_manual_assign_text:function(){
		if($F('assign_number_name_entry')==''){
			// $('assign_number_manual_text_controls').setOpacity(1.0);
			// $('assign_number_auto_controls').setOpacity(1.0);
		}else{
			this.focus_manual_assign_text();
		}
	},
	focus_manual_assign_text:function(){
		$('assign_number_name_drop').reset();
		// $('assign_number_manual_text_controls').setOpacity(1.0);
		// $('assign_number_auto_controls').setOpacity(0.25);
	},
	focus_auto_assign_drop:function(){
		$('assign_number_name_entry').reset();
		//$('assign_number_manual_text_controls').setOpacity(0.25);
		// $('assign_number_auto_controls').setOpacity(1.0);
	}
	
});


var Callers = new _Callers('callers', '/sound_delivery/callers');








var DownloadHelp = {
	itunes_mac:function(o){
		o=$(o);
		$('help_dl_itunes_mac').show();
		$('help_dl_itunes_win').hide();
		o=$(o);
		o.addClassName('help_platform_tabs_selected');
		o.next().removeClassName('help_platform_tabs_selected');
	},
	itunes_win:function(o){
		o=$(o);
		$('help_dl_itunes_mac').hide();
		$('help_dl_itunes_win').show();
		o.addClassName('help_platform_tabs_selected');
		o.previous().removeClassName('help_platform_tabs_selected');
	}
}























var Invites = {
	controller_path:'/sound_delivery/invites',
	default_subject:'',
	default_body:'Type an optional custom message here.',
	
	send:function(){
		this.do_send('invite_send_error', 'preview_invite_outer');
	},
	//NOT USED!!:send_from_preview
	send_from_preview:function(){
		this.do_send('invite_send_error', 'invite_outer');
	},
	do_send:function(error_element, outer_element){
		//error checking done on server
		this.check_data_integrity(function(success, msg, body_html, ev){
			if(!success){
				$('send_invite_headline').update('Invite not sent');
				Notice.flash_error(outer_element);
				Notice.shake(outer_element);
				$(error_element).update(msg);
				$(error_element).show();
			}else{
				//send it | it was sent
				Request(this.controller_path+'/send_invite', ev, function(r){
					//go to thanks page even on error since we allow multiple emails and 
					//do not want to re-send ones ever
					$(error_element).hide();
					$('invite_create_message_outer').hide();
					//make sure that this has been updated with the correct preview
					$('invite_preview_message_outer').show();
					$('thanks_for_sending_an_invite').update(r.msg);
					$('thanks_for_sending_an_invite').show();
					$('preview_buttons_lower').hide();
					$('preview_buttons_lower_after_send').show();
					
					if(r.ok){
						$('send_invite_headline').update('Invite sent!');
						Notice.flash_ok('preview_invite_outer');//same as outer_element usually
					}else{
						$('thanks_for_sending_an_invite').hide();
						Notice.flash_error(outer_element);
						Notice.shake(outer_element);
						
						if(r.had_a_success){
							$('send_invite_headline').update('There was a problem with at least one invite');
							$('thanks_for_sending_an_invite').show();
							$('thanks_for_sending_an_invite').update(r.succeeded);
						}else{
							$('send_invite_headline').update('No invites were sent');
						}
						
						$(error_element).update(r.msg);
						$(error_element).show();
					}
				}.bind(this));
				
			}
		});
	},
	//go to the server to see if the supplied data is ok to use
	check_data_integrity:function(callback){
		var ev = this.get_entered_values();
		
		Request(this.controller_path+'/ok_to_preview', ev, function(r){
			callback.apply(this, [r.ok, r.msg, r.body_html, ev]);
		}.bind(this));
	},
	
	get_entered_values:function(){
		var inv_body = $F('invite_body');
		if(inv_body==this.default_body) inv_body = null;
		return {send_to:$F('invite_email'),
						subject:$F('invite_subject'),
						body:inv_body,
						dear:$F('invite_dear'),
						cc_me:$('invite_cc_me').checked
						};
	},
	
	preview:function(){
		this.check_data_integrity(function(success, msg, body_html, ev){
			if(!success){
				Notice.flash_error('invite_outer');
				Notice.shake('invite_outer');
				$('invite_send_error').update(msg);
				$('invite_send_error').show();
				return;
			}else{
				$('invite_send_error').hide();
				$('send_invite_headline').update('Preview your invitation');
				
				if(ev.body==this.default_body){
					$('preview_msg_body_preview').hide();
					$('preview_msg_body_preview').update('');
				}else{
					$('preview_msg_body_preview').show();
					$('preview_msg_body_preview').update(body_html);
				}
				
				$('preview_subject').update(ev.subject);
				$('preview_email_to').update(ev.send_to);
				
				if(ev.dear==''){
					$('preview_dear').update("Hello");
				}else{
					$('preview_dear').update("Dear "+ev.dear);
				}
				
				if(ev.cc_me){
					$('preview_send_cc').update('Sending you a copy of this invitation');
				}else{
					$('preview_send_cc').update('Not sending you a copy of this invitation');
				}
				
			}
			$('invite_create_message_outer').hide();
			$('invite_preview_message_outer').show();
		});
	},
	change:function(){
		$('send_invite_headline').update('Invite someone to leave you a message');
		$('invite_create_message_outer').show();
		$('invite_preview_message_outer').hide();
	},
	reset:function(){
		$('invite_email').reset();
		$('invite_dear').reset();
		$('invite_subject').value = this.default_subject;
		$('invite_body').value = this.default_body;
		$('invite_body').addClassName('dim_textarea');
		$('invite_cc_me').checked = true;
		
		$('send_invite_headline').update('Invite someone to leave you a message');
		
		$('preview_buttons_lower_after_send').hide();
		$('preview_buttons_lower').show();
		$('thanks_for_sending_an_invite').hide();
		$('invite_create_message_outer').show();
		$('invite_preview_message_outer').hide();
	},
	
	
	add_multiple:function(){
		Poster.show('add_multiple_emails');
	},
	save_new_multiple:function(){
		var ne = $F('mult_email_new');
		
		if(ne==''){
			Warn('You must enter an email address!');
			return;
		}
		
		if($F('invite_email')==''){
			$('invite_email').value = ne;
		}else{
			$('invite_email').value = $F('invite_email')+', ' + ne;
		}
		$('mult_email_new').reset();
		Poster.hide('add_multiple_emails');
	},
	
	
	focus_body:function(o){
		o=$(o);
		if($F(o)==''){
			o.value = this.default_body;
			o.addClassName('dim_textarea');
		}else if($F(o) == this.default_body){
			o.value = '';
			o.removeClassName('dim_textarea');
		}
	},
	blur_body:function(o){
		o=$(o);
		if($F(o)==''){
			o.value = this.default_body;
			o.addClassName('dim_textarea');
		}else{
			o.removeClassName('dim_textarea');
		}
	},
	keypress_body:function(o){
		if($F(o) != this.default_body){
			o.removeClassName('dim_textarea');
		}
	}
}















var MinuteGoal = {
	callback:null,
	set_custom_minute_goal:function(){
		var goal = $F('custom_min_goal_input');
		
		if(goal != ''){
			Poster.hide('custom_min_goal');
			if(this.callback != null){
				this.callback(goal);
			}
		}else{
			Poster.flash_error('custom_min_goal');
			Warn('You must enter a number of minutes!');
		}
	},
	handle_min_bucket_change:function(o){
		o=$(o);
		if($F(o)=='other'){
			this.callback = function(num){
				var x = $('reg_minutes');
				x.options[x.length] = new Option(num.toString() + ' minutes',num);
				x.selectedIndex = x.length-1;
			}.bind(this);
			Poster.show('custom_min_goal');
		}
	}
}










var Preferences = {
	controller_path:'/sound_delivery/users',
	save_notifications:function(){
		Request(this.controller_path+'/save_notifications', {on:$('email_notifications_send').checked}, function(r){
			Notice.respond(r);
			
			var pci = $('poster_email_notification_prefs').select('.poster_chrome_icon')[0];
			
			if(r.enabled){
				$('email_notification_icon_small').style.backgroundImage = 'url(/images/sound_delivery/email_notifications_on_64.png)';
				pci.src = '/images/sound_delivery/email_notifications_on.png';
			}else{
				$('email_notification_icon_small').style.backgroundImage = 'url(/images/sound_delivery/email_notifications_off_64.png)';
				pci.src = '/images/sound_delivery/email_notifications_off.png';
			}
			
			Poster.hide('email_notification_prefs');
			
		}.bind(this));
	},
	change_email:function(){
		var email = $F('new_email');
		var email_2 = $F('new_email_2');
		
		if(email!=email_2){
			this.error_change_email('Please enter the same email twice.');
			return;
		}
		
		Request(this.controller_path+'/change_email', {email:email}, function(r){
			
			if(r.ok){
				Notice.respond(r);
				Poster.hide('change_email');
				$('new_email_2').reset();
				$('new_email').reset();
			}else{
				this.error_change_email(r.msg);
			}
		}.bind(this));
	},
	error_change_email:function(msg){
		Notice.text(msg);
		$('new_email_2').reset();
		$('new_email').reset();
		Notice.shake('change_email_deck_all');
		Poster.flash_error('change_email');
	},
	
	
	change_name:function(){
		var name = $F('new_name');
		
		if(name == ''){
			this.error_change_name('You must enter a new name');
			return;
		}
		
		Request(this.controller_path+'/change_name', {name:name}, function(r){
			if(r.ok){
				Notice.respond(r);
				Poster.hide('change_name');
				$('new_name').reset();
			}else{
				this.error_change_name(r.msg);
			}
		}.bind(this));
	},
	error_change_name:function(msg){
		Notice.text(msg);
		Notice.shake('change_name_all_deck');
		Poster.flash_error('change_name');
	},
	
	
	change_password:function(){
		var pwd = $F('new_password');
		var pwd_2 = $F('new_password_2');
		
		if(pwd == ''){
			this.error_change_password('Your password may not be blank.');
			return;
		}
		
		if(pwd != pwd_2){
			this.error_change_password('Your entered two different new passwords. Please try again.');
			$('new_password').reset();
			$('new_password_2').reset();
			return;
		}
		
		Request(this.controller_path+'/change_password', {password:pwd}, function(r){
			if(r.ok){
				Notice.respond(r);
				Poster.hide('change_password');
				$('new_password').reset();
				$('new_password_2').reset();
			}else{
				this.error_change_password(r.msg);
			}
		}.bind(this));
	},
	error_change_password:function(msg){
		Notice.text(msg);
		Notice.shake('change_password_all_deck');
		Poster.flash_error('change_password');
	},
	
	
	
	
	
	
	
	
	
	
	
	
	
	change_registry:function(){
		var reg_data = this._get_reg_fields();
		
		Request(this.controller_path+'/change_registry', reg_data, function(r){
			if(!r.ok){
				this.error_change_registry(r.msg);
			}else{
				Notice.respond(r);
				Poster.flash_ok('change_registry');
			}
		}.bind(this));
		
	},
	_get_reg_fields:function(){
		var ctr = $('edit_reg_all_items');
		var fields = {};
		
		ctr.select('input[type="text"]').each(function(t){
			fields[t.id.replace('reg_','')] = $F(t);
		});
		
		ctr.select('select').each(function(t){
			fields[t.id.replace('reg_','')] = $F(t);
		});
		
		return fields;
	},
	error_change_registry:function(msg){
		Warn(msg);
		Notice.shake('edit_reg_all_items');
		Poster.flash_error('change_registry');
	}
	
	
	
	
}










var Support = {
	controller_path:'/sound_delivery/users',
	contact:function(){
		var m = $F('help_message_entry');
		
		Request(this.controller_path+'/contact_support', {message:m}, function(r){
			if(!r.ok){
				Notice.flash_error('help_right');
				Notice.shake('help_right');
				$('help_message_send_error_deck').update(r.msg);
				$('help_message_send_error_deck').show();
			}else{
				Notice.flash_ok('help_right');
				$('help_send_message_controls').update(r.msg);
				$('help_send_message_controls').addClassName('successful_message_sent_help');
			}
		}.bind(this));
		
	},
	feedback:function(){
		var m = $F('help_feedback_entry');
		
		Request(this.controller_path+'/contact_feedback', {message:m, allow_quote:$('feedback_public').checked}, function(r){
			if(!r.ok){
				Notice.flash_error('help_right');
				Notice.shake('help_right');
				$('help_feedback_send_error_deck').update(r.msg);
				$('help_feedback_send_error_deck').show();
			}else{
				Notice.flash_ok('help_right');
				$('help_send_feedback_controls').update(r.msg);
				$('help_send_feedback_controls').addClassName('successful_message_sent_help');
			}
		}.bind(this));
		
	},
	
	
	switch_pane:function(o,n){
		$('help_left').select('a').each(function(t){t.removeClassName('selected');});
		$(o).addClassName('selected');
		$$('.help_right_area').each(function(t){t.hide();});
		$('help_box_'+n).show();
	}
	
	
	
}














































