$(document).ready(function($) {

    function is_int(value) {
        if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
            return true;
        } else {
            return false;
        }
    }

    function goToByScroll(id) {
        if ($("#" + id).offset()) {
            $('div.products').animate({scrollTop: $("#" + id).offset().top - 680}, 'fast');
        }
    }

    var current_menu = $("#menu_tree").find(".menu_link")[0];

    $(".menu_link").click(function() {
        $(this).parent().parent().find(".submenu").hide();
        $(this).parent().find(".submenu").fadeIn("fast");

        //Reset bold for all menu-items
        current_menu = $(this);
        if (current_menu.parent()) {

            current_menu.parent().find(".menu_link").css("font-weight", "bold")

            if (current_menu.parent().parent()) {
                current_menu.parent().parent().find(".menu_link").css("font-weight", "")
            }

        }

    })

    var pathname = window.location.pathname;

    if (pathname == "/") {
        $("#bottom_images").show();
    }

    if (pathname.substring(1, 9) == "products") {
        current_menu = $("nav").find("#cat_" + pathname.substring(10, 12))
        current_menu.css("font-weight", "bold");

        current_menu.parent().parent().find(".menu_link").css("font-weight", "bold")
        current_menu.parent().parent().find(".submenu").show();

        //Scroll to current product
        if (is_int(pathname.substring(12))) {
            goToByScroll("product_"+pathname.substring(12))
        }

    }

    //If flatpage and chosen bold it
    if (pathname.substring(1, 5) == "page") {
        var s = pathname.substring(1).replace(/\//g, "_")
        var a = $("#" + s);
        a.css("font-weight", "bold");

        if (a.parent().parent().attr("id") != "menu_tree") {
            a.parent().parent().find(".submenu").show();
        }
    }

    //If news, bold for any of those
    if (pathname.substring(1, 5) == "page" && pathname.substring(6, 13) == "nyheter") {
        var a = $("#page_nyheter_");
        a.parent().css("font-weight", "bold");
    }

    //If kokeposer, super special case..
    if (pathname.substring(1, 5) == "page" && pathname.substring(6, 15) == "kokeposer") {
        var a = $("#cat_12");
        a.css("font-weight", "bold");
        a.parent().parent().find(".menu_link").css("font-weight", "bold")
        a.parent().parent().find(".submenu").show();
    }

    $('#slider').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:4,
        animSpeed:500, //Slide transition speed
        pauseTime:3000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:false, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:true, //Use left & right arrows
        pauseOnHover:true, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0.8, //Universal caption opacity
        beforeChange: function() {
        },
        afterChange: function() {
        },
        slideshowEnd: function() {
        } //Triggers after all slides have been shown
    });

});
