(function () {
    $(document).ready(function () {


        $("div.tabs ul li a").click(function () {
            //console.debug(this.text);
            //console.debug($(this).text());

            $('#selectedTab').val(this.text);

        });

        /* Header slideshow */
        var slideshow = $(".slideshow");
        var slideshowIndex = 1;
        if (slideshow.length) {
            slideshow.find(".navigation a").show().click(function (e) {
                var link = $(this);
                e.preventDefault();
                if ($(this).hasClass("next")) {
                    if (slideshowIndex < 4) {
                        slideshowIndex++;
                    }
                    link = slideshow.find(".navigation a:eq(" + (slideshowIndex - 1) + ")");
                }
                else {
                    slideshowIndex = parseInt($(this).text(), 10);
                    link = $(this);
                }
                moveTo(slideshow.find(".items"), slideshowIndex, -510)
                toggleNavigation(link);
            });
        }


        /* News slideshow */
        var newsslider = $(".news-slider");
        var newssliderIndex = 1;
        if (newsslider.length) {
            newsslider.find(".news-slider-menu a").show().click(function (e) {
                var link = $(this);
                e.preventDefault();
                if ($(this).hasClass("next")) {
                    if (newssliderIndex < 4) {
                        newssliderIndex++;
                    }
                    link = newsslider.find(".news-slider-menu a:eq(" + (newssliderIndex - 1) + ")");
                }
                else {
                    newssliderIndex = parseInt($(this).text(), 10);
                    link = $(this);
                }
                moveTo(newsslider.find(".items"), newssliderIndex, -615)
                toggleNavigation(link);
            });
        }


        /* Videospiller (kun flash foreløpig) */
        var videoteaser = $("#video-teaser");
        if (videoteaser.length) {
            videoteaser.find(".video-link").click(function (e) {
                e.preventDefault();
                showVideo($("#video-container"));
            });
        }


    });

    function showVideo(videoContainer) {
        $("<div title=\"Klikk for å lukke\"></div>").css({
            position: "fixed",
            width: $(window).width(),
            height: $(window).height(),
            backgroundColor: "#000",
            cursor: "pointer",
            opacity: 0,
            top: 0,
            left: 0,
            zIndex: 98,
            overflow: "hidden"
        })
			.appendTo("body")
			.animate({ opacity: .35 }, 500, function () {
			    videoContainer.fadeIn(300);
			})
			.click(function () {
			    var self = $(this);
			    videoContainer.fadeOut(200, function () {
			        self.fadeOut(300, function () {
			            self.remove();
			        });
			    });
			});
    }

    /* Tabulert artikkel/liste */
    var $tabs = $(".tabs");
    if ($tabs.length === 1) {
        /* Viser kun første element */
        $tabs.find(".tab-content").each(function (index) {
            var $this = $(this);
            if (index > 0) {
                $this.hide();
            }
        });
        /* Binder klikk i menyen */
        $tabs.find(">ul a").click(function (e) {
            e.preventDefault();
            var $this = $(this);
            $this.parent().addClass("active").siblings().removeClass("active");
            $tabs.find('.tab-content').hide();
            $($this.attr("href")).fadeIn();
        });
    }

    /* FAQ */
    var $faq = $(".faq-list");
    if ($faq.length === 1) {
        var $links = $faq.find(">ul>li>a");
        $links.click(function (e) {
            e.preventDefault();
            var $this = $(this);

            if ($this.hasClass("active")) {
                $this.removeClass("active");
            }

            else {

                $links.removeClass("active");
                $this.addClass("active")
            }

        });
    }

    /* Expandable contact list */
    var $listContainer = $(".contact-list-expandable");
    if ($listContainer.length) {
        $listContainer.each(function () {
            var $this = $(this);
            var $listItems = $this.find(">ul>li");
            $listItems.each(function () {
                var $item = $(this);
                $item.find(".contact-expand").click(function (e) {
                    e.preventDefault();
                    var $this = $(this);

                    if ($item.hasClass("active")) {

                        $item.removeClass("active");

                    }
                    else {
                        $listItems.removeClass("active");
                        $item.addClass("active");
                    }
                });
            });
        });
    }

    /* Image Galleries */
    var $galleries = $(".galleries .gallery");
    var imagesPerSet = 8;
    if ($galleries.length) {
        $galleries.each(function (index) {
            var $this = $(this);
            var $images = $this.find(".gallery-image");
            var setCount = Math.ceil($images.length / imagesPerSet);
            var currentSet = 1;

            $images.wrapAll('<div class="imagesets" style="width: ' + setCount * 700 + 'px; height: 272px;"/>');

            $images.each(function (index) {
                var i = index + 1;
                var imageset = Math.floor(i / imagesPerSet + (i % imagesPerSet == 0 ? 0 : 1));
                $(this).addClass("imageset-" + imageset);
            });

            for (var i = 1; i <= setCount; i++) {
                $this.find(".imageset-" + i).wrapAll('<div class="imageset-' + i + '" style="width: 680px; height: 272px; float: left; margin-right: 10px;"/>');
            }

            $this.append('<div class="gallery-nav"><a href="#" class="prev">« Forrige</a> <span class="nav-index">1</span>/' + setCount + ' <a href="#" class="next">Neste »</a></div>');

            $this.find(".gallery-nav .prev, .gallery-nav .next").click(function (e) {
                e.preventDefault();
                if ($(e.target).attr("class") == "prev") {
                    currentSet = (currentSet > 1) ? currentSet - 1 : setCount;
                }
                else {
                    currentSet = (currentSet < setCount) ? currentSet + 1 : 1;
                }
                $this.find(".nav-index").text(currentSet);
                moveTo($this.find(".imagesets"), currentSet, -690);
            });

        });
    }


    function moveTo(items, index, step) {
        items.css({
            marginLeft: step * (index - 1)
        });
    }

    function toggleNavigation(item) {
        item.addClass('active').siblings().removeClass('active');
    }

})(jQuery);
