$(document).ready(function() {

	// Manufacturer Widget
	//
	// on change event check if selecton is the default
	// and if it isn't make a submit, since there is no sumbit
	// button this is required
	$('#main_widget_manufacturer').find('.manufacturer_select').change(function() {
		var manufacturer = $(this).val();

		if (manufacturer != '') {
			$('#main_widget_manufacturer').find('form').trigger('submit');
		}
	});

	// main_widget_search

	$('#search_item').focus(function() {
		$('.search_wrapper,.search_widget_wrapper').addClass('search_wrapper_hover');
		var search_item_value = $(this).val();
		if (search_item_value == $(this).attr('data-defaultv')) {
			$(this).val('');
		}

		$('#layout_modal').css({
			'height': $('body').height() + 'px',
			'opacity': '0.5'
		}).show();

		$('.search_widget_wrapper').css({
			'z-index': '9999'
		});
	});

	$('#layout_modal').click(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'));
		}
		$('#layout_modal').hide();
		$('.search_widget_wrapper').css({
			'z-index': '1'
		});

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

	var liveSearch_jqxhr = false;
	
	$('#search_item').bind('keyup', function(e) {
		e.preventDefault();
		
		if (liveSearch_jqxhr) {
			liveSearch_jqxhr.abort();
		}
		
		var search = $(this).val();
		var item_category = $('#search_item_category').val();

		if (search.length > 2) {
			liveSearch_jqxhr = $.ajax({
				url: '/index.php?website_modul=website_search&nosess=true&search=' + encodeURIComponent(search) + '&item_category=' + item_category
			});

			liveSearch_jqxhr.success(function(data) {

				$('#searchResultBox').empty().remove();

				$('<div id="searchResultBox">' + data + '</div>').appendTo('#search_result');
			});
		} else {

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




	// main_widget_menu
	$('.item_group_level1').unbind('click').bind('click', function(e) {
		e.preventDefault();

		// selectors (4 performance)
		var item_group_level1_wrapper = $(this).parents('.item_group_level1_wrapper');
		var item_group_level1_wrapper_state = (item_group_level1_wrapper.hasClass('open')) ? 'open' : false;

		// heights
		var level1_height = $(this).height();
		var level2_wrapper_height = $(this).siblings('.item_group_level2_wrapper').height();

		$('.item_group_level1_wrapper').stop().animate({
			'height': level1_height + 'px'
		}).removeClass('open');

		item_group_level1_wrapper.stop().animate({
			'height': (item_group_level1_wrapper_state == 'open') ? level1_height + 'px' : (level1_height + level2_wrapper_height) + 'px'
		});

		if (item_group_level1_wrapper_state == 'open') {
			item_group_level1_wrapper.removeClass('open');
		} else {
			item_group_level1_wrapper.addClass('open');
		}
	});


	$('.higher_level a').hover(function() {
		$('.hover', this).stop().animate({
			'height': '35px',
			'top': '0px'
		}, 100);
	}, function() {
		if (!$(this).hasClass('active')) {
			$('.hover', this).stop().animate({
				'height': '0px',
				'top': '35px'
			}, 1000);
		}
	});

	/*	$('.higher_level a').each(function() {
	 if ($(this).hasClass('active')) {
	 $('.hover', this).css({
	 'height': '35px',
	 'top': '0px'
	 });
	 
	 //			if (!$(this).parents('.item_group_level1_wrapper').hasClass('open')) {
	 //				$(this).parents('.item_group_level1_wrapper').children('.item_group_level1').trigger('click');
	 //			}
	 }
	 });
	 */
	// main_widget_login
	$('#account_kontext .login_name').focus(function() {
		var login_text_value = $(this).val();
		if (login_text_value == $(this).attr('data-defaultv')) {
			$(this).val('');
		}
	});

	$('#account_kontext .login_name').blur(function() {
		var login_text_value = $(this).val();
		if (login_text_value == $(this).attr('data-defaultv') || login_text_value == '') {
			$(this).val($(this).attr('data-defaultv'));
		}
	});

	$('#account_kontext .login_password').focus(function() {
		var login_pass_value = $(this).val();
		if (login_pass_value == $(this).attr('data-defaultv')) {
			$(this).val('');
		}
	});

	$('#account_kontext .login_password').blur(function() {
		var login_pass_value = $(this).val();

		if (login_pass_value == $(this).attr('data-defaultv') || login_pass_value == '') {
			$(this).val($(this).attr('data-defaultv'));
		}
	});


	// main_widget_shoppingcart

	$('.main_widget_shoppingcart2').click(function(e) {
		e.preventDefault();

		if (!$('.main_widget_shoppingcart2').hasClass('main_widget_shoppingcart2_active') && $('.main_widget_account_wrapper').hasClass('main_widget_account_wrapper_active')) {
			$('#account_kontext').slideToggle();
			$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		}

		$('#shoppingcart_kontext').slideToggle();
		if ($('.main_widget_shoppingcart2').hasClass('main_widget_shoppingcart2_active')) {
			$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		}
		else {
			$('.main_widget_shoppingcart2').addClass('main_widget_shoppingcart2_active');
		}

	});

	$('.main_widget_language').click(function(e) {
		e.preventDefault();

		if (!$('.main_widget_language').hasClass('main_widget_language_active') && $('.main_widget_language_wrapper').hasClass('main_widget_language_wrapper_active')) {
			$('#language_kontext').slideToggle();
			$('.main_widget_language_wrapper').removeClass('main_widget_language_wrapper_active');
		}

		$('#language_kontext').slideToggle();
		if ($('.main_widget_language').hasClass('main_widget_language_active')) {
			$('.main_widget_language').removeClass('main_widget_language_active');
		}
		else {
			$('.main_widget_language').addClass('main_widget_language_active');
		}

	});

	$('#login_save_session').click(function(e) {
		e.preventDefault();

		if ($('#login_save_session').hasClass('login_save_session_active')) {
			$('#login_save_session').removeClass('login_save_session_active');
			$('#field_login_save_session').val(0);
		}
		else {
			$('#login_save_session').addClass('login_save_session_active');
			$('#field_login_save_session').val(1);
		}
	});

	$('.main_widget_account_wrapper').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').slideToggle();
			$('.main_widget_shoppingcart2').removeClass('main_widget_shoppingcart2_active');
		}

		$('#account_kontext').slideToggle();
		if ($('.main_widget_account_wrapper').hasClass('main_widget_account_wrapper_active')) {
			$('.main_widget_account_wrapper').removeClass('main_widget_account_wrapper_active');
		}
		else {
			$('.main_widget_account_wrapper').addClass('main_widget_account_wrapper_active');
		}
	});

	$('a.no_children').unbind('click').bind('click', function(e) {
		e.preventDefault();
	});


	// item_categories_dropdown START
	var isMobile = false;
	var mobileAgents = ['android', 'webos', 'iphone', 'ipad', 'ipod', 'blackberry', 'symbian'];

	for (agent in mobileAgents) {
		if (navigator.userAgent.match(new RegExp(mobileAgents[agent], 'i'))) {
			isMobile = true;

			break;
		}
	}

	var m1 = 0;
	var m2 = 0;

	if (isMobile == true) {
		$('.menu_item_wrapper').unbind('mouseenter').bind('mouseenter', function() {
			var sub_wrapper_height = $('.sub_inner_wrapper', this).outerHeight();
			$('.sub_outer_wrapper', this).stop().animate({
				'height': sub_wrapper_height + 'px'
			}, 100);

			$('.menu_item', this).addClass('menu_item_hover');
		});

		$('.menu_item_wrapper').unbind('mouseleave').bind('mouseleave', function() {
			$('.sub_outer_wrapper', this).stop().animate({
				'height': '0px'
			}, 200);

			$('.menu_item', this).removeClass('menu_item_hover');


		});

		$('.menu_top_link').unbind('click').bind('click', function(e) {
			e.preventDefault();
		});
	}
	else {
		$('.menu_item_wrapper').hover(function() {
			var buttonElement = $('.menu_item', this);
			var menuElement = $('.sub_outer_wrapper', this);
			var sub_wrapper_height = $('.sub_inner_wrapper', this).outerHeight();

			var t_id = $(this).attr('rel');

			buttonElement.addClass('menu_item_hover');

			m2 = $(this).attr('rel');
			m1 = 1;

			setTimeout(function() {
				if (m1 == 1 && t_id == m2) {
					menuElement.stop().animate({
						'height': sub_wrapper_height + 'px'
					}, 200);
				}

			}, 500);

		}, function() {
			var buttonElement = $('.menu_item', this);
			var menuElement = $('.sub_outer_wrapper', this);
			buttonElement.removeClass('menu_item_hover');
			m1 = 0;
			setTimeout(function() {
				menuElement.stop().animate({
					'height': '0px'
				}, 200);
			}, 500);

		});


		/*
		 $('.menu_item_wrapper').unbind('mouseover').bind('mouseover', function() {
		 var sub_wrapper_height = $('.sub_inner_wrapper', this).outerHeight();
		 $('.sub_outer_wrapper', this).stop().animate({
		 'height': sub_wrapper_height + 'px'
		 }, 100);
		 
		 $('.menu_item', this).addClass('menu_item_hover');
		 });
		 
		 
		 $('.menu_item_wrapper').unbind('mouseout').bind('mouseout', function() {
		 $('.sub_outer_wrapper', this).stop().animate({
		 'height': '0px'
		 }, 200);
		 
		 $('.menu_item', this).removeClass('menu_item_hover');
		 });*/
	}
	// item_categories_dropdown END



	$('.content_box_structure_list_image').unbind('mouseenter').bind('mouseenter', function() {
		$(this).stop().animate({
			'opacity': '0.75'
		}, 150);
	});

	$('.content_box_structure_list_image').unbind('mouseleave').bind('mouseleave', function() {
		$(this).stop().animate({
			'opacity': '1'
		}, 1500);
	});


	/* Logo rodeo
	 -----------------------------------------------------*/
	if (typeof logoRodeoImages === 'object') {
		var rodeoSpool = $('#logoRodeoSpool');
		var curImgC;
		var imgCOverall = 0;
		var curItem;
		var curItemWidth = 0;
		var durationPer100px = 5000;
		var duration = 0;

		$.each(logoRodeoImages, function(k, img) {
			curImgC = $('<div><img src="' + img + '" /></div>').appendTo('#logoRodeoSpool');

			imgCOverall += curImgC.outerWidth(true);
		});

		//alert(imgCOverall);

		if (logoRodeoActive) {
			var logoRodeoScroller = function() {
				curItem = $('div', rodeoSpool).first();
				curItemWidth = curItem.outerWidth(true);

				duration = durationPer100px / (100 / curItemWidth);

				$(curItem).clone().appendTo(rodeoSpool);

				$(rodeoSpool).css({
					width: ($(this).width() + curItemWidth * 2) + 'px'
				}).animate({
					left: -curItemWidth + 'px'
				}, duration, 'linear', function() {
					//$(curItem).appendTo(rodeoSpool);
					$(curItem).remove();

					$(rodeoSpool).css({
						left: '0px',
						width: ($(this).width - curItemWidth) + 'px'
					});

					logoRodeoScroller();
				});
			};

			setTimeout(logoRodeoScroller, 100);
			//logoRodeoScroller();
		}
	}


	/* Footer contact
	 -----------------------------------------------------*/
	$('#footerContactParent h2').bind('click', function() {
		if ($('#footerContactParent').hasClass('isOpen')) {
			$('#footerContactParent').animate({
				'top': '-40px',
				'height': '40px'
			}).removeClass('isOpen');
		} else {
			$('#footerContactParent').animate({
				'top': '-406px',
				'height': '406px'
			}).addClass('isOpen');
		}
	});
	$('#footerContactParent button').bind('click', function(e) {
		e.preventDefault();
		
		if ($('#contact_forename').val() != '' && $('#contact_surname').val() != '' && $('#contact_e_mail').val() != '') {
			$("#contact_account_contact").val('ready_to_fly');
			$('#footerContactParent form').trigger('submit');
		}
	});
});
