$(document).ready(function(){
	$('#subnavi').append('<li><a href="#" title="Diese Seite drucken" id="print">Drucken</a></li>');
	$('#print').bind("click", (function() {
		window.print();
		
		return false;
	}));
	
	$('#search_period_from').attr("disabled","disabled");
	$('#search_period_to').attr("disabled","disabled");
	
	$('#edit-submit').click(function () {
		$('#search_period_from').removeAttr("disabled");
		$('#search_period_to').removeAttr("disabled");
	});
	
	$.dpText = {
		TEXT_PREV_YEAR		:	'ein Jahr zurück',
		TEXT_PREV_MONTH		:	'einen Monat zurück',
		TEXT_NEXT_YEAR		:	'ein Jahr vor',
		TEXT_NEXT_MONTH		:	'einen Monat vor',
		TEXT_CLOSE			:	'Kalendar Schließen',
		TEXT_CHOOSE_DATE	:	'Datum wählen'
	}
	
	Date.format = 'yyyy-mm-dd';
	
	$(function()
	{
		$('.date-pick').datePicker()
		$('#search_period_from').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#period_to').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#search_period_from').dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
		
		$('#search_period_to').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#period_from').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
		$('#search_period_to').dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
	});
	
	$('#gallery img').each(function(i) {
		$(this).click(function() {
			var source = $(this).attr('src');
			source = source.replace(/thumbnails\//, '');
		
			$('#preview').attr('src', source);
		});
		$(this).attr('title', 'Dieses Bild vergrößern');
	});
	
	$('#search_continent').change(function() {
		$('#search_country').attr('disabled','disabled');
		
		$.ajax({
			type: 'POST',
			url: 'reisen/suche-laender',
			dataType: 'json',
			data: 'continent=' + $(this).val(),
			success: function(data) {
				var output = '<option value="">Wählen Sie ein Land</option>';
				
				for (p in data) {
					output += '<option value=\"' + data[p].code + '\">' + data[p].name + '</option>\n';
				}
				
				$('#search_country').html(output);
			}
		});
		
		$('#search_country').removeAttr('disabled');
	});
	
	$('#search_country').change(function() {
		if ($('#search_continent').val() == '') {
			$('#search_continent').attr('disabled','disabled');
		
			$.ajax({
				type: 'POST',
				url: 'reisen/suche-kontinent',
				dataType: 'json',
				data: 'country=' + $(this).val(),
				success: function(data) {
					$('#search_continent option').each(function() {
						$(this).removeAttr('selected')
					});
				
					$('#search_continent option[value="' + data.continent + '"]').attr('selected','selected');
				}
			});
		
			$('#search_continent').removeAttr('disabled');
		}
	});
});
