﻿ProjectListHandler = function () {
    this.Items = {};
    this.ImageItemWidth = 268;
    this.PagingItemDefaultColor = "#000000";
    this.PagingItemHighlightColor = "#FF0000";
}

ProjectListHandler.prototype = {
    Init: function () {
        if ($j(".ProjectListItem").length > 0) {
            $j(".ProjectItemCategory").each(function () {
                var curCat = $j(this);

                projectListHandler.Items[curCat.attr("Key")] = {};
                projectListHandler.Items[curCat.attr("Key")].ListItems = $j(".ProjectListItem", curCat);
                projectListHandler.Items[curCat.attr("Key")].Cat = curCat;
            });

            projectListHandler.InitializeItemsAndPaging();
            projectListHandler.BindImageSlideshows();
        }
    },

    InitializeItemsAndPaging: function () {
        for (var catKey in projectListHandler.Items) {
            var curCatItems = projectListHandler.Items[catKey].ListItems;
            var curCat = projectListHandler.Items[catKey].Cat;

            if (curCatItems.length > 0) {
                curCatItems.css("background-color", $j("#siteContentWide").css("background-color"));

                // Show first item.
                // [MB]
                var firstListItemInCat = curCatItems.first().css("display", "block");

                // Create paging.
                // [MB]
                var pagingContainer = $j(".PagingContainer", curCat);
                if (pagingContainer.length > 0) {
                    for (var itemIdx = 0; itemIdx < curCatItems.length; itemIdx++) {
                        var pagingItem = $j("<span class=\"PagingItem\" style=\"cursor:pointer; margin:0px 1px 0px 1px;\">" + (itemIdx + 1) + "</span>");
                        pagingItem.attr("ItemIdx", itemIdx).attr("CatKey", catKey);

                        pagingItem.click(function () {
                            var clickedItem = $j(this);
                            projectListHandler.SwitchItem(clickedItem.attr("CatKey"), clickedItem.attr("ItemIdx"));
                        });

                        pagingContainer.append(pagingItem);

                        // Add divider dashes.
                        if (itemIdx != (curCatItems.length - 1)) {
                            pagingContainer.append(" | ");
                        }
                    }

                    //$j(".PagingItem", pagingContainer).first().css("color", projectListHandler.PagingItemHighlightColor);
                }
            }
        }
    },

    SwitchItem: function (catKey, itemIdx) {
        if (projectListHandler.Items[catKey] && projectListHandler.Items[catKey].ListItems[itemIdx]) {
            var curItems = projectListHandler.Items[catKey];
            var curCat = curItems.Cat;

            // Hiding potentially open map.
            // [MB]
            projectListHandler.HideEnvironmentInformation(curCat);

            var curCatItems = curItems.ListItems;
            var itemToShow = $j(curCatItems[itemIdx]);
            var itemToHide = $j(".ProjectListItem", curCat);

            var pagingItems = $j(".PagingItem", curCat);
            //pagingItems.css("color", projectListHandler.PagingItemDefaultColor);
            //$j(pagingItems[itemIdx]).css("color", projectListHandler.PagingItemHighlightColor);

            itemToHide.hide();
            itemToShow.show();

            //            itemToHide.animate({ "opacity": 0 }, 150, function () {
            //                var oldHeight = curCat.height();
            //                $j(this).css("display", "none");
            //                var targetHeight = itemToShow.height();

            //                curCat.css("height", oldHeight).animate({ /*"height": targetHeight + 25*/}, 150, function () {
            //                    itemToShow.css({
            //                        "opacity": 0,
            //                        "display": "block"
            //                    }).animate({
            //                        "opacity": 1
            //                    }, 150);
            //                });
            //            });
        }
    },

    BindImageSlideshows: function () {
        for (var catKey in projectListHandler.Items) {
            var curCat = projectListHandler.Items[catKey].Cat;
            var curCatItems = projectListHandler.Items[catKey].ListItems;

            for (var catItemIdx = 0; catItemIdx < curCatItems.length; catItemIdx++) {
                var curListItem = projectListHandler.Items[catKey].ListItems[catItemIdx];
                var imagesInCat = $j("img.SlideshowImage", curListItem);

                $j(".MovingContainer", curListItem).css("width", projectListHandler.ImageItemWidth * imagesInCat.length);
                $j(".ImageSlideshowContainer", curListItem).attr("CurrentImageIdx", 0);
                $j(".LbSlideshowPosition", curListItem).html("Bild 1 von " + imagesInCat.length);
                $j(".SlideImageLeft", curListItem).click(function (e) {
                    projectListHandler.SlideImages($j(this), -1);
                });
                $j(".SlideImageRight", curListItem).click(function (e) {
                    projectListHandler.SlideImages($j(this), 1);
                });
            }
        }
    },

    SlideImages: function (jClickedArrow, idxShift) {
        var slideshowContainer = jClickedArrow.parents(".ImageSlideshowContainer");

        // Preventing parallel animations.
        // [MB]
        if ((slideshowContainer.length > 0) && (slideshowContainer.attr("IsAnimating") != "true")) {
            slideshowContainer.attr("IsAnimating", "true");

            // Getting information about current animation.
            // [MB]
            var movingContainer = $j(".MovingContainer", slideshowContainer);
            var slideshowImages = $j("img.SlideshowImage", slideshowContainer);
            var currentIdx = parseInt(slideshowContainer.attr("CurrentImageIdx"));
            var newIdx = currentIdx + idxShift;

            // Preventing out of range errors.
            // [MB]
            if (newIdx < 0) {
                newIdx = 0;
            }
            if (newIdx >= (slideshowImages.length)) {
                newIdx = (slideshowImages.length) - 1;
            }

            if (newIdx != currentIdx) {
                var newLeft = (newIdx * projectListHandler.ImageItemWidth) * -1;
                movingContainer.stop().animate({ left: newLeft }, 500, function () {
                    slideshowContainer.attr("IsAnimating", "false");
                    slideshowContainer.attr("CurrentImageIdx", newIdx)
                    $j(".LbSlideshowPosition", slideshowContainer).html("Bild " + (newIdx + 1) + " von " + slideshowImages.length);
                });
            }
            else {
                slideshowContainer.attr("IsAnimating", "false");
            }
        }
    },

    ShowEnvironmentInformation: function (linkItem, address) {
        var container = $j(linkItem).parents(".SectionContainer");

        if (container.length > 0) {
            var overlay = $j(".MapOverlay", container);

            if (overlay.length > 0) {
                var overlayHeight = overlay.parents(".SectionContainer")[0].offsetHeight;

                var mapContainerDiv = $j(".MapContainerDIV", container);
                mapContainerDiv.css("height", overlayHeight - 92);

                overlay.stop().css({
                    "display": "block",
                    "height": "0px"
                }).animate({
                    "height": overlayHeight - 68
                }, 300, function () {
                    var mapContainerDiv = $j(".MapContainerDIV", container);

                    if (mapContainerDiv.length > 0) {
                        listMapHelper.RegisterMapContainer(container, mapContainerDiv[0], address);
                    }
                });
            }
        }
    },

    HideEnvironmentInformation: function (item) {
        var container = $j(item).parents(".SectionContainer");

        if (container.length > 0) {
            var overlay = $j(".MapOverlay", container);

            if ((overlay.length > 0) && (overlay.css("display") == "block")) {
                overlay.stop().animate({
                    "height": "0px"
                }, 300, function () {
                    $j(this).css({
                        "display": "none"
                    });

                    var mapContainerDiv = $j(".MapContainerDIV", container);

                    if (mapContainerDiv.length > 0) {
                        listMapHelper.ResetMap(mapContainerDiv.attr("id"));
                    }
                });
            }
        }
    }
}

var projectListHandler = new ProjectListHandler();
