var curImage = 0;
var homeImageAnimation;
var home_animation_on = false;
var imageContainerToggler = true;

var currentSearchModelId = 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' + ((imageContainerToggler) ? 1 : 2)).stop().animate({
			'opacity': 0
		}, 1500);
		
		$('#home_animatedcar' + ((imageContainerToggler) ? 2 : 1)).css({
			'backgroundImage': 'url(/userfiles/home_animation/' + homeAnimationImages[curImage] + ')'
		}).stop().animate({
			'opacity': 1
		}, 1500, function() {
			$('#home_animatedcar_trigger a').removeClass('current').eq(curImage).addClass('current');

			homeImageAnimation = setTimeout("showNextHomeImage()", 6000);

			++curImage;

			imageContainerToggler = !imageContainerToggler;
		});
	});
}
*/
/*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);

var subMenuTimer;
var carSearchMenuTimer;
var carSearchMenuStayOpened = false;

function hideSubMenu() {
	$('#menu_sub_container').stop().animate({
		'height': '0px'
	}, 200);
}

function hideCarSearchMenu() {
	if (!carSearchMenuStayOpened) {
		$('#menu_carsearch').stop().animate({
			'height': '0px'
		}, 200);
	}
}

$(document).ready(function() {
//	$('#home_slider *').draggable({
//		'drag': function(e, ui) {
//			$('#dragInfo').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 */
	$('#menu_wrapper a').hover(function() {
		if (!carSearchMenuStayOpened) {
			$(this).trigger('click');
		}
	}, function() {
		subMenuTimer = setTimeout("hideSubMenu()", 500);
	}).click(function(e) {
		e.preventDefault();

		hideCarSearchMenu();
		clearTimeout(subMenuTimer);

		var parentCon = $(this).attr('rel');

		$('#menu_sub_container').stop().animate({
			'height': '297px'
		}, 200);
		$('#menu_sub_container div').animate({
			'top': '-297px'
		}, 200, function() {
			$('#menu_sub_container a').removeClass('show');
			$('#menu_sub_container a[rel=' + parentCon + ']').addClass('show');

			$(this).animate({
				'top': '0px'
			}, 200);
		});

//		$(this).trigger('mouseenter');
	});

	$('#menu_sub_container').hover(function() {
		clearTimeout(subMenuTimer);
	}, function() {
		subMenuTimer = setTimeout("hideSubMenu()", 500);
	});
	/* Main menu END */


	/* 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')
		;

		// Google analytics
		_gaq.push(['_trackEvent', 'Formular gesandt', 'Click', 'Angebot weiterempfohlen']);

		$('#form_recommendation').attr({
			method: 'post'
		}).submit();
	});
	/* Recommendation END */
	
	
	// Galleries
	$('.fe_sd_gallery_item').hover(function() {
		$(this).stop().animate({
			'backgroundColor': '#BD2725'
		});
	}, function() {
		$(this).stop().animate({
			'backgroundColor': '#D7D7D7'
		});
	});

	// 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');
*/

	// B2B menu item
	$('#fe_top_menu a[rel="menuitem206"]').click(function(e) {
		e.preventDefault();

		var $this = this;

		$('#b2b_registration_button,#b2b_password_button').attr({
			'href': $($this).attr('href')
		});

		var b2bMenuItemLeft = $(this).position().left;

		if ($('#b2b_login_container').hasClass('isOpen')) {
			$('#b2b_login_container').stop().animate({
				'top': '-369px'
			}).removeClass('isOpen');
		} else {
			$('#b2b_login_container').css({
				'left': (b2bMenuItemLeft - 50) + 'px'
			}).stop().animate({
				'top': '0px'
			}).addClass('isOpen');
		}
	});

	$('#b2b_login_user,#b2b_login_pass').focus(function() {
		if ($(this).val() == 'E-Mail' || $(this).val() == 'Passwort') {
			$(this).val('');
		}
	});
	$('#b2b_login_user').blur(function() {
		if ($(this).val() == '') {
			$(this).val('E-Mail');
		}
	});
	$('#b2b_login_pass').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Passwort');
		}
	});

	$('#b2b_submit_button').click(function() {
		var form = $('<form></form>').attr({
			'method': 'post',
			'action': $('#b2b_registration_button').attr('href')
		});


		$('#b2b_login_user,#b2b_login_pass').clone().appendTo(form);
		
		$('<input />').attr({
			'name': 'dealer_stat',
			'value': 'exec',
			'type': 'hidden'
		}).appendTo(form);

		form.appendTo('body').submit();
	});

	// Main menu carsearch
	/*$('#menu_wrapper a[rel=parentCon_221]').unbind('mouseenter mouseleave').hover(function() {
		hideSubMenu();
		clearTimeout(carSearchMenuTimer);

		$('#menu_carsearch').stop().animate({
			'height': '297px'
		}, 200);
	}, function() {
		carSearchMenuTimer = setTimeout("hideCarSearchMenu()", 500);
	}).click(function() {
		window.location.href = '/Autos/';
	});*/
	$('#menu_wrapper a[rel=parentCon_221]').unbind('mouseenter mouseleave click').click(function() {
		window.location.href = '/Autos/';
	});

	$('#carSearchForm').hover(function() {
		clearTimeout(carSearchMenuTimer);
	}, function() {
		carSearchMenuTimer = setTimeout("hideCarSearchMenu()", 500);
	});


	// Car search
	$('.carCategoriesBox a').click(function() {
		$(this).toggleClass('active');

		var keys = $(this).attr('rel').split(',');

		if ($(this).hasClass('active')) {
			$.each(keys, function(index, key) {
				$('#category option[value="' + key + '"]').attr({
					'selected': 'selected'
				});
			});
		} else {
			$.each(keys, function(index, key) {
				$('#category option[value="' + key + '"]').attr({
					'selected': false
				});
			});
		}

		$('#category').trigger('change');
	});

	// Search change
	$('#carSearchForm select, #carSearchForm input[type="checkbox"]').change(function() {
		$.post('/Autos/fetchSearcherData/', $('#carSearchForm').serialize(), function(data) {
			$('#carsearch_results strong').text(data.cnt); 
		}, 'json');
	});


	$('#home_carsearch_manufacturer').change(function() {
		var curManu = $(this).val();

		$('#home_carsearch_model').empty().append('<option value="">&nbsp;</option>');

		if (!isNaN(curManu) && curManu) {
			$.post('/Autos/fetchModels/', {
				'manufacturer': curManu
			}, function(data) {
				$.each(data[curManu], function(k, modelObj) {
					$('#home_carsearch_model').append('<option value="' + modelObj.id + '" ' + ((modelObj.id == currentSearchModelId) ? 'selected="selected"' : '') + '>' + modelObj.bez + ' (' + modelObj.cnt + ')</option>');
				});

				//reloadSearcherData();
				$('#transmission').trigger('change');
			}, 'json');
		} else {
			//reloadSearcherData();
		}
	});

	$('#home_animatedcar1').rsanimator({
		'images': homeAnimationImages,
		'width': 960,
		'height': 297,
		'indicator': {
			'container': $('#home_animatedcar_trigger'),
			'elementClass': 'carTrigger',
			'elementClassActive': 'current'
		}
	});
		
});


