﻿//change float delimiter to . for calculations
function parseLocalNum(num) {
	return +(num.replace(",", "."));
}

// fade in cover layer
function showCoverLayer() {
	if (Modernizr.rgba) {
		$('.cover').fadeIn();
	} else {
		$('.cover').show().css({ 'opacity': 0.6 });
	}
}

// initialize input field placeholder as "in-field-labels" (necessary for older browsers ...)
function initPlaceholder(selector) {
	$(selector)
		.focus(function () {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		})
		.blur(function () {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		})
		.blur()
		.parents('form').submit(function () {
			$(this).find('[placeholder]').each(function () {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});

}

/* PORTAL */
(function ($) {
	$(function () {
		initPlaceholder('input[placeholder], textarea[placeholder]');
	});

	// display simple search mask
	$(function () {
		$('header .open-search-overlay').click(function (e) {
			e.preventDefault();
			showCoverLayer()
			$('.search-overlay').fadeIn();

			$('.search-overlay li')
			.find('input:checked')
			.parent()
			.addClass('current');
		});
		$('.search-overlay li input').change(function () {
			$('.search-overlay li')
			.removeClass('current')
			.find('input:checked')
			.parent()
			.addClass('current');
		});
	});
	
	// open links with class "flash" in popup, if flash version is supported
	$(function () {
		if (swfobject.hasFlashPlayerVersion("10.0.0")) {
			$("a.flash").each(function () {
				$(this).bind('click', function (e) {
					e.preventDefault();
					var param = 'status=' + $(this).data('status') + ' ' +
						',menubar=' + $(this).data('menubar') + ' ' +
						',scrollbars=' + $(this).data('scrollbars') + ' ' +
						',resizable=' + $(this).data('resizable') + ' ' +
						',height=' + $(this).data('height') + ' ' +
						',width=' + $(this).data('width'),
						popup = window.open(this.href, $(this).data('name'), param);
					popup.focus();
				});
			});
		}
	});

	// hide cover & (search-)overlays when cover is clicked
	$(function () {
		$('.cover, .closeSearchMask').click(function () {
			$('.cover, .search-overlay').fadeOut();
		});
	});

	// wrap tables with div that has a class and a span
	$(function () {
		$('.channel-subsite table:not(.styled), .channel-start table:not(.styled)').wrap('<div class="data-table" />');
		$('.channel-subsite .data-table, .channel-start .data-table').append('<span />');
		if ($.browser.msie && $.browser.version < 9) {
			$("table tr:nth-child(2n+1)").addClass("even");
		}
		$('table.marke-detail')
			.parent().find('>span').hide()
			.end()
			.find('.btn_paging')
			.click(function (e) {
				e.preventDefault();
				if (!$(this).hasClass('expanded')) {
					$(this)
						.addClass('expanded')
						.parents('table').find('tbody').show()
						.parents('.data-table').find('>span').show();
				} else {
					$(this)
						.removeClass('expanded')
						.parents('table').find('tbody').hide()
						.parents('.data-table').find('>span').hide();
				}
			});
	});

	// form error-messages
	$(function () {
		$(".error-messages").delegate("a", "click", function (e) {
			e.preventDefault();
			$($(this).attr("href")).focus();
		});
	});

	// js print button
	$(function () {
		$('.shareAndPrint').append('<a href="#" class="print"><span></span>Drucken</a>');
		$('.shareAndPrint').delegate('.print', 'click', function () {
			window.print();
		});
	});

	// my data, registration, contact
	$(function () {
		$('.myData').delegate('.btnEdit, .btnBack', 'click', function (e) {
			e.preventDefault();
			$(this).parents('section').toggleClass('editmode');
		});
	});

	// sitemap - distinguisch click on link / click on open-close-button
	$(function () {
		$('#sitemap-accordion h2 a').click(function (e) {
			e.stopPropagation();
			location.href = $(this).attr('href');
		});
	});

	// product scroller on channel startpages
	$(function () {
		$('.product-carousel').each(function (index) {
			$(this).jCarouselLite({
				btnNext: $(this).parent().find('.next'),
				btnPrev: $(this).parent().find('.prev'),
				start: 0,
				scroll: 3
			});
		});
	});

	// latest recipes - link on list element
	$('.box-teaser.latest-recipes')
		.css({'cursor': 'pointer'})
		.delegate('li', 'click', function () {
			window.location = $(this).find('a').attr('href');
		})
		.delegate('a', 'click', function (e) {
			e.preventDefault();
		});

	// billa kocht fuer sie: close-button
	$('div.menu-map').delegate('.btn_paging', 'click', function (e) {
		e.preventDefault();
		$(this).parents('.menu-map').slideUp(450, 'easeInOutQuart').html('');
	});


	// flyer image map
	$(function () {
		// use area alt attribute to generate class attribute
		// reason: lack of .NET support for image maps
		var areas = $('map area');

		areas.each(function (index, area) {
			var jqArea = $(area);
			var alt = jqArea.attr('alt');
			jqArea.addClass(alt);
		});

		// map hover
		$('div.flyer-map map')
			.delegate('area', 'mouseover', function () {
				$('#' + $(this).attr('class')).addClass('mouseover');
			})
			.delegate('area', 'mouseout', function () {
				$('#' + $(this).attr('class')).removeClass('mouseover');
			})
			.delegate('area', 'click', function (e) {
				e.preventDefault();
			});
	});

	$(function() {
		// prp table: tr clickable
		$('table.prp-table')
			.delegate('tr', 'click', function() {
				window.location = $(this).find('td a').attr('href');
			})
			.delegate('a', 'click', function(e) {
				e.preventDefault();
			});


		// Searchlist li clickable
		$('ul.searchlist')
			.delegate('li', 'click', function() {
				window.location = $(this).find('h3 a').attr('href');
			})
			.delegate('a', 'click', function(e) {
				e.preventDefault();
			});

		// various fixes for old internet explorer
		if ($.browser.msie && $.browser.version < 9) {
			$("div.poll-navigation ol li:nth-child(2n)").addClass("even");
		}
	});
})(jQuery);


/* FRISCH GEKOCHT */
(function ($) {
	/* fg subnavi */
	$(function () {
		if ($.browser.msie && $.browser.version < 9) {
			$("#main.fg #subnavigation.fg-subnavigation>ul>li:nth-child(2n)").addClass("even");
		}
	});

	// latest recipes - link on list element
	$('.fg .sheets .newRecipes')
		.css({'cursor': 'pointer'})
		.delegate('li', 'click', function () {
			window.location = $(this).find('a').attr('href');
		})
		.delegate('a', 'click', function (e) {
			e.preventDefault();
		});

	// slide up / down cooking steps
//    $(function () {
//	$('#main.fg .recipe-detail .cookingsteps h2')
//		.css({ 'cursor': 'pointer' })
//		.click(function () {
//			$(this).parent().find('ul').slideToggle(250, 'easeInOutQuart');
//		});
//    });

	// slide up / down videopage preparation
    $(function () {
	    $('#main.fg .videopage .preparation-container h2')
		    .css({'cursor':'pointer'})
		    .click(function(){
			    $(this).parent().find('article').slideToggle(250, 'easeInOutQuart');
		    });
    });

	// js print button
	$(function () {
		$('#main.fg .recipe-actions .actions')
			.delegate('.print', 'click', function (e) {
				e.preventDefault();
				window.print();
			});
	});

	// product scroller on channel startpages
//	$(function () {
//		$(".recipe-actions .jCarouselLite").jCarouselLite({
//			btnNext: ".next",
//			btnPrev: ".prev",
//			vertical: false,
//			visible: 2,
//			circular: true,
//			easing: 'easeOutQuart',
//			speed: 800,
//			start: 0,
//			scroll: 1
//		});
//	});

	// fg overlay - send2friend - character counter
	$(function() {
		$('body')
			.delegate('div.fg-overlay-content.s2f textarea', 'keyup', function() {
				var c = $(this).val().length;
				if (c >= 2000) {
					$(this).val($(this).val().substring(0, 1999));
				}
				$('div.fg-overlay-content.s2f .character-counter').text(2000 - c);
			});
	});

	// hide fg overlay
	$(function () {
		$('body').delegate('div.fg-overlay-content.s2f .btnCancel, div.fg-overlay', 'click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			if ($(e.target).hasClass('fg-overlay') || $(e.target).parents('a').hasClass('btnCancel')) {
				$('.cover, .fg-overlay').fadeOut();
			}
		});
	});


	/*datepicker*/
	$(function () {
		$(".datepicker").datepicker({
			yearRange: '1900:2010',
			changeMonth: true,
			changeYear: true,
            dateFormat: 'dd.mm.yy',
            dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
            monthNamesShort: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez']
		});
	});

	$(function () {
		$( ".slider" ).slider({
			range: "min",
			value:0,
			min: 0,
			max: 300,
			step: 10,
			slide: function( event, ui ) {
				$(".amount").val(ui.value);
			}
		});
		$(".amount").val($(".slider").slider("value"));
	});
	
	$(function () {
		$( ".slider1" ).slider({
			range: "kcal",
			value:0,
			min: 0,
			max: 2000,
			step: 100,
			slide: function( event, ui ) {
				$(".amount1").val( ui.value);
			}
		});
		$(".amount1").val($(".slider1").slider("value"));
	});
})(jQuery);


/* SHOP */
(function ($) {
	// show / hide categories overlay on shop start page
	$(function () {
		$('div.categories-compact')
			.mouseenter(function(){
				$('div.categories-compact div.overlay').fadeIn(150, 'easeInSine');
			})
			.mouseleave(function(){
				$('div.categories-compact div.overlay').fadeOut(150, 'easeOutSine');
			})
	});

	// show / hide mini shopping cart overlay
	$(function () {
		$('div.mini-cart')
			.bind('mouseenter', function(){
				showMiniCart();
			}).bind('mouseleave', function () {
				hideMiniCart();
			});
		$('a.mini-cart-button')
			.bind('focus', function(){
				showMiniCart();
			}).bind('blur', function () {
				hideMiniCart();
			});
	});

	var addRules;
	// show / hide extra fields for delivery address dependant on checked radio button
	$(function(){
		$('.choose-delivery-address input:radio').click(function(){
			var index = $(this).index('.choose-delivery-address input:radio');
			if (index === 1) {
				$('.separate-delivery-address').fadeIn(150, 'easeOutSine');
                $(".txtDeliveryFirstname").rules("add", {
                    required: true,
                    maxlength: 20,
                    messages:{
                        required: err_li_firstname_req,
                        maxlength: err_li_firstname_len
                    }
                });
                $(".txtDeliverySurname").rules("add", {
                    required: true,
                    maxlength: 30,
                    messages:{
                        required: err_li_surname_req,
                        maxlength: err_li_surname_len
                    }
                });

                $(".txtDeliveryCompany").rules("add", {
                    maxlength: 50,
                    messages:{
                        maxlength: err_li_company_len
                    }
                });

                $(".txtDeliveryStreet").rules("add", {
                    required: true,
                    maxlength: 40,
                    messages:{
                        required: err_li_adress_req,
                        maxlength: err_li_adress_len
                    }
                });

                $(".txtDeliveryZIP").rules("add", {
                    required: true,
                    maxlength: 4,
                    minlength: 4,
                    number: true,
                    messages:{
                        required: err_li_zip_req,
                        maxlength: err_li_zip_cor,
                        minlength: err_li_zip_cor,
                        number: err_li_zip_cor
                    }
                });

               $(".txtDeliveryCity").rules("add", {
                    required: true,
                    maxlength: 30,
                    messages:{
                        required: err_li_city_req,
                        maxlength: err_li_city_len
                    }
                });
			} else {
				$('.separate-delivery-address').fadeOut(150, 'easeOutSine');
                if(addRules === 1){
                    $(".txtDeliveryFirstname").rules("remove", "required maxlength");
                    $(".txtDeliverySurname").rules("remove", "required maxlength");
                    $(".txtDeliveryCompany").rules("remove", "maxlength");
                    $(".txtDeliveryStreet").rules("remove", "required maxlength");
                    $(".txtDeliveryZIP").rules("remove", "required maxlength minlength number");
                    $(".txtDeliveryCity").rules("remove", "required maxlength");
                    $('#aspnetForm').valid();
                }
			}
            addRules = 1;
        });
		$('.choose-delivery-address input:radio:checked').trigger('click');
	});

	// hide product overlay
	$(function () {
		$('body').delegate('.ShopProductOverlay, .CloseProductOverlay', 'click', function (e) {
			e.stopPropagation();
			if ($(e.target).hasClass('ShopProductOverlay') || $(e.target).hasClass('CloseProductOverlay')) {
				$('.cover, .ShopProductOverlay').fadeOut();
			}
		});
	});
})(jQuery);




function showMiniCart() {
	if ($('div.cart-overlay:visible').length > 0)
		return;
	$('div.cart-overlay')
	.css({ 'left': '-999em' })
	.show(0, function () {
		var maxHeight = Math.floor(($(window).height() - 330) / 20) * 20;
		if ($(this).find('ul').css({ 'height': 'auto' }).height() > maxHeight) {
			$(this)
				.find('ul').css({ 'height': maxHeight })
				.end()
				.find('.more-products-in-cart').show();
		} else {
			$(this).find('.more-products-in-cart').hide();
		}
	})
	.hide(0)
	.css({ 'left': -197 })
	.slideDown(150, "easeInSine");
}

function hideMiniCart() {
	$('div.cart-overlay').slideUp(150, "easeOutSine");
}

// helper to calculate height and position of shop product overlay
function adjustProductOverlayPosition(overlay) {
	var o_height, // height of overlay
		oc_height, // height of overlay content
		oc_top,	// top of overlay content
		oc_padding;

	o_height = parseInt($(overlay).height());
	oc_padding = parseInt($(overlay).find('>div').css('padding-top')) + parseInt($(overlay).find('>div').css('padding-bottom'));
	oc_height = $(overlay).find('>div').height() + oc_padding;

	if (o_height > oc_height) {
		$(overlay)
			.find('>div')
			.css({
				'top': parseInt((o_height - oc_height)/2),  // ( full height - overlay content height ) / 2
				'overflow' : 'visible'
			});
	} else {
		$(overlay)
			.find('>div')
			.css({
				'top': 0,
				'height': (o_height - oc_padding),
				'overflow': 'auto'
			});
	}
}

