﻿
var galleries;


/*  gallery class */

function ImageGallery(id, increment) {
    this.GalleryContainer = $('#' + id);
    this.increment = increment;

    // photoCount should be pulled in dynamically
    this.photoCount = this.GalleryContainer.find('input.GalleryImageCount').val(); ;

    this.imagesClickable = this.GalleryContainer.find('input.GalleryImagesClickable').val();

    this.noImagesUrl = this.GalleryContainer.find('input.GalleryNoImagesUrl').val();

    //array of image urls
    this.imageUrls = eval('([' + this.GalleryContainer.find('input.GalleryImageUrls').val() + '])');

    // static slideshow item width
    this.itemWidth = 125;

    // number of items visible at once in slider
    this.itemsPerSlide = 3;

    // slider delay in milliseconds
    this.slideDelay = 500;

    // max possible width of sliding image container
    this.maxWidth;

    // currently selected item
    this.currentItem = 1;

    this.imageLoading = false;

    this.maxX;

    this.precacheImage = new Image();

    this.GalleryThumbsContainer = this.GalleryContainer.find('.bp-GalleryThumbs-Container');
    this.GalleryThumbsSlideshow = this.GalleryContainer.find('.bp-GalleryThumbs');
    this.GalleryPhotosContainer = this.GalleryContainer.find('.bp-GalleryPhotos-Container');
    this.GalleryPhotosImage = this.GalleryContainer.find('.bp-GalleryPhotos-Image');

    this.initViewer();
}


ImageGallery.prototype.initViewer = function() {
    if (this.photoCount > 0) {
        //initialize the large image viewer with the first image in our array

        this.GalleryPhotosImage.find('img.primary').attr('src', this.imageUrls[0]);

        if (this.imagesClickable == "true") {
            this.GalleryPhotosImage.find('a.primaryLink').attr('href', this.getURL(this.imageUrls[0]));
        }


        this.GalleryPhotosImage.find('.bp-GalleryPhotos-Caption').html($('.bp-GalleryCaption-' + 1).html());

        var ref = this;

        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Next').click(function(e) {
            galleries[ref.increment].navigateItems(1);
            return false;
        });

        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Prev').click(function(e) {
            galleries[ref.increment].navigateItems(-1);
            return false;
        });

        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Next').click(function(e) {
            galleries[ref.increment].slidePhotos(-1);
            return false;
        });
        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Prev').click(function(e) {
            galleries[ref.increment].slidePhotos(1);
            return false;
        });


        this.GalleryThumbsContainer.find('.bp-GalleryThumb-Image').each(function(i) {
            $(this).find('a').click(function(e) {
                galleries[ref.increment].showItemImage(i + 1);
                return false;
            });
        });


        this.initSlider();
    }
    else {
        // load coming soon image
        this.GalleryPhotosImage.find('img.primary').attr('src', this.noImagesUrl);

        // hide navigation controls
        this.GalleryContainer.find('.bp-GalleryPhotos-Controls').addClass('disabled');

        // hide thumbnails container
        this.GalleryThumbsContainer.hide();
    }
}

ImageGallery.prototype.getURL = function(url) {
    if (this.imagesClickable == "true") {
        var url = url.split('?')[0];

        if (url.indexOf('.tmb') > -1) {
            url = url.replace('.tmb', '.sflb');
        }
    }
    return url;
}


/* only used for larger item view navigation */
ImageGallery.prototype.jumpToItem = function(item) {
    var newX = -((item - 1) * this.itemWidth);

    if (newX > 0) {
        // hitting left edge
        return;
    }
    else if (newX < (0 - this.maxX)) {
        // hitting right edge
        return;
    }

    var ref = this;
    this.GalleryThumbsSlideshow.animate({ top: 0, left: newX }, this.slideDelay, function() { ref.updateThumbnailNavigation(); });
}


ImageGallery.prototype.slidePhotos = function(dir) {
    // dir = -1 for sliding forward, 1 for sliding backward
    var startX = parseInt(this.GalleryThumbsSlideshow.css('left'), 10);

    var newX = startX + (this.itemWidth * dir);
    var diff = startX % this.itemWidth;


    if (Math.abs(diff) > 0) {
        // halfway through a slide, ignore click
        return;
    }

    if ((dir == 1) && (startX == 0)) {
        // hitting left edge
        return;
    }
    else if ((dir == -1) && (newX < (0 - this.maxX))) {
        // hitting right edge
        return;
    }
    else {
        var ref = this;
        this.GalleryThumbsSlideshow.animate({ top: 0, left: newX }, this.slideDelay, function() { ref.updateThumbnailNavigation(); });
    }
}


ImageGallery.prototype.updateItemNavigation = function() {
    // handle next button for full-size image
    if (this.currentItem == 1) {
        // first item
        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Prev').addClass('disabled');

        if (this.currentItem != this.photoCount) {
            this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Next').removeClass('disabled');
        }
        else {
            this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Next').addClass('disabled');
        }
    }
    else if (this.currentItem == this.photoCount) {
        // last item
        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Prev').removeClass('disabled');
        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Next').addClass('disabled');
    }
    else {
        // mid-stream
        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Prev').removeClass('disabled');
        this.GalleryPhotosContainer.find('.bp-GalleryPhotos-Controls-Next').removeClass('disabled');
    }
}


ImageGallery.prototype.updateThumbnailNavigation = function() {
    var startX = parseInt(this.GalleryThumbsSlideshow.css('left'), 10);

    // handle previous buttons thumbnails
    if (startX == 0) {
        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Prev').addClass('disabled');
    }
    else {
        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Prev').removeClass('disabled');
    }

    // handle next button for thumbnails
    if (startX == (0 - this.maxX)) {
        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Next').addClass('disabled');
    }
    else {
        this.GalleryThumbsContainer.find('.bp-GalleryPhotos-Controls-Next').removeClass('disabled');
    }
}

ImageGallery.prototype.navigateItems = function(dir) {
    if ((dir > 0) && this.currentItem < this.photoCount) {
        this.showItemImage(this.currentItem + dir);
    }
    else if ((dir < 0) && this.currentItem > 1) {
        this.showItemImage(this.currentItem + dir);
    }

    this.updateItemNavigation();
}


/* only for item viewer */
ImageGallery.prototype.showItemImage = function(imgNum) {
    var fadeDelay = parseInt(this.slideDelay / 2, 10);
    var ref = this;
    this.imageLoading = true;
    var image = new Image();

    setTimeout("galleries[" + ref.increment + "].showImageLoader()", 500);

    $(image).load(function() {
        ref.imageLoading = false;
        
        ref.GalleryPhotosImage.find('.loader').css('visibility', 'hidden');

        if (ref.imagesClickable == "true") {
            ref.GalleryPhotosImage.find('img.primary').fadeTo(fadeDelay, 0,
	            function() {
	                ref.GalleryPhotosImage.find('img.primary').attr('src', ref.imageUrls[imgNum - 1]);
	                ref.GalleryPhotosImage.find('a.primaryLink').attr('href', ref.getURL(ref.imageUrls[imgNum - 1]));
	            }
            );
        }
        else {
            ref.GalleryPhotosImage.find('img.primary').fadeTo(fadeDelay, 0,
	            function() {
	                ref.GalleryPhotosImage.find('img.primary').attr('src', ref.imageUrls[imgNum - 1]);
	            }
            );
        }


        ref.GalleryPhotosImage.find('img.primary').fadeTo(fadeDelay, 1);

        ref.currentItem = imgNum;

        // update caption text for selected image
        ref.GalleryPhotosImage.find('.bp-GalleryPhotos-Caption').html(ref.GalleryThumbsSlideshow.find('.bp-GalleryCaption-' + ref.currentItem).html());
        ref.jumpToItem(ref.currentItem);
        ref.updateItemNavigation();

    });


    image.src = this.imageUrls[imgNum - 1]; //pre-cache image
}


ImageGallery.prototype.showImageLoader = function() {
    if (this.imageLoading)
        this.GalleryPhotosImage.find('.loader').css('visibility', 'visible')
}

ImageGallery.prototype.initSlider = function() {
    if (this.photoCount > 0) {
        // important: width of rbItemViewerSlideshow should be equal to ( item width * number of items )
        // in order to scroll horizontally properly
        // e.g. 9 items, 138px item width = container is 1242px wide
        this.maxWidth = this.photoCount * this.itemWidth;
        this.maxX = this.maxWidth - (this.itemsPerSlide * this.itemWidth);

        this.GalleryThumbsSlideshow.css('width', this.maxWidth + 10);

        this.updateItemNavigation();
        this.updateThumbnailNavigation();
    }

    // hide slider controls if less than <items per slide> images are displayed
    if (this.photoCount <= this.itemsPerSlide) {
        this.GalleryThumbsSlideshow.find('.bp-GalleryPhotos-Controls').addClass('disabled');
    }
}
