$(document).ready(function() {

    var flashPath = $('a.staticFlashPath').attr("href"); // added by CSD, Mehdi Haresi: path to flash for accessing radiospot.swf
    var imagePath = $('a.staticImagePath').attr("href"); // added by CSD, Mehdi Haresi: path to images

    $("body").append(
    //'<script type="text/javascript" src="scripts/jquery.scrollTo-min.js"></script>'+  // moved to masterpage
	    '<script type="text/javascript">' +
		'$.scrollTo.defaults.axis = "xy";' +
		'</script>'/*+   // CSD - commented elements have been moved to masterpage
    	'<link rel="stylesheet" type="text/css" media="all" href="scripts/selectbox/css/jquery.selectbox.css" />'+
	    '<script type="text/javascript" src="scripts/selectbox/js/jquery.selectbox-0.6.1.js"></script>'*/
	);

    /* CONFIG */
    xOffset = 10;
    yOffset = 20;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result		
    /* END CONFIG */
    $("input").hover(function(e) {
        this.t = this.title;
        this.title = "";
        $("body").append("<p id='tooltip'>" + this.t + "</p>");
        $("#tooltip")
			.css("position", "absolute")
			.css("display", "none")
			.css("background-color", "#ece2c6")
			.css("padding", "5px")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.css("z-index", "10000000")
			.fadeIn("slow");
        if ($("#tooltip").text() == "") $("#tooltip").remove();
    },
	function() {
	    this.title = this.t;
	    $("#tooltip").remove();
	});
    $("input").mousemove(function(e) {
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });

    $("#no_javascript").css("display", "none");

    $("#navi .search input").val(""); /*CSD: changed selector */
    $("#navi .search input").val("SUCHE");  /*CSD: changed selector */

    // removed CSD-mbusch 06.08.2009: created dynamically at WNJaNatuerlichPromoElementControl
    //    //Home 
    //    $('.home #promo a.tvspot').click(function(e) {
    //    	$('.home .popup .content').append('<object type="application/x-shockwave-flash" style="width:545px; height:307px;" data="http://www.youtube.com/v/yYQmljFyXTE&amp;hl=de&amp;fs=1"><param name="movie" value="http://www.youtube.com/v/yYQmljFyXTE&amp;hl=de&amp;fs=1" /></object>');
    //    	$('.home .popup').fadeIn();
    //    	
    //    	setFullHeight($('.home .popup'));
    //    });
    //

    $('.home .popup .close').click(function(e) {
        e.preventDefault();
        $('.home .popup .content object').remove();
        $('.home .popup').fadeOut();
    });

    //Drucken
    $('#printforward .print').click(function(e) {
        e.preventDefault();
        window.print();
    });



    //Weiterempfehlen Popup
    $('#printforward .forward').click(function(e) {
        e.preventDefault();
        $('body').prepend('' +

    	'<div class="popup" id="forward_popup" style="display:none;">' +
    		'<div class="content" >' +
    			'<div class="header">' +
    				'<img src="GetAbsoluteUrl.axd?path=Resources/Static/Images/header_weiterleiten.png" alt="Weiterleiten" title="Weiterleiten" />' +
    				'<h2>"' + $('title').html() + '"</h2>Senden Sie diese Seite an einen Freund oder Bekannten.' +
    			'</div>' +
				'<form action="#" method="post" id="forward_form">' +
		            '<fieldset>' +
		            	'<label for="email_empfaenger">E-Mail Adresse Empfänger:</label>' +
		                '<input type="text" name="email_empfaenger" id="email_empfaenger" class="shaded" />' +
		            '</fieldset>' +
		            '<fieldset>' +
		            	'<label for="email_absender">E-Mail Adresse Absender:</label>' +
		                '<input type="text" name="email_absender" id="email_absender" class="shaded" />' +
		            '</fieldset>' +
		            '<fieldset>' +
		            	'<label for="name_absender">Name Absender</label>' +
		                '<input type="text" name="name_absender" id="name_absender" class="shaded" />' +
		            '</fieldset>' +
		            '<fieldset>' +
		            	'<label for="nachricht">Ihre Nachricht:</label>' +
		                '<textarea cols="61" rows="5" name="nachricht" id="nachricht" class="shaded"></textarea>' +
		            '</fieldset>' +
				'</form>' +
				'<div class="buttons">' +
		        	'<a id="forward_cancel" href="javascript:void();" class="button cancel">Abbrechen</a>' +
		        	'<a id="forward_commit" href="javascript:void();" class="button commit">Absenden</a>' +
		        '</div>' +
    			'<a href="javascript:void();" class="close"><span class="invisible">X</span></a>' +
    		'</div>' +
    	'</div>');

        $(document).scrollTop(0);

        $('#forward_popup').fadeIn("slow");
        setFullHeight($('#forward_popup'));

        $('#forward_popup .close, #forward_popup #forward_cancel').click(function(e) {
            e.preventDefault();
            $('#forward_popup').fadeOut();
        });

        $('#forward_popup #forward_commit').click(function(e) {
            e.preventDefault();
            var sending = false;

            var passFields = $('#name_absender').val() != "" && $('#nachricht').val() != "";
            var passEmails = checkMail($('#email_empfaenger').val()) && checkMail($('#email_absender').val());

            if (passEmails) {
                if (passFields && !sending) {
                    sending = true;

                    //SENDEN!!!
                    $('#forward_popup #forward_commit').html("<em>Wird gesendet ...</em>");
                    $('#forward_popup #forward_commit').animate({ opacity: 1.0 }, 850, function() {
                        var ajaxManager = $find("WNJaNatuerlichMasterPage_WNJaNatuerlichMasterPage_radAjaxManager");
                        var arguments = "SendForwardMail";
                        arguments += "#" + $('#email_empfaenger').val();
                        arguments += "#" + $('#email_absender').val();
                        arguments += "#" + $('#name_absender').val();
                        arguments += "#" + $('#nachricht').val();
                        ajaxManager.ajaxRequest(arguments);
                    });
                }
                else
                    alert("Bitte füllen Sie das Namens- und das Nachrichtenfeld aus.");
            }
            else
                alert("Bitte Geben Sie in beiden E-Mail Feldern korrekte Adressen an.");
        });
    });

    $('.open_tnb').click(function(e) {
        e.preventDefault();
        $('body').prepend('' +

    	'<div class="popup" id="tnb_popup" style="display:none;">' +
    		'<div class="content" >' +
    			'<div class="header">' +
    				'<h2>' + agbheadline + '</h2>' +
					'<br />' +
					'<p>' + agbtext + '</p>' +
					'<br />' +
    			'</div>' +
				'<div class="buttons">' +
		        	'<a id="tnb_cancel" href="javascript:void();" class="button cancel">Schließen</a>' +
		        '</div>' +
    			'<a href="javascript:void();" class="close"><span class="invisible">X</span></a>' +
    		'</div>' +
    	'</div>');

        $(document).scrollTop(0);

        $('#tnb_popup').fadeIn("slow");
        setFullHeight($('#tnb_popup'));

        $('#tnb_popup .close, #tnb_popup #tnb_cancel').click(function(e) {
            e.preventDefault();
            $('#tnb_popup').fadeOut();
        });

    });

    //E-Cards
    $('.ecards ul.ecards a').click(function(e) {
        e.preventDefault();
        $('.ecards #ecard_confirmation').css("display", "none");
        $('.ecards #ecard_send').css("display", "none");
        $(document).scrollTop(0);
        $('.ecards .popup').fadeIn();
        $('.ecards #ecard_edit').fadeIn();

        setFullHeight($('.ecards .popup'));
        //$('.ecards .popup').height($('body').height());
    });

    $('.ecards .popup .close, .ecards .popup #ecard_cancel').click(function(e) {
        e.preventDefault();
        $('.ecards #ecard_confirmation').fadeOut();
        $('.ecards #ecard_send').fadeOut();
        $('.ecards .popup').fadeOut();

        $('#name_empfaenger').val("");
        $('#name_absender').val("");
        $('#nachricht').val("");
        $('#email_empfaenger').val("");
        $('#email_absender').val("");
    });

    $('.ecards .popup #ecard_preview').click(function(e) {
        e.preventDefault();

        var passFields = $('#name_empfaenger').val() != "" && $('#name_absender').val() != "" && $('#nachricht').val() != "";
        var passEmails = checkMail($('#email_empfaenger').val()) && checkMail($('#email_absender').val());

        if (passFields) {
            if (passEmails) {
                $('.ecards #ecard_edit').fadeOut(function() {
                    $('.ecards #ecard_send p').html(nl2br($('#nachricht').val()));
                    $('.ecards #ecard_send').fadeIn();
                    CarryData();
                });
            }
            else
                alert("Bitte Geben Sie in beiden E-Mail Feldern korrekte Adressen an.");
        }
        else
            alert("Bitte füllen Sie beide Namensfelder und das Nachrichtenfeld aus.");
    });

    $('.ecards .popup #ecard_back').click(function(e) {
        e.preventDefault();
        $('.ecards #ecard_send').fadeOut(function() {
            $('.ecards #ecard_edit').fadeIn();
        });
    });

    $('.ecards .popup #ecard_commit').click(function(e) {
        e.preventDefault();
        $('.ecards #ecard_send').fadeOut(function() {
            $('.ecards #ecard_confirmation').fadeIn();
            SendCommit();
        });
    });

    $('.ecards .popup #ecard_sendagain').click(function(e) {
        e.preventDefault();
        $('.ecards #ecard_confirmation').fadeOut(function() {
            $('.ecards #ecard_edit').fadeIn();
        });
    });


    //Newsletter

    $("#newsletter_anmeldung .toggle,#subnavi_newsletter_anmeldung .toggle").click(function() {
        var display = $(this).parent().find(".formular").css("display");
        if ($(this).parent().find("#newsletter_confirm").css("display") == "block") return;
        if ($(this).parent().find("#newsletter_done").css("display") == "block") return;
        switch (display) {
            case "none":
                $(this).parent().find(".formular").slideDown("slow");
                $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").addClass("newsletter_active");
                break;
            case "block":
                $(this).parent().find(".formular").slideUp("slow");
                $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").removeClass("newsletter_active");
                break;
        }
    });

    $("#newsletter_formular").submit(function(e) { e.preventDefault(); });

    $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find(".newsletter_subscribe").click(function() {

        if (!checkMail($("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find("input[name='email']").val())) { alert("Bitte tragen Sie eine gültige E-Mail Adresse ein!"); return; }

        $("#newsletter_formular").slideUp("slow", function() {
            $("#newsletter_confirm").find("#newsletter_email_marker").html($("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find("input[name='email']").val());
            $("#newsletter_confirm").slideDown("slow");
        });
    });

    $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find(".newsletter_go_back").click(function() {
        $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find("input[name='email']").val("E-Mail Adresse");
        $("#newsletter_confirm").slideUp("slow", function() {
            $("#newsletter_formular").slideDown("slow");
        });
    });

    $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find(".newsletter_close").click(function() {
        $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find("input[name='email']").val("E-Mail Adresse");
        $("#newsletter_done").slideUp("slow", function() {
            $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").removeClass("newsletter_active");
        });
    });

    $("#newsletter_anmeldung, #subnavi_newsletter_anmeldung").find(".newsletter_confirm").click(function() {
        $("#newsletter_confirm").slideUp("slow", function() {

            /***** HIER AJAX CODE FÜR NEWSLETTER ANMELDUNG ******/
            sendMail();
            $("#newsletter_done").slideDown("slow")
            /*if (sendMail())
            $("#newsletter_done").slideDown("slow");
            else
            $("#newsletter_confirm").slideDown("slow");*/
        });


    });

    //Beliebteste und neueste Rezepte Navigation

    $(".recipe_promo").find(".scroll_vertical").attr("rel", "0");

    $(".recipe_promo").each(function() {

        anzahlPromo = $(this).find("li").length;

        if (anzahlPromo <= 3) return;

        $(this).find(".scroll_vertical").scrollTo(0, 0);

        //$(this).find(".navigation").append('<a class="navigate_left"><img src="gfx/arrow_left.png" alt="Zurueck" title="Zurueck" /></a>');
        $(this).find(".navigation").append('<a class="navigate_left"><img src="GetAbsoluteUrl.axd?path=Resources/Static/Images/arrow_left.png" width="6" height="9" alt="Zurueck" title="Zurueck" /></a>');
        for (var i = 0; i < Math.floor(anzahlPromo / 3); i++)
            $(this).find(".navigation").append('<a class="navigate_to" rel="' + i + '">' + (i + 1) + '</a>');
        //$(this).find(".navigation").append('<a class="navigate_right"><img src="gfx/arrow_right.png" alt="Vor" title="Vor" /></a>');
        $(this).find(".navigation").append('<a class="navigate_right"><img src="GetAbsoluteUrl.axd?path=Resources/Static/Images/arrow_right.png" width="6" height="9" alt="Vor" title="Vor" /></a>');

        $(this).find(".navigation").find(".navigate_to:first").addClass("active");

        // HIER NAVIGATIONSPUNKTE DEAKTIVIEREN

        $(this).find(".navigation").find(".navigate_to").click(function() {
            var scrollPos = parseInt($(this).attr("rel")) * 3;
            $(this).parents(".navigation").find("a.navigate_to").removeClass("active");
            $(this).addClass("active");
            if ($(this).parents(".recipe_promo").find(".scroll_vertical").find("li:eq(" + (scrollPos) + ")").length == 0) return;
            $(this).parents(".recipe_promo").find(".scroll_vertical").stop().scrollTo($(this).parents(".recipe_promo").find(".scroll_vertical").find("li:eq(" + (scrollPos) + ")"), 800);
            $(this).parents(".recipe_promo").find(".scroll_vertical").attr("rel", scrollPos);
        });

        $(this).find(".navigation").find(".navigate_right").click(function() {
            var scrollPos = parseInt($(this).parents(".recipe_promo").find(".scroll_vertical").attr("rel")) + 3;
            if ($(this).parents(".recipe_promo").find(".scroll_vertical").find("li:eq(" + (scrollPos) + ")").length == 0) return;
            $(this).parents(".navigation").find("a.navigate_to").removeClass("active");
            $(this).parents(".navigation").find("a.navigate_to:eq(" + Math.floor(scrollPos / 3) + ")").addClass("active");
            $(this).parents(".recipe_promo").find(".scroll_vertical").stop().scrollTo($(this).parents(".recipe_promo").find(".scroll_vertical").find("li:eq(" + (scrollPos) + ")"), 800);
            $(this).parents(".recipe_promo").find(".scroll_vertical").attr("rel", scrollPos);
        });

        $(this).find(".navigation").find(".navigate_left").click(function() {
            var scrollPos = parseInt($(this).parents(".recipe_promo").find(".scroll_vertical").attr("rel")) - 3;
            if (scrollPos < 0) return;
            $(this).parents(".navigation").find("a.navigate_to").removeClass("active");
            $(this).parents(".navigation").find("a.navigate_to:eq(" + Math.floor(scrollPos / 3) + ")").addClass("active");
            $(this).parents(".recipe_promo").find(".scroll_vertical").stop().scrollTo($(this).parents(".recipe_promo").find(".scroll_vertical").find("li:eq(" + (scrollPos) + ")"), 800);
            $(this).parents(".recipe_promo").find(".scroll_vertical").attr("rel", scrollPos);
        });

    });

    $(".recipe_results").attr("rel", "0");

    $(".recipe_results").each(function() {
        var pos = $(this).attr("rel");
        var anzahlPromo = $(this).find("li").length;
        if (anzahlPromo <= 3) return;

        $(this).append('<div class="fadeOut" style="position:absolute; display:none; top:42px; left:0px; width:550px; height:538px; background-color:#fbfaf5;"></div>');

        $(this).find("li").filter(":gt(8)").hide();

        $(this).find(".navigation").append('<a class="navigate_left"><img src="GetAbsoluteUrl.axd?path=Resources/Static/Images/arrow_left.png" alt="Zurueck" title="Zurueck" /></a>');

        for (var i = 0; i <= Math.floor(anzahlPromo / 9); i++) {
            $(this).find(".navigation").append('<a class="navigate_to" rel="' + i + '">' + (i + 1) + '</a>');
        }
        $(this).find(".navigation").append('<a class="navigate_right"><img src="GetAbsoluteUrl.axd?path=Resources/Static/Images/arrow_right.png" alt="Vor" title="Vor" /></a>');

        $(this).find(".navigation").find("a.navigate_to:first").after('<span class="dotsBefore">...</span>');
        $(this).find(".navigation").find("a.navigate_to:last").before('<span class="dotsAfter">...</span>');

        crop_navigation($(this));

    });


    $(".recipe_results").find(".navigate_right").click(function() {
        var pos = parseInt($(this).parents(".recipe_results").attr("rel")) + 9;
        anzahlPromo = $(this).parents(".recipe_results").find("li").length;
        if (pos >= anzahlPromo) return;

        $(this).parents(".recipe_results").find(".fadeOut").fadeIn("slow", function() {
            $(this).parents(".recipe_results").find("li").hide();
            $(this).parents(".recipe_results").find("li").slice(pos, pos + 9).show();
            $(this).parents(".recipe_results").attr("rel", pos);
            crop_navigation($(this).parents(".recipe_results"));
            $(this).fadeOut("slow");
        });

    });

    $(".recipe_results").find(".navigate_left").click(function() {
        var pos = parseInt($(this).parents(".recipe_results").attr("rel")) - 9;
        anzahlPromo = $(this).parents(".recipe_results").find("li").length;
        if (pos < 0) return;

        $(this).parents(".recipe_results").find(".fadeOut").fadeIn("slow", function() {
            $(this).parents(".recipe_results").find("li").hide();
            if (pos == 0) $(this).parents(".recipe_results").find("li").filter(":lt(" + (pos + 9) + ")").show();
            else $(this).parents(".recipe_results").find("li").slice(pos, pos + 9).show();
            $(this).parents(".recipe_results").attr("rel", pos);
            crop_navigation($(this).parents(".recipe_results"));
            $(this).fadeOut("slow");
        });

    });

    $(".recipe_results").find(".navigate_to").click(function() {
        var pos = parseInt($(this).attr("rel")) * 9;
        anzahlPromo = $(this).parents(".recipe_results").find("li").length;
        if (pos < 0) return;

        $(this).parents(".recipe_results").find(".fadeOut").fadeIn("slow", function() {
            $(this).parents(".recipe_results").find("li").hide();
            if (pos == 0) $(this).parents(".recipe_results").find("li").filter(":lt(" + (pos + 9) + ")").show();
            else $(this).parents(".recipe_results").find("li").slice(pos, pos + 9).show();
            $(this).parents(".recipe_results").attr("rel", pos);
            crop_navigation($(this).parents(".recipe_results"));
            $(this).fadeOut("slow");
        });

    });


    //String "E-Mail Adresse" bei Newsletter. Von Georg abgeschrieben.
    $("#newsletter_anmeldung input,#subnavi_newsletter_anmeldung input").focus(function() {
        if ($(this).val() == "E-Mail Adresse")
            $(this).val("");
    });

    $("#newsletter_anmeldung input,#subnavi_newsletter_anmeldung input").blur(function() {
        if ($(this).val() == "")
            $(this).val("E-Mail Adresse");
    });



    //Sitemap
    $("#sitemap_table").find("td").children("ul").each(function() {
        $(this).children("li").children("a").addClass("heading");
        $(this).children("li").children("a").addClass("heading").click(function(e) {
            if ($(this).parent("li").children("ul").is(":visible")) return;

            if ($(this).parent("li").children("ul").length == 0) return;
            e.preventDefault();
            //$("body").css("font-size","100px");
            $(this).parent("li").children("ul").slideDown("slow", function() { $("#content_sub_background, #kaefer").css("position", "static").css("position", "absolute"); });

            $(this).parent("li").children("ul").find("a").click(function(e) {
                var isSubnavi = $(this).parent("li").children("ul").length;

                if (isSubnavi == 0) return;
                if ($(this).parent("li").children("ul").is(":visible")) return;

                e.preventDefault();
                $(this).parent("li").children("ul").slideDown("slow", function() { $("#content_sub_background, #kaefer").css("position", "static").css("position", "absolute"); });
            });
        });
    });

    $(".sitemap_open_all").click(function(e) {
        $("#sitemap_table").find("td").children("ul").children("li").children("ul").find("ul").css("display", "block");
        $("#sitemap_table").find("td").children("ul").children("li").children("ul").slideDown("slow", function() { $("#content_sub_background, #kaefer").css("position", "static").css("position", "absolute"); });
    });

    $(".sitemap_close_all").click(function(e) {
        $("#sitemap_table").find("td").children("ul").children("li").find("ul").slideUp("slow", function() { $("#content_sub_background, #kaefer").css("position", "static").css("position", "absolute"); });
    });

    // SELECTBOX RECPLACEMENT

    // CSD - commented code (belongs to the rating feature)
    //$("select").selectbox();


    //String "SUCHE" bei Klick auf "SUCHE" löschen.
    $('#navi .search input').focus(function() { /* CSD changed Selector */
        if ($(this).attr("value") == "SUCHE")
            $(this).attr("value", "");
    });
    $('#navi .search input').blur(function() { /* CSD changed Selector */
        if ($(this).attr("value") == "")
            $(this).attr("value", "SUCHE");
    });

    $("#fontsize_plus").click(function(e) {
        e.preventDefault();
        var size = parseFloat($("body").css("font-size"));
        size += 1;
        $("body").css("font-size", size + "px");

        // set cookie
        eraseCookie('JaN_fontsize');
        createCookie('JaN_fontsize', size, 1);
    });

    $("#fontsize_minus").click(function(e) {
        e.preventDefault();
        var size = parseFloat($("body").css("font-size"));
        size -= 1;
        $("body").css("font-size", size + "px");

        // set cookie
        eraseCookie('JaN_fontsize');
        createCookie('JaN_fontsize', size, 1);
    });

    //############### SUCHE FILTER UND NAVIGATION #############################

    $(".search_results").children("ul").children("li").css("display", "none");

    $("#content_search_tabs li").each(function() {
        var filter = $(this).find("a").attr("class").split("_")[1];
        if (filter == "no-filter") return;
        var anzahl = $(".search_results li.category_" + filter).length;

        $(this).find("a").append('<span class="count">(' + anzahl + ')</span>');
    });

    var anzahl = $(".search_results").children("ul").children("li").length;
    $("#content_search_tabs li:eq(0)").find("a").append('<span class="count">(' + anzahl + ')</span>');

    var anzahl = $(".recipe_results li").length;
    //$("#recipe_results_header").append('<span class="count">(' + anzahl + ')</span>');

    $("#content_search_tabs a").click(function(e) {
        e.preventDefault();
        var filter = $(this).attr("class").split("_")[1];
        $(".search_results").children("ul").children("li").css("display", "none");
        //$(".search_results li.category_"+filter).css("display","block");
        //if(filter=="no-filter") $(".search_results li").css("display","block");

        $(this).parents("ul").find("li").removeClass("active");
        $(this).parents("li").addClass("active");

        activeFilter = filter;
        navigate(0);
    });

    navigate(0);



    // breadcrumb

    $("#breadcrumb, .breadcrumb").each(function() {
        var length = 0;
        $(this).find("a").each(function() {
            length += $(this).width();
        });

        $(this).find("a:first").after(' <span class="dots" style="display:none"> | ...</span>');

        var doIt = false;
        while (length > 380) {
            //alert(length);
            length = 0;
            $(this).find("span.dots").show();
            $(this).find("a:eq(1)").remove();
            $(this).find("a").each(function() {
                length += $(this).width();
            });
            doIt = true;
        }
        var code = $(this).html();
        if (doIt) $(this).html(code.replace(/(\|  ){2,}/g, "").replace(/(\|  \|)/g, "|"));
        else $(this).find("span.dots").hide();
        /*var anzahl = $(this).find("a").length;
        for(i=0; i<anzahl-4;i++)
        $(this).find("a:eq("+i+")").remove();
        if(anzahl>=4) $(this).prepend("...");*/
    });

    // ------------------------------------------------------------

    //Tiefen/Levels als class hinzufügen (level0, level1, ...):
    addLevels('#treenavi > ul > li', 0);
    function addLevels(s, level) {
        $(s + ' > a').addClass("level" + level);

        if ($(s + ":has(ul)").length > 0)
            addLevels(s + " > ul > li", ++level);
    }


    $(".script-open-iframe").click(function(e) {
        e.preventDefault();

        // Parse HEIGHT and WIDTH of IFRAME
        var width = parseInt($(this).attr("rel").split("x")[0]);
        var height = parseInt($(this).attr("rel").split("x")[1]);

        if (width == 0 || height == 0) return;

        // get full height of content H
        var bodyHeight = $('body').height();
        var windowHeight = $(window).height();
        var h = (bodyHeight > windowHeight ? bodyHeight : windowHeight);

        $(document).scrollTop(0);

        $("body").append('<div id="iframe-darker"></div><iframe id="iframe" scrolling="no" border="0" frameborder="0"></iframe><div id="iframe-closer"></div>');

        $("#iframe-darker")
			.css("position", "absolute")
			.css("top", "0px")
			.css("left", "0")
			.css("width", "100%")
			.css("background-color", "#000")
			.css("opacity", 0.7)
			.css("display", "none")
			.css("z-index", "50000");
        $("#iframe-darker").height(h);
        // ONLY IF FRAME BECAUSE OF NO DYNAMIC HEIGHT
        if (h < height) $("#iframe-darker").height(height + 50);
        $("#iframe-darker").fadeIn("slow");

        $("#iframe").attr("scrolling", "no");
        $("#iframe").attr("frameborder", "0");
        $("#iframe").attr("src", $(this).attr("href"));
        $("#iframe")
			.css("position", "absolute")
			.css("left", "550px")
			.css("top", "100px")
			.css("width", width + "px")
			.css("height", height + "px")
			.css("margin-left", (-width / 2) + "px")
			.css("border", "none")
			.css("z-index", "50100");

        $("#iframe-closer").append('<img src="' + imagePath + '/popup_closebtn.png" alt="Schließen" />');
        $("#iframe-closer")
			.css("position", "absolute")
			.css("top", "105px")
			.css("left", "550px")
			.css("margin-left", (width / 2 + 7) + "px")
        //.css("background-color","#fff")
			.css("font-weight", "bold")
			.css("cursor", "pointer")
        //.css("padding","7px")
			.css("z-index", "50200");

        $("#iframe-closer").click(function() {
            $(this).remove();
            $("#iframe").remove();
            $("#iframe-darker").fadeOut("slow", function() {
                $(this).remove();
            });
        });


    });


    // FLASH RADIOSPOTS --------------------------------------------

    $('ul.radiospots li').each(function() {

        //URI der mp3 speichern.
        var mp3uri = $('a.radiospot', this).attr("href");
        var bgimg = $('img', this).attr("src");
        var bgId = $('img', this).attr("id"); // Added by CSD, Mehdi Haresi: needed to pass to radiospot.swf because of asp.net generated id

        //Elemente entfernen.
        $('a.radiospot', this).remove();
        $('h4', this).replaceWith('<h4>' + $('h4 a', this).text() + '</h4>'); // Changed by CSD, Mehdi Haresi: makes no sense to replace h4 with b tag -> changed to h4 again

        //SWFObject ausführen.
        var vars = {};
        var params = { menu: 'false', wmode: 'transparent' };
        var attributes = { id: 'radiospot', name: 'radiospot' };

        swfobject.embedSWF(flashPath + "radiospot.swf?mp3uri=" + mp3uri + "&bgimg=" + bgimg, bgId, "175", "100", "9.0.0", flashPath + "js/expressInstall.swf", vars, params, attributes); // Changed by CSD, Mehdi Haresi: pass dynamic bg id
    });

    // ------------------------------------------------------------

    // FLASH PRODUKTÜBERSICHT/HTML CONNECTION ---------------------
    $('#treenavi > ul').addClass("active");

    $('#treenavi ul li a').click(function() {
        if ($(this).siblings().length == 0) //Keine Kindelemente mehr = benachbarte <ul>'s.
            return;

        //Eventuell offene "Kinder" zürücksetzen.
        $(this).parent().find('ul ul').slideUp(200);
        $(this).parent().find('ul').removeClass("active");
        $(this).parent().find('ul a').removeClass("active");

        //Nachbarn zurücksetzen.
        $(this).parent().siblings('li').find('ul').slideUp(200);
        $(this).parent().siblings('li').find('ul').removeClass("active");
        $(this).parent().siblings('li').find('a').removeClass("active");

        $(this).next('ul').slideDown(200);
        $(this).addClass("active");
        $(this).siblings("ul").addClass("active");

        deepestOpenSubtree = $(this).next('ul');

        //Flash Connection:
        var level = $(this).attr("class").split(" ")[0].substr(5); //Level aus class="..." extrahieren.
        if (thisMovie("men3d")) {
            thisMovie("men3d").jsMenuElementClicked($(this).attr("rel"), level);
        }
    });

    // set fontsize if cookie present
    setFontsize();

    // close tree
    closeTree();

    // ------------------------------------------------------------
});


// FLASH PRODUKTÜBERSICHT/HTML CONNECTION ---------------------

var deepestOpenSubtree;
var htmlTreeEnabled = true;

function highlightTreeItem(id) {
    //$("#treenavi ul.active li a").removeClass("active");
    //$("#treenavi ul.active li a").filter(function() {return $(this).attr("rel") == id;}).addClass("active");
}

function openSubTree(id) {
    with ($("#treenavi ul.active li a").filter(function() { return $(this).attr("rel") == id; })) {
        addClass("active");
        parent().siblings('li').find('ul').slideUp(200);
        parent().siblings('li').find('a').removeClass("active");

        next('ul').slideToggle(200);

        deepestOpenSubtree = next('ul');
    }
}

function closeSubTree() {
    deepestOpenSubtree.removeClass("active");
    deepestOpenSubtree.prev("a").removeClass("active");
    deepestOpenSubtree.slideUp(200);
    deepestOpenSubtree = deepestOpenSubtree.parent().parent();
}

function closeTree() {
    $('#treenavi ul ul').hide();
    $('#treenavi > ul').addClass("active");
}

function enableTree() {
    if (htmlTreeEnabled)
        return;

    $('#treenavi .sucheaktiv').remove();
    $('#treenavi > ul').fadeIn(200);

    htmlTreeEnabled = true;
}

function disableTree() {
    if (!htmlTreeEnabled)
        return;

    $('#treenavi > ul').fadeOut(200, function() {
        if ($('#treenavi .sucheaktiv').length < 1)
            $('#treenavi > ul').after("<div class='sucheaktiv'><em>Suche ist aktiv.</em></div>");
    });

    htmlTreeEnabled = false;
}

function thisMovie(movieName) {
    return window[movieName] ? window[movieName] : document[movieName];
}

// ------------------------------------------------------------
	
		
function crop_search_navigation(jQueryObject) {
		
    jQueryObject.find(".navigation").find("a.navigate_to").css("display", "inline").removeClass("active");
    jQueryObject.find(".navigation").find(".dotsBefore, .dotsAfter").css("display", "none");
		
    jQueryObject.find(".navigation").find("a.navigate_to").each(function() {
        var activePage = activeOffset / maxItems;
        if (parseInt($(this).text()) - 1 == activePage) $(this).addClass("active");

        if (parseInt($(this).text()) - 1 == 0) return;
        if (parseInt($(this).text()) - 1 + 1 == $(this).parents(".navigation").find("a.navigate_to").length) return;

        if (parseInt($(this).text()) - 1 == activePage) return;
        if (parseInt($(this).text()) - 1 - 1 == activePage) return;
        if (parseInt($(this).text()) - 1 + 1 == activePage) return;
		
        if (parseInt($(this).text()) - 1 - 2 == activePage) $(this).parents(".navigation").find(".dotsAfter").css("display", "inline");
        if (parseInt($(this).text()) - 1 + 2 == activePage) $(this).parents(".navigation").find(".dotsBefore").css("display", "inline");

        if (activePage == 0 && parseInt($(this).text()) - 1 == 2) return;
        if (activePage == $(this).parents(".navigation").find("a.navigate_to").length - 1 && parseInt($(this).text()) - 1 == $(this).parents(".navigation").find("a.navigate_to").length - 3) return;
		
        $(this).css("display", "none");
		
	});
	
    if (jQueryObject.find(".navigation").find("a.navigate_to").length <= 4) jQueryObject.find(".navigation").find(".dotsBefore, .dotsAfter").css("display", "none");
	
	
}


function crop_navigation(jQueryObject) {
	
    jQueryObject.find(".navigation").find("a.navigate_to").css("display", "inline").removeClass("active");
    jQueryObject.find(".navigation").find(".dotsBefore, .dotsAfter").css("display", "none");
	
    jQueryObject.find(".navigation").find("a.navigate_to").each(function() {
        var activePage = Math.floor(parseInt($(this).parents(".recipe_results").attr("rel")) / 9);
        if (parseInt($(this).attr("rel")) == activePage) $(this).addClass("active");
		
        if (parseInt($(this).attr("rel")) == 0) return;
        if (parseInt($(this).attr("rel")) + 1 == $(this).parents(".navigation").find("a.navigate_to").length) return;
		
		
		
        if (parseInt($(this).attr("rel")) == activePage) return;
        if (parseInt($(this).attr("rel")) - 1 == activePage) return;
        if (parseInt($(this).attr("rel")) + 1 == activePage) return;
		
        if (parseInt($(this).attr("rel")) - 2 == activePage) $(this).parents(".navigation").find(".dotsAfter").css("display", "inline");
        if (parseInt($(this).attr("rel")) + 2 == activePage) $(this).parents(".navigation").find(".dotsBefore").css("display", "inline");
		
        if (activePage == 0 && parseInt($(this).attr("rel")) == 2) return;
        if (activePage == $(this).parents(".navigation").find("a.navigate_to").length - 1 && parseInt($(this).attr("rel")) == $(this).parents(".navigation").find("a.navigate_to").length - 3) return;
		
        $(this).css("display", "none");
		
	});
	
    if (jQueryObject.find(".navigation").find("a.navigate_to").length <= 4) jQueryObject.find(".navigation").find(".dotsBefore, .dotsAfter").css("display", "none");
	
}


function setFullHeight(layer) {
	var bodyHeight = $('body').height();
	var windowHeight = $(window).height();
	var h = (bodyHeight > windowHeight ? bodyHeight : windowHeight);
	
	$(layer).height(h);
}

function checkMail(mail) {    
	return mail.search("@") != -1 && mail.substring(mail.search("@"), mail.length).indexOf('.') != -1;
}

function nl2br(str) {
    return str.replace(/([^>]?)\n/g, '$1' + '<br />' + '\n');
}

function navigate(offset) {
	
	/* IE BUG FIX */
	
	index = 0;
    if (offset < 0) offset = 0;
	if(activeFilter != "no-filter")
	{
		$(".search_results").children("ul").fadeOut("slow",function(){
            $(".search_results li.category_" + activeFilter).each(function() {
                if (!(index >= offset && index < offset + maxItems)) $(this).css("display", "none");
                else $(this).css("display", "block");
				index++;
			});
            $(".search_results").css("height", ($(".search_results").height()) + "px");
			$(this).fadeIn("slow");
		});
	}
	else
	{
		$(".search_results").children("ul").fadeOut("slow",function(){
			$(".search_results").children("ul").children("li").each(function() {
                if (!(index >= offset && index < offset + maxItems)) $(this).css("display", "none");
                else $(this).css("display", "block");
				index++;
			});
            $(".search_results").css("height", ($(".search_results").height()) + "px");
			$(this).fadeIn("slow");
		});
	}
	
	activeOffset = offset;
	$(".search_results").find(".navigation").empty();
    if (offset >= maxItems) $(".search_results").find(".navigation").append('<a href="javascript:navigate(' + (activeOffset - maxItems) + ')"><img src="' + $(".image_arrow_left").attr("src") + '"></a>');
    else $(".search_results").find(".navigation").append('<a class="navigate_left"><img src="' + $(".image_arrow_left").attr("src") + '" width="6" height="9"></a>');
	anzahlItems = 0;
    if (activeFilter != "no-filter") anzahlItems = $(".search_results li.category_" + activeFilter).length;
	else anzahlItems = $(".search_results").children("ul").children("li").length;
    var anzahlSeiten = anzahlItems / maxItems;
    for (i = 0; i < anzahlSeiten; i++) {
        $(".search_results").find(".navigation").append('<a class="navigate_to" href="javascript:navigate(' + (i * maxItems) + ')">' + (i + 1) + '</a>');
	}
    if (offset + maxItems < anzahlItems) $(".search_results").find(".navigation").append('<a href="javascript:navigate(' + (activeOffset + maxItems) + ')"><img src="' + $(".image_arrow_right").attr("src") + '"></a>');
    else $(".search_results").find(".navigation").append('<a class="nagivate_right"><img src="' + $(".image_arrow_right").attr("src") + '" width="6" height="9"></a>');
	
	$(".search_results").find(".navigation").find("a.navigate_to:first").after('<span class="dotsBefore">...</span>');
	$(".search_results").find(".navigation").find("a.navigate_to:last").before('<span class="dotsAfter">...</span>');
	
	crop_search_navigation($(".search_results"));
	
    $("#content_sub_background").css("position", "static").css("position", "absolute");
    $("#kaefer").css("position", "static").css("position", "absolute");
}
	
/* ############## GLOBALE VARIABLEN ############ */

var anzahlItems = 0;
var activeOffset = 0;
var activeFilter = "no-filter";
var index = 0;
var maxItems = 4;
var newsletter_email_confirmed = 0;


// Muss in (window).load ausgeführt werden, da erst hier der vollständige Content geladen ist.
$(window).load(function() {
    //3 Spalten korrekte Höhe setzen.    
    var maxHeight = 0;
    $('#tables .table').each(function() {
        if ($(this).height() > maxHeight)
            maxHeight = $(this).height();
    });
    $('#tables .table').each(function() {
        $(this).css("height", maxHeight);
    });
    $('#kaefer, #content_sub_background').css('position', 'static').css('position', 'absolute');

});

// set fontsize of body
function setFontsize()
{
    var size = null;
    size = readCookie('JaN_fontsize');

    if (size)
    {
        $("body").css("font-size", size + "px");
    }
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name, value, days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";

    if (document.cookie)
    {
        var ca = document.cookie.split(';');

        for (var i = 0; i < ca.length; i++)
        {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

function eraseCookie(name)
{
    createCookie(name, "", -1);
}
// /cookie functions