jQuery.fn.slideshow = function(getPreferences, onChange)
{
    return this.each(function()
    {
        var pref = getPreferences.call(this);
        var one = pref.content.length;
        var two = pref.content.length-1;

        var img1 = $('<img id="img1"/>').attr('src', pref.content[0].img);
        var img2 = $('<img id="img2"/>').attr('src', pref.content[1].img);
        $(this).empty().append(img1).append(img2);
        $(this).find('img').css('display', 'none').css('position', 'absolute').css('top', '0px').css('left','0px');
        var activeItem = 0;

        var fading = function()
        {
            activeItem = (++activeItem) % pref.content.length;
            img2.attr('src', pref.content[activeItem].img);
            var t = img2;img2 = img1;img1 = t;
            timeout = setTimeout(function()
            {
                if(typeof onChange == 'function'){onChange.call(pref.content[activeItem]);}
                img1.fadeIn(pref.fadePeriod, fading);
                img2.fadeOut(pref.fadePeriod);
            }, pref.showPeriod)
        }
        img1.fadeIn(pref.fadePeriod, function(){fading();});
        onChange.call(pref.content[activeItem]);
        var timeout = null;

        $(document).bind('nextSlide',  function(event, data)
        {
            activeItem = data.slide - 1;
            if(typeof onChange == 'function'){onChange.call(pref.content[activeItem]);}
            img1.attr('src', pref.content[activeItem].img);
            img2.attr('src', pref.content[activeItem].img);
        })
    });
}