var cAddKidProfile = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// variable_list: settings
		Object.extend(this.s, {
			//moduleName: 'AdminModules__AddFavorites',
			moduleName: 'MemberProfileEditExtended',
			json: {
				ver: '0.1',
				meta: {},
				data: {
					requests: []
				}
			},
			uniqueID: 0,
			type: null,
			a_element: null,
			table_element: null
			/* put extensions to collections here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			requests: []
			/* put extensions to collections here */
		});
		
		//Perform Functions
		this._findTable();
		this._initAddKidListener(el);
	},
	
	_initAddKidListener: function(el) {
		this.s.a_element=el;
		this.eObserve(this.s.a_element, 'click', this.showAddForm.bind(this));
	},
	
	_findTable: function() {
		this.s.table_element = document.getElementById('kids_table');
	},
	
	showAddForm: function(el) {
		if(this.s.uniqueID < 100) {
			var rowHTML = this._getInputField('name', this.s.uniqueID, '');
			rowHTML += this._getInputField('age', this.s.uniqueID, '');
			
			rowHTML += this._getBirthdayField(0, 0, this.s.uniqueID);
			rowHTML += this._getGenderField(null, this.s.uniqueID);
			rowHTML += this._getTwinField(null, this.s.uniqueID);
			
			rowHTML += this._getInputField('school', this.s.uniqueID, '');
			rowHTML += this._getInputField('special', this.s.uniqueID, '');
			
			rowHTML += '<td>&nbsp;</td>';
			
			var newKidForm = document.createElement('tr');
			newKidForm.setAttribute('id','new_kid_data_'+this.s.uniqueID);
			Element.addClassName(newKidForm, 'new_kid_data_'+this.s.uniqueID);
			Element.addClassName(newKidForm, 'kid_data');
			Element.addClassName(newKidForm, 'new_kid');
			
			var newName = document.createElement('td');
			newName.setAttribute('id','new_cell_name_'+this.s.uniqueID);
			newName.innerHTML = this._getInputField('name', this.s.uniqueID, '');
			newKidForm.appendChild(newName);
			
			/*
			var newAge = document.createElement('td');
			newAge.setAttribute('id','new_cell_age_'+this.s.uniqueID);
			newAge.innerHTML = this._getInputField('age', this.s.uniqueID, '');
			newKidForm.appendChild(newAge);
			*/
			
			var newBirth = document.createElement('td');
			newBirth.setAttribute('id','new_cell_birthday_'+this.s.uniqueID);
			newBirth.setAttribute('nowrap', 'nowrap');
			newBirth.innerHTML = this._getBirthdayField(0, 0, 0, this.s.uniqueID);
			
			newKidForm.appendChild(newBirth);
			new cBirthdayFields(newBirth);
			
			//shout( newBirth.innerHTML, 'chris');
			
			var newGender = document.createElement('td');
			newGender.setAttribute('id','new_cell_gender_'+this.s.uniqueID);
			newGender.innerHTML = this._getGenderField(null, this.s.uniqueID);
			newKidForm.appendChild(newGender);
			
			/*
			var newTwin = document.createElement('td');
			newTwin.setAttribute('id','new_cell_twin_'+this.s.uniqueID);
			newTwin.innerHTML = this._getTwinField(null, this.s.uniqueID);
			newKidForm.appendChild(newTwin);
			
			var newSchool = document.createElement('td');
			newSchool.setAttribute('id','new_cell_school_'+this.s.uniqueID);
			newSchool.innerHTML = this._getInputField('school', this.s.uniqueID, '');
			newKidForm.appendChild(newSchool);
			
			var newSpecial = document.createElement('td');
			newSpecial.setAttribute('id','new_cell_special_'+this.s.uniqueID);
			newSpecial.innerHTML = this._getInputField('special', this.s.uniqueID, '');
			newKidForm.appendChild(newSpecial);
			*/
			var actions = document.createElement('td');
			Element.addClassName(actions, 'functions');
			actions.setAttribute('align','center');
			newKidForm.appendChild(actions);
			
			var a = document.createElement('a');
			a.setAttribute('href','javascript:void(0)');
			Element.addClassName(a, "delete");
			a.innerHTML = 'Delete';
			actions.appendChild(a);
			this.s.uniqueID ++;
			this.s.table_element.getElementsByTagName("tbody")[0].appendChild(newKidForm);
			new cProfileKids(newKidForm);
		}
	},
	
	_getInputField: function(field_name, id, value) {
		return '<input id="new_'+field_name+'_'+id+'" name="new_'+field_name+'_'+id+'" value="'+value+'"/>';
	},
	
	_getBirthdayField: function(month, day, year, id) {
		var select_html = '<select id="new_birth_month_'+id+'" name="new_birth_month_'+id+'" class="birth_month">';
		for(var i=1; i < 13; i++) {
			
			select_html += '<option value="'+i+'"';
			if( month == i) {
				select_html += ' selected="selected" class="selected"';
			}
			if(i < 10) {
				select_html += '>0'+i+'</option>';
			}
			else {
				select_html += '>'+i+'</option>';
			}
		}
		
		select_html += '</select> / ';
		select_html += '<select id="new_birth_day_'+id+'" name="new_birth_day_'+id+'" class="birth_day">';
		for(var i=1; i < 32; i++) {
			select_html += '<option value="'+i+'"';
			if( day == i) {
				select_html += ' selected="selected" class="selected"';
			}
			if(i < 10) {
				select_html += '>0'+i+'</option>';
			}
			else {
				select_html += '>'+i+'</option>';
			}
		}
		select_html += '</select> / ';
		
		var Today = new Date();
		var ThisYear = Today.getFullYear();
		select_html += '<select id="new_birth_year_'+id+'" name="new_birth_year_'+id+'" class="birth_year">';
		for(var i=ThisYear; i > ThisYear-40; i--) {
			select_html += '<option value="'+i+'"';
			if( year == i) {
				select_html += ' selected="selected" class="selected"';
			}
			select_html += '>'+i+'</option>';
		}
		select_html += '</select>';
		
		return select_html;

	},
	_getTwinField: function(twin, id) {
		var select_html = '<select id="new_twin_'+id+'" name="new_twin_'+id+'">';
		var twin_choice_array =Array('No', 'Yes');
		for(var i=0; i < 2; i++) {
			select_html += '<option value="'+i+'"';
			if( twin == twin_choice_array[i]) {
				select_html += ' selected="selected" class="selected"';
			}
			select_html += '>'+twin_choice_array[i]+'</option>';
		}
		select_html += '</selected>';
		return select_html;
	},
	_getGenderField: function(gender, id) {
		var select_html = '<select id="new_gender_'+id+'" name="new_gender_'+id+'">';
		var gender_choice_array =Array('Female', 'Male');
		for(var i=0; i < 2; i++) {
			select_html += '<option value="'+i+'"';
			if( gender == gender_choice_array[i]) {
				select_html += ' selected="selected" class="selected"';
			}
			select_html += '>'+gender_choice_array[i]+'</option>';
		}
		select_html += '</selected>';
		return select_html;
	}
	
});

EventSelectors.register({
	'a.add_kid_link': function(el, index, id) {
		new cAddKidProfile(el);
	}
}, true);
