
window.addEvent('domready', function() {

	// var wasUserWelcomed = Cookie.read('welcomed');
	// if (wasUserWelcomed == null) {
	// 	$('hovercontainer').fade('show');
	// 	Cookie.write('welcomed', 'yes', { duration: 30, path: '/' });
	// }

	if (!$chk($('timeoutinfo'))) { return; }

	var hin = {'height': 250};
	var weg = {'height': 20, 'overflow': 'hidden'};

	var timeoutinfom = new Fx.Morph('timeoutinfo', {duration: 'short'});
	var musikinfom = new Fx.Morph('musikinfo', {duration: 'short'});
	var talkinfom = new Fx.Morph('talkinfo', {duration: 'short'});

	$('timeoutinfo').addEvent('mouseenter', function () {
		timeoutinfom.start(hin);
		musikinfom.start(weg);
		talkinfom.start(weg);
	});
	$('musikinfo').addEvent('mouseenter', function () {
		timeoutinfom.start(weg);
		musikinfom.start(hin);
		talkinfom.start(weg);
	});
	$('talkinfo').addEvent('mouseenter', function () {
		timeoutinfom.start(weg);
		musikinfom.start(weg);
		talkinfom.start(hin);
	});

	// Tooltips für die Dörfer
	$$('area').each(function (item) {
		item.addEvent('click', function () {
			return false;
		});
	});

	$each(towns, function (town) {
		var town_text = "<strong>"+town.name+"</strong>";
		$each(town.groups, function (group) {
			town_text += '<br/><a href="' + group.link + '">' + group.name + '</a>';
		});
		$(town.id).addEvent('mouseenter', function () {
			setTooltip(town_text, town.x, town.y);
		})
	});

	$('maptooltip').addEvent('mouseleave',function () {
		$('maptooltip').fade('out');
	});
	$('map').addEvent('mouseleave',function () {
		$('maptooltip').fade('out');
	});

	// Tooltips für die Navigationspfeile
	$$("#leftarrow img").each(function (item, index) {
		item.addEvent('mouseenter', function () {
			setTooltipLR("letzter Timeout", 0, 95, "L");
		});
		item.addEvent('mouseleave', function () {
			$('pfeiltooltip').fade('out');
		})
	});
	$$("#rightarrow img").each(function (item, index) {
		item.addEvent('mouseenter', function () {
			setTooltipLR("nächster Timeout", 755, 95, "R");
		});
		item.addEvent('mouseleave', function () {
			$('pfeiltooltip').fade('out');
		})
	});

	$('hoverclose').addEvent('click', function () {
		$('hovercontainer').fade('hide');
	});

	$$('input[type=text]').each(function (item) {
		var oldvalue;
		item.addEvent('focus', function() {
			if (item.get('value') == item.get('title')) {
				item.set('value','');
				oldvalue = item.getStyle('color')
				item.setStyle('color','black');
			}
		});
		item.addEvent('blur', function () {
			if (item.get('value') == '') {
				item.set('value', item.get('title'));
				item.setStyle('color',oldvalue);
			}
		});
	});

	startCountdown(nexttimeout);

	if ($chk($('watchphotos'))) {
		milkbox.addEvent('xmlGalleries',function(){
			$('watchphotos').addEvent('click', function(e){
				e.preventDefault();
				milkbox.showGallery({ gallery:'xmlG1'});
			});
		});
		milkbox.addGalleries(xmlgallery);
	}

	$$('#kontaktimpressum a').each(function (item) {
		item.addEvent('click', function () {
			var req = new Request.JSON({
				url:item.get('href'),
				method:'get',
				onSuccess: function (json, text) {
					$('hoverheadline').set('html',json.headline);
					$('hovertext').set('html', json.text);
					$('hovercontainer').fade('show');

					$$('#hovercontainer form').each(function (form) {
						form.addEvent('submit', function () {
							sendForm(form);
							return false;
						});
					});
				}
			}).send();
			return false;
		});
	});
});

function showPhoto(index) {
	milkbox.showGallery({ gallery:'xmlG1', index:index });
}

function setTooltip (text, left, top) {
	$("maptooltipinhalt").set('html', text);

	$("maptooltip").setStyles({
		bottom: top,
		right: left,
		display: "table",
		opacity: 0
	});
	$("maptooltip").fade('in');

	$$('#map a').each(function (item) {
		item.addEvent('click', function () {
			towns.each(function (town) {
				town.groups.each(function (group) {
					if (group.link == item.get('href')) {
						$('hoverheadline').set('html', group.name);
						$('hovertext').set('html', group.text);
						$('hovercontainer').fade('show');
					}
				})
			});
			return false;
		});
	});
}

function setTooltipLR (text, left, top, lr) {
	$("pfeiltooltipinhalt").set('text', text);
	$("pfeiltooltip").setStyles({
		top: top,
		left: left,
		display: "table",
		opacity: 0
	});
	var pfeil = $$("#pfeiltooltip .unten img")[0];
	if (lr == "L") {
		pfeil.setStyles({
			"margin-left": "13px"
		});
	}
	if (lr == "R") {
		pfeil.setStyles({
			"margin-left": "145px"
		});
	}
	$("pfeiltooltip").fade(0.9);
}

function startCountdown (datetime) {
	var ziel = new Date(datetime);
	var jetzt = new Date();

	var differenz = (ziel - jetzt) / 1000;
	var text = "";

	if (differenz < 0) {
		text = "";
	}
	else {
		var tage = Math.floor(differenz / 86400);
		var stunden = Math.floor((differenz % 86400) / 3600);
		var minuten = Math.floor(((differenz % 86400) % 3600) / 60);
		text = "noch " + tage + " Tage, " + stunden + " Std. und " + minuten + " Min.";
	}

	$('countdown').set('text', text);
	window.setTimeout("startCountdown("+ziel.getTime()+");", 10000);
}

function change_bg (pattern) {
	var bgimage = $$('body')[0].getStyle('background-image');
	var newbgimage = bgimage.replace(/\/pattern\d+/, '/'+pattern);
	$$('body')[0].setStyle('background-image', newbgimage);
}

function sendForm (form) {
	var req = new Request.JSON({
		url:form.get('action'),
		onSuccess:function(json,text) {
			$('hoverheadline').set('html',json.headline);
			$('hovertext').set('html', json.text);

			$$('#hovercontainer form').each(function (form) {
				form.addEvent('submit', function () {
					sendForm(form);
					return false;
				});
			});
		},
		data:form.toQueryString()
	});
	req.send();
}

