function guriguri() {
  $(function() {
    $("div.guriguri_button").each(function() {
      var divtag = $(this);
      divtag.html("<img src=\"/wp-content/plugins/infinite-scroll/ajax-loader.gif\"><br /><span style=\"background-color:#fff;\">ぐりぐり写真を読み込んでいます...</span>");
    });
    $("div.guriguri").each(function() {
      var divtag = $(this);
      var jsontext = divtag.text();
      var json = $.parseJSON(jsontext);
      if(json) {
        var src = json.src;
        var inner_divtag = $("<div />");
        var imgtag = $("<img src='" + src + "' />");
        var imgtag2 = $("<img src='" + src + "' />");
        var xx = 0;
        var direction = 1;
        var automove = true;

        divtag.css("height", "0px");
        divtag.css("overflow", "hidden");
        //divtag.css("font-size", "0px");
        divtag.html("");
        divtag.append(inner_divtag);
        inner_divtag.append(imgtag);
        if(!$.browser.msie) {
          inner_divtag.append(imgtag2);
        }
        //imgtag.css("position", "relative");
        imgtag2.css("position", "relative");

        //imgtag.css("z-index", 10);
        //imgtag2.css("z-index", 100);

        imgtag.bind('load', function() {
          var width = parseInt(json.width, 10);
          var height = parseInt(json.height, 10);
          var count = parseInt(json.count, 10);
          if (!width) {
            width = imgtag.width();
          }
          if (!height) {
            height = width * 3 / 4;
          }
          var w_count = imgtag.width() / width;
          var h_count = imgtag.height() / height;
          if (!count) {
            count = w_count * h_count;
          }

          inner_divtag.css("width", imgtag.width() * 2 + "px");

          divtag.css("z-index", "10");
          divtag.hide();
          divtag.css("width", width + "px");
          divtag.css("height", height + "px");
          //divtag.animate({height: height + "px"}, 1000);
          divtag.fadeIn("slow");

          //var count = imgtag.height() / height;
          //imgtag2.css("top", - height * (h_count + 1) + "px"); // diff 1 page
          imgtag2.css("opacity", 0);

          var show = function(x) {
            var x2 = x;
            x2 = Math.min(x2, width - 1);
            x2 = Math.max(x2, 0);

            var p = x2 * (count * 2 - 1) / width;
            var fp = Math.floor(p);
            var opacity = 0;
            if (fp % 2 == 1) {
              opacity = p - fp;
            }
            var page = Math.floor(fp / 2);

            //imgtag.css("marginTop", -page * height + "px");

            var t_page = page % h_count;
            var l_page = (page - t_page) / h_count;

            //var page2 = page + 1;
            //var t_page2 = page2 % h_count;
            //var l_page2 = (page2 - t_page2) / h_count;

            imgtag2.css("opacity", opacity);
            //imgtag2.css("top", - (t_page2 + h_count) * height + "px");
            //imgtag2.css("left", - l_page2 * width + "px");
            if (t_page != h_count - 1) {
              //imgtag2.css("top", - (1 + h_count) * height + "px");
              //imgtag2.css("left", - (l_page + w_count) * width + "px");
              imgtag2.css("top", - height + "px");
              imgtag2.css("left", - w_count * width + "px");
            } else {
              imgtag2.css("top", (h_count - 1) * height + "px");
              imgtag2.css("left", - (w_count + 1) * width + "px");
            }
 
            inner_divtag.css("marginTop", - t_page * height + "px");
            inner_divtag.css("marginLeft", - l_page * width + "px");   
   
            //console.log("1:" + t_page + ", " + l_page);
            //console.log("2:" + t_page2 + ", " + l_page2);
          };

          divtag.mouseout(function(event) {
            automove = true;
          });

          divtag.mouseover(function(event) {
          automove = false;
          });

          divtag.mousemove(function(event) {
            xx = event.pageX - $(event.currentTarget).offset().left;
            show(xx);
          });

          var speed = parseInt(json.speed, 200);
          $.timer(speed, function() {
            if (automove) {
              xx += direction * (width / (count * 2 - 1)) * Math.random();
              if (xx >= width) { direction = -1; }
              if (xx < 0)      { direction = 1; }
              show(xx);
            }
          });
        });
      }
    });
  });
}

