var curImage = 0;
var homeImageAnimation;
var home_animation_on = false;
var imageContainerToggler = true;
//var file_name = 'Headgrafik_';
//var homeAnimationImages = [file_name + "1.jpg",file_name + "2.jpg",file_name + "3.jpg",file_name + "4.jpg",file_name + "5.jpg",file_name + "6.jpg",file_name + "7.jpg",file_name + "8.jpg",file_name + "9.jpg",file_name + "10.jpg",file_name + "11.jpg",file_name + "12.jpg",file_name + "13.jpg",file_name + "14.jpg",file_name + "15.jpg",file_name + "16.jpg",file_name + "17.jpg"];

$(document).ready(function() {

	// navi top (from new seo concept)
	var seo_wrapper_height = $('.list_houses_seo_wrapper').height();
	
	if (seo_wrapper_height === null) {
		$('#navi_wrapper').css({
			'top': '20px'
		});
	}

	// content-height
	var navi_spitzl = $('#navi_spiztl').position();
	var news_spitzl = $('#news_spitzl').position();
	var testimonials_spitzl = $('#testimonials_spitzl').position();

	var navi_top = (navi_spitzl == null) ? 0 : navi_spitzl.top;
	var news_top = (news_spitzl == null) ? 0 : news_spitzl.top;
	var testimonials_top = (testimonials_spitzl == null) ? 0 : testimonials_spitzl.top;

	var content_height = news_top + navi_top + testimonials_top;

	// content_height for search (search == home_id)
	if ($('#search_wrapper').length > 0) {
		content_height = 570;
	}

	$('#testimonials_content_wrapper').css({'top': navi_top + 'px'});
	$('#news_wrapper').css({'top': navi_top + testimonials_top + 'px'});
	$('#content_wrapper').css({'min-height': content_height + 'px'});

	// Logo
	$('#logo').hover(function() {
		$(this).stop().animate({
			'top': '5px',
			'height': '123px'
		});
	}, function() {
		$(this).stop().animate({
			'top': '20px',
			'height': '103px'
		});
	});

	// Dekra logo
	$('#dekra_logo').hover(function() {
		$(this).stop().animate({
			'top': '0px'
		});
	}, function() {
		$(this).stop().animate({
			'top': '-30px'
		});
	});

	/* Main menu BEG */
//	$('#menu_wrapper a.level2_item').click(function(event) {
//		event.preventDefault();
//
//		$(this).parent('div.level2_wrapper').trigger('mouseover');
//	});

	$('.level2_wrapper').mouseover(function() {
		var $this = this;

/*		$(this).css({
			'background-image': 'url(/website/themes/drevohaus.de/images/menu_level2_hover.png)'
		});
*/
		var height = 0;
		$('.level3_item', this).each(function() {
			height = height + $(this).outerHeight();
		});

	       var leftOffset = 721 - $(this).position().left;

	       var newLeft = 0;
	       if (leftOffset < 290) {
			       newLeft = -(290 - 144);
	       }

		$('.level3_wrapper', this).css({
			'left': newLeft + 'px'
		}).stop().animate({
			'height': height + 'px'
		}, 200).hover(function() {
			$('a.level2_item', $this).addClass('hover');
		}, function() {
			$('a.level2_item', $this).removeClass('hover');
		});
;
	}).mouseout(function() {
/*		$(this).css({
			'background-image': 'url(/website/themes/drevohaus.de/images/menu_pipe.png)'
		});
*/
		$('.level3_wrapper', this).stop().animate({
			'height': '0px'
		}, 200);
	});

	$('.level3_item').hover(function() {
		$(this).stop().animate({
			'backgroundPosition': '-290px 0px'
		}, 1500, 'easeOutElastic');
	}, function() {
		$(this).stop().animate({
			'backgroundPosition': '0px 0px'
		});
	});

	/* Main menu END */


	/* Sliding header BEG */

	if (homeAnimationImages) {
		$.each(homeAnimationImages, function(index, element) {
			$('<a></a>').click(function() {
				clearTimeout(homeImageAnimation);

				$('#headerTrigger a.current').removeClass('current');
				$(this).addClass('current');

				showNextHomeImage(index);
			}).appendTo('#headerTrigger');
		});

		$('#headerTrigger a').eq(0).trigger('click');
	}

/*
	// Header animation old
	$('#headerTrigger').click(function() {
		if (homeAnimationImages) {
			if (home_animation_on) {
				window.clearInterval(homeImageAnimation);

				home_animation_on = false;

				$(this).html('abspielen');
			} else {
				showNextHomeImage();

				homeImageAnimation = window.setInterval("showNextHomeImage()", 7000);

				home_animation_on = true;

				$(this).html('anhalten');
			}
		}
	}).trigger('click');
*/

	/* Sliding header END */


	/* Navi BEG */
	$('#navi a.navi_inactive span').css({'opacity': 0});
	$('#navi a.navi_inactive').hover(function() {
		$('span', this).stop().animate({
			'opacity': 1
		});
	}, function() {
		$('span', this).stop().animate({
			'opacity': 0
		});
	});

	/* Navi END */

	/* Recommendation BEG */
	$("#fe_recommend").click(function (e) {
		e.preventDefault();

		$("#recommendation_container").animate({
			'top': '-400px',
			'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': '#ffffff'
		});
	}, function() {
		$(this).stop().animate({
			'backgroundColor': '#c0c0c0'
		});
	});

	/* Home search BEG */
	$('#search_input').focus(function() {
		if ($(this).val() == 'Textsuche...') {
			$(this).val('');
			$(this).css({
				'color': '#447b30',
				'font-weight': 'bold'
			});
		}
	});

	$('#search_input').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Textsuche...');
			$(this).css({
				'color': '#d6d6d6',
				'font-weight': 'normal'
			});
		}
	});

	$('.frontend_arrow_point').hover(function() {
		$(this).stop().animate({
			'background-position': '10px 0px'
		});
	}, function() {
		$(this).stop().animate({
			'background-position': '0px 0px'
		});
	});

	/* Home search END */

	/* Houses overview BEG */
	$('.houses_home_item').hover(function() {
		$(this).stop().animate({
			'color': '#447b30'
		}, 100);
	}, function() {
		$(this).stop().animate({
			'color': '#000000'
		}, 1000);
	});
	/* Houses overview END */

	/* Facebook share 
	$('#fb_share').click(function(e) {
		e.preventDefault();
		var FBShareWin = window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location.href), "FBShareWin", "width=600,height=400,status=yes,scrollbars=no,resizable=no");
		FBShareWin.focus();
	});
*/

});


/*
function showNextHomeImage() {
	if (curImage >= homeAnimationImages.length) {
		curImage = 0;
	}

	var nextImage = new Image;
	$(nextImage).attr({
//		'src': '/userfiles/home_animation/' + homeAnimationImages[curImage]
		'src': '/website/dynamic_images/' + homeAnimationImages[curImage]
	}).load(function() {
		$('#graphic_header').animate({
			'opacity': 0
		}, 500, function() {
			$(this).css({
//				'backgroundImage': 'url(/userfiles/home_animation/' + homeAnimationImages[curImage] + ')'
				'backgroundImage': 'url(/website/dynamic_images/' + homeAnimationImages[curImage] + ')'
			}).animate({
				'opacity': 1
			}, 2000);

			++curImage;
		});
	});
}*/
function showNextHomeImage(showImage) {
	clearTimeout(homeImageAnimation);
	$('#graphic_header').stop();

	if (!isNaN(showImage)) {
		curImage = showImage;
	}

	if (curImage >= homeAnimationImages.length) {
		curImage = 0;
	}

	var nextImage = new Image;
	$(nextImage).attr({
		'src': '/website/dynamic_images/' + homeAnimationImages[curImage]
	}).load(function() {

		$('#graphic_header' + ((imageContainerToggler) ? 1 : 2)).stop().animate({
			'opacity': 0
		}, 1500);

		$('#graphic_header' + ((imageContainerToggler) ? 2 : 1)).css({
			'backgroundImage': 'url(/website/dynamic_images/' + homeAnimationImages[curImage] + ')'
		}).stop().animate({
			'opacity': 1
		}, 1500, function() {
			$('#headerTrigger a').removeClass('current').eq(curImage).addClass('current');

			homeImageAnimation = setTimeout("showNextHomeImage()", 6000);

			++curImage;

			imageContainerToggler = !imageContainerToggler;
		});
	});
}

function toggleModalLayer(showhide) {
	if (showhide == 'show') {
		$('#feModalLayer').css({
			'opacity': 0,
			'display': 'block',
			'width': '100%',
			'height': $(document).height() + 'px'
		}).animate({
			'opacity': 0.7
		});
	} else if (showhide == 'hide') {
		$('#feModalLayer').animate({
			'opacity': 0
		}, false, function() {
			$(this).css({
				'display': 'none'
			});
		});
	}
}


