var t, brandClass = '';

function agco_home_brand_timer() {
    setTimeout(function() {
        var active_brand = $('#brands').attr('class');
        var next_brand = $('#brands .links li.'+active_brand).next().find('a');
        
        if(active_brand != "valtra") {
            agco_home_brands('on', next_brand);
        }
        else {
            agco_home_brands('on', $('#brands .links li.challenger a'));
        }
        
        agco_home_brand_timer();
    }, 3500);
}
function agco_home_brands(mode, object) {
    if (mode == "on") {
        brandClass = $(object).parent().attr('rel');
        
        // Don't activate if already active
        if($('#brands').attr('class') == brandClass) {
            return;
        }
        
        t = setTimeout(function() {
            $('#brands').removeAttr('class').addClass(brandClass);
            $('div.images img:visible', '#brands').fadeOut();
            $('div.images img.' + brandClass, '#brands').fadeIn();
        }, 350);
    }
    else {
        clearTimeout(t);
    }
}
var agco_home_showcase = {
    init: function() {
        agco_home_showcase.update();
        // Scrollable
        $('#showcase .scrollable').scrollable({
            size: '1',
            clickable: false,
            speed: 1000,
            vertical: false
        });
    },
    update: function() {
        // Equal Heights
        maxHeight = 0;
        $('div.showcase-item', '#showcase').removeAttr('style').each(function() {
            thisHeight = $(this).height();
            if(thisHeight > maxHeight) {
                maxHeight = thisHeight;
            }
        });
        $('#showcase').add('div.showcase-item', '#showcase').add('div.scrollable', '#showcase').height(maxHeight);
    }
};
$(function() {
    // Brands
    $('#brands li:not(.more-link) a').hover(function() {
        agco_home_brands('on', this);
    }, function() {
        agco_home_brands('off', this);
    });
    // Random Brand
    var randomBrand = Math.floor(Math.random() * 4);
    $('#brands li:eq(' + randomBrand + ') a').trigger('mouseenter');
    // Brand timer
    agco_home_brand_timer();
    // Showcase
    agco_home_showcase.init();
    $('#font-scaler a').click(function() {
        agco_home_showcase.update()
    });
    // Weather
    $('#block-weather .tabs a').click(function() {
        $('#block-weather').removeClass('fahrenheit centigrade').addClass($(this).attr('class'));
    });
});