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');

        $("#facebook_btn").stop().fadeTo(0, 0.6);
        $("#facebook_btn").hover(
            function(){
                $(this).stop().fadeTo("fast", 1.0);
            },
            function(){
                $(this).stop().fadeTo("slow", 0.6);
            }
        );

        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", "/filearchive/Image/kart/" + $(this).attr("name") + ".jpg");
            }
        });
    }
    ;

    return {
        init : init,
        showList : showList
    }
}();


$(document).ready(function() {
    akerbrygge.init();
});


