(function($){
    
    $.fn.extend({
        slideshow: function(userOptions) {
            var containerSelector = this.selector;
            var containerId = this.attr('id');
            var defaults = {
                imgPath: '/photo/slideshow/',
                width: 938,
                height: 200,
                imgList : '',
                effect: 'fade',
                time: 4000
            };

            var options =  $.extend(defaults, userOptions);
            
            return this.each(function() {
                var o = options;
                
                o.slideCount = 0;
                for (var image in o.imgList) {
                    if (o.imgList.hasOwnProperty(image)) {
                        o.slideCount++;
                    }
                }
            
                // current slide id
                function getLast() {
                    return $(containerSelector + ' img').last().attr('id');
                }
            
                // value from string
                function getValue(id) {
                    return parseInt(id.match(/img_(\d+)$/)[1], 10);
                }
            
                // get next slide from slide array
                function getNext()
                {
                    var next = getValue(getLast());
                    next++;
                    if (next > o.slideCount) {
                        next = 1;
                    }
                    var id = 'image_' + next;
                
                    return o.imgList[id];
                }
            
                function getNextHtml(next)
                {
                    var html = '<img id="'+next.id+'" src="'+o.imgPath+next.src+'" alt="'+next.alt+'" title="'+next.title +'" width="'+o.width+'" height="'+o.height+'" />';
                    return html;
                }
            
                // load second slide
                var next = getNext();
                if (!$(next.id).length) {
                    var second = $(getNextHtml(next));
                    $(containerSelector).append(second);
                }
            
                var nextImage = next.id;

                        
                $(containerSelector).cycle({
                    fx:       o.effect,
                    timeout:  o.time,
                    before: function (curr, next, opts) {
                        if (!opts.addSlide) {
                            return;
                        }
                   
                        nextImage = next.id;
                   
                        // determine next slide
                        var nextNr = getValue(getLast());
                        nextNr++;
                        if (nextNr>o.slideCount) {
                            nextNr = 1;
                        }
                   
                        var nextImg = o.imgList['image_' + nextNr];
                        if (!$('#' + nextImg.id).length) {
                            opts.addSlide(getNextHtml(nextImg));
                        } else {
                            opts.addSlide = false;
                        }
                    }
                });
            
                // save cookie on document 
                $(window).unload(function() {
                    var nDays = 365;
                    var today = new Date();
                    var expire = new Date();
                    if (nDays === null || nDays === 0) {
                        nDays = 1;
                    }
                    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
                    document.cookie = containerId + "=" + escape(getValue(nextImage)) + ";expires=" + expire.toGMTString();
                });
                
            });
        }
    });
})(jQuery);


