/******************************************************************************************************
*  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: '/cos/r/i/feature-cos.jpg', caption: "The College of Science building features tributes to scientists that have personified the spirit of discovery."},
    {filename: '/cos/r/i/g/1.jpg', caption: "<a href=\"http://www.marshall.edu/isat/\" title=\"Integrated Science and Technology\">IST</a> students diving with Manatees in Florida with the endangered species course."},
    //{filename: '/cos/r/i/g/2.jpg', caption: "Fung is using the Atomic Force Microscope to write patterns of DNA on silicon substrates in Dr. Norton's lab."},
    {filename: '/cos/r/i/g/3.jpg', caption: "Marshall students learn methods in water quality assessment through hands-on experience."},
    {filename: '/cos/r/i/g/4.jpg', caption: "Integrated Science students use GIS software to map animal population density."},
    {filename: '/cos/r/i/g/6.jpg', caption: "<a href=\"http://www.marshall.edu/isat/\" title=\"Integrated Science and Technology\">IST</a> students and faculty survey an Appalachian stream prior to developing a HEC-RAS discharge model."},
    //{filename: '/cos/r/i/g/7.jpg', caption: "Students in BSC 250, Microbiology, prepare to look at bacterial cultures."},
    //{filename: '/cos/r/i/g/10.jpg', caption: "Geology students at an outcrop in Lavalette."},
    //{filename: '/cos/r/i/g/11.jpg', caption: "Recipients of the Robert Fox Fieldcamp Scholarship."},   
    {filename: '/cos/r/i/g/13.jpg', caption: "Amanda is using an infrared spectrometer to study artificial amino acid dyes. These dyes are potentially useful for preparing fluorescent peptides and proteins."},
    //{filename: '/cos/r/i/g/15.jpg', caption: "Kristen presenting her research on the characterization of structural properties of proteins encapsulated in nanoenvironment of reverse micelles to the WV state legislature."},
    {filename: '/cos/r/i/g/16.jpg', caption: "Students work in the mobile water quality laboratory aboard the Chattanooga Star."}
    //{filename: '/cos/r/i/g/17.jpg', caption: "Pi Mu Epsilon went to a conference with several students: (l. to r.) Tue Ngoc Ly, Gustavo S&aacute, Wen Xue, John Stonestreet, Bonnie Shook, Shannon Miller, Elizabeth Duke, and Ashley Tucker."},
    //{filename: '/cos/r/i/g/18.jpg', caption: "Undergraduate Mathematics major, Anthony Justice, begins construction of a mechanical integrator during the first phase of construction."},
    //{filename: '/cos/r/i/g/21.jpg', caption: "Members of the Differential Analyzer Team, Stacy Scudder, Richard Merrit, and Dr. Bonita Lawrence work on the first phase of construction for the Marshall Differential Analyzer Project."},
    //{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: '/cos/r/i/g/urawards.jpg', caption: "UNDERGRAD RESEARCH AWARDS NOW AVAILABLE!!! <a href=\"http://www.marshall.edu/cos/urawards.asp\">Click here</a> for more info!"}
);

// 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);
    }
}