(function() {
    jQuery.widget('pkp.bannerChanger', {
        options: {
            url: '',
            start: 0
        },

        banners: {},
        current: 0,
        interval: null,

        _create: function() {
            var self = this,
                o    = self.options,
                el   = self.element,
                container = $('<div/>').addClass('container')
                                       .appendTo(el),
                navigation = $('<ul/>').addClass('navigation')
                                       .appendTo(el);

            el.addClass('pkp-banner')
              .css('width', o.width)
              .css('height', o.height);

            $.ajax({
                url: o.url,
                type: 'get',
                dataType: 'json',
                success: function(r) {
                    for (i=0; i<r.length; i++) {
                        var anchor = $('<a/>').attr('href', 'banner_' + i);
                        $('<li/>').append(anchor)
                                  .appendTo(navigation);
                    }

                    $('a', navigation).click(function(e) {
                        e.preventDefault();

                        var id = $(this).attr('href')
                                        .replace('banner_', '');
                        self.select(id);
                    });

                    self.banners = r;
                    self.select(o.start);
                }
            });
        },

        draw: function() {
            var self = this,
                o    = self.options,
                el   = self.element;

            clearInterval(self.interval);

            var container  = el.find('.container');
            var navigation = el.find('.navigation');

            $('a', navigation).removeClass('active');
            $('a[href=banner_' + self.current + ']', navigation).addClass('active');

            container.empty();
            container.flash({
                swf: self.banners[self.current],
                width: o.width,
                height: o.height,
                params: {
                    loop: '0',
                    wmode: 'transparent'
                }
            });

            self.interval = setInterval(function() {
                container.flash(function() {
                    var currentFrame = this.TGetProperty('/', 4),
                        totalFrames  = this.TGetProperty('/', 5);

                    if (typeof currentFrame == 'undefined' ||
                        typeof totalFrames == 'undefined') {

                        return;
                    }

                    if (currentFrame == totalFrames) {
                        self.next();
                    }
                });
            }, 500);
        },

        next: function() {
            var self = this;

            if ((self.banners.length - 1) == self.current) {
                self.current = 0;
            } else {
                self.current += 1;
            }

            self.draw();
        },

        select: function(value) {
            var self = this;

            self.current = parseInt(value);
            self.draw();
        },

        destroy: function()  {
            this.element.find('.navigation a').unbind('click');
            this.element.empty();
            this.element.removeClass('pkp-banner');
        }
    });
})(jQuery);
