/**
 * Search data accessor and container.
 * 
 * Provides cookie accessing methods.
 * 
 * ----
 * 
 * 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_Data = new Class
({
	Implements: Events,
	
	cookie_name: null,
	options: new Hash({
		'location':'',
		'dates':{
			'arrival':0,
			'departure':0,
			'tolerance':{
				'arrival':0,
				'departure':0
			}
		},
		'persons':{
			'adults':2,
			'children':[]
		},
		'category':[0],
		'options':{
			'max_price':0,
			'max_beach_distance':0,
			'pets_allowed':false,
			'smoking':null
		}
	}),
	
	/**
	 * The constructor.
	 * 
	 * @param string cookie_name
	 */
	initialize: function ( cookie_name ) {
		this.cookie_name = cookie_name;
		this.read();
		this.valid_data();
	},
	
	read: function () {
		this.options.extend(new Hash(JSON.decode(Cookie.read(this.cookie_name))));
		this.valid_data();
	},
	
	write: function () {
		document.cookie = this.cookie_name + '=' + encodeURIComponent(JSON.encode(this.options.getClean())) + '; path=/; expires=' + new Date().clearTime().increment('day', 1).format('%c') + ';';
	},
	
	/**
	 * Make data valid - even if the cookie contains bull****
	 *   .. we want valid data, especially in the dates ..
	 *   
	 */
	valid_data: function () {
		
		// initialize the date fields - arrival
		if(this.options.dates.arrival == 0) {
			d = new Date().clearTime();
			this.options.dates.arrival = d.getTime();
		}
		
		// departure date comes per default 7 days after arrival date
		if(this.options.dates.departure == 0) {
			this.options.dates.departure = new Date(this.options.dates.arrival).increment('day', 7);
		}
		
		// departure MUST come after (at least one day) arrival date
		departure_minimal_ts = new Date(this.options.dates.arrival).increment('day', 1).getTime();
		if(this.options.dates.departure < departure_minimal_ts) {
			this.options.dates.departure = departure_minimal_ts;
		}
	}
})
