var Reminders = Class.create();
Reminders.prototype = {
	initialize: function(options) {
		this.rooturl = '/alumni/reunions/remind';
		this.setoptions(options);
		this.isIE = navigator.appVersion.match(/MSIE/) == "MSIE";
		Event.observe(window,'load',this.initCheckboxes.bind(this));
		this.persons = new Array();
	},
	
	setoptions: function(options) {
		this.options = {
			ajaxurl: this.rooturl+'/ajaxcalls/ajaxcalls.cfm',
			loadingindicator: 'loadingindicator',
			memberscontainer: 'classmembers',
			maxcheckcount: 3
		}
		Object.extend(this.options, options || {});
	},
	
	initCheckboxes: function(){
		var checkboxNodes = $A($(this.options.memberscontainer).getElementsByClassName('remindcheckbox'));
		checkboxNodes.each(function(checkboxNode){
			Event.observe(checkboxNode,'click',this.toggleMember.bindAsEventListener(this))
		}.bind(this));
	},
	
	toggleMember: function(e){
		var elem = Event.element(e);
		if (elem.checked){
			if (this.persons.size() < this.options.maxcheckcount){
				this.persons.push(elem.getAttribute('value'));
			}else{
				window.alert('You have already selected 3 friends to remind.\n\nIf you would like to send reminders to more friends you will need to visit this page again.');
				elem.checked = false;
			}
		}else{
			var i = this.persons.indexOf(elem.getAttribute('value'));
			this.persons[i] = null;
			this.persons = this.persons.compact();
		}
	},
	
	getMembers: function(){
		if ($F('classyrselect') != '0'){
			var personparams = this.serializePersons();
			new Ajax.Updater(this.options.memberscontainer,this.options.ajaxurl,{method:'post', parameters:'action=getmembers&classyr='+$F('classyrselect')+personparams, onComplete:this.getMembersHandler.bind(this)});
		}else{
			window.alert('Please select a class year.');
		}
	},
	
	getMembersHandler: function(t){
		this.showNavigation();
		this.initCheckboxes();
	},
	
	showNavigation: function(){
		var navNodes = document.getElementsByClassName('remindernavigation');
		navNodes.each(function(navNode){
			navNode.style.display = 'block';
		});
	},
	
	hideNavigation: function(){
		var navNodes = document.getElementsByClassName('remindernavigation');
		navNodes.each(function(navNode){
			navNode.style.display = 'none';
		});
	},
	
	getStep2: function(){
		if (this.persons.size() > 0){
			var personparams = this.serializePersons();
			new Ajax.Updater(this.options.memberscontainer,this.options.ajaxurl,{method:'post', parameters:'action=getstep2'+personparams});
			var navNodes = document.getElementsByClassName('remindernavigation');
			navNodes.each(function(navNode){
				navNode.innerHTML = '<form>' +
					'<input type="button" value="Send Reminder" onclick="Remind.sendReminder();" />' +
				'</form>'
			});
			$('classyearselect').style.display = 'none';
			$('startover').style.display = 'block';
		}else{
			window.alert('You must select at least one friend to send a reminder to before continuing.');
		}
	},
	
	serializePersons: function(){
		//returns serialize version of this.persons array - includes leading &
		var personparam = '';
		this.persons.each(function(person){
			personparam += '&persons='+person;
		});
		return personparam;
	},
	
	sendReminder: function(){
		this.hideNavigation();
		$(this.options.loadingindicator).toggle();
		$(this.options.memberscontainer).style.display = 'none';
		var personsparams = this.serializePersons();
		new Ajax.Updater(this.options.memberscontainer,this.options.ajaxurl,{method:'post', parameters:'action=sendreminder&currentuser='+$F('currentuseremail')+'&'+$('message').serialize()+personsparams, onSuccess:this.sendReminderHandler.bind(this)});
	},
	
	sendReminderHandler: function(){
		$(this.options.loadingindicator).toggle();
		$(this.options.memberscontainer).style.display = 'block';
	}
}
var Remind = new Reminders();
