/*
*   Written By: Björn Olsson
*   Sublime Consulting
*/
var ImageGalleryItem = Class.create();
Object.extend(ImageGalleryItem.prototype, {
    initialize: function(thumbnailUrl, fullsizeUrl, owner, index, page) {
        this.ThumbnailUrl = thumbnailUrl;
        this.FullSizeUrl = fullsizeUrl;
        this.Owner = owner;
        this.Index = index;
        this.Page = page;
    }
});

var ImageGallery = Class.create();
Object.extend(ImageGallery.prototype, {
    initialize: function(virtualPath, pageSize, callback) {
        this.Images = new Array();
        this.PageSize = pageSize;
        this.ShowingThumbnails = true;
        this._onInitializedCallback = callback;
        
        var ajaxRequestUrl = "/Ajax/ImageGallery.ashx?v=" + encodeURIComponent(virtualPath);
        var reference = this;
        
        new Ajax.Request(ajaxRequestUrl, {
            method: "get",
            requestHeaders: { Accept: "application/json" },
            onSuccess: function(transport) {
                var result = transport.responseText.evalJSON(true);
                
                for(var i = 0; i < result.Images.length; i++) {
                    reference.Images.push(new ImageGalleryItem(result.Images[i].ThumbnailUrl, result.Images[i].FullSizeUrl, result.Images[i].Owner, i, Math.ceil((i + 1) / reference.PageSize)));
                }
                
                reference.TotalCount = reference.Images.length;
                reference.TotalPageCount = Math.ceil(reference.TotalCount / reference.PageSize);
                
                reference.OnInitialized();
            }
        });
    }
});
ImageGallery.prototype.GetThumbnails = function(page, callback) {
    this._onThumbnailsLoadedCallback = callback;
    
    if(page < 1)
        page = 1;
    
    var result = new Array();
    var skip = (page - 1) * this.PageSize;
    var index = 0;
    
    for(var i = skip; i < this.TotalCount && index < this.PageSize; i++)
    {
        index++;
        result.push(this.Images[i]);
    }
    
    this.PreloadThumbnails(result);
}
ImageGallery.prototype.PreloadThumbnails = function(images) {
    this._thumbnailsLoaded = 0;
    this._thumbnails = images;
    this._thumbnailsToLoad = this._thumbnails.length;
    
    if(navigator.appVersion.indexOf("MSIE")!=-1) {
        this.OnThumbnailsLoaded();
        return;
    }
    
    var reference = this;
    
    for(var i = 0; i < this._thumbnailsToLoad; i++)
    {
        var image = new Image();
        image.onload = function() {
            reference._thumbnailsLoaded++;
    
            if(reference._thumbnailsLoaded >= reference._thumbnailsToLoad)
                reference.OnThumbnailsLoaded();
        };
        image.src = this._thumbnails[i].ThumbnailUrl;
    }
}
ImageGallery.prototype.GetImage = function(index, callback) {
    this._onImageLoadedCallback = callback;
    
    this.PreloadImage(this.Images[index]);
}
ImageGallery.prototype.PreloadImage = function(image) {
    this._image = image;
    var reference = this;
    
    var imageLoader = new Image;
    imageLoader.onload = function() {
        reference.OnImageLoaded();
    };
    imageLoader.src = image.FullSizeUrl;
}
ImageGallery.prototype.OnInitialized = function() {
    this._onInitializedCallback();
}
ImageGallery.prototype.OnThumbnailsLoaded = function () {
    this._onThumbnailsLoadedCallback(this._thumbnails);
}
ImageGallery.prototype.OnImageLoaded = function () {
    this._onImageLoadedCallback(this._image);
}