var BannerGallery = {
// Options
    option_wrap:                                true,
    option_fade_on_load:                        false,
    option_hide_controls:                       true,
    option_banner_rotation_sec:                 0,
    option_rotation_stop_on_manual_pick:        true,
    option_rotation_pause_on_controls:          true,
    option_rotation_continue_after_controls:    true,
    option_force_image_load:                    true,
    option_no_fade:                             false,
// What images to use.
    image_left:                                 '',
    image_right:                                '',
    image_center:                               '',
// Callback functions. If left to null, will preload to defaults.
    callback_image_change:                      null,
    callback_controls_show:                     null,
    callback_controls_hide:                     null,
    callback_banner_show:                       null,
    callback_banner_hide:                       null,
// Internal vars, shouldn't ever need to be touched
    private_mouseover_counter:                  0,
    private_current_banner:                     0,
    private_banners:                            [],
    private_rotate_pe:                          null,

// Functions!
    setup:                      function() {
    // Setup default effects if not defined
        if (BannerGallery.callback_banner_hide   == null)
            BannerGallery.callback_banner_hide   = BannerGallery.banner_fade;
        if (BannerGallery.callback_banner_show   == null)
            BannerGallery.callback_banner_show   = BannerGallery.banner_appear;
        if (BannerGallery.callback_controls_hide == null)
            BannerGallery.callback_controls_hide = BannerGallery.controls_fade_timer;
        if (BannerGallery.callback_controls_show == null)
            BannerGallery.callback_controls_show = BannerGallery.controls_appear;
        if (BannerGallery.callback_image_change  == null)
            BannerGallery.callback_image_change  = BannerGallery.banner_update;
    // Create the banner div
        if ($('BannerGallery-Banner') == null)
            $('BannerGallery').appendChild(new Element('div', { 'id': 'BannerGallery-Banner' }));

    // Create the banner controls
        $('BannerGallery').appendChild(new Element('div', { 'id': 'BannerGallery-Controls' }));
        if (this.option_hide_controls) {
            $('BannerGallery-Controls').hide();
            Event.observe('BannerGallery', 'mouseover', BannerGallery.controls_action_mouseover);
            Event.observe('BannerGallery', 'mouseout',  BannerGallery.controls_action_mouseout);
        }

    // Left button
		var left_link = new Element('a', { 'id': 'BannerGallery-Controls-Left' });
		left_link.addClassName('BannerGallery-Controls-Button');
        $('BannerGallery-Controls').appendChild(left_link);
        $('BannerGallery-Controls-Left').appendChild(new Element('img', { 'src': BannerGallery.image_left }));
        Event.observe('BannerGallery-Controls-Left', 'click', function(event) {
            BannerGallery.banner_rotate_left(event);
            if(this.private_rotate_pe && this.option_rotation_stop_on_manual_pick)
                this.private_rotate_pe.stop();
        }.bind(this));

    // Right button
		var right_link = new Element('a', { 'id': 'BannerGallery-Controls-Right'});
		right_link.addClassName('BannerGallery-Controls-Button');
        $('BannerGallery-Controls').appendChild(right_link);
        $('BannerGallery-Controls-Right').appendChild(new Element('img', { 'src': BannerGallery.image_right }));
        Event.observe('BannerGallery-Controls-Right', 'click', function(event) {
            BannerGallery.banner_rotate_right(event);
            if(this.private_rotate_pe && this.option_rotation_stop_on_manual_pick)
                this.private_rotate_pe.stop();
        }.bind(this));

    // Setup the PE
        if (this.option_banner_rotation_sec > 0)
            this.private_rotate_pe = new PeriodicalExecuter(BannerGallery.banner_rotate_right, this.option_banner_rotation_sec);
    },

    banner_add:                 function(title, alt, html, img_url) {
        var index = BannerGallery.private_banners.length;
        BannerGallery.private_banners[index] = new BannerGallery_Banner(title, alt, html, img_url);

		var new_link = new Element('a', { 'id': 'BannerGallery-Controls-Button-'+index });
		new_link.addClassName('BannerGallery-Controls-Button');
        $('BannerGallery-Controls').appendChild(new_link);

        if (index==0) {
            $('BannerGallery-Controls-Button-0').addClassName('active');
            if (BannerGallery.option_fade_on_load)
                $('BannerGallery-Banner').hide();
            $('BannerGallery-Banner').update(BannerGallery.private_banners[BannerGallery.private_current_banner].html);
            if (BannerGallery.option_fade_on_load)
                BannerGallery.callback_banner_show();
        }

        Event.observe('BannerGallery-Controls-Button-'+index, 'click', function(event) {
            BannerGallery.banner_set_index(event);
            if(this.private_rotate_pe && this.option_rotation_stop_on_manual_pick)
                this.private_rotate_pe.stop();
        }.bind(this));
        $('BannerGallery-Controls-Button-'+index).appendChild(new Element('img', { 'src': BannerGallery.image_center, 'title': title, 'alt': alt, 'style': 'display: inline' }));
    },

    banner_rotate_left:         function() {
        BannerGallery.banner_set_index(BannerGallery.private_current_banner-1);
    },

    banner_rotate_right:        function() {
        BannerGallery.banner_set_index(BannerGallery.private_current_banner+1);
    },

    banner_set_index:           function(index) {
        $$('.BannerGallery-Controls-Button.active').each(function (button) { button.removeClassName('active'); });
        if (typeof index == 'object')
            index = parseInt(index.element().parentNode.id.match(/\d+/), 10);
    // Verify bounds
        if (index >= BannerGallery.private_banners.length) {
            if (BannerGallery.option_wrap)
                index = 0;
            else
                index = BannerGallery.private_banners.length-1;
        }
        if (index < 0) {
            if (BannerGallery.option_wrap)
                index = BannerGallery.private_banners.length-1;
            else
                index = 0;
        }
    // Update Banner
        BannerGallery.private_current_banner = index;
        $('BannerGallery-Controls-Button-'+index).addClassName('active');
        BannerGallery.callback_banner_hide();
    },

    banner_update:              function() {
        $('BannerGallery-Banner').update(BannerGallery.private_banners[BannerGallery.private_current_banner].html);
        BannerGallery.banner_image_complete();
    },

    banner_image_complete:      function() {
        if (BannerGallery.private_banners[BannerGallery.private_current_banner].image.complete != true && BannerGallery.option_force_image_load == true)
            window.setTimeout('BannerGallery.banner_image_complete()', 50);
        else
            BannerGallery.callback_banner_show();
    },

    banner_fade:                function() {
        if(this.option_no_fade) {
            $('BannerGallery-Banner').hide();
            BannerGallery.banner_update();
        } else {
            new Effect.Fade($('BannerGallery-Banner'), {afterFinish: BannerGallery.banner_update, duration:.3});
        }
    },

    banner_appear:              function() {
        if(this.option_no_fade)
            setTimeout(function(){$('BannerGallery-Banner').show();}, 100);
        else
            new Effect.Appear('BannerGallery-Banner', {duration:.3});
    },

    controls_appear:            function() {
        if (BannerGallery.private_banners.length > 1) {
            if(this.option_no_fade)
                $('BannerGallery-Controls').show();
            else
                new Effect.Appear('BannerGallery-Controls', {duration:.1});
            if (BannerGallery.private_rotate_pe != null && BannerGallery.option_rotation_pause_on_controls)
                BannerGallery.private_rotate_pe.currentlyExecuting = true;
        }
    },

    controls_fade_timer:        function() {
         window.setTimeout('BannerGallery.controls_fade()', 250);
    },

    controls_fade:              function() {
        if (BannerGallery.private_mouseover_counter == 0) {
            if(this.option_no_fade)
                $('BannerGallery-Controls').hide();
            else
                new Effect.Fade('BannerGallery-Controls', {duration:.1});
            if (BannerGallery.private_rotate_pe != null && BannerGallery.option_rotation_continue_after_controls)
                BannerGallery.private_rotate_pe.currentlyExecuting = false;
        }
    },

    controls_action_mouseover:  function() {
        BannerGallery.controls_action(1);
    },

    controls_action_mouseout:   function() {
        BannerGallery.controls_action(-1);
    },

    controls_action:            function(mouse_over) {
        if (!isNaN(parseInt(mouse_over, 10)))
            BannerGallery.private_mouseover_counter += mouse_over;

        if (BannerGallery.private_mouseover_counter > 0)
            BannerGallery.callback_controls_show();
        if (BannerGallery.private_mouseover_counter == 0)
            BannerGallery.callback_controls_hide();

        if (BannerGallery.private_mouseover_counter < 0)
            BannerGallery.private_mouseover_counter = 0;
        if (BannerGallery.private_mouseover_counter > 1)
            BannerGallery.private_mouseover_counter = 1;
    }
}

var BannerGallery_Banner = Class.create({
    initialize:                 function (title, alt, html, img_url) {
        this.html       = html;
        this.alt        = alt;
        this.title      = title;
        this.img_url    = img_url;
    // Preload the image!
        this.image      = new Image();
        this.image.src  = img_url;
    }
})

