﻿var bwesaGalleries = {
    Galleries: null,
    Initialize: function(galleryID, photoID) {
        if (this.Galleries == null) { this.Galleries = callGetGalleriesByCategoryID(mainMenu.CategoryID()); }
        if (galleryID == null) {
            this.LoadGalleries();
        }
        else if (galleryID != null) {
            if (this.Galleries[galleryID].GalleryItems == null || this.Galleries[galleryID].GalleryItems.length == 0) {
                this.Galleries[galleryID].Index = galleryID;
                this.Galleries[galleryID].GalleryItems = callGetGalleryItemsByID(this.Galleries[galleryID].ID);
            }
            if (photoID == null) {
                this.LoadGallery(galleryID);
            }
            else if (photoID != null) {
                this.LoadPhotoItem(galleryID, photoID);
            }
        }
    },
    LoadGalleries: function() {
        $("#galleryContainer").setTemplate($("#galleryListTemplate").html(), null, { filter_data: false })
                              .processTemplate(this.Galleries);

    },
    LoadGallery: function(galleryID) {
        $("#galleryContainer").setTemplate($("#galleryItemsTemplate").html(), null, { filter_data: false })
                              .processTemplate(this.Galleries[galleryID]);
    },
    LoadPhotoItem: function(galleryID, photoID) {
        if (this.Galleries[galleryID].GalleryItems != null) {
            $("#galleryContainer").setTemplate($("#galleryItemTemplate").html(), null, { filter_data: false })
                                  .setParam("photoIndex", photoID)
                                  .setParam("photoCount", this.Galleries[galleryID].GalleryItems.length)
                                  .setParam("galleryID", galleryID)
                                  .setParam("galleryTitle", this.Galleries[galleryID].Title)
                                  .processTemplate(this.Galleries[galleryID].GalleryItems[photoID]);
        }
    }
};

