if (typeof(m) == "undefined" || !m) {
	var m = {};
}

(function($) {
    m.rollUp = function(el, images, links)
    {
        this.el = el;
        this.images = images;
        this.links = links;
        this.current = 0;
        this.interval = null;
    }

    m.rollUp.prototype.setup = function()
    {
        var that = this;
        var aNodes = $('a[class!=imageLink]', this.el);
		//console.log(aNodes);
        $.each(this.images, function(i, img) {
            $(aNodes[i]).click(function(e) {
                e.preventDefault();

                that.stopInterval();
                that.setCurrentImage(i);
            });
        });

		$.haveLocalI(this.images);

        this.startInterval();
    }

    m.rollUp.prototype.stopInterval = function()
    {
        window.clearInterval(this.interval);
    }

    m.rollUp.prototype.startInterval = function()
    {
        var that = this;
        this.interval = window.setInterval(function() {
            var pos = that.current;
            if (pos == (that.images.length - 1)) {
                pos = 0;
            } else {
                pos += 1;
            }

            that.setCurrentImage(pos);
        }, 5000);
    }

    m.rollUp.prototype.setCurrentImage = function(pos)
    {
        this.current = pos;
        $('.image', this.el)
            .css({
                'background-image' : 'url(' + this.images[pos] + ')',
                'opacity' : '0'
            })
            .animate({opacity: 1}, 1000)
        ;
        $('.imageLink', this.el).attr('href',this.links[pos])


        $('.current').removeClass('current');
        $('li:eq(' + pos + ')', this.el).addClass('current');
    }
}) (jQuery);
