var img_curenta = 1;
var loaded = {};
function next() {
    temp = img_curenta;
    
    do {
    img_curenta ++; 
    if (img_curenta > nr_img) 
        img_curenta= 1; 
    } while (!loaded["img_"+img_curenta]);
    
//    if (loaded["img_"+img_curenta] == 1) 
    {
      $("#img_"+temp).stop().fadeOut("slow");
      $("#img_"+img_curenta).stop().fadeIn("slow");
      
    }
}

  $("#head_img img").load(function () {
             loaded[ $(this).attr("id") ] = 1;
          });

var loaded = {};          
$(function () {                                
  
      loaded[ $(this).attr("img_"+img_curenta) ] =1;
      window.setInterval("next()", slide_delay);
      
      for (var i = 2; i <= nr_img; i++ )
        $("<img id='img_"+i+"' src='"+rest_img[i]+"' style='width:100%;height:100%;'/>").appendTo("#head_img")
        .hide()
        .load(function () {
            loaded[ $(this).attr("id") ] =1;
        })
        .wrap("<div style='position:absolute;left:0px;top:0px;width:100%;height:100%;'></div>");
})
