﻿Device = {
    Gallery: {
        Populate: function(data) {
            Device.Gallery.Data = data;
            Device.Gallery.Count = data.length;
            /* preload */
            var preloadArr = [];
            for (var key in data) {
                preloadArr[key] = new Image();
                preloadArr[key].src = data[key];
            }
        },
        Data: new Array(),
        Index: 0,
        Count: 0,
        Lightbox: {
            Open: function(index) {
                Device.Gallery.Index = index;
                Tre.Lightbox.Open('DeviceGalleryLightBox', '<div class="imgContainer"><img id="DeviceGalleryImage" src="' + Device.Gallery.Data[Device.Gallery.Index] + '" alt="" /></div><p class="footer"><a href="javascript:Device.Gallery.Lightbox.Prev();"><img src="/media/images/prev.png" /></a><span id="DeviceGalleryImageNr">' + (Device.Gallery.Index + 1) + '</span> av ' + Device.Gallery.Count + '<a href="javascript:Device.Gallery.Lightbox.Next();"><img src="/media/images/next.png" /></a></p>');
                $('.DeviceGalleryLightBox .imgContainer').click(function(event) {
                    if (event.pageX - $(this).offset().left < $(this).outerWidth() / 2) { Device.Gallery.Lightbox.Prev(); }
                    else { Device.Gallery.Lightbox.Next(); }
                });
            },
            Next: function() {
                if (Device.Gallery.Index + 1 >= Device.Gallery.Count) { Device.Gallery.Index = 0; }
                else { Device.Gallery.Index++; }
                $('#DeviceGalleryImageNr').text(Device.Gallery.Index + 1);
                $('#DeviceGalleryImage').attr('src', Device.Gallery.Data[Device.Gallery.Index]);
            },
            Prev: function() {
                if (!Device.Gallery.Index) { Device.Gallery.Index = Device.Gallery.Count - 1; }
                else { Device.Gallery.Index--; }
                $('#DeviceGalleryImageNr').text(Device.Gallery.Index + 1);
                $('#DeviceGalleryImage').attr('src', Device.Gallery.Data[Device.Gallery.Index]);
            }
        }
    }
}