var curImage = 0;
var homeImageAnimation;
var home_animation_on = false;
var imageContainerToggler = true;

var currentSearchModelId = false;


$(document).ready(function() {
	/* Menu BEG */
	var menuItems = $('#menu_padder a.level3_item').length;
	var leaveTimeout;
	
	$('#menu_padder a.level3_item').css({
		'width': Math.floor($('#menu_padder').width() / menuItems) + 'px'
	});
	$('.mainMenuFooter').css({
		'width': Math.floor($('#menu_padder').width() / $('.mainMenuFooter').length - 45) + 'px'
	});

	$('#menu_padder a.level3_item').mouseenter(function() {
		clearTimeout(leaveTimeout);
		
		$('.menuSubcontainer')./*css({
			'display': 'none'
		})*/stop().animate({
			'height': '0px'
		});
		$('.menu_arrow_bg').css({
			'display': 'none'
		});
		
		var curRelation = $(this).attr('rel');
		
		var thisLeft = $(this).position().left;
		
		if (thisLeft + $('#' + curRelation).width() > 994) {
			thisLeft = thisLeft - ($('#' + curRelation).width() - $(this).width()) + 3;
		}
		
		$('.menu_arrow_bg', this).css({
			'display': 'block'
		});
		
		$('#' + curRelation).css({
			//'display': 'block',
			'left': thisLeft + 'px'
		}).stop().animate({
			'height': ($('#' + curRelation).children('a').length * 50) + 'px'
		}, 250);
	}).mouseleave(function() {
		var $this = this;
		
		leaveTimeout = setTimeout(function() {
			var curRelation = $($this).attr('rel');

			$('#' + curRelation)./*css({
				'display': 'none'
			})*/stop().animate({
				'height': '0px'
			});
			
			$('.menu_arrow_bg', $this).css({
				'display': 'none'
			});
		}, 300);
	});
	
	$('.menuSubcontainer').mouseenter(function() {
		clearTimeout(leaveTimeout);
		
		var curRelation = $(this).attr('id');

		$('#menu_padder a.level3_item[rel="' + curRelation + '"]').addClass('active');
	}).mouseleave(function() {
		$('.menuSubcontainer')./*css({
			'display': 'none'
		})*/stop().animate({
			'height': '0px'
		});
		$('.menu_arrow_bg').css({
			'display': 'none'
		});
		
		var curRelation = $(this).attr('id');

		$('#menu_padder a.level3_item[rel="' + curRelation + '"]').removeClass('active');
	});
	
	$('.menuSubcontainer').find('a:first').css({
		'backgroundPosition': '0px -100px'
	}).hover(function() {
		$(this).css({
			'backgroundPosition': '0px -50px'
		});
	}, function() {
		$(this).css({
			'backgroundPosition': '0px -100px'
		});
	});

	/* Recommendation BEG */
	$("#recommendation_toggler").click(function (e) {
		e.preventDefault();

		$("#recommendation_container").animate({
			'top': '-420px',
			'height' : '380px'
		}, 700);
	});

	$("#hide_recommendation").click(function () {
		$("#recommendation_container").animate({
			'top': '0px',
			'height' : '0px'
		}, 700);
	});

	var recommendation_message = '';
	$('#recommendation_message').keyup(function(ekey) {
		$('#recommendation_msg_count').text(100 - $(this).val().length);
		if (recommendation_message.length >= 100 && ekey.which != 8 && ekey.which != 46) {
			$(this).val(recommendation_message);
		} else {
			recommendation_message = $(this).val();
		}
	});

	$("#send_recommendation").click(function () {
		$('<input>')
			.css('display', 'none')
			.attr({
				name: 'recommendation_sent',
				value: 'sent_true'
			})
			.appendTo('#form_recommendation')
		;

		$('#form_recommendation').attr({
			method: 'post'
		}).submit();
	});
	/* Recommendation END */
	
	
	// Galleries
	$('.fe_sd_gallery_item').hover(function() {
		$(this).stop().animate({
			'backgroundColor': '#00763B'
		});
	}, function() {
		$(this).stop().animate({
			'backgroundColor': '#D7D7D7'
		});
	});
	
	
	// Domain selector
	$('#domainselector_toggler').click(function(e) {
		e.preventDefault();
		
		var $this = this;
		
		$('#domainSelector_container').css({
			'left': $($this).position().left + 'px',
			'top': ($($this).offset().top + $($this).height() + 10) + 'px'
		}).toggle();
	});


	// News
	$('.newsDetailLink').click(function(e) {
		e.preventDefault();
		
		var rel = $(this).attr('rel');
		var height;

		if ($('#' + rel).hasClass('isOpen')) {
			height = 0;
			
			$('#' + rel).removeClass('isOpen');
		} else {
			height = $('#' + rel + ' div.newsMessageContent').innerHeight();
			
			$('#' + rel).addClass('isOpen');
		}

		$('#' + rel).stop().animate({
			'height': height + 'px'
		});
	});
	$('.instantOpen').trigger('click');
	

	// Header animation
	if (typeof homeAnimationImages != 'undefined' && homeAnimationImages) { 
		if (homeAnimationImages.length > 1) {
			$('#header_images').rsanimator({
				'images': homeAnimationImages,
				'width': 960,
				'height': 498,
				'description': $('#header_image_description')/*,
				'indicator': {
					'container': $('#home_animatedcar_trigger'),
					'elementClass': 'carTrigger',
					'elementClassActive': 'current'
				}*/
			});
		} else if (homeAnimationImages.length > 0) {
			$('#header_images').css({
				'backgroundImage': 'url(' + homeAnimationImages[0].imagefile + ')'
			});
		}
	}


	// Landkarte start
	var infoBoxes = {
		'bayerbach': { 'top': 36, 'width': 140 },
		'unterwattenbach': { 'top': 126, 'width': 140 },
		'woerth': { 'top': 156, 'width': 140 },
		'vilsbiburg': { 'top': 286, 'width': 140 },
		'moosburg': { 'top': 230, 'width': 100 },
		'allersdorf': { 'top': 4, 'width': 140 },
		'artlkofen': { 'top': 112, 'width': 140 },
		'bergham': { 'top': 266, 'width': 140 },
		'bergkirchen': { 'top': 412, 'width': 140 },
		'eglberg': { 'top': 176, 'width': 140 },
		'eichenkofen': { 'top': 338, 'width': 140 },
		'enghausen': { 'top': 215, 'width': 140 },
		'ergoldsbach': { 'top': 67, 'width': 140 },
		'goldbrunn': { 'top': 300, 'width': 140 },
		'jenkofen': { 'top': 230, 'width': 140 },
		'jesendorf': { 'top': 266, 'width': 60 },
		'landersdorf': { 'top': 195, 'width': 140 },
		'obernumberg': { 'top': 406, 'width': 140 },
		'pfarrkofen': { 'top': 149, 'width': 140 },
		'rieder_im_feld': { 'top': 253, 'width': 80 },
		'sulding': { 'top': 328, 'width': 140 }
	};

	$('.infobox').hover(function() {
		var thisId = $(this).attr('id').replace(/_info/, '');

		$('.infobox_text', this).css({
			'display': 'block'
		});

		$(this).stop().animate({
			'height': '288px',
			'width': '389px',
			'top': -(268 - infoBoxes[thisId].top) + 'px'
		}, 300);
	}, function() {
		var thisId = $(this).attr('id').replace(/_info/, '');

		$('.infobox_text', this).css({
			'display': 'none'
		});

		$(this).stop().animate({
			'height': '20px',
			'width': infoBoxes[thisId].width + 'px',
			'top': infoBoxes[thisId].top + 'px'
		}, 250);
	});
	// Landkarte end

});


