var thisGalleryId;
function initializeGallery(galleryId) {
    var galleryDiv = NC.$('gallerySlideshow');
    if (galleryDiv && galleryId > 0) {
        thisGalleryId = galleryId;
        new NC.Ajax('/gallery/embedslideshow/' + galleryId, {onReady: setupGallery}).request();
    }
}
function setupGallery() {
    if (this.response && this.response.length > 0) {
        NC.$('gallerySlideshow').setHtml(this.response);
        var prev = NC.$('NC_Gallery_Previous');
        if (prev) {
            prev.onclick = getPhoto;
        }
        var next = NC.$('NC_Gallery_Next');
        if (next) {
            next.onclick = getPhoto;
        }
        var table = NC.$('NC_Gallery');
        var col = NC.$('NC_Gallery_Col_1')
        table.style.height = col.getFirst().getProperty('height') + 'px';
        col.style.width = col.getFirst().getProperty('width') + 'px';
    }
}
function getPhoto() {
    if (this.href) {
        var parts = this.href.split('/');
        var id = parts[parts.length -1];
        new NC.Ajax('/gallery/getPhoto/' + thisGalleryId + '/' + id, {onReady: setGallery}).request();
        return false;
    }
}

function setGallery() {
    if (this.responseXML && this.responseXML.documentElement) {
        var xml = new NC.Xml(this.responseXML.documentElement);

        var next = xml.getTagValue('next');
        var previous = xml.getTagValue('previous');
        var nextImage = xml.getTagValue('nextImage');
        var previousImage = xml.getTagValue('previousImage');
        var name = xml.getTagValue('name');
        var description = xml.getTagValue('description');
        var imageHeight = xml.getTagValue('imageHeight');
        var imageWidth = xml.getTagValue('imageWidth');
        var image = xml.getTagValue('image');

        if (imageHeight.length > 0 && imageWidth.length > 0 && image.length > 0) {
            NC.$('NC_Gallery').style.height = imageHeight + 'px';
            NC.$('NC_Gallery_Col_1').style.width = imageWidth + 'px';
            NC.$('NC_Gallery_Col_1').setHtml('<img src="' + image + '" alt="' + name + '" />');
        }
        var information = '<h3>' + name + '</h3>';
        if (description.length > 0) {
            information += '<p>' + description + '</p>';
        }
        NC.$('NC_Gallery_Details').setHtml(information);
        NC.$('NC_Gallery_Previous').href = '/gallery/embedslideshow/' + thisGalleryId + '/' + previous;
        NC.$('NC_Gallery_Next').href = '/gallery/embedslideshow/' + thisGalleryId + '/' + next;

        if (nextImage && nextImage.length > 0) {
            var nextPreload = new Image();
            nextPreload.src = nextImage;
        }
        if (previousImage && previousImage.length > 0) {
            var prevPreload = new Image();
            prevPreload.src = nextImage;
        }
    }
}