function init_accordion() {
	if ($$('.accordion').length) {
		$$('.accordion h4').setStyle('cursor', 'pointer');
		var accordion = new Accordion($$('.accordion h4'), $$('.accordion p'), {
			opacity: false,
			onActive: function(toggler, element){
				toggler.setStyle('text-decoration', 'none');
				var accordion_image = element.getElement('.accordion_image');
				if (accordion_image) {
					accordion_image.setStyle('display', 'none');
					$('box4').setStyle('background-image', 'url(' + accordion_image.src + ')');
				} else {
					$('box4').setStyle('background-image', '');
				}
			},
			onBackground: function(toggler, element){
				toggler.setStyle('text-decoration', 'underline');
			}
		});
	}
}

function init_map_link() {
	var map_link = $('anreise_karte');
	if (map_link) {
		map_link.addEvent('click', function(event) {
			event.stop();
			var big_map = $('big_map');
			if (!big_map) {
				big_map = new Element('img', {
					'src': './images/anreise_karte_gross.jpg',
					'width': '530',
					'height': '530',
					'id': 'big_map',
					'events': {
						'click': function() {
							this.setStyle('display', 'none');
							$('box2').setStyle('height', 'auto');
						}
					}
				});
				big_map.inject($('box2'));
				$('box2').setStyle('height', '515px');
			} else {
				big_map.setStyle('display', 'block');
				$('box2').setStyle('height', '515px');
			}
		});
	}
}

function init_photo_gallery() {
	photos = $$('.img_bar a');
	if (photos.length) {
		photos.addEvent('click', photo_click.bindWithEvent(this));
		current_photo = photos[0];

		var big_photo_cnt = $('big_photo_cnt');
		if (!big_photo_cnt) {
			big_photo_cnt = new Element('div', {
				'id': 'big_photo_cnt'
			});
			var big_photo = new Element('img', {
				'id': 'big_photo',
				'width': '500',
				'height': '330',
				'alt': ''
			});
			var photo_controls = new Element('div', {
				'class': 'photo_controls'
			});
			var photo_controls2 = new Element('div', {
				'class': 'photo_controls',
				'styles': {
					'text-align': 'center'
				}
			});
			var next_link = new Element('div', {
				'html': 'nächstes Bild',
				'styles': {
					'float': 'right'
				},
				'events': {
					'click': next_photo
				}
			});
			var previous_link = new Element('div', {
				'html': 'vorheriges Bild',
				'styles': {
					'float': 'left'
				},
				'events': {
					'click': previous_photo
				}
			});
			var close_link = new Element('span', {
				'html': 'Ansicht schließen',
				'events': {
					'click': close_photo
				}
			});
			big_photo_cnt.inject($('box2'));
			big_photo.inject(big_photo_cnt);
			photo_controls.inject(big_photo_cnt);
			photo_controls2.inject(big_photo_cnt);
			next_link.inject(photo_controls);
			previous_link.inject(photo_controls);
			close_link.inject(photo_controls2);
		}
	}
}

function photo_click(event) {
	event.stop();
	if (event.target.tagName == 'IMG') {
		current_photo = event.target.parentNode;
	}	else if (event.target.tagName == 'A') {
		current_photo = event.target;
	}
	show_current_photo();
}

function next_photo() {
	var index = photos.indexOf(current_photo);
	if (index == photos.length - 1) {
		current_photo = photos[0];
	} else {
		current_photo = photos[index + 1];
	}
	show_current_photo();
}

function previous_photo() {
	var index = photos.indexOf(current_photo);
	if (index == 0) {
		current_photo = photos[photos.length - 1];
	} else {
		current_photo = photos[index - 1];
	}
	show_current_photo();
}

function show_current_photo() {
	var big_photo_cnt = $('big_photo_cnt');
	if (big_photo_cnt.getStyle('display') == 'none') {
		big_photo_cnt.setStyle('display', 'block');
	}
	$('big_photo').set('src', current_photo.href);
}

function close_photo() {
	var big_photo_cnt = $('big_photo_cnt');
	if (big_photo_cnt.getStyle('display') != 'none') {
		big_photo_cnt.setStyle('display', 'none');
	}
}

function init_form_validation() {
	var forms = $$('.formcheck');
	if (forms.length) {
		
		forms.addEvent('submit', function(event) {
			var fvm = this.getElements('.fvm');
			fvm.destroy();
			
			var i = 0;
			var required_fields = this.getElements('.required, .validate-email');
			required_fields.each(function(el){
				if (el.hasClass('required') && !el.value.length) {
					event.stop();
					el.addClass('invalid');
					if (i == 0) {
						el.focus();
					}
					var info_node = new Element('span', {
						'class': 'fvm error_missing_value',
						'html': 'Bitte ausfüllen'
					}).inject(el, 'after');
					i++;
				} else if (el.hasClass('validate-email') && !/^[\w+-]+(\.[\w+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i.test(el.value)) {
					event.stop();
					el.addClass('invalid');
					if (i == 0) {
						el.focus();
					}
					var info_node = new Element('span', {
						'class': 'fvm error_missing_value',
						'text': 'Gültige E-Mail Adresse benötigt'
					}).inject(el, 'after');
				} else {
					el.removeClass('invalid');
				}
			});
		});
	}
}

window.addEvent('domready', function() {
	init_accordion();
	init_map_link();
	init_photo_gallery();
	init_form_validation();
});

function getMaps(adressString){ 
    //Maps initialisieren und in einen Container laden:
            //-------------------------------------------------
            var geocoder = new google.maps.Geocoder();
            var geoLocation = new google.maps.LatLng(0, 0);
            var myOptions = {
                  zoom: 10,
                  center: geoLocation,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                  }
            };

            var map = new google.maps.Map(document.getElementById("map_container"), myOptions) ;
            var marker = new google.maps.Marker({
              position: geoLocation,
              map: map,
              draggable: false
            });

            //Adresse ermitteln und die Karte ausrichten:
            //-------------------------------------------
            geocoder.geocode({'address': adressString},
            function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                var position = results[0].geometry.location;
                map.setCenter(position);
                map.setZoom(13);
                marker.setPosition(position);
              } else {
                document.getElementById("map_container").innerHTML = 'Die Position konnte nicht automatisch ermittelt werden. ' + status;
              }
            })
}
