
var curImage = 0;

var homeImageAnimation;

var home_animation_on = false;

function showNextHomeImage(showImage) {
	clearTimeout(homeImageAnimation);
	$('#home_animatedcar').stop();
	
	if (!isNaN(showImage)) {
		curImage = showImage;
	}

	if (curImage >= homeAnimationImages.length) {
		curImage = 0;
	}

	var nextImage = new Image;
	$(nextImage).attr({
		'src': '/userfiles/home_animation/' + homeAnimationImages[curImage]
	}).load(function() { 
		$('#home_animatedcar').stop().animate({
			'opacity': 0
		}, 500, function() {
			$('#home_animatedcar_trigger a').removeClass('current').eq(curImage).addClass('current');

			$(this).css({
				'backgroundImage': 'url(/userfiles/home_animation/' + homeAnimationImages[curImage] + ')'
			}).animate({
				'opacity': 1
			}, 500, function() {
				homeImageAnimation = setTimeout("showNextHomeImage()", 4000);

				++curImage;
			});
		});
	});
}

//homeImageAnimation = window.setInterval("showNextHomeImage()", 5000);

$(document).ready(function() {
//	$('#home_animatedcar_trigger').draggable({
//		'drag': function(e, ui) {
//			$(this).html(ui.position.left + ' / ' + ui.position.top);
//		}
//	});

	$.each(homeAnimationImages, function(index, element) {
		$('<a></a>').click(function() {
			clearTimeout(homeImageAnimation);

			$('#home_animatedcar_trigger a.current').removeClass('current');
			$(this).addClass('current');

			showNextHomeImage(index);
		}).appendTo('#home_animatedcar_trigger');
	});

	$('#home_animatedcar_trigger a').eq(0).trigger('click');

	/* Footer sides */
	$('#fe_footer_sides').css({
		'top': ($('#fe_wrapper-container').height() - 193) + 'px',
		'left': '0px'
	});


	/* Main menu BEG */
	var mainMenuItemWidth = 958 / $('#menu_wrapper div.level2_wrapper').length;

	$('#menu_wrapper div.level2_wrapper,#menu_wrapper div.level2_wrapper a.level2_item').css({
		'width': mainMenuItemWidth + 'px'
	});

	$('#menu_wrapper a.level2_item').click(function(event) {
		if ($('a.level3_item', $(this).parent()).length > 0) {
			event.preventDefault();

			$(this).parent('div.level2_wrapper').trigger('mouseover');
		}
	});

	$('.level2_wrapper').mouseover(function() {
		var $this = this;

		var height = 0;

		$('.level3_item', $this).each(function() {
			height = height + $(this).outerHeight();
		});

		var leftOffset = 958 - $($this).position().left;

		var newLeft = 0;
		if (leftOffset < 225) {
			newLeft = -(225 - mainMenuItemWidth);
		}

		$('.level3_wrapper', $this).stop().css({
			'left': newLeft + 'px'
		}).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 */


	/* Recommendation BEG */
	$("#recommendation_toggler").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': '#9F80B5'
		});
	}, function() {
		$(this).stop().animate({
			'backgroundColor': '#59366D'
		});
	});

	// Animation
	$('#home_animationTrigger').click(function() {
		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');

	$('#home_carsearch_manufacturer').change(function() {
		var curModel = $(this).val();

		$('#home_carsearch_model').empty().append('<option value="">&nbsp;</option>');
 
		if (!isNaN(curModel) && curModel) { 
			$.post('/Fahrzeuge/fetchModels/', {
				'manufacturer': curModel
			}, function(data) { 
				$.each(data[curModel], function(k, modelObj) {
					$('#home_carsearch_model').append('<option value="' + modelObj.id + '">' + modelObj.bez + ' (' + modelObj.cnt + ')</option>');
				});
			}, 'json');
		}
	});
	
	$('#header_searchContainer').fadeTo('fast', 0.5);
	
	$('#header_searchContainer').mouseenter(function() {
		$('#header_searchContainer').stop().fadeTo('fast', 1.0);
	});
	
	$('#header_searchContainer').mouseleave(function() {
		$('#header_searchContainer').stop().fadeTo('fast', 0.5);
	});
});


