var akerbrygge = function () {
    var 
    slideshow = null,
    categorySelect,
    shopsPerCol = 8,

    handleCategorySelectMouseOver = function () {
        var height = categorySelect.find(".categories").height();
        categorySelect.find(".categories-wrapper").animate({ height: height + "px" }, { duration: 500, queue: false, easing: "easeinout" });
    },

    handleCategorySelectMouseOut = function () {
        categorySelect.find(".categories-wrapper").animate({ height: "0px" }, { duration: 500, queue: false, easing: "easeinout" });
    },

    checkCurrent = function () {
        var that = $(this);

        if (typeof topmenupoint == "undefined") {
            topmenupoint = "none-";
        }

        that.find("a").each(function () {
            if ($(this).attr("href") == window.location.pathname.toString() || $(this).attr("href").indexOf(topmenupoint) != -1) {
                $(this).addClass("current");
            }
        });
    },

    showList = function (buildingId) {
        var html = $("<span />");
        var currentCount = 0;
        var currentNode = $("<div />");

        for (var shopid in shopJson) {
            var shop = shopJson[shopid];
            if (shop.location == buildingId) {
                if (currentCount != 0 && currentCount % shopsPerCol == 0) {
                    html.append(currentNode);
                    currentNode = $("<div />");
                }
                currentNode.append($("<a />").attr("href", shop.url).text(shop.title));
                currentCount++;
            }
        }
        html.append(currentNode);
        $("#oversiktskart-overlay .list").html(html);
        $("#oversiktskart-overlay").show();
        $("#oversiktskart-overlay .close").one("click", function () {
            $("#oversiktskart-overlay").hide();
        });
        $("#oversiktskart-help").hide();
    },

    init = function () {
        Cufon.replace('h1,h2,h3,.intro p, .shop-view .right-col .kontakt-info .telephone td', { fontFamily: 'Clarendon LT Std' });
        Cufon.replace('#menu ul li a', { fontFamily: 'DIN-Medium' });
        Cufon.replace('#footer-wrap p', { fontFamily: 'DIN-Regular' });

        $('#top-tabs img.shopping').hover(function () {
            this.src = '/images/fane_shopping_hover.png';
        }, function () {
            this.src = '/images/fane_shopping.png';
        });

        $('#top-tabs img.facebook').hover(function () {
            this.src = '/images/fane_facebook_hover.png';
        }, function () {
            this.src = '/images/fane_facebook.png';
        });

        $('#top-tabs img.twitter').hover(function () {
            this.src = '/images/fane_twitter_hover.png';
        }, function () {
            this.src = '/images/fane_twitter.png';
        });

        $('#top-tabs img.english').hover(function () {
            this.src = '/images/fane_english_hover.png';
        }, function () {
            this.src = '/images/fane_english.png';
        });
       
        if ($("#feature-wrap").length > 0) {
            slideshow = $("#feature-wrap").letItSlide();
            $(".tooltip").each(function (index, elem) {
                var $this = $(elem);
                var linkElem = $this.find("a");
                var link = linkElem.attr("href");
                var target = linkElem.attr("target");
                var linkHtml = "<a href='" + link + "'";

                if (target != "") {
                    linkHtml += " target='" + target + "'";
                }

                linkHtml += " />";
                $this.wrap(linkHtml);
                var text = linkElem.html();
                linkElem.parent().append($("<span class='link'>" + text + "</span>"));
                linkElem.remove();
            });
        }
        $("#menu li").each(checkCurrent);

        categorySelect = $(".category-select");
        $(categorySelect).bind("mouseenter", handleCategorySelectMouseOver)
                         .bind("mouseleave", handleCategorySelectMouseOut);

        $('.gallery').galleryView({
            panel_width: 680,
            panel_height: 300,
            frame_width: 107,
            frame_height: 53,
            show_captions: false,
            easing: "easeout"
        });

        $("a.kart").tooltip({
            delay: 0,
            showURL: false,
            bodyHandler: function () {
                return $("<img/>").attr("src", "/images/kart/" + $(this).attr("name") + ".jpg");
            }
        });
    }
    ;

    return {
        init: init,
        showList: showList
    }
} ();


$(document).ready(function() {
    akerbrygge.init();
});



