$(document).ready(function() {
    promotions_slideshow.setup();
})

var promotions_slideshow = {
    container: null,
    total_items: 0,
    last_item: null,
    items: [],
    start_delay: 4000,//time in ms after page load
    entry_delay: 4000,//delay between entries scroll
    closed_delay: 2000,//
    list_height: 0,
    
    setup: function()  {

        this.container = $('#special-promo');
        if(this.container.length == 0) return;

        this.items = $('.entry', this.container);
         $(this.items).map(function(i, el){
            //promotions_slideshow.list_height += parseInt($(el).outerHeight());
        });
        promotions_slideshow.list_height = parseInt($('#promo-items').outerHeight());
        
        $(this.container).addClass('closed');

        this.total_items = this.items.length;
        
        var active_entry = $('.active', this.container)[0];

        this.last_item = $(this.items).index(active_entry);
        
        setTimeout(function(){
            promotions_slideshow.changePromo();
        }, this.start_delay);        


        $('#toggle-promo a').bind('click', function(event) {
            event.preventDefault();
            
            var inner_element  = $('.inner', this.container).first();
            $(promotions_slideshow.items).clearQueue();
            
            $(promotions_slideshow.container).removeClass('ie-slide-over');
            
            if($(promotions_slideshow.container).hasClass('closed')) {
                $(promotions_slideshow.container).addClass('no-anim');
                
                

                $(promotions_slideshow.items).animate({
                    queue: false,
                    opacity: 0
                  }, 500, function() {
                });
                
                $(inner_element).animate({
                    queue: false,
                    height: promotions_slideshow.list_height + parseInt($('#promo-items').css('marginTop'))
                  }, 500, function() {
                    
                    $(promotions_slideshow.container).removeClass('closed');
                    $('#toggle-promo a').html('less');
                    
                    $(promotions_slideshow.items).css({'top':0});
                    $(promotions_slideshow.container).addClass('opened');

                    $(promotions_slideshow.container).addClass('ie-slide-over');
                    
                    $(promotions_slideshow.items).animate({
                        queue: false,
                        opacity: 1
                      }, 500, function() {
                    });                    
                });

            } else  {

                $(promotions_slideshow.items).animate({
                    queue: false,
                    opacity: 0
                  }, 500, function() {
                    
                    $(inner_element).animate({
                        queue: false,
                        height: 50
                      }, 500, function() {

                        $(promotions_slideshow.container).addClass('closed');
                        $('#toggle-promo a').html('more');

                        $(promotions_slideshow.items).animate({
                            queue: false,
                            opacity: 1
                          }, 500, function() {
                            $(promotions_slideshow.container).addClass('ie-slide-over');
                            
                        });
                    });                    
                
                });
                
                setTimeout(function(){
                    $(promotions_slideshow.container).removeClass('no-anim');
                    $(promotions_slideshow.container).removeClass('opened');

                    setTimeout(function(){
                        promotions_slideshow.changePromo();
                        
                    }, promotions_slideshow.closed_delay);
                    
                }, 1500);

                
            }
            
        }); 
    },
    
    changePromo: function() {
        
        if($(promotions_slideshow.container).hasClass('animating') || $(promotions_slideshow.container).hasClass('no-anim')) return;
        
        $(promotions_slideshow.container).addClass('animating');
        
        var last_entry = $('.active', promotions_slideshow.container).first();
        $(last_entry).addClass('last');

		var next_index = this.last_item + 1;
		if(next_index == this.total_items) {
			next_index = 0;
		}

        var next_entry = promotions_slideshow.items[next_index];
       
        var last_pos = $(last_entry).position();
        
		$(last_entry).animate({
            queue: false,
			top: last_pos.top + 60
		  }, 1000, function() {
            $(last_entry).removeClass('last active');
            /*
            $(last_last_entry).removeClass('last');
            $(last_last_entry).removeClass('active');
            $(next_last_entry).removeClass('new');
            $('#home-slides-container').removeClass('animating');
            */
		});        

        $(next_entry).css({'top': -60});
        $(next_entry).addClass('new active');

		$(next_entry).animate({
            queue: false,
			top: 0
		  }, 1000, function() {
            
            $(next_entry).removeClass('new');
            
            $(promotions_slideshow.container).removeClass('animating');

            setTimeout(function(){
                promotions_slideshow.changePromo();
            }, promotions_slideshow.entry_delay);        

		});
        
        this.last_item = next_index;
    }
}
