var search = {
	from : false,
	to : false,
	query : false,
	category : false,
	town : false,
	region : false,
	attribute : false,
	offset : 0,
	limit : 10,
	result : false,
	searchForm : false,
	today : false,
	time : false,
	clockInterval : false,
	clock : {},
	appBase : false,
	tickets : false,
	init : function(opts) {
		if (opts!=undefined) {
			
			if (opts.searchForm != undefined) {
				this.searchForm = opts.searchForm;
				if (this.searchForm.today != undefined) {
					var today_parts = this.searchForm.today.value.split(" ");
					this.today = this._stringToDate(today_parts[0]);
					this.time = today_parts[1];
					
					var today_time_parts = this.time.split(":");
					var server_datetime= this.today;
					server_datetime.setHours(today_time_parts[0]);
					server_datetime.setMinutes(today_time_parts[1]);
					
					var client_date =  new Date;
					
					this.clock = {
						'last_adjusted' : client_date.getTime(),
						'servertime' : server_datetime.getTime()
					}
					if (this.clockInterval == false ) {
						this.clockInterval = window.setInterval(jQuery.proxy(this.setTime,this), 1000);
					}
				}
				this.today = (!this.today) ? new Date() : this.today;
			}
			this.to = (this.today) ? this._jsDateToString(this.today) : this._jsDateToString(new Date);
			this.from = (this.today) ? this._jsDateToString(this.today) : this._jsDateToString(new Date);
			
			jQuery.each(opts, jQuery.proxy(function (k,e) {
				if (k!='searchForm') {
					if (this[k] != undefined) {
						this[k] = e;
					}
				}
			},this));
			// ggf. initiale Suche starten?
			if (typeof(this.initSearch)=='function') {
				this.initSearch(opts);
			}
			
		}
	},
	setTime:function() {
		var client_date =  new Date;
		var client_now = client_date.getTime();
		var diff = client_now - this.clock.last_adjusted;
		this.clock.last_adjusted  = client_now;
		this.clock.servertime+=diff;
		client_date.setTime(this.clock.servertime);
		var hours = client_date.getHours();
		hours = (hours<10) ? "0"+hours : hours;
		var minutes = client_date.getMinutes();
		minutes = (minutes<10) ? "0"+minutes : minutes;
		this.time = hours+":"+minutes;
	},
	dateSearch : function (from, to, query, category, region, town, offset, limit) {
		console.debug("dateSearch");
		this.showSpinner();
		
		var data = {
			'offset' 	: 	((offset==null) ? this.offset : offset),
			'limit' 	: 	((limit==null) ? this.limit : limit)
		};
		
		data['from'] = (from) ? from : this.from;
		this.from = data['from'];
		
		
		data['to'] = (to) ? to : this.to;
		this.to = data['to'];
		
		if (!this.attribute) {
			
			
			data['query'] = (query) ? query : this.query;
			this.query = data['query'];
			
			data['category'] = (category) ? category : this.category;
			this.category = data['category'];
			
			data['region'] 	= (region) 	? region : this.region;
			data['town'] 	= (town) 	? town : this.town;
			
			this.region = data['region'];
			if (this.tickets ) {
				data['ticket'] = "J";
			}
		} else {
			data['attribute'] = this.attribute;
			this.query = false;
			this.category = false;
			this.region = false;
			this.town = false;
			this.tickets = false;
		}
		
		
		// serverseitig die Detaillinks substituieren
		if (this.listResultDetailLink != undefined) {
			data['listResultDetailLink'] = this.listResultDetailLink;
		}
		if (this.listResultTicketLink != undefined) {
			data['listResultTicketLink'] = this.listResultTicketLink;
		}
		
		jQuery.ajax({
			'data':data,
			'url'		: this.appBase+'dateSearch.php',
			'cache'		: false,
			'success'	: jQuery.proxy(function(json,status) {
				if (json==null) {
					this._callbackError(false, false, "leeres Json vom Server")
				}
				this.hideSpinner();
				if (json.success) {
					this.showListResults(json.success)
				} else {
					this.showEmptyListResult();
				}
			},this),
			'timeout'	: 25000,
			'error'		: jQuery.proxy(function() {
				this.hideSpinner();
				this.showEmptyListResult();
			},this),
			'dataType'	: 'json'
			
		});
		
	},
	findEventById : function(id, a) {
		this.showSpinner();
		var data = {'id' : id};
		// serverseitig die Detaillinks substituieren
		if (this.addressDetailLink != undefined) {
			data['addressDetailLink'] = this.addressDetailLink;
		}
		jQuery.ajax({
			'data':data,
			'url'		: this.appBase+'findEventById.php',
			'cache'		: false,
			'success'	: jQuery.proxy(function(json,status) {
				if (json==null) {
					this._callbackError(false, false, "leeres Json vom Server")
				}
				this.hideSpinner();
				if (json.success) {
					this.showDetailResult(json.success)
				} else {
					this.showEmptyDetailResult();
				}
			},this),
			'timeout'	: 25000,
			'error'		: jQuery.proxy(function() {
				this.hideSpinner();
				this.showEmptyDetailResult();
			},this),
			'dataType'	: 'json'
			
		});
	},
	getToday : function() {
		return this._jsDateToString(this.today);
	},
	_parseDate : function(d) {
		
		if (d!=null) {
			var regs = d.match(/(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):.*/);
			
			if (regs) {
				var date={};
				date['year'] = regs[1];
				date['month'] = regs[2];
				date['day'] = regs[3];
				date['hour'] = regs[4];
				date['minutes'] = regs[5];
				date['date'] = regs[3]+"."+regs[2]+"."+regs[1];
				date['time'] = regs[4]+":"+regs[5];
				return date;
			}
		}
		return false;
	},
	_jsDateToString : function(d) {
		var day = d.getDate();
		var month = d.getMonth() + 1;
		var year = d.getFullYear();
		
		day = (parseInt(day)<10) ? "0"+String(day) : day; 
		month = (parseInt(month)<10) ? "0"+String(month) : month; 
		return day+"."+month+"."+year;
	},
	_stringToDate : function(str) {
		var regs = str.match(/(\d{2})\.(\d{2})\.(\d{4})/);
		var ret = false;
		if (regs) {
			ret = new Date(regs[3],(regs[2] - 1),regs[1]);
		}
		return ret;
	},
	_callbackError : function(jqXHR, textStatus, errorThrown) {
		this.hideSpinner();
		console.debug(errorThrown);
	}
};
