﻿// slideshow.js
// ------------
// Adds slideshow functionality to a page via the ?SlideshowId= URL parameter, and
// the StartSlideshow() function.

// PUBLIC DECLARATIONS

var HandleAutostart;
var StartSlideshow;



// Hide private members...
( function( $ ) {

    // PUBLIC FUNCTIONS
    
    // Starts any slideshows specified on the URL.
    HandleAutostart = function() {
        
        // Get the slideshow specified - exit if there is none
        var slideshowId = $.getURLParam( 'SlideshowId' );
        if ( !slideshowId ) {
            return;
        }
        
        // Start the given slideshow
        StartSlideshow( slideshowId );
    
    }; // end HandleAutostart()
    
    
    
    // Loads and start the slideshow with the given ID.
    StartSlideshow = function( slideshowId ) {
        
        // If the slideshow isn't on the page already, add it
        var slideshowSelector = '#' + CONTAINER_ID_PREFIX + slideshowId + ' a';
        if ( !SlideshowExists( slideshowId ) ) {
            LoadSlideshow( slideshowId );
            $( slideshowSelector ).lightBox( SLIDESHOW_OPTIONS );
        }
        
        // Start the slideshow
        $( slideshowSelector ).eq( 0 ).click();
    };
    
    
    
    // PRIVATE FUNCTIONS
    
    // Loads the given slideshow photos via ajax and adds them to the page.
    var LoadSlideshow = function( slideshowId ) {
        
        // Add the slideshow container
        var containerId = CONTAINER_ID_PREFIX + slideshowId;
        $( 'body' ).append( 
            '<div class="slideshow" id="' + containerId + '"></div>'
        );
        var $container = $( '#' + containerId );
        
        // Load its photos via Ajax
        $.ajax( { 
            async: false,
            cache: false,
            data: { SlideshowId: slideshowId },
            url: TranslatePath( '~/Ajax/Slideshows/GetById.aspx' ),
            type: 'POST',
            success: function( data ) {
                $container.html( data );
            }
        } );
        
    }; // end LoadSlideshow()
    
    
    
    // Returns true if the given slideshow is on the page, false otherwise.
    var SlideshowExists = function( slideshowId ) {
        
        var containerId = CONTAINER_ID_PREFIX + slideshowId;
        if ( $( '#' + containerId ).size() > 0 ) {
            return true;
        }
        else { 
            return false;
        }
    };
    
    
    
    // Translates app-relative paths ( ~/js/slideshow.js ) to root-relative 
    // paths for the current server (e.g. /oakvilleproperties/js/slideshow.js ).
    var TranslatePath = function( appRelativePath ) {
        
        // If we're running locally, set the local app path...
        var appBasePath;
        if ( location.href.match( 'localhost' ) ) {
            appBasePath = '/oakvilleproperties';
        }
        
        // Otherwise, assume app lives in the root
        else {
            appBasePath = '';
        }
        
        // Translate the given path and return it
        var result = appRelativePath.replace( '~', appBasePath );
        return result;
    };
    
    
    
    // PRIVATE CONSTANTS
    
    var CONTAINER_ID_PREFIX = 'Slideshow_';
    var SLIDESHOW_OPTIONS = {
        containerResizeSpeed: 600,
        fixedNavigation: true,
        imageLoading: TranslatePath( '~/images/lightbox/loading.gif' ), 
        imageBtnPrev: TranslatePath( '~/images/lightbox/prevlabel.png' ), 
        imageBtnNext: TranslatePath( '~/images/lightbox/nextlabel.png' ), 
        imageBtnClose: TranslatePath( '~/images/lightbox/closelabel.png' ), 
        imageBlank: TranslatePath( '~/images/lightbox/blank.gif' ),
        overlayOpacity: 0.7
    };
    
    

    // MAIN SCRIPT

    // When document has loaded...
    $( function() {
        
        // Maximize window if this is marked as a campaign (IE only)
        if ( $.getURLParam( 'campaign' ) ) {
            window.moveTo(0,0);
            window.resizeTo(screen.width,screen.height);
        }
        
        // Handle any url-parameter slideshows
        HandleAutostart();
        
    } );

} )( jQuery ); // end hide private members
