$(document).ready(function() {
	var alwaysEmptyOnFocus = false;

	// Replace existing focus event actions
	$('#search_item').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#search_item').val();

			if (search_item_value === $('#search_item').attr('data-defaultv') || search_item_value === '') {
				$('#search_item').val($('#search_item').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});
	$('#search_item2').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#search_item2').val();

			if (search_item_value === $('#search_item2').attr('data-defaultv') || search_item_value === '') {
				$('#search_item2').val($('#search_item2').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});
	$('#search_item3').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#search_item3').val();

			if (search_item_value === $('#search_item3').attr('data-defaultv') || search_item_value === '') {
				$('#search_item3').val($('#search_item3').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});

	$('.search_widget_wrapper form .search_button').click(function(e) {
		e.preventDefault();

		var allOk = true;

		if ($('#search_item').val() === $('#search_item').attr('data-defaultv')) {
			$('#search_item').val('alle/all');
		}
		if (!$('#search_item2').val() || $('#search_item2').val() === $('#search_item2').attr('data-defaultv')) {
			if ($('#search_item3').val() && $('#search_item3').val() !== $('#search_item3').attr('data-defaultv')) {
				$('#search_item2').val($('#search_item3').val());
				$('#search_item3').val('');
			} else {
				allOk = false;
			}
		}
		if ($('#search_item3').val() === $('#search_item3').attr('data-defaultv')) {
			$('#search_item3').val('');
		}

		if (allOk) {
			$(this).prop('disabled', true).val('Lade');

			$('.search_widget_wrapper .loading_indicator').css({
				display: 'block'
			});
			$('#layout_laden').css({
						'height': $('body').height() + 'px',
						'opacity': '0.5'
			}).show();
			$('#layout_laden_window').css({
				display: 'block'
			});
			$(this).parent('form').submit();
		} else {
			alert('Bitte geben Sie einen Suchbegriff ein');
		}
	});




	$('#searchcontent_item').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#searchcontent_item').val();

			if (search_item_value === $('#searchcontent_item').attr('data-defaultv') || search_item_value === '') {
				$('#searchcontent_item').val($('#searchcontent_item').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});
	$('#searchcontent_item2').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#searchcontent_item2').val();

			if (search_item_value === $('#searchcontent_item2').attr('data-defaultv') || search_item_value === '') {
				$('#searchcontent_item2').val($('#searchcontent_item2').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});
	$('#searchcontent_item3').unbind('focus').bind('focus', function() {
		$('#account_kontext,#shoppingcart_kontext').css({
			'display': 'none'
		});

		$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');

		var search_item_value = $(this).val();

		if (search_item_value === $(this).attr('data-defaultv') || alwaysEmptyOnFocus) {
			$(this).val('');
		}
	}).bind('blur', function() {
		setTimeout(function() {
			$('.search_wrapper,.search_widget_wrapper').removeClass('search_wrapper_hover');

			var search_item_value = $('#searchcontent_item3').val();

			if (search_item_value === $('#searchcontent_item3').attr('data-defaultv') || search_item_value === '') {
				$('#searchcontent_item3').val($('#searchcontent_item3').attr('data-defaultv'));
			}

			$('#searchResultBox').empty().remove();
		}, 250);
	});

	$('.search_widget_wrapper form .searchcontent_button').click(function(e) {
		e.preventDefault();

		var allOk = true;

		if ($('#searchcontent_item').val() === $('#searchcontent_item').attr('data-defaultv')) {
			$('#searchcontent_item').val('alle/all');
		}
		/*if (!$('#searchcontent_item2').val() || $('#searchcontent_item2').val() === $('#searchcontent_item2').attr('data-defaultv')) {
			if ($('#searchcontent_item3').val() && $('#searchcontent_item3').val() !== $('#searchcontent_item3').attr('data-defaultv')) {
				$('#searchcontent_item2').val($('#searchcontent_item3').val());
				$('#searchcontent_item3').val('');
			} else {
				allOk = false;
			}
		}
		if ($('#searchcontent_item3').val() === $('#searchcontent_item3').attr('data-defaultv')) {
			$('#searchcontent_item3').val('');
		}*/

		if (allOk) {
			$(this).prop('disabled', true).val('Lade');

			$('.search_widget_wrapper .loading_indicator').css({
				display: 'block'
			});
			$('#layout_laden').css({
				'height': $('body').height() + 'px',
				'opacity': '0.5'
			}).show();
			$('#layout_laden_window').css({
				display: 'block'
			});
			$(this).parent('form').submit();
		} else {
			alert('Bitte geben Sie einen Suchbegriff ein');
		}
	});

	// Replace existing click event actions
	$('.main_widget_account_wrapper').unbind('click').bind('click', function(e) {
		e.preventDefault();

		if (!$('.main_widget_account_wrapper').hasClass('main_widget_account_wrapper_active') && $('.main_widget_shoppingcart2').hasClass('main_widget_shoppingcart2_active')) {
			$('#shoppingcart_kontext').css({
				'display': 'none'
			});
			$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		}

		if ($('.main_widget_account_wrapper').hasClass('main_widget_account_wrapper_active')) {
			$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');

			$('#account_kontext').css({
				'display': 'none'
			});
		} else {
			$('.main_widget_account_wrapper').addClass('main_widget_account_wrapper_active');

			$('#account_kontext').css({
				'display': 'block'
			});
		}
	});

	// Replace existing click event actions
	$('.main_widget_shoppingcart2').unbind('click').bind('click', function(e) {
		e.preventDefault();

		if ($(this).hasClass('empty_card')) {
			window.location.href = '/Warenkorb/';
		}

		if (!$('.main_widget_shoppingcart2').hasClass('main_widget_shoppingcart2_active') && $('.main_widget_account_wrapper').hasClass('main_widget_account_wrapper_active')) {
			$('#account_kontext').css({
				'display': 'none'
			});
			$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		}

		if ($('.main_widget_shoppingcart2').hasClass('main_widget_shoppingcart2_active')) {
			$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');

			$('#shoppingcart_kontext').css({
				'display': 'none'
			});
		} else {
			$('.main_widget_shoppingcart2').addClass('main_widget_shoppingcart2_active');

			$('#shoppingcart_kontext').css({
				'display': 'block'
			});
		}

	});


	/* Logo rodeo
	 -----------------------------------------------------*/
	/*$('#logoRodeoSpool div').hover(function() {
		$(this).stop().animate({
			'opacity': 1
		}, 200);
	}, function() {
		$(this).stop().animate({
			'opacity': 0.3
		}, 200);
	});*/


	/* Navigation
	 -----------------------------------------------------*/
	$.post('/index.php?website_modul=website_menu', {
		config: {
			fullWidth: true,
			depth: 2,
			splitInParentGroups: true
		}
	}, function(data) {
		$('#main_menu_ajax_container').html(data);

		var subElements = $('#main_widget_item_categories_dropdown .main_navigation_subnav .subNavRoller .subbox').length;
		var subBoxWidth = $('#main_widget_item_categories_dropdown .main_navigation_subnav .subNavRoller .subbox').width();
		var minHeight = 250;

		// Set max sub nav height 
		var offset = $('#main_widget_item_categories_dropdown .main_navigation_subnav').offset();
		var topOffset = 0;
		if (offset) {
			topOffset = offset.top;
		}
		var maxSubHeight = $(window).height() - topOffset - 50;
		$('#main_widget_item_categories_dropdown .main_navigation_subnav').css({
			'maxHeight': maxSubHeight + 'px'
		});

		// Set roller width
		$('#main_widget_item_categories_dropdown .main_navigation_subnav .subNavRoller').css({
			'width': (subBoxWidth * subElements) + 'px'
		});

		// Add sub vert. scroller
		$('#main_widget_item_categories_dropdown .main_navigation_subnav .subNavRoller .subbox').each(function() {
			var $this = this;

			$(this).css({
				'maxHeight': maxSubHeight + 'px'
			});

			if ($(this).height() < minHeight) {
				$(this).css({
					'height': minHeight + 'px'
				});
			}

			var box = $('.subboxScroller', $this);
			var boxRealHeight = box.outerHeight();

			if (boxRealHeight > maxSubHeight) {
				$(this).addClass('scrollable');

				// Scroll up
				$('.subboxScrollDown', this).bind('click', function(e) {
					e.preventDefault();

					var curPos = box.position().top;
					var newPos = curPos - maxSubHeight;
					var maxPos = maxSubHeight - boxRealHeight - (2 * $(this).height());

					if (newPos < maxPos) {
						newPos = maxPos;
					}

					$('.subboxScroller', $this).animate({
						'top': newPos + 'px'
					});
				});

				// Scroll down
				$('.subboxScrollUp', this).bind('click', function(e) {
					e.preventDefault();

					var curPos = box.position().top;
					var newPos = curPos + maxSubHeight;
					var maxPos = 0;

					if (newPos > maxPos) {
						newPos = maxPos;
					}

					$('.subboxScroller', $this).animate({
						'top': newPos + 'px'
					});
				});
			}
		});


		var defaultDelay = 200;
		var microDelay = 5;
		var delay = defaultDelay;
		var animationDuration = 200;
		var delayTimeout, menuItemTimeout;

		// Replace existing hover event actions
		$('#main_widget_item_categories_dropdown').bind('mouseenter', function() {
			delayTimeout = setTimeout(function() {
				delay = microDelay;
			}, defaultDelay);
		}).bind('mouseleave', function(e, param1) {
			clearTimeout(delayTimeout);
			clearTimeout(menuItemTimeout);
			delay = defaultDelay;

			$('#main_widget_item_categories_dropdown .main_navigation_subnav').stop().animate({
				'height': '0px'
			}, animationDuration, function() {
				if (param1 != 'hideSubsOnly') {
					$('.menu_item_hover').trigger('defocus');
				}

				$(this).removeClass('visible');
			});
		});

		$('#main_widget_item_categories_dropdown .menu_item').unbind('mouseenter mouseleave').bind('mouseenter', function() {
			var button = $(this);

			var isEmpty = $(this).hasClass('isEmpty');

			menuItemTimeout = setTimeout(function() {
				$('.menu_item_hover').trigger('defocus');

				button.addClass('menu_item_hover');

				$('<span class="itemRoof"></span>').appendTo(button);
				$('<span class="itemBottom"></span>').appendTo(button);

				var buttonRel = button.attr('data-rel');

				if (!isEmpty) {
					var subBox = $('.subbox[data-rel="' + buttonRel + '"]');
					var newHeight = $(subBox).outerHeight();
					if (newHeight > maxSubHeight) {
						newHeight = maxSubHeight;
					} else if (newHeight < minHeight) {
						newHeight = minHeight;
					}

					var pos = $(subBox).position();

					if (pos) {
						var newLeft = $(subBox).position().left;

						$('#main_widget_item_categories_dropdown .main_navigation_subnav').addClass('visible').stop().animate({
							'height': newHeight + 'px'
						}, animationDuration);

						$('#main_widget_item_categories_dropdown .main_navigation_subnav .subNavRoller').stop().animate({
							'left': -newLeft + 'px'
						}, animationDuration);
					}
				} else {
					$('#main_widget_item_categories_dropdown').trigger('mouseleave', ['hideSubsOnly']);
				}
			}, delay);
		}).bind('defocus', function() {
			var button = $(this);

			button.removeClass('menu_item_hover');

			$('.itemRoof', button).remove();
			$('.itemBottom', button).remove();
		});
	});



	// Click action on newsletter widget
	$('.newsletter_widget_wrapper').click(function() {
		//window.location.href = '/newsletter_registration/';
	});

	// Remove live search
	$('#search_item').unbind('keyup');

	// Left category navigation
	$('.itemcategory_widget_wrapper .level_1.active').click(function(e) {
		e.preventDefault();

		if ($(this).hasClass('isClosed')) {
			$('.itemcategory_widget_wrapper .level_2,.itemcategory_widget_wrapper .level_3').css({
				display: 'block'
			});
		} else {
			$('.itemcategory_widget_wrapper .level_2,.itemcategory_widget_wrapper .level_3').css({
				display: 'none'
			});
		}

		$(this).toggleClass('isClosed');
	});
	$('.itemcategory_widget_wrapper .level_2.active').click(function(e) {
		e.preventDefault();

		if ($(this).hasClass('isClosed')) {
			$('.itemcategory_widget_wrapper .level_3').css({
				display: 'block'
			});
		} else {
			$('.itemcategory_widget_wrapper .level_3').css({
				display: 'none'
			});
		}

		$(this).toggleClass('isClosed');
	});

});
