var Timer;
var now = -1;

function makeRandom() {
    while(1){
        var num = Math.round(Math.random() * (PHOTO.length - 1));
        if(now != num) {
            now = num;
            return num;
        }
    }
}

function effect(){
    $("#KeyImage").prepend(PHOTO[makeRandom()]);
    $("#KeyImage > img:first").css({'position':'absolute', 'z-index':1, 'width':'579px', 'height':'211px'})
    
    $("#KeyImage > img").each(
        function(){
            var target = $(this);
            
            if(target.css("z-index") == 2){
                target.fadeTo(2000, 0,
                    function(){
                        target.remove();
                        $("#KeyImage > img").css({'position':'relative', 'z-index':2, 'width':'579px', 'height':'211px'});
                        
                        clearTimeout(Timer);
                        Timer = setInterval("effect()", INTERVAL);
                    }
                );
            }
        }
    );
}

$(document).ready(
    function(){
        $("#KeyImage").css({'margin':'0 0 -1px 0'});
        $("#KeyImage").prepend(PHOTO[makeRandom()]);
        $("#KeyImage > img").css({'position':'relative', 'z-index':2, 'width':'579px', 'height':'211px'});
        
        Timer = setInterval("effect()", INTERVAL);
    }
);

