var $block_banner, $banner_images, $main_image, $thumbnail_images, $next_button, $next_link;

/* htmlファイルの方で宣言したid/classを変数に埋め込む */
function bannerBlock(blockbanner, bannerImage, largeImage, smallImage, button, link, descriptions) {
	/* bannerライクなレイアウトブロック */
	$block_banner = blockbanner;
	$banner_images = bannerImage;
	$main_image = largeImage;
	$thumbnail_images = smallImage;
	$next_button = button;
	$next_link = link;
	$contents_descriptions = descriptions

	bannerLikeSlider();
}

/* bannerライクなスライドショー機能 */
function bannerLikeSlider(){
	
	/* レイアウト指定 */
	$($block_banner).css({
		"position" : "relative",
		"color" : "#333",
		"width": "724px",
		"height": "295px",
		"margin" : "0",
		"overflow" : "hidden",
		"height" : "295px"
	});
	
	$($block_banner).find("img").css({
		"display" : "block",
		"border" : "0 none",
		"border-radius" : "4px",
		"-webkit-border-radius" : "4px",
		"-moz-border-radius" : "4px"
	});
	
	
	$($banner_images).css({
		"position" : "absolute",
		"right" : "0",
		"top" : "0",
		"width" : "195px",
		"height" : "300px",
		"overflow" : "hidden"
	});
	
	$($thumbnail_images).css({
		"position" : "absolute",
		"left" : "0",
		"bottom" : "0",
		"width" : "195px"
	});
	
	$($thumbnail_images).find("a").css({
		"position" : "relative",
		"display" : "block",
		"width" : "195px",
		"height" : "95px",
		"padding-top" : "5px"
	});
	
	$($thumbnail_images).find("a > img.min").css({
		"width" : "195px",
		"height" : "95px"
	});
	
	$($thumbnail_images).find("a > img.max").css({
		"display" : "none",
		"width" : "524px",
		"height" : "295px"
	});
	
	$($main_image).css({
		"position" : "relative",
		"width" : "524px",
		"height" : "295px"
	});
	
	$($main_image).find("img").css({
		"margin" : "0",
		"width" : "524px",
		"height" : "295px"
	});

	$($main_image).find("a > .min").css({
		"display" : "none"
	});
	
	$($main_image).find("a > .max").css({
		"width" : "524px",
		"height" : "295px"
	});
	
	$($next_button).css({
		"position" : "absolute",
		"right" : "89px",
		"bottom" : "5px",
		"width" : "42px",
		"height" : "42px",
		"background" : "url(/system/files/index/images/banner-button.png) 0% 0% no-repeat",
		"border" : "0 none",
		"cursor" : "pointer"
	});
	
	$($next_button).find(".btn-down").css({
		"background-position" : "0% 100%"
	});
	
	$($contents_descriptions).css({
		"display" : "none"
	});

	var height = $($thumbnail_images + " a").height();
	var timeout = 0;
	var imageCounter = 0;
	var counter;
	
	
	/* 一定時間隔ごと、あるいはnextボタンでメイン画像を切り替える */
	function nextImage() {
		clearTimeout(timeout);
		
		var blockLength = $("#blockB a").length;
		$($thumbnail_images).css({"bottom" : "0"});
		$($thumbnail_images).animate({"bottom" : -height}, 600);
		$($thumbnail_images + " a:nth-child(" + blockLength + ")").prependTo($thumbnail_images);
		
		
//		$myclone = $("#blockB a:last-child").clone();
		$myclone = $("#blockB a:nth-child(" + blockLength + ")").clone();
//		$href = $("#blockB a:last-child").attr("href");
		$href = $("#blockB a:nth-child(" + blockLength + ")").attr("href");
				
		var image_manipulation = $myclone.html();
		
		//メイン画像の変更
		$big_image = image_manipulation.replace(/_small/, "");
		$($main_image + " a").html($big_image);
		$($main_image + " img").removeClass("min").addClass("max");
		
		
		//メイン画像サイズの調整+表示
		$($main_image + " img").css({
			"opacity" : "0",
			"width" : "524px",
			"height" : "295px"
		}).animate({"opacity" : "1"}, 600);
		
		//画像のリンク先を指定
		$($next_link).attr("href", $href);

		//最初のメイン画像のdescriptionを表示(reloadした時に最初のdescriptionが表示されないバグのごまかし)
//		var contentsLength = $($contents_descriptions).length;
//		console.log(contentsLength);
		$($contents_descriptions)[0].style.display = "block";
		
		//nextImage()が起動する度にどのサムネイル画像がメイン来るか判定し、それに関連したdescriptionを表示する
		timeout = setTimeout(function() {
			nextImage();
			imageCounter++;
			var description_length = $($contents_descriptions).length;
			var i = imageCounter % description_length;
			$($contents_descriptions).css({"display" : "none"});
			$($contents_descriptions)[i].style.display = "block";

		}, 5000);
	}
	
	/* nextボタンをクリックした時の制御 */
	$($block_banner)
		.mouseenter(function() {
			$($next_button).stop().animate({"opacity" : "1"});
		})
		.mouseleave(function() {
			$($next_button).stop().animate({"opacity" : "0"});
		});
	
	/* nextボタンの表示・非表示設定 */	
	$($next_button)
		.css({"opacity" : "0"})
		.bind("keydown mousedown", function() {
			$(this).addClass("btn-down");
		})
		.bind("keyup blur mouseup mouseleave", function() {
			$(this).removeClass("btn-down");
		})
		.click(function(){
			nextImage();
			imageCounter++;
			var description_length = $($contents_descriptions).length;
			var i = imageCounter % description_length;
			$($contents_descriptions).css({"display" : "none"});
			$($contents_descriptions)[i].style.display = "block";
		});
		
	nextImage();

}

