/******************************************************************************************************
*  GALLERY.JS - PHOTO SLIDESHOW
*
*  Written by Nicholas Slate - Property of Marshall University COSITC
*  Last modified: 11/3/2008
*
*  Depends on: JQuery 1.2 (www.jquery.com), JQuery Cycle Plugin (www.malsup.com/jquery/cycle/) 
*******************************************************************************************************/


// Specify the image files.... Extend or contract array as needed
//   Image in HTML is removed to provide a clean slate for the gallery
var fadeImages = new Array(
    {filename: '/physics/images/default1.jpg', caption: "Daniel Velazquez presenting at Sigma Xi."},
    {filename: '/physics/images/default2.jpg', caption: "Josh Hafer presenting at Sigma Xi."},
    {filename: '/cos/r/i/g/22.jpg', caption: "Steven Taylor ('06 physics BS graduate and MUSOM student) and Dr. Thomas Wilson in the lab working on an American Foundation for the Blind TECH collaboration."}
<!--	{filename: '/physics/images/DSCN0170.jpg', caption: "Students in Dr. Richard Bady's lab Physical Science 109."}, -->
<!--	{filename: '/physics/images/DSCN0171.jpg', caption: "Students in Dr. Richard Bady's lab Physical Science 109."}, -->
<!--	{filename: '/physics/images/DSCN0173.jpg', caption: "Students in Dr. Richard Bady's lab Physical Science 109."} -->
);

// settings for the photo gallery
var imageWidth = 600;
var imageHeight = 275;
var imageIdPrefix = 'GalleryImage_'; // change if image IDs conflict with other page elements
var captionFadeDuration = 500;

// persistant storage for needed values
var captionElement;
var captionOpacity = 1;
var defaultImageCaption = '';
var firstTransition = true;

// dom ready event
$(document).ready(function(){                     
    
    // if there are images to display
    if (fadeImages && fadeImages.length && fadeImages.length > 0) {
    
        // collect some frequently used properties
        var imageContainer = $('#FeaturePictureContainer');
        imageContainer.empty();
        
        // create caption container
        captionElement = window.document.createElement('div');
        captionElement.id = 'FeaturePictureCaption';
        captionElement.innerHTML = $('#FeaturePicture').attr('alt');
        $('#PageFeatureInner').prepend(captionElement);
        
        // get opacity specified for caption in stylesheet
        if (jQuery.browser.msie) {
            captionOpacity = $(captionElement).css('filter'); // get value from filter property
            captionOpacity = captionOpacity.replace(/[^0-9\.]/g, ''); // replace anything but numbers and decimals (leave opacity value)
            captionOpacity = captionOpacity / 100; // put value in decimal form
        }
        else {
            captionOpacity = $(captionElement).css('opacity');
        }
        
        // hold created elements until they're placed
        var newImage;
        // create images for gallery
        for (var index in fadeImages) {
            if (!isNaN(index)) {
                newImage = window.document.createElement('img');
                newImage.id = imageIdPrefix + index;
                newImage.width = imageWidth;
                newImage.style.width = imageWidth + 'px'; // set width style too just to make IE gets it
                newImage.height = imageHeight;
                newImage.style.height = imageHeight + 'px'; // set height style too just to make IE gets it
                newImage.src = fadeImages[index].filename;
                imageContainer.append(newImage);
            }
        }
        
        // cycle through images placed in container
        imageContainer.cycle({ 
            fx:         'fade',                     // fade is the only transition available in this build (others available at www.malsup.com/jquery/cycle/)
            speed:      3000,                       // duration of transition
            timeout:    9000,                       // interval between transitions
            pause:      1,                          // pause on mouse over
            next:       '#FeaturePictureContainer', // clicking on this element will start transition
            before:     galleryBefore,              // actions performed before each transition
            after:      galleryAfter                // actions performed after each transition
        });
    }
});

// actions performed before each transition
function galleryBefore() {
    // prevent flicker on load
    if (!firstTransition) {
        // hide caption
        $(captionElement).fadeOut(captionFadeDuration);
    }
    firstTransition = false;
}
// actions performed after each transition
function galleryAfter() {
    var imageIndex = parseInt(this.id.substring(imageIdPrefix.length));
    // set caption text and display
    if (typeof(fadeImages[imageIndex]) != 'undefined' && typeof(fadeImages[imageIndex].caption) != 'undefined') {
        if (jQuery.browser.msie) {
            $(captionElement).css('opacity', captionOpacity); // set opacity again to remind IE
		    $(captionElement).css('filter', 'alpha(opacity=' + (captionOpacity*100) + ')');
		}
        $(captionElement).html(fadeImages[imageIndex].caption);
        $(captionElement).fadeIn(captionFadeDuration);
    }
}