/**
 * A guest detail dialogue.
 * 
 * ----
 * 
 * LICENCE
 * 
 * This work is licensed under Creative Commons Attribution-Share Alike 3.0 (Germany).
 * For details on this licence visit http://creativecommons.org/licenses/by-sa/3.0/de/
 * 
 * All rights reserved
 * 
 * @author Jakob Hohlfeld | http://www.netronaut.de
 * @copyright 2010 by Jakob Hohlfeld
 * 
 */
var Minisearch_FormOptions = new Class
({
	Extends: NetronautUI_Widget,
	
	childrenSet: null,
	childRowTemplate: null,
	
	render: function () {
		this.dom.el.addClass('minisearch-form-options');
		var form = new Element('form', {events:{'submit':function(event){event.stop();this.fireEvent('save')}.bind(this)}}).inject(this.dom.el);
		new Element('div', {'class':'caption', text:'Suchoptionen anpassen'}).inject(form);
		new Element('div', {'class':'minisearch-pane-left'}).adopt(
			new Element('p', {text:'Bitte geben Sie die Anzahl der Personen an, für die Sie eine Übernachtung suchen.'}),
			new Element('div', {'class':'subcaption', text:'Erwachsene'}),
			new Element('fieldset', {'class':'minisearch-form1'}).adopt(new Element('p').adopt(
				this.dom.adults = new Element('input', {type:'text', maxlength:'2', 'class':'text'}),
				new Element('span', {text:'Erwachsene'})
			)),
			new Element('div', {'class':'subcaption', text:'Kinder'}),
			this.childrenSet = new Element('fieldset', {'class':'minisearch-form2'}).adopt((this.childRowTemplate = new Element('p').adopt(
				new Element('input', {type:'text', maxlength:'2', 'class':'text'}),
				new Element('span', {text:'Kind/er - Alter (0-17):'}),
				new Element('input', {type:'text', maxlength:'2', 'class':'text'}),
				new Element('span', {text:'Jahre'}),
				new Element('button', {type:'button', 'class':'delete'})
			)).clone()),
			new Element('p').adopt(
				new Element('a', {'class':'minisearch-button-addchild', href:'javascript:void(0);', text:'+ Weitere Kinder', events:{'click':this.addChild.bind(this, [])}})
			)
		).inject(form);
		new Element('div', {'class':'minisearch-pane-right'}).adopt(
			new Element('p', {text:'Mit folgenden weiteren Optionen können Sie Ihre Suche stärker einschränken.'}),
			new Element('div', {'class':'subcaption', text:'Weitere Optionen'}),
			new Element('fieldset', {'class':'form3'}).adopt(
				new Element('p').adopt(
					new Element('label', {'for':'max_price', text:'Maximaler Tagespreis'}),
					this.dom.option_max_price = new Element('input', {id:'max_price', type:'text', 'class':'text'}),
					document.createTextNode('EUR')
				),
				new Element('p').adopt(
					new Element('label', {'for':'max_beach_distance', text:'Maximale Entfernung zum Strand'}),
					this.dom.option_max_beach_distance = new Element('input', {id:'max_beach_distance', type:'text', 'class':'text'}),
					document.createTextNode('km')
				),
				new Element('p').adopt(
					new Element('label', {'for':'pets_allowed', text:'Haustiere erlaubt'}),
					this.dom.option_pets_allowed = new Element('input', {id:'pets_allowed', type:'checkbox', 'class':'checkbox'})
				)
			),
			new Element('div', {'class':'subcaption', text:'Raucher / Nichtraucher'}),
			new Element('fieldset', {'class':'form4'}).adopt(
				new Element('ul').adopt(
					new Element('li').adopt(
						new Element('input', {id:'smoking_1', type:'radio', 'name':'smoking', value:null}),
						new Element('label', {'for':'smoking_1', text:'egal'})
					),
					new Element('li').adopt(
						new Element('input', {id:'smoking_2', type:'radio', 'name':'smoking', value:true}),
						new Element('label', {'for':'smoking_2', text:'Nichtraucher'})
					),
					new Element('li').adopt(
						new Element('input', {id:'smoking_3', type:'radio', 'name':'smoking', value:false}),
						new Element('label', {'for':'smoking_3', text:'Raucher'})
					)
				)
			)
		).inject(form);
		this.dom.option_smoking = this.dom.el.getElements('input[id^=smoking]');
		new Element('div', {'class':'clearfix'}).inject(form);
		new Element('p', {'class':'minisearch-control-submit'}).adopt(
			new Element('input', {'type':'button', 'name':'cancel', 'value':'Abbrechen', events:{
				'click':this.fireEvent.pass('cancel', this),
				'keyup':function(event){if(event.key=='enter')this.fireEvent('cancel')}.bind(this)
			}}),
			new Element('input', {'type':'submit', 'name':'save', 'value':'OK'})
		).inject(form);
	},
	
	/**
	 * Add a row with child details.
	 * 
	 */
	addChild: function ( count, age ) {
		var count = count || 1;
		var age = age || 0;
		var node = this.childRowTemplate.clone();
		node.getElements('input')[0].value = count;
		node.getElements('input')[1].value = age;
		node.inject(this.childrenSet);
		node.getElements('input')[(count=='1'?1:0)].select();
		node.getElement('button.delete').addEvent('click', function(event){
			this.removeChild(event.stop());
		}.bind(this));
	},
	
	/**
	 * Remove a row with child details.
	 * 
	 * @param Event event The event from the corresponding row.
	 */
	removeChild: function ( event ) {
		var row = event.target.getParent('p');
		row.destroy();
		if(!this.childrenSet.getElements('p').length) {
			this.addChild('0', '0');
		}
	},
	
	/**
	 * Get the component's input data.
	 * 
	 * @return object
	 */
	getData: function () {
		var data = {};
		
		// get persons
		var num_adults = Number(this.dom.el.getElements('fieldset')[0].getElement('input').value);
		data.persons = {
			adults: /^\d+$/.test(num_adults) && num_adults > 0  ? num_adults : 2,
			children: []
		};
		
		// handle children
		var children_age_map = new Hash();
		this.dom.el.getElements('fieldset')[1].getElements('p').each(function(row){
			var count = parseInt(row.getElements('input')[0].value);
			var age = parseInt(row.getElements('input')[1].value);
			if(!/^\d+$/.test(count)) count = 1;
			if(!/^\d+$/.test(age) || !(age >= 0 && age < 18)) age = 0;
			if(count < 1) return;
			
			// group children by age
			children_age_map.set(age, children_age_map.get(age)+count);
		});
		children_age_map.each(function(count, age){data.persons.children.push({age:age, count:count})});
		
		// get other options
		data.options = {
			max_price: parseInt(this.dom.option_max_price.value),
			max_beach_distance: parseInt(this.dom.option_max_beach_distance.value),
			pets_allowed: this.dom.option_pets_allowed.checked,
			smoking: (this.dom.option_smoking[0].checked) ?null :this.dom.option_smoking[1].checked ?false :true
		}
		
		return data;
	},
	
	/**
	 * Update the component's form elements.
	 * 
	 * @param object data
	 */
	setData: function ( data ) {
		
		// set persons
		this.dom.el.getElement('input').value = data.persons.adults;
		this.childrenSet.empty();
		data.persons.children.each(function(item){
			this.addChild(item.count, item.age);
		}, this);
		if(!data.persons.children.length) {
			this.addChild('0', '0');
		}
		
		// set other options
		this.dom.option_max_price.value = (i=parseInt(data.options.max_price)) ?i :'';
		this.dom.option_max_beach_distance.value = (i=parseInt(data.options.max_beach_distance)) ?i :'';
		this.dom.option_pets_allowed.checked = data.options.pets_allowed;
		this.dom.option_smoking[data.options.smoking==null?0:data.options.smoking?2:1].checked = true;
	}
})
