﻿var colors = ["#c41001", "#51bef7", "#005200", "#ffc44e", "#9ff733", "#293569", "#df45ff", "#ff025f", "#006fa6", "#f27501", "#8a1259", "#90da00", "#deff1c", "#560027", "#8effaf", "#007b44", "#008e72", "#ff6245", "#6e8fb2", "#001b76"];

$.extend($.validator.messages, {
	required: 'Dieses Feld muss ausgefüllt werden'
});

$(document).ready(function () {
	if ($("body").hasClass('showIntro')) {
		beginIntro();
	}

	$(".colorField").each(function (index) {
		$(this).css('backgroundColor', colors[index]);
		animateToColor(this, index, colors);
	});

	$("a.image").fancybox();
	//$(".ticketButton").fancybox();

	$("form").validate();

	$("#printButton").click(function () {
		window.print();
	});
});

function beginIntro() {
	var navigationContainer = $("#navigationContainer");
	var content = $("#content");
	
	navigationContainer.children().hide();
	content.children().hide();
	
	for (i = 1; i <= 3; i++) {
		navigationContainer.append($('<div class="colorField"></div>'));
	}
	for (i = 1; i <= 9; i++) {
		content.append($('<div class="colorField"></div>'));
	}
	
	window.setTimeout(endIntro, 5000);
}
function endIntro() {
	var navigationContainer = $("#navigationContainer");
	var content = $("#content");

	content.children(".colorField").stop(true, true).hide(100, function () {
		content.children(".colorField").remove();
		content.children().show();
	});
	navigationContainer.children(".colorField").stop(true, true).hide(100, function () {
		navigationContainer.children(".colorField").remove();
		navigationContainer.children().show();
	});
}

function animateToColor(element, index, colors) {
	var newIndex = index + 1;
	if (newIndex == colors.length) newIndex = 0;

	var random = randomInteger(0, 8);
	if (!$(element).hasClass('noImage') && random == 5 && images.length > 0) {
		var randomImageIndex = randomInteger(0, images.length - 1);
		var image = $('<img class="colorFieldImage" src="' + images[randomImageIndex] + '" />');
		image.hide().appendTo($(element));

		$(element).find(".colorFieldImage").fadeIn(randomInteger(2500, 5000), function () {
			$(element).find(".colorFieldImage").fadeOut(randomInteger(2500, 5000), function () {
				$(this).remove();
				animateToColor(element, index + 1, colors);
			});
		});
	} else {
		$(element).animate({ backgroundColor: colors[index] }, randomInteger(2500, 5000), function () {
			animateToColor(this, newIndex, colors);
		});
	}
}

function randomInteger(min, max) {
	if (min > max) return (-1);
	if (min == max) return (min);
	return (min + parseInt(Math.random() * (max - min + 1)));
}
