jQuery.fn.homeGallery = function(options) {

    // Default options
    var opt = $.extend({
        targetDiv: ".gallery-item",
        animTimer: 900,
        easing: "easeOutQuint",
        autoplay: true,
        playDelay: 3200,
        sleepOnLoop: 0
    }, options);

    //init
    var pane;
    _max = 0;
    _current = 0;
    var maindiv;
    var panelnav;
    var _iteration = 0;


    //for each gallery passed (for multiple galleries per page)
    return this.each(function() {

        pane = $(this).children(opt.targetDiv); //scoop up media pane content
        maindiv = $(this); //using this a lot, lets var it up - performance increase?
        _max = pane.length - 1;

        $(maindiv).html(''); //clear media pane of static content, we can rebuild you, faster stronger blah bleh bleugh
        $(maindiv).css('position', 'relative'); //should really do this in css - but to be sure???  dunno...

        if (pane.length != 0) {
            // change maindiv content to first pane and set the timer going for first change
            ChangePane(_current);
            if (opt.autoplay && _max > 1) {
                setTimeout(function() { AutoPlay() }, opt.playDelay);
            }
        } else {
            maindiv.html('<p style="border:solid 1px red;padding:5px;color:red;">No gallery content found.  Gallery plugin stopped.</p>');
        }

    });

    function AutoPlay() {

        if (opt.autoplay) {
            //if (_iteration <= opt.sleepOnLoop) {
            _current++;
            _iteration++;
            if (_current > _max) {
                _current = 0;
            }
            ChangePane(_current);
            setTimeout(function() { AutoPlay() }, opt.playDelay); // re-re-re-recursion
            //}
        }
    }

    function StopAll() {
        maindiv.stop();
    }

    function ChangePane(target) {
        StopAll();
        newpane = NewPane(target);
        AnimatePane(newpane);
    }

    function NewPane(target) {
        newpane = $(pane[target]);
        newpane.addClass('waiting')
        newpane.css({ opacity: 0, position: 'absolute', top: '0px' });
        return newpane;
    }

    function AnimatePane(replacement) {
        jQuery.easing.def = opt.easing;
        maindiv.append(replacement);
        replacement.animate({ opacity: 1 }, opt.animTimer, function() {
            maindiv.find('.remove').not(replacement).remove();
            replacement.addClass('remove');
        });

    }


}
