// Booking class
function Booking (type) {
	this.pageOverlay = function(){
		ov = $('BookingOverlay');
		if(ov != null) {
			ov.show();
		} else {
			var overlay = document.createElement("div");
			overlay.addClassName("BookingOverlay");
			overlay.setAttribute("id", "BookingOverlay");
			
			var background = document.createElement("div");
			background.addClassName("BookingBackground");
			
			var content = document.createElement("div");
			content.addClassName("BookingContent");
			content.setAttribute("id", "BookingContent");
			content.appendChild(document.createTextNode("Loading..."));
			
			overlay.appendChild(background);
			overlay.appendChild(content);
			page = $('pageContainer');
			page.appendChild(overlay);
		}
		
	};
	this.hidePageOverlay = function(){
		$('BookingOverlay').hide();
	};
	this.runURL = function(date, details) {
		this.runURLParam(date, details, '')
	};
	this.runURLParam = function(date, details, param) {
		new Ajax.Updater('BookingContent', '/cms/index.php?eID=brauereibesichtigung&tx_brauereibesichtigung_booking[date]='+date+'&tx_brauereibesichtigung_booking[details]='+details+'&'+param, 
			{
				evalScripts: true,
				onComplete: function() {
					if($('submit-popup') != null) {
						$('submit-popup').observe('click', function(){
							if($('agb') != null){
								if($('agb').checked)
									book.runURLParam(date, details, $('inner-form').serialize());
								else
									alert("Sie müssen den Allgemeinen Geschäftsbedigungen zustimmen um die Buchung abzuschließen!");
							} else {
								book.runURLParam(date, details, $('inner-form').serialize());
							}
							return false;
						});
					}
				}
			}
		);
	};
    this.startClick = function(inlineParam) {
    	this.pageOverlay();
    	var parts = inlineParam.split("-");
    	this.runURL(parts[0]+'-'+parts[1]+'-'+parts[2], parts[3]);
        return true;
    };
}

var book = new Booking();


// Start Booking
Event.observe(window, 'load', function () {
	var dates = $$('.event_box');
	if(dates.length > 0) {
		dates.each(function(s){
			var ele = s;
			s.observe('click', function(s,e){
				book.startClick(ele.readAttribute('rel'));
			});
		});	
	}
});
