var randompicTimer = 5000, $picrotation, $picrotation_text, rotationActive = true;
var currentPic = Math.round( Math.random() * (arrPics.length-1) );

function randompic() {
    var $hiddenLayer = $('div.layer:hidden',$picrotation);
    var $visibleLayer = $('div.layer:visible',$picrotation);
    
    // 1. HG im versteckten Bild setzen
    $hiddenLayer.css('backgroundImage','url("'+arrPics[currentPic]['src']+'")');

    // 2. sichtbaren layer ausblenden, versteckten Layer einfaden
    $('a',$visibleLayer).hide();
    $visibleLayer.fadeOut('slow',function() {
        $picrotation_text.hide();
        $hiddenLayer.fadeIn('slow',function() {
            
            if(arrPics[currentPic]['link'] == '') {
                $picrotation_text.html(arrPics[currentPic]['text']).show();
                $('a',$picrotation).addClass('nolink').attr('href','');
            } else {
                $picrotation_text.html('<a href="'+arrPics[currentPic]['link']+'">'+arrPics[currentPic]['text']+'</a>').show();
                $('a',$hiddenLayer).attr('href',arrPics[currentPic]['link']).show();
                arrPics[currentPic]['link'].indexOf('http:') == 0 ? $('a',$picrotation).attr('target','_blank') : $('a',$picrotation).attr('target','');
                arrPics[currentPic]['link'] == '' ?  $('a',$picrotation).addClass('nolink').attr('href','') : $('a',$picrotation).removeClass('nolink');
                $('a',$picrotation).removeClass('nolink');
            }

            currentPic == arrPics.length-1 ? currentPic = 0 : currentPic = currentPic+1;
            
            if(arrPics.length > 1 && rotationActive == true  ) {
                setTimeout(randompic,randompicTimer);
            }
        
        });
    });

}

$(function() {
    $picrotation = $('#picrotation');
    $picrotation_text = $('p.text',$picrotation);
    $('div:first',$picrotation).show();
    
    $('a.nolink',$picrotation).live('click',function() {
        return false;
    });

    // initial the picrotation
    randompic();

});
