var toolboxHighlightIndex = 0;

function animateToolbox() {
    var links = $('.ToolboxWidget .content a');
    if (links.length > 0) {
        current = links.eq(toolboxHighlightIndex);
        links.removeClass('highlight');
        current.addClass('highlight');
        toolboxHighlightIndex++;
        if (toolboxHighlightIndex == links.length) {
            toolboxHighlightIndex = 0;
        }
        setTimeout("animateToolbox()", 2000);
    }
}

function animateReferences() {
    $('#references').each(function() {
        if ($(this).find('.block').length < 2)
            return;
        var active = $(this).find('.active');
        var next =  active.next();
        if (next.length == 0) {
            next = $(this).find('.block:first');
        }

        active.fadeOut("slow", function() {
            $(this).removeClass('active');
        });

        next.fadeIn("slow", function() {
            $(this).addClass('active');
        });
        setTimeout("animateReferences()", 5000); 
    });
}

function animateTeaser() {
    $('#teasers').each(function() {
        if ($(this).find('.teaser').length < 2)
            return;
        var active = $(this).find('.active');
        var next =  active.next();
        if (next.length == 0) {
            next = $(this).find('.teaser:first');
        }

        active.fadeOut("slow", function() {
            $(this).removeClass('active');
        });

        next.fadeIn("slow", function() {
            $(this).addClass('active');
        });
        setTimeout("animateTeaser()", 8000); 
    });
}


$(function() {
    setTimeout("animateToolbox()", 1000);

    $("img[rel]").overlay();


    /** teaser animation **/
    setTimeout("animateTeaser()", 8000); 
    setTimeout("animateReferences()", 5000); 


    $('.widget .next').click(function() {
        var items = $(this).parent().parent().find('.items .wrapper');
        var item = items.find('.item');
        var itemWidth = item.outerWidth()
        var maxScroll = -(item.length - 1) * itemWidth;
        marginLeft = parseInt(items.css('marginLeft'));

        if (maxScroll < marginLeft) {
            newScroll = marginLeft - itemWidth;
            items.animate({marginLeft: newScroll + 'px'}, "fast");
        }
    });

    $('.widget .prev').click(function() {
        var items = $(this).parent().parent().find('.items .wrapper');
        var item = items.find('.item');
        var itemWidth = item.outerWidth()

        var maxScroll = 0;
        marginLeft = parseInt(items.css('marginLeft'));

        if (maxScroll > marginLeft) {
            newScroll = marginLeft + itemWidth;
            items.animate({marginLeft: newScroll + 'px'}, "fast");
        }
    });

    $('.rollOut').click(function() {
        $(this).find('.content').slideToggle();
    });

    $('.PartnerWidget .scrollable .wrapper,.TestimonialWidget .scrollable .wrapper').each(function() {
        var items = $(this).find('.item');
        var num_items = items.length;
        var rand = Math.floor(Math.random() * num_items);
        var marginLeft = -rand * items.outerWidth();

        $(this).css({marginLeft: marginLeft + 'px'});

    });

    $('.overlayTrigger').click(function() {
        var content = $(this).parent().find('.overlayContent');
        var box = $('<div class="overlayContent">' + content.html() + '</div>');
        $('body').append(box);

        box.overlay({
            load: true,
            fixed: false,
            mask: {
                color: '#000',
                loadSpeed: 200,
                opacity: 0.5
            },
        });
    });

    $('a.button').each(function() {
        $(this).html('<span>' + $(this).html() + '</span>');
        $(this).addClass('image-button');
        $(this).removeClass('button');
    });

    $('.image-overlay .prev').click(function() {
        var overlay = $(this).parent();
        var prev = overlay.prev();
        var trigger = $('img[rel=#'+prev.attr('id')+']');
        trigger.trigger('click');
    });

    $('.image-overlay .next').click(function() {
        var overlay = $(this).parent();
        var next = overlay.next();
        var trigger = $('img[rel=#'+next.attr('id')+']');
        trigger.trigger('click');
    });
});


