var cProfileKids = 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: null,
			type: null,
			editNode: null,
			deleteNode: null,
			trNode: el
			/* put extensions to collections here */
		});

		// initialize collections
		Object.extend(this.c, {
			requests: []
			/* put extensions to collections here */
		});


		//Perform Functions
		this._initEditDeleteListener(el);
	},
	addRequest: function(requestName, dataObject) {
		this.c.requests.push({
			id: this.c.requests.length,
			type: requestName,
			data: dataObject
		});
	},

	failedJson: function(t) {
		shout('failed', 'chris');
	},
	//json methods
	JsonOut: function() {
		//construct the post
		this.s.json.data.requests = this.c.requests;

		//this.setMeta('columns', '');
		var data = '__json=' + this.s.moduleName + '&data=' + toJSONString(this.s.json);
		this.c.requests = [];
		//alert(data);
		//send out the request
		var myAjax = new Ajax.Request(window.location,
		{
			method: 'post',
			parameters: data,
			onSuccess: this.JsonIn.bind(this),
			onFailiure: this.failedJson.bind(this)
		});
	},

	JsonIn: function(t) {
		var result = t.responseText.parseJSON();
		(result.responses.length).times(function(i) {
			if(result.responses[i].type == 'deleteKid'){
				Element.remove( this.s.trNode );

			}
		}.bind(this));
	},
	DeleteOnClick: function(el) {

		//ok i need that <td> id plz
		var id = this.s.trNode.getAttribute("id");

		if(Element.hasClassName(this.s.trNode, 'new_kid'))
			Element.remove( this.s.trNode );

		this.addRequest("deleteKid", {
				uniqueID: id,
				type: this.s.type
			});
		this.JsonOut();
	},

	EditOnClick: function(el) {
		//ok i need that <td> id plz
		var id = this.s.trNode.getAttribute("id");

		//ok i need all the TD elements
		var td_array = this.s.trNode.getElementsByTagName("td");

		//foreach element change the innerHTML to Element.addClassName(childSpan, this.s.openClass);
		if(!Element.hasClassName(this.s.trNode, 'openForm')) {

			for(var i=0; i < td_array.length; i++) {
				var field_name = td_array[i].getAttribute("class");
				if (field_name == null) field_name = td_array[i].className;
				if(field_name == 'name' /*|| field_name == 'age' || field_name == 'school' || field_name == 'special'*/) {
					td_array[i].innerHTML = this._getInputField(field_name, id, td_array[i].innerHTML);
				}
				else if(field_name == 'birthday') {
					var birth_array=td_array[i].innerHTML.split('/');
					td_array[i].innerHTML = this._getBirthdayField(birth_array[0], birth_array[1], birth_array[2], id);
					new cBirthdayFields(td_array[i]);
				}
				else if(field_name == 'gender') {
					td_array[i].innerHTML = this._getGenderField(td_array[i].innerHTML, id);
				}
				/*else if(field_name == 'twin') {
					td_array[i].innerHTML = this._getTwinField(td_array[i].innerHTML, id);
				}*/
			}
			Element.addClassName(this.s.trNode, 'openForm');
		}
		/*this.addRequest("editKid", {
			uniqueID: id,
			type: null
		});*/
	},
	//Given: the initial <TR>s found with class "kid_data"
	//Find: get the edit link element and start listening to it
	//We Need to Find what to listen for
	_initEditDeleteListener: function(el) {
		var a_link_array = el.getElementsByTagName("a");
		for(var i=0; i < a_link_array.length; i++) {
			if(Element.hasClassName(a_link_array[i], 'edit')) {
				//store this is the one and only class=edit
				this.s.editNode = a_link_array[i];
				//start the listener
				this.eObserve(a_link_array[i], 'click', this.EditOnClick.bind(this));
			}
			else if(Element.hasClassName(a_link_array[i], 'delete')) {
				//store this is the one and only class=delete
				this.s.deleteNode = a_link_array[i];
				//start the listener
				this.eObserve(a_link_array[i], 'click', this.DeleteOnClick.bind(this));
			}
		}
	},
	//Declare Functions
	_getInputField: function(field_name, id, value) {
		return '<input id="'+field_name+'_'+id+'" name="'+field_name+'_'+id+'" value="'+value+'"/>';
	},
	_getBirthdayField: function(month, day, year, id) {
		var select_html = '<select id="birth_month_'+id+'" name="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="birth_day_'+id+'" name="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="birth_year_'+id+'" name="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="twin_'+id+'" name="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="gender_'+id+'" name="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({
	'tr.kid_data': function(el, index, id) {
		new cProfileKids(el);
	}
});
