

$(document).ready(function() {

   



    $("#fontBigger").click(function() {
        $("#fontsize").attr("href", "/lib/css/sizeBBB.css");
        $.cookie('MiljostatusSize', '3', { expires: 30, path: '/', secure: false });
        $(this).addClass("underline");
        $("#fontBig").removeClass("underline");
        $("#fontNormal").removeClass("underline");
        return false;

    });

    $("#fontBig").click(function() {
        $("#fontsize").attr("href", "/lib/css/sizeBB.css");
        $.cookie('MiljostatusSize', '2', { expires: 30, path: '/', secure: false });
        $(this).addClass("underline");
        $("#fontBigger").removeClass("underline");
        $("#fontNormal").removeClass("underline");
        return false;
    });

    $("#fontNormal").click(function() {
        $("#fontsize").attr("href", "/lib/css/sizeB.css");
        $.cookie('MiljostatusSize', '1', { expires: 30, path: '/', secure: false });
        $(this).addClass("underline");
        $("#fontBigger").removeClass("underline");
        $("#fontBig").removeClass("underline");
        return false;
    });




    $("#definitionlist .showdefinition").click(function() {
        $(this).next(".definition").toggle();
        return false;
    });

    $("#norwegianlink").click(function() {
        var completeURL = location.href;
        var hostname = location.hostname;
        var host_no = "www.miljostatus.no";
        var fixedURLtmp = completeURL.replace(hostname, host_no);
        var fixedURLtmpp = fixedURLtmp.replace("/en/", "/no/");
        var fixedURL = fixedURLtmpp.replace("spraak=EN", "spraak=NO");
        location.href = fixedURL;
    });

    $("#englishlink").click(function() {
        var completeURL = location.href;
        var hostname = location.hostname;
        var host_en = "www.environment.no";
        var fixedURLtmp = completeURL.replace(hostname, host_en);
        var fixedURLtmpp = fixedURLtmp.replace("/no/", "/en/");
        var fixedURLtmppp = fixedURLtmpp.replace("spraak=NO", "spraak=EN");
        var fixedURL = fixedURLtmppp.replace("nn=true", "n=n");
        location.href = fixedURL;
    });


    $("#loadHelp").click(function() {
        var helppage = $("#loadHelp").attr("href");
        $("#displayHelp").load(helppage);
        return false;
    });

    $("#toggleFacta").click(function() {


        if ($("#FaktaBody").css("display") == "block") {
            $("#FaktaBody").fadeOut();
        }
        else {
            $("#FaktaBody").fadeIn();
        }
        //$("#FaktaBody").toggle();
        return false;
    });




    /*skjuler temamodulen dersom det vises temaoverskrift men ingen linker under. oppstår knyttet til manglende oversatte engelske sider*/
    var temacontainer = $("#temacontainer li").length;
    if (temacontainer == 0) {
        $("#temacontainer").hide();
    };



    var pageURL = $("#pageURL").attr("href");
    if (pageURL) {
        /* $("#share").html("<a target='_blank' title='Facebook: Del p&aring; facebook' style='padding-top:0px;padding-left:23px;' href='http://www.facebook.com/share.php?u=http://www.miljostatus.no" +pageURL+"'><img style='vertical-align:bottom;' src='/lib/img/facebook_share_icon.gif' border='0'></a>");*/
    }

    //tipafriend 
    $(".tipAFriendLink").click(function() {
        $(".tipBox").toggle();
        return false;
    });


    /* QUIZ */

    $("#questions .question:last input:radio").click(function() {
        $("#questions .question:last").find(".checkR").fadeIn();
    });

    $("#questions .question:first").find(".prevQ").hide();
    $("#questions .question:first").fadeIn();
    $("#questions .question:last").find(".nextQ").hide();
    $("#questions").find(".checkR").hide();
    //$("#questions .question:last").find(".checkR").show();


    $("#questions .nextQ").click(function() {

        var selected = $(this).parent().find("input:checked").val();
        if (selected) {
            $(this).parent(".question").hide();
            $(this).parent(".question").next(".question").fadeIn();
        } else {
            alert("Velg ett av alternativene.");

        }

        return false;
    });


    $("#questions .prevQ").click(function() {
        $(this).parent(".question").hide();
        $(this).parent(".question").prev(".question").fadeIn();
        return false;
    });

    $("#questions .checkR").click(function() {
        $(this).hide();
        var s = Math.floor(Math.random() * 100000);
        var answers = $("#questions input:radio:checked").serialize();
        var QuizID = $("#QuizID").val();
        var URL = "/Templates/services/QuizCheckResults.aspx?Quiz=" + QuizID + "&" + answers + "&s=" + s;

        $.get(URL, function(data) {
            var resultsXML = data;
            var results = $(resultsXML).find("result");
            var score = $(resultsXML).find("score");

            score.each(function() {
                var total = $(this).children('total').text();
                var correct = $(this).children('correct').text();
                var responsText = "<h2>Du svarte korrekt p&aring; " + correct + " av totalt " + total + " sp&oslash;rsm&aring;l.</h2><br/>";
                $("#response").html(responsText);
            });

            results.each(function() {
                var qID = $(this).children('qID').text();
                var status = $(this).children('status').text();

                if (status.indexOf("RIKTIG") >= 0) {
                    $("#q" + qID).append("  &nbsp;(Riktig)");
                    $("#q" + qID).removeClass("wrong");
                    $("#q" + qID).addClass("correct");
                } else {
                    $("#q" + qID).append("  &nbsp;(Feil)");
                    $("#q" + qID).removeClass("correct");
                    $("#q" + qID).addClass("wrong");
                }

            });

            $("#questions .status").fadeIn();
            $("#response").fadeIn();

        });

        $("#questions .nextQ").hide();
        $("#questions .prevQ").hide();
        $("#questions .question .radios input").hide();
        $("#questions .question .radios label").hide();
        $("#questions .question .yourAnswerWas").show();
        $("#questions .question .radios input:checked").next("label").show();
        $("#questions .question").fadeIn();
        $("#questions .startOver").fadeIn();

        return false;
    });

    $(".startOver").click(function() {

        var url = location.href;
        document.location = url;

        /*
        $(this).hide();
        $("#questions .question .yourAnswerWas").hide();
        $("#questions .question").hide();
        $("#questions .question .radios input").show();
        $("#questions .question .radios label").show();
        $("#questions .nextQ").show();
        $("#questions .prevQ").show();
        $("#questions .question:first").find(".prevQ").hide();
        $("#questions .question:last").find(".nextQ").hide();
        $("#questions .question:last").find(".checkR").show();
        $("#questions .question .status").hide();
        $("#questions .question:first").fadeIn();
        $("#questions .question .radios").show();
        $("#response").hide();
        */

        return false;
    });




    /*IMAGEGALLERY*/

    var images = $("#imageContainer img");
    var count = images.length;
    var firstsrc = $("#imageContainer img:first").attr("src");
    var firsttext = $("#imageContainer img:first").attr("alt");
    if (firsttext) {

        firsttext = firsttext;
    } else {
        firsttext = " ";
    }

    if (count > 0) {
        $(".media-nav .head").append("<img src='" + firstsrc + "'/>");
        $(".media-nav .head").append("<h5>" + firsttext + "</h5>");

        images.each(function() {
            var mysrc = $(this).attr("src");
            var mytext = $(this).attr("alt");
            if (mytext) {
                mytext = mytext;
            } else {
                mytext = " ";
            }

            $(".media-nav .body").append("<a href='/'><img src='" + mysrc + "' alt='" + mytext + "' class='thumb'/></a>");
        });

        $(".media-nav").fadeIn();
    }

    $(".media-nav a img").click(function() {
        var url = $(this).attr("src");
        var text = $(this).attr("alt");
        var bigimg = $(".media-nav .head").find("img")

        bigimg.attr("src", url);
        $(".media-nav .head").find("h5").text(text);

        return false;
    });



});



function show_fylke(selected)
{ 
	window.open(selected.value,"","", "");
}
// Opens a pop-up window. 
 var my_popup;
   function MM_openBrWindow(theURL,winName,features) {
      if (my_popup) {
         if (my_popup.closed){
         }
         else {
            my_popup.close();
         }
      }
      my_popup = window.open(theURL,winName,features);
   }



