$(document).ready(function() {
	
	// main_widget_search
	var search_item_selector = $('#search_item');
	
	search_item_selector.focus(function() {
		$('.search_wrapper').addClass('search_wrapper_hover');
		var search_item_value = search_item_selector.val();
		
		if (search_item_value == 'Suchartikel hier eingeben') {
			search_item_selector.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').removeClass('search_wrapper_hover');
		var search_item_value = search_item_selector.val();
		
		if (search_item_value == 'Suchartikel hier eingeben' || search_item_value == '') {
			search_item_selector.val('Suchartikel hier eingeben');
		}
		$('#layout_modal').hide();
		$('.search_widget_wrapper').css({
			'z-index': '1'
		});
		
		$('#searchResultBox').empty().remove();
	});
	
	search_item_selector.live('keyup', function(e) {
		e.preventDefault();

		var search = $(this).val();
		
		if (search.length > 2) {
			var jqxhr = $.ajax({
				'url': '/index.php?website_modul=website_search&search=' + search
			});
			
			jqxhr.success(function(data){
				
				$('#searchResultBox').empty().remove();
				
				searchbox = $('<div id="searchResultBox">'+data+'</div>')
					.appendTo('#search_result');
			});
			
			jqxhr.error(function(data) {
				alert(data);
			});
		} 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
	var login_text_selector = $('.main_widget_login_wrapper input[type="text"]');

	login_text_selector.focus(function() {
		var login_text_value = login_text_selector.val();
		if (login_text_value == 'E-Mail Adresse') {
			login_text_selector.val('');
		}
	});

	login_text_selector.blur(function() {
		var login_text_value = login_text_selector.val();
		
		if (login_text_value == 'E-Mail Adresse' || login_text_value == '') {
			login_text_selector.val('E-Mail Adresse');
		}
	});


	var login_pass_selector = $('.main_widget_login_wrapper input[type="password"]');

	login_pass_selector.focus(function() {
		var login_pass_value = login_pass_selector.val();
		if (login_pass_value == 'Passwort') {
			login_pass_selector.val('');
		}
	});

	login_pass_selector.blur(function() {
		var login_pass_value = login_pass_selector.val();

		if (login_pass_value == 'Passwort' || login_pass_value == '') {
			login_pass_selector.val('Passwort');
		}
	});
	
	
	// main_widget_shoppingcart
	$('.shoppingcart_icon_hover').css({
		'opacity': 0
	});

	$('.main_widget_shoppingcart').hover(function() {
		$('.shoppingcart_icon_hover').stop().animate({
			'opacity': 1
		});
	}, function() {
		$('.shoppingcart_icon_hover').stop().animate({
			'opacity': 0
		});
	});
	
	$('#shoppingcart_icon').click(function(e){
		e.preventDefault();
		$('#shoppingcart_kontext').slideToggle();
	});
	
	
	$('a.no_children').unbind('click').bind('click', function(e) {
		e.preventDefault();
	});
	
	
	// item_categories_dropdown START
	$('.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_wrapper').unbind('mouseleave').bind('mouseleave', function() {
		$('.sub_outer_wrapper', this).stop().animate({
			'height': '0px'
		}, 200);
	});

	// item_categories_dropdown END
	
	
	
	
	
});

