//再挑戦
$(function(){
  //初期設定
  $("#itemlist").show();
  $("#itemlist li:first").addClass("active");
  //画像サイズを取得
  var imageWidth = $("#image_window").width();
  var imageSum = $("#image_reel img").size();
  var imageReelWidth = imageWidth * imageSum;
  //リールのサイズをセット
  $("#image_reel").css({'width':imageReelWidth});
  //
  rotate = function(){
    var triggerID = $("a",$active).attr("rel")-1;
	var image_reelPosition = triggerID * imageWidth;
	$("#itemthumb li").removeClass('active');
	$active.addClass('active');
	//スライドさせる
	$("#image_reel").animate({
      left:-image_reelPosition
    },500);
  };
  
  //
  rotateSwitch = function(){
    play = setInterval(function(){
      $active = $('#itemthumb li.active').next();
	  if($active.length===0){
        $active = $('#itemthumb li:first');
	  }
	  rotate();
    },7000);
  };
  rotateSwitch();
  
  //大画像にマウスオーバーしてるときは自動で変えない
  $("#image_reel a").hover(function(){
    clearInterval(play);
  }, function(){
    rotateSwitch();
  });
  
  //サムネイルクリック時のイベント
  $("#itemthumb li").click(function(){
    $active = $(this);
	clearInterval(play);
	rotate();
	rotateSwitch();
	return false;
  });
  
  //nextボタン
  $("#nextBtn").click(function(){
     $active = $('#itemthumb li.active').next();
	  if($active.length===0){
        $active = $('#itemthumb li:first');
	  }
	clearInterval(play);
	rotate();
	rotateSwitch();
	return false;
  });
  //prevボタン
  $("#prevBtn").click(function(){
     $active = $('#itemthumb li.active').prev();
	  if($active.length===0){
        $active = $('#itemthumb li:last');
	  }
	clearInterval(play);
	rotate();
	rotateSwitch();
	return false;
  });
});

// hotitem
/*
$(function(){
  $("#itemthumb a").click(function(){
    $("#itemlist").hide();
    $("#topitem img").before("<img src='"+$("img",this).attr("src")+"' alt=''>");
    $("#topitem").attr("href",$(this).attr("href"));
	$("#topitem img:last").fadeOut("fast",function(){
      $(this).remove();
    });
    $("#itemlist").show();
	return false;
  });
});
*/

//function nextImg()
//カレントイメージの次のイメージをメイン領域にセット
//次のイメージが無い場合は最初のイメージをセット
function nextImg(){
  $("#itemlist").hide();
  var $nextImage;
  //カレントイメージを取得し、次のイメージを保持
  $("#itemthumb li").each(function(i){
    if($("img",this).attr("src") == $("#topitem img").attr("src")){
      $nextImage = $(this).next();
	  if($nextImage.length == 0){
        $nextImage = $("#itemthumb li:first");
	  }
	}
  });
  $("#topitem img").before("<img src='"+$("img",$nextImage).attr("src")+"' alt=''>");
  $("#topitem").attr("href",$("a",$nextImage).attr("href"));
  $("#topitem img:last").fadeOut("fast",function(){
    $(this).remove();
  });
  $("#itemlist").show();
  return false;
};

//function prevImg()
//カレントイメージの前のイメージをメイン領域にセット
//前のイメージが無い場合は最後のイメージをセット
function prevImg(){
  $("#itemlist").hide();
  var $prevImage;
  //カレントイメージを取得し、前のイメージを保持
  $("#itemthumb li").each(function(i){
    if($("img",this).attr("src") == $("#topitem img").attr("src")){
      $prevImage = $(this).prev();
	  if($prevImage.length == 0){
        $prevImage = $("#itemthumb li:last");
	  }
	}
  });
  $("#topitem img").before("<img src='"+$("img",$prevImage).attr("src")+"' alt=''>");
  $("#topitem").attr("href",$("a",$prevImage).attr("href"));
  $("#topitem img:last").fadeOut("fast",function(){
    $(this).remove();
  });
  $("#itemlist").show();
  return false;
};

/*
function timer(){
  setTimeout(function(){
    var $nextImage;
    //カレントイメージを取得し、次のイメージを保持
    $("#itemthumb li").each(function(i){
      if($("img",this).attr("src") == $("#topitem img").attr("src")){
        $nextImage = $(this).next();
        if($nextImage.length == 0){
          $nextImage = $("#itemthumb li:first");
	    }
      }
    });
    $("#topitem img").before("<img src='"+$("img",$nextImage).attr("src")+"' alt=''>");
    $("#topitem").attr("href",$("a",$nextImage).attr("href"));
    $("#topitem img:last").fadeOut("slow",function(){
      $(this).remove();
    });
    return false;
  },6000);
}
*/
