/*
2011-06-27 only show dropdown menu on dnyt (DIS/PLAY - MHI)
*/
$(function () {

    $("#topnavigation li span.selected").parents("li").css("background-image", "none").prev("li").css("background-image", "none");


    $("a")
		.focus(function () { $(this).blur(); });
    $(".clickable").css({ "cursor": "pointer"/*,"border":"1px solid red"*/ })
		.click(function () { location.href = $(this).find(($(this).find("a.reverse").attr("href") != undefined) ? "a.reverse" : "a").attr("href"); });
    $(".bgchange").mouseenter(function () {
        var bgchange = $(this);
        $("li").mouseenter(function () {
            if ($(this).find("input") && $(this).find("input").val() != "") {
                var splitrel = $(this).find("input").val().split(";");
                bgchange.css({ "backgroundImage": "url(/Files/System/Graphic/" + splitrel[1] + ")" });
                bgchange.find("div").css({ "display": "none" });
                bgchange.find("*[rel!='']").removeClass("selected");
                $(this).addClass("selected");
                $("." + splitrel[0]).css({ "display": "block" });
            }
        });
    });

    $("img.reverse,input.reverse")
		.mouseenter(function () { $(this).attr({ "src": $(this).attr("src").replace(".png", "-reverse.png") }); })
		.mouseleave(function () { $(this).attr({ "src": $(this).attr("src").replace("-reverse.png", ".png") }); });
    $("a.reverse")
		.mouseenter(function () { $(this).css({ "backgroundImage": $(this).css("backgroundImage").replace(".png", "-reverse.png") }); })
		.mouseleave(function () { $(this).css({ "backgroundImage": $(this).css("backgroundImage").replace("-reverse.png", ".png") }); });

    //	$('td img').parent().wrapInner("<p></p>");

    // DIS/PLAY ... SORRY FOR THE TRY CATCH :,-)	
    try {
        $('td').each(function () {
            if (!$(this).find('p, h1, h2, h3, small').length) {
                $(this).wrapInner("<p></p>");
            }
        });
    } catch (err) {
    }

    $('.indeksering').prepend('<div style="float: left; clear: left; width: 100%; height: 5px;"><img height="5" width="100%" alt="" src="/Files/System/Graphic/gradiant-top.png"></div>');
    $('.indeksering').append('<div class=\'bgindeksering\'><p><b>Indholdsfortegnelse</b></p><ul></ul></div>');
    //<p class=\'small\'>&nbsp;</p>');
    $('h3').each(function () {
        if ($(this).html() != '&nbsp;' && $(this).attr('class') != 'h3 noindex') {
            //if ($(this).text().length > 1) alert(".");
            $(this).wrap('<a name=\'' + $(this).text() + '\' class=\'generated\'></a>');
            $('.indeksering').find('ul').append('<li>' + '<a href=\'' + location.href + '#' + $(this).text() + '\'>' + $(this).text() + '</a></li>');
        }
    });
    $('.indeksering').append('<div style="float: left; clear: left; width: 100%; height: 5px; margin-top: -5px;"><img height="5" width="100%" alt="" src="/Files/System/Graphic/gradiant-bottom.png"></div>');

    $('.paragraphsetup4mouseover')
		.mouseenter(function () {
		    $(this).css({ 'backgroundImage': 'url(/Files/System/Graphic/paragraphsetup4-bg2.png)' });
		}).mouseleave(function () {
		    $(this).css({ 'backgroundImage': 'url(/Files/System/Graphic/paragraphsetup4-bg1.png)' });
		});
    $('.more').hide();
    $('.current').show();
    $('.paragraphsetup4 .open')
		.click(function () {
		    $('.more').hide();
		    $('.current').show();
		    $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().hide().next('.more').show();
		});


    $('.foldudbox .open').show();
    $('.foldudbox .content').hide();

    $('.foldudbox .open a').click(function () {
        $('.foldudbox .open').hide();
        $('.foldudbox .content').show();
    });

    // change: is hidden with css and opened again with noscript css to avoid flicker at pageload
    // $('.faqsmall .div5 span').hide(); 
    // $('.faqsmall .div3 .close').hide();

    $('.faqsmall .div3')
		.find('.open').click(function () {
		    $(this).parent().parent().parent().parent().find('.div3 .open').hide();
		    $(this).parent().parent().parent().parent().find('.div3 .close').show();
		    $(this).parent().parent().parent().parent().find('.div5 span').show();
		})
		.next('.close').click(function () {
		    $(this).parent().parent().parent().parent().find('.div3 .open').show();
		    $(this).parent().parent().parent().parent().find('.div3 .close').hide();
		    $(this).parent().parent().parent().parent().find('.div5 span').hide();
		});


    /* scripting for FAQ on Nyt & Sundt pages */
    /* is hiding answer excet for two lines by toggling a classname. 
    the "paragraphContent" is set in paragraph template B11-T11 */
    $(".faqlargeturkis .div5 .paragraphContent").each(function () {
        $(this)
				.wrapInner("<div class='faqAnswer' />")
				.prepend("<a name='" + $(this).attr('id') + "'></a>")
				.append("... <a href='javascript:void()' class='faqReadMore'>L&aelig;s mere</a>");
    })
    var faqAnchor = ""
    $(".faqReadMore").click(function () {
        faqAnchor = $(this).parent("td.paragraphContent").attr("id");
        $(this).prev(".faqAnswer").toggleClass("opened");
        $(window.location).attr('href', '#' + faqAnchor);
        if ($(this).text() == "Skjul tekst") {
            $(this).text("L\u00E6s mere");
        } else {
            $(this).text("Skjul tekst");
        }
    });





    $('.dnytogsundtbar .name').html(DwPageName_3);

    $('.dnytmenu, .dnytnewsletter').hide();



    if (DwPageName_3 != '<!--@DwPageName_3-->') {
        $('.dnytbarleft')
			.mouseover(function () {
			    $(this).find('.dnytmenu').show();
			})
			.mouseout(function () {
			    $(this).find('.dnytmenu').hide();
			})
			.find('.name').html(DwPageName_3);
    }

    if (DwPageName_3 != '<!--@DwPageName_3-->') {
        if (DwPageID_2 != '17') {
            /*$('.dnyt .dnytogsundtbarleft')*/
            $('.dnytogsundtbarleft')
			.mouseover(function () {

			    if (keepDropDownMenuClosed != 1) {
			        $(this).find('.dnytogsundtmenu').show();
			        $(".infoholder").hide();
			        $("#tilmeldNS").addClass("insideMenu");
			        var $lineHeight = 23;
			        /*$('.dnyt .dnytogsundtbarleft ul li').each(function(){*/
			        $('.dnytogsundtbarleft ul.sub li,.dnyt .dnytogsundtbarleft ul li').each(function () {

			            if ($(this).height() >= 2 * $lineHeight) {
			                $(this).addClass('text2Lines')
			            }
			        })
			    }
			})
			.mouseout(function () {
			    $(this).find('.dnytogsundtmenu').hide();
			    $(".infoholder").show();
			    $("#tilmeldNS").removeClass("insideMenu");
			})
			.find('.name').html(DwPageName_3);
        }
    }


    /* click tilmeld NYHEDSBREV * * * * * */

    var keepDropDownMenuClosed = 0

    $("#tilmeldNS").click(function () {
        var $link = $(this);

        if (
            $link.is("#contentholder .nytogsundt #tilmeldNS")
        )
        { return true; }

        $(".tilmeldFormLayer").show();
        $("#lukNS").show();
        $(this).hide();
        $(".dnytogsundtbarleft .dnytogsundtmenu").hide();
        keepDropDownMenuClosed = 1
        return false
    })

    $("#lukNS").click(function () {
        $(".tilmeldFormLayer").hide();
        $("#tilmeldNS").show();
        keepDropDownMenuClosed = 0
        return false;
    })


    if (DwPageName_3 != '<!--@DwPageName_3-->') {
        strDwPageName_3 = DwPageName_3.toLowerCase()
        if (strDwPageName_3.indexOf("artikler") != -1) {
            // hvis sidenavnet indeholder "artikler" skal det ikke inds?ttes !
            $('.dnytogsundtbarleft').find('.name').html('<span><span>' + DwPageName_3 + '</span></span>');
        } else {
            $('.dnytogsundtbarleft').find('.name').html('<span><span>Artikler i ' + DwPageName_3 + '</span></span>');
        }
    }

    if (DwPageID_3 == '48') { // Hvis "ARKIV" i nyt og sundt
        $('.dnytogsundtbarleft').find('.name').html('<span><span>Artikler i ' + DwPageName_4 + '</span></span>');
    }

    $('.dnytbarright')
		.mouseover(function () {
		    $(this).find('.dnytnewsletter').show();
		})
		.mouseout(function () {
		    $(this).find('.dnytnewsletter').hide();
		});

    /* 
    Nyt og sundt - ARKIV	
    Script til h?jremenu med tidligere numre: 
    */
    //hide all ul.sub except first 
    $("#tidligereNumre ul.sub").filter(":not(:first)").hide();
    // open ul.sub when click on header, hide others
    $("#tidligereNumre h3").each(function () {
        $(this).click(function () {
            $("#tidligereNumre ul.sub").hide();
            $(this).parents("li").children("ul.sub").show('fast');
        }) // click 
    }) // each

    if (!$(".dnytogsundtmenu .row2").length > 0) {
        $(".dnytogsundtmenu,#dnytogsundtContainer").addClass("singleRow");
        $("#contentholder img.sdk-dropdown-bg1-bottom").attr("src", "/Files/System/Graphic/sdk-dropdown-bg1-bottom-narrow.png");
    }

    $(".dnyt-page #erMedlemDiv").show();

    $('.opencloseheader').show();
    $('.openclosecontent').hide();

    $('.opencloseheader a[class!=""]').click(function () {
        $('.opencloseheader.' + $(this).attr('class')).hide();
        $('.openclosecontent.' + $(this).attr('class')).show();
    });

    $('.openclosecontent a[class!=""]').click(function () {
        $('.opencloseheader.' + $(this).attr('class')).show();
        $('.openclosecontent.' + $(this).attr('class')).hide();
    });

    if ($('#cart').length) {
        $('#cart').parent().css({ 'position': 'relative' });
        $('.point').find('tr:first').hide();
        $('.point').each(function () {
            var splitcoors = $(this).find('tr:first td').text().split(',');
            var pointobj = $(this);
            $('#cart').parent()
				.append('<div class="cartpoint"><span class="cartimg"><img src="' + splitcoors[2] + '" alt="" /></span>' +
					'<span class="cartbox">' + pointobj.find('td:last').html() + '</span></div>')
				.find('.cartpoint:last').css({ 'position': 'absolute', 'left': splitcoors[0] + 'px', 'top': splitcoors[1] + 'px' })
					.mouseenter(function () {
					    $(this).find('.cartbox').show();
					})
					.mouseleave(function () {
					    $(this).find('.cartbox').hide();
					});
        });
    }

    $('img.info, span.info').wrap('<span class="infoholder"></span>').each(function () {
        $(this).parent()
			.append('<span class="infotitle"><span><img src="/Files/System/Graphic/info.png" alt="" class="info" />' +
				'&nbsp; <b>Om forfatteren</b><br />' + $(this).attr('title') + '</span><img src="/files/system/graphic/forfatterboks-bottom.png" class="bottom"/></span>')
			.mouseenter(function () {
			    $(this).find('.infotitle').show();
			})
			.mouseleave(function () {
			    $(this).find('.infotitle').hide();
			});
    });

    $('img.question').wrap('<span class="infoholder"></span>').each(function () {

        $(this).parent()
			    .append('<span class="infotitle"><span><img src="/Files/System/Graphic/question.png" alt="" class="info" />' +
				    '&nbsp; <b>Om hj&aelig;lp</b><br />' + $(this).attr('title') + '</span><img src="/files/system/graphic/forfatterboks-bottom.png" class="bottom"/></span>')
			    .mouseenter(function () {
			        $(this).find('.infotitle').show();
			    })
			    .mouseleave(function () {
			        $(this).find('.infotitle').hide();
			    });

    });

    $("#leftnavigation li li a").each(function () {
        //$(this).append($(this).css("height") + "/" + $(this).height());
        if ($(this).height() > 33) {
            $(this).parent("li").addClass("longText").attr("title", $(this).height());
        }
    })
    $("#leftnavigation li li li a").each(function () {
        //$(this).append($(this).css("height") + "/" + $(this).height());
        if ($(this).height() > 33) {
            $(this).parent("li").addClass("longText").addClass(">33")
        } else {
            $(this).parent("li").removeClass("longText").addClass("!>33");
        }
    })

});
