var impGalleryActiveInstances = 0;
var impGallerySettings = {};

jQuery.fn.extend({
    gallery: function(setConfig) {
        return this.each(function() {
            var config = {
                name: 'slideshow' + impGalleryActiveInstances,
                transition: "blend",
                timeout: 10000,
                transtime: 1000,
                playing: "true",
                order: "true",
                height: null,
                width: null,
                timer: null
            }
            if (setConfig != null) {
                for (var c in setConfig) {
                    config[c] = setConfig[c];
                }
                setConfig.transition != null
            }

            $(this).addClass('slideshow');
            $(this).addClass(config.name);
            $(this).children('li').css({ opacity: 0.0 });
            $(this).children('li:first').addClass('active');
            $(this).children('li:first').css({ opacity: 1.0 });

            if (config.width != null && config.height != null) {
                $(this).css({ width: config.width + 'px', height: config.height + 'px', padding: 0, margin: 0, overflow: 'hidden' });
            }
            if (config.playing == "true") {
                config.timer = setTimeout('impGalleryTransition("' + config.name + '")', config.timeout);
            }
            config.count = $(this).children('li').length;
            config.current = 0;
            impGallerySettings[config.name] = config;
            impGalleryActiveInstances++;
        });
    }
});

function impGalleryTransition(targetGallery, transConfig) {
    var config = impGallerySettings[targetGallery];
    // if this function is called outside of the normal loop order, clear the loop and restart.
    clearTimeout(config.timer);

    if (transConfig != null) {
        if (transConfig.to == "play") {
            config.playing = "true";
        } else if (transConfig.to == "stop") {
            config.playing = "false";
            return false;
        }
    }
                
    if (config.playing == "true") {
        config.timer = setTimeout('impGalleryTransition("' + targetGallery + '")', config.timeout);
    }

    var transition = config.transition;
    if (transConfig != null) {
        if (transConfig.transition != null) {
            transition = transConfig.transition;
        }
    }
    if (transition == 'horizontal') {
        transition = 'right';
    }

    var $active = $('.' + targetGallery).children('li:eq(' + config.current + ')');
    $active.addClass('last-active');
    /*
    var $active;
    $active = $('.' + targetGallery + ' .active');
    if ($active.length == 0) {
    $active = $('.' + targetGallery + ' :last')
    };
    */

    var nextInt;
    nextInt = config.current + 1;
    if (transConfig != null) {
        if (transConfig.to != null) {
            if (IsInt(transConfig.to)) {
                nextInt = parseFloat(transConfig.to);
                if (transition == 'right' && nextInt < config.current) {
                    transition = 'left';
                }
            } else if (transConfig.to == "prev") {
                nextInt = config.current - 1;
                if (transition == 'right') {
                    transition = 'left';
                }
            } else if (transConfig.to == "first") {
                nextInt = 0;
                if (transition == 'right') {
                    transition = 'left';
                }
            } else if (transConfig.to == "last") {
                nextInt = config.count - 1;
            }
        }
    }

    nextInt = (nextInt + config.count) % config.count;

    var $next = $('.' + targetGallery).children('li:eq(' + nextInt + ')');
    if (nextInt == config.current) {
        return
    }

    /*
    var $next;
    if ($active.next().length) {
    $next = $active.next()
    } else {
    $next = $('.' + targetGallery + ' :first')
    }
    */




    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );




    if (transition == "fade") {//fade through background colour
        $active.css({ opacity: 1.0 })
            .animate({ opacity: 0.0 }, config.transtime / 2, function() {

                $next.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, config.transtime / 2, function() {
                    $active.removeClass('active last-active');
                });
            });

    } else if (transition == "right" && config.width != null && config.height != null) {
        $active.css({ opacity: 1.0, left: 0 + 'px' })
            .animate({ left: -config.width + 'px' }, config.transtime);
        $next.css({ opacity: 1.0, left: config.width + 'px' })
            .addClass('active')
            .animate({ left: 0 + 'px' }, config.transtime, function() {
                $active.removeClass('active last-active')
                        .css({ opacity: 0.0 })
            });

    } else if (transition == "left" && config.width != null && config.height != null) {
        $active.css({ opacity: 1.0, left: 0 + 'px' })
            .animate({ left: config.width + 'px' }, config.transtime);
        $next.css({ opacity: 1.0, left: -config.width + 'px' })
            .addClass('active')
            .animate({ left: 0 + 'px' }, config.transtime, function() {
                $active.removeClass('active last-active')
                        .css({ opacity: 0.0 })
            });

    } else { // blend to next image ( default )
        $next.css({ opacity: 0.0 })
            .addClass('active')
            .animate({ opacity: 1.0 }, config.transtime, function() {
                $active.removeClass('active last-active')
                        .css({ opacity: 0.0 })
            });
    }
    config.current = nextInt;
    if (config.writeInt != null) {
        $('.' + config.writeInt).text(nextInt + 1);
    }
    if (config.highlight != null) {
        $('.' + config.highlight + " .highlight").removeClass("active");
        $('.' + config.highlight + " [command=" + nextInt + "] .highlight").addClass("active");
    } 
    
}

function IsInt(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}
function IsNum(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

