﻿/*  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 = 176;
    
    // 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.maxX;
    

    this.GalleryThumbsContainer = this.GalleryContainer.find('.pmv-GalleryThumbs-Container');
    this.GalleryThumbsSlideshow = this.GalleryContainer.find('.pmv-GalleryThumbs');
    this.GalleryPhotosContainer = this.GalleryContainer.find('.pmv-GalleryPhotos-Container');
    this.GalleryPhotosImage = this.GalleryContainer.find('.pmv-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.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Caption').html($('.pmv-GalleryCaption-' + 1).html());

        var ref = this;

        this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Next').click(function(e) {
            galleries[ref.increment].navigateItems(1);
            return false;
        });

        this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Prev').click(function(e) {
            galleries[ref.increment].navigateItems(-1);
            return false;
        });

        this.GalleryThumbsContainer.find('.pmv-GalleryPhotos-Controls-Next').click(function(e) {
            galleries[ref.increment].slidePhotos(-1);
            return false;
        });
        this.GalleryThumbsContainer.find('.pmv-GalleryPhotos-Controls-Prev').click(function(e) {
            galleries[ref.increment].slidePhotos(1);
            return false;
        });


        this.GalleryThumbsContainer.find('.pmv-GalleryThumb-Image').each(function(i) {
            $(this).find('a').click(function(e) {
                galleries[ref.increment].showItemImage(i + 1);
                return false;
            });
        });

        this.GalleryThumbsContainer.find('.pmv-GalleryThumb-Image:first').addClass('selected');


        this.initSlider();
    }
    else {
        // load coming soon image
        this.GalleryPhotosImage.find('img.primary').attr('src', this.noImagesUrl);

        // hide navigation controls
        this.GalleryContainer.find('.pmv-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('.pmv-GalleryPhotos-Controls-Prev').addClass( 'disabled' );
	    
	    if( this.currentItem != this.photoCount ) {
			this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Next').removeClass( 'disabled' );
		}
		else {
		    this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Next').addClass( 'disabled' );
		}
    }
    else if( this.currentItem == this.photoCount ) {
		// last item
	    this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Prev').removeClass( 'disabled' );
	    this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Next').addClass( 'disabled' );
    }
    else {
		// mid-stream
	    this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Controls-Prev').removeClass( 'disabled' );
	    this.GalleryPhotosContainer.find('.pmv-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('.pmv-GalleryPhotos-Controls-Prev').addClass( 'disabled' );
    }
    else {
	    this.GalleryThumbsContainer.find('.pmv-GalleryPhotos-Controls-Prev').removeClass( 'disabled' );
    }

    // handle next button for thumbnails
    if( startX == ( 0 - this.maxX ) ) {
	    this.GalleryThumbsContainer.find('.pmv-GalleryPhotos-Controls-Next').addClass( 'disabled' );
    }
    else {
	    this.GalleryThumbsContainer.find('.pmv-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;
    
    if(this.imagesClickable == "true")
     {
         this.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
     {
      this.GalleryPhotosImage.find('img.primary').fadeTo( fadeDelay, 0,
	        function() {
	            ref.GalleryPhotosImage.find('img.primary').attr( 'src', ref.imageUrls[imgNum -1] );
	        }
        );
     }
	     

    this.GalleryPhotosImage.find('img.primary').fadeTo( fadeDelay, 1 );    
    this.GalleryThumbsSlideshow.find('.pmv-GalleryThumb-Image:eq(' + (this.currentItem - 1) + ')').removeClass('selected');
    
    this.currentItem = imgNum;

    this.GalleryThumbsSlideshow.find('.pmv-GalleryThumb-Image:eq(' + (this.currentItem - 1) + ')').addClass('selected');

    // update caption text for selected image
    this.GalleryPhotosContainer.find('.pmv-GalleryPhotos-Caption').html( this.GalleryThumbsSlideshow.find('.pmv-GalleryCaption-' + this.currentItem).html() );
    this.jumpToItem( this.currentItem );
    this.updateItemNavigation();
}


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 + 5 );

		this.updateItemNavigation();
	    this.updateThumbnailNavigation();
    }

    // hide slider controls if less than <items per slide> images are displayed
    if( this.photoCount <= this.itemsPerSlide ) {
	    this.GalleryThumbsSlideshow.find('.pmv-GalleryPhotos-Controls').addClass( 'disabled' );
	}			
}