var block_rotation_timer = false;
var block_rotation = false;
var block_rotation_number = 0;


$(document).ready(function(){
    // init teaser rotation functionality
    $("#teaser a:not(#teaser div a)").mouseenter(function(){
        block_rotation = true;

        switch_image = $(this).attr("class");
        $("#teaser div a").attr("href", $(this).attr("href"));

        $("#teaser").css("backgroundImage", "url(" + WEB_ROOT + "_style/images/teaser_" + switch_image + ".jpg)");
        $("#teaser div").fadeOut(function(){
            $("#teaser div").css("backgroundImage", "url(" + WEB_ROOT + "_style/images/teaser_" + switch_image + ".jpg)");
            $("#teaser div").show();
        });
    }).mouseleave(function(){
        block_rotation = false;
        if (block_rotation_timer){
            clearTimeout(block_rotation_timer);
        }
        block_rotation_timer = setTimeout('rotate_teaser()', 8000);
    });

    block_rotation_timer = setTimeout('rotate_teaser()', 8000);
});


function rotate_teaser(){
    if (block_rotation == true){
        return;
    }

    if (++block_rotation_number > 2){
        block_rotation_number = 0;
    }

    var n = 0;
    $("#teaser a:not(#teaser div a)").each(function(){
        if (n++ == block_rotation_number){
            $(this).mouseenter().mouseleave();
            return;
        }
    });
}
