$(document).ready( function() {

	var layer = new Anfahrt();
	layer.init();

	$('.date-pick').datePicker()
	$('#anreisetag').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#abreisetag').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#abreisetag').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#anreisetag').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);

});

function Anfahrt() {

	this.modalWindow = modalWindow;

	this.init = function() {
		var that = this;

		if (document.getElementById('anfahrt')) {
			$( '#anfahrt a' ).bind( 'click', function() {
				that.view();
			} );
		}
	}

	this.view = function( oElement ) {
		this.modalWindow.windowId = "myModal";
		this.modalWindow.width = 480;
		this.modalWindow.height = 445;
		this.modalWindow.content = $( '.layer' ).html();
		this.modalWindow.open();
	};

}

var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		$(".modal-overlay").click(function(){modalWindow.close();});
	}
};
