﻿/*Written by Courtney R. S. May 2011
    Design By Courtney (www.designbycourtney.com)
    courtney.designbycourtney@gmail.com 
    May be distributed and used as you please as long as my information is not removed.*/
jQuery.noConflict();
jQuery(document).ready(function () {
    //NOTE: in the chosen directory, the first image must be named 0, and all images following it must be in numerical order.
    var directory = "directory"; //image directory
    var extension = ".jpg"; //all image extensions must be the same.
    var max = 8; //name of the last image in the sequence
    var fadeInSpeed = 1000; //milliseconds : 1000 = 1 second
    var fadeOutSpeed = 2000; //milliseconds : 1000 = 1 second
    var delayTime = 2000; //delay time (how long the image 'sits there' before fading out to the next image)

    ///don't modify

    var i = 0; //do not modify
    var myImages = new Array();

    preloadPaths();
    fadeIn();

    function fadeIn() {
        try {
            var test = myImages[i].dbcIsLoaded;
        }
        catch (Error) {
            alert('Error. Image invalid.');
        }

        if (!myImages[i].dbcIsLoaded) {
            setTimeout(fadeIn, 1000);
            return;
        }
        var containerRatio = jQuery('#imageContainer').width() / jQuery('#imageContainer').height();

        var currentImage = myImages[i];

        if (currentImage.width > jQuery('#imageContainer').width() || currentImage.height > jQuery('#imageContainer').height()) {
            if (currentImage.width < currentImage.height) {
                var imageRatio = currentImage.width / currentImage.height;
                if (imageRatio < containerRatio) {
                    jQuery('#image').height(jQuery('#imageContainer').height());
                    jQuery('#image').width(jQuery('#imageContainer').height() * imageRatio);
                }
                else {
                    var ratio = currentImage.height / jQuery('#imageContainer').height();
                    var height = currentImage.height * ratio;
                    jQuery('#image').height(height);
                    jQuery('#image').width(height * imageRatio);
                }
            }
            else if (currentImage.width > currentImage.height) {
                var imageRatio = currentImage.width / currentImage.height;
                if (imageRatio < containerRatio) {
                    jQuery('#image').height(jQuery('#imageContainer').height());
                    jQuery('#image').width(jQuery('#imageContainer').height() * imageRatio);
                }
                else {
                    var ratio = jQuery('#imageContainer').width() / currentImage.width;
                    var width = currentImage.width * ratio;
                    jQuery('#image').width(width);
                    jQuery('#image').height(width * (1 / imageRatio));
                }
            }
        }
        else {
            jQuery('#image').height(currentImage.height);
            jQuery('#image').width(currentImage.width);
        }
        jQuery('#image').attr('src', currentImage.src); //.extension
        jQuery('#image').fadeTo(fadeInSpeed, 1.0, function () {
            fadeOutImage();
        });
    }

    function preloadPaths() {
        for (var imageIndex = 0; imageIndex <= max; imageIndex++) {
            var path = directory + '/' + imageIndex + extension;
            var image = new Image();
            image.src = path;
            image.dbcImageIndex = imageIndex;
            myImages[imageIndex] = image;
            myImages[imageIndex].dbcIsLoaded = false;
            myImages[imageIndex].onload = function () {
                this.dbcIsLoaded = true;
            }
        }
    }

    function fadeOutImage() {
        jQuery('#image').fadeTo(delayTime, 1.0, function () {
			if(i<max)
			{
				jQuery('#imagecell').css("background-image", "url(directory/bg" + i + ".jpg)");
				
			}
			else
				jQuery('#imagecell').css("background-image", "url(directory/bg8.jpg)");
			fadeOutCode();
        });
    }

    function fadeOutCode() {
        jQuery('#image').fadeOut(fadeOutSpeed, function () {
            assignNextImage();
            fadeIn();
        });
    }

    function assignNextImage() {
        if (i == max)
            i = 0;
        else {
            i++;
        }
    }
});
