CAROUSAL = function() {
    this.initialize();
};

CAROUSAL.prototype = {
    initialize: function() {
        this.animation_time = 3000;
        this.start_delay_time = 5000;
        this.repeat_delay_time = 6000;
        this.banner_class = 'promo-item';
        this.banner_image_class = 'promo-image-banner';
        this.banner_object;
        this.banner_object_class = 'banner';
        this.banner_current;
        this.banner_current_class = 'current';
        this.banner_count = 1;
        //pixel value for the below variables - leave off the px
        this.banner_height = 530;
        this.banner_width = 960;
        this.banner_pad_top = 200;
        this.banner_offset = 900;
    },
    setBannerCount: function () {
        var global = this;
        global.banner_count = 0;
    	global.banner_object.each( function() {
            global.banner_count++;
        });
    },
    setBannerCurrent: function() {
        var global = this;
        global.banner_object.first().addClass(global.banner_current_class);
        global.banner_current = global.banner_object.first();
    },
    setBannerObject: function() {
        var global = this;
        //update global banner object to current state.
        global.banner_object = $('.' + global.banner_class);
    },
    markBanners: function() {
        var global = this;
        //reset current count
        global.banner_count = 0;
    	global.banner_object.each( function() {
            if ( $(this).children().hasClass(global.banner_image_class) ) {
                $(this).addClass(global.banner_object_class).attr('id',global.banner_object_class + ++global.banner_count );        
            }
            else {
                $(this).remove();
            }
        });
    },
    createMenu: function() {
        var global = this;
        var local_banner_count = 0;
        //find the menu wrglobaler
        var menu_object = $('#h-promos-menu');
        //pull the desired button code
        var button_code = menu_object.html();
        //create clean slate
        menu_object.empty();
        
        //for each banner add a button (assuming function is called when there is more than one banner)
    	global.banner_object.each( function() {
            local_banner_count++;
            //add button
            menu_object.globalend(button_code)
            menu_object.children().last().attr('name',global.banner_object_class + local_banner_count).click(function(){
                global.showBanner('.' + global.banner_object_class + local_banner_count);
            });
        });
        //retrieve the new code add to the menu
        //menu_object = $('#h-promos-menu');
        //mark the first link as active
        menu_object.children().first().addClass('active');
        
        
        //show the menu
        menu_object.show();        
    },
    showBanner: function(banner_id) {
        var global = this;
        global.stopCarousal();
        //skip function if the selected banner is the current banner
        //if( banner_id == global.banner_current.attr('id') ) { return true; }
        
        //get current state of the banners
        //global.banner_object = $('.promo-item');
        //complete all animations
        //global.banner_object.stop('true','true').find('img').stop('true','true');
        //hide the current banner
        
        //show the selected banner
        
    
    },
    stopCarousal: function() {
        var global = this;
        global.banner_object.stop('true','true').find('.' + global.banner_image_class + ' img').stop('true','true');
    
    },
    startCarousal: function() {
        var global = this;
        //update global banner object to current state.
        global.setBannerObject();
        var local = {
            banner_first: global.banner_object.first(),
            banner_next: global.banner_object.first().next(),
            banner_last: global.banner_object.last()
        };
        //stop current animations
        global.banner_object.stop('true','true').find('.' + global.banner_image_class + ' img').stop('true','true');
        
        //hide the current banner
        local.banner_first.find('.' + global.banner_image_class + ' img').animate({
            height: 0,
            width: 0,
            paddingTop:global.banner_pad_top
        },global.animation_time, function(){
            //animation complete
        });
        //show the next banner
        local.banner_next.find('.' + global.banner_image_class + ' img').animate({
            height: global.banner_height,
            width: global.banner_width,
            paddingTop: 0
        },global.animation_time, function(){
            //animation complete
        });        

        //position the next banner
        local.banner_next.animate({
            left: 0
        },global.animation_time, function(){
            //animation complete
            //change the current status
            local.banner_first.removeClass(global.banner_current_class).css('left',global.banner_offset + 'px');
            local.banner_next.addClass(global.banner_current_class);
            //move first banner to the end of the list
            local.banner_last.after(global.banner_object.first());
        });
    },
    run: function() {
        var global = this;
        $(function() {
            global.banner_object = $('.' + global.banner_class);
            global.markBanners();
            global.setBannerCurrent();
            
            if(global.banner_count > 1 ) {
                //global.createMenu();
                setTimeout('delayStart()', global.start_delay_time);
            }
        });
    }
};

var carousal = new CAROUSAL
carousal.run();

//function exists so we can run setTimeout
function delayStart() {
    carousal.startCarousal();
    setTimeout('delayStart()', carousal.repeat_delay_time);
}

