var img_list = [];
var img_switch = true;
var img_list_idx = 1;
//var baseURLRegEx = /(http:\/\/)?[^\.\s\/]*\.?[^\.\s\/]*\.?[^\.\s\/]*(\/[^\.\s\/]+\/)*/;
//var baseURL = baseURLRegEx.exec(document.location);
//var baseURL = baseURL[0];

$(document).ready(function() {
  $.getJSON("/rotate_img/ajax_dir_list.php", function(data) {
    $.each(data, function(i, item) {
      img_list[i] = new Image()
      img_list[i].src = "/rotate_img/" + item;
    });
  });
  
  $('body').ajaxStop(function() {
    $("#rotating-image-0").html("<div id=\"rotating-image-1\"></div>");
    $("#rotating-image-1").attr("style", "background-image: url(" + img_list[img_list_idx].src + ");");
    
    $(window).get(0).setInterval(function() {
      if (img_list_idx != (img_list.length - 1)) {
        img_list_idx++;
      }
      else {
        img_list_idx = 0;
      }
      
      if (img_switch) {
        $("#rotating-image-1").fadeIn("slow", function() {
          $("#rotating-image-0").attr("style", "background-image: url(" + img_list[img_list_idx].src + ");");
        });
        img_switch = false;
      }
      else {
        $("#rotating-image-1").fadeOut("slow", function() {
          $(this).attr("style", "background-image: url(" + img_list[img_list_idx].src + ");");
        });
        img_switch = true;        
      }
        
    }, 5000);
  });
});