var time;

$(function (){
    animation();
    
    $('a.homepageBannerChange').click(function (){
        var position = $(this).closest('div').index();
        var active_position = $('div.homepage-banner-header div.active').index();
        console.log(position);
        console.log(active_position);
        $('a.homepageBannerChange').parent().removeClass('active');
        $(this).parent().addClass('active');
        
        box = $('div.homepage-banner-detail:eq('+ active_position +')');
        //box.fadeOut(1990);
        box.removeClass('active');
        box = $('div.homepage-banner-detail:eq('+ position +')');
        //box.fadeIn(1990);
        box.addClass('active');
        
        clearTimeout(time);
    });
});

function animation(){
    time = setTimeout(function (){
        var active_position = $('div.homepage-banner-header div.active').index();
        
        if (active_position == $('div.homepage-banner-detail').length - 1){
            var position = 0;
        } else{
            var position = active_position+parseInt(1,10);
        }
        
        box = $('div.homepage-banner-detail:eq('+ active_position +')');
        link = $('div.homepage-banner-link:eq('+ active_position +')');
        
        //box.fadeOut(1990);
        box.removeClass('active');
        link.removeClass('active');
        
        box = $('div.homepage-banner-detail:eq('+ position +')');
        link = $('div.homepage-banner-link:eq('+ position +')');
        //box.fadeIn(1990, function (){
        //    animation();
        //});
        box.addClass('active');
        link.addClass('active');
        animation();
    }, 5000);
}

