$(function() {
	$('.date-pick').datePicker();

	//** Zeitspannendropdown
	var weekday = (new Date()).getDay();
	var today = (new Date()).getDate()
	var dates = {today:{},tomorrow:{},thisWeek:{},thisWeekend:{},nextWeek:{},nextWeekend:{}}
	for(var s in dates){ dates[s].begin = new Date(); dates[s].end = new Date();}
	dates.tomorrow.begin.setDate(today+1);
	dates.tomorrow.end.setDate(today+1);
	dates.thisWeek.begin.setDate(today+1-weekday);
	dates.thisWeek.end.setDate(today+7-weekday);
	dates.thisWeekend.begin.setDate(today+6-weekday);
	dates.thisWeekend.end.setDate(today+7-weekday);
	dates.nextWeek.begin.setDate(today+8-weekday);
	dates.nextWeek.end.setDate(today+14-weekday);
	dates.nextWeekend.begin.setDate(today+13-weekday);
	dates.nextWeekend.end.setDate(today+14-weekday);

	$('span#label-datum').after('<div class="ttg-first-col"><div><select name="zeitraum" id="zeitraum"><option value="none" ></option><option value="today">heute</option><option value="tomorrow">morgen</option><option value="thisWeek">diese Woche</option><option value="thisWeekend">dieses Wochenende</option><option value="nextWeek">nächste Woche</option><option value="nextWeekend">nächstes Wochenende</option></select></div></div>').next().next().addClass('ttg-text ttg-second-col');
	$('span#label-datum').clone().prependTo('div#ttg-vera div.ttg-first-col');
	$('span#label-datum:eq(0)').remove();
	$('span#label-datum').parent().parent().removeClass('ttg-text').addClass('ttg-select');

	$('select#zeitraum').change(function() {
		if($(this).val() != 'none') {
			$('input#vom').val(dates[$(this).val()].begin.to_s());
			$('input#bis').val(dates[$(this).val()].end.to_s());
		}
	});

	$('input#vom, input#bis').change(function() {
		if($('input#bis').val().to_date() && $('input#vom').val().to_date()) {
			$('input#bis').val($('input#bis').val().to_date() < $('input#vom').val().to_date() ? $('input#vom').val() : $('input#bis').val());
			$('input#bis').val($('input#bis').val().to_date().to_s());
			$('input#vom').val($('input#vom').val().to_date().to_s());
			for(var s in dates) {
				if(dates[s].begin.to_s() == $('input#vom').val() && dates[s].end.to_s() == $('input#bis').val()) {
					$('select#zeitraum > option[value=' + $('select#zeitraum').val()+ ']').removeAttr('selected');
					$('select#zeitraum > option[value=' + s + ']').attr('selected', 'selected');
					return;
				}
			}
			$('select#zeitraum > option[value=none]').attr('selected', 'selected');
		}
	});

	$('input#vom, input#bis').each(function() {
		if($(this).val() == '') {
			$(this).val(dates.today.begin.to_s())
			$('select#zeitraum > option[value=today]').attr('selected', 'selected');
		} else {
			for(var s in dates) {
				if(dates[s].begin.to_s() == $('input#vom').val() && dates[s].end.to_s() == $('input#bis').val()) {
					$('select#zeitraum > option[value=' + $('select#zeitraum').val() + ']').removeAttr('selected');
					$('select#zeitraum > option[value=' + s + ']').attr('selected', 'selected');
					return;
				}
			}
		}
	});
	//***
});

// Patches
String.prototype.leftPad = function (l, c) { return new Array(l - this.length + 1).join(c || '0') + this; };
String.prototype.to_date = function () { if(this.split('.').length != 3) {return false;} if(this.split('.')[2].length != 4) {return false;} var date = new Date(this.split('.')[1]+'/'+this.split('.')[0]+'/'+this.split('.')[2]); return date == "Invalid Date" ? false : date; };
Date.prototype.to_s = function() { return  this.getDate().toString().leftPad(2,'0') + "." + (this.getMonth() + 1).toString().leftPad(2,'0') + "." + this.getFullYear().toString(); };

// Start- und Enddatum beim Absenden des Suchformulars in Timestamps umwandeln
function toEpoch(datum) {
  epochDatum = new Date();
  try {
    epochDatum.setMonth(datum.split(".")[1]-1, datum.split(".")[0]);
    epochDatum.setFullYear(datum.split(".")[2]);
  }
  catch(err) {/* aktuelles Datum verwenden */}
  epochDatum.setHours(0,0,0,0);
  return parseInt(epochDatum.getTime()/1000);
}
function makeVaForm() {
  document.getElementsByName("start")[0].value = toEpoch(document.getElementsByName("vom")[0].value);
  document.getElementsByName("end")[0].value = 23*3600+59*60+59+toEpoch(document.getElementsByName("bis")[0].value);
}

