(function($){
	$.fn.swapPhoto = function(options){
		if(!this) return this;
		
		options = $.extend({}, $.fn.swapPhoto.defaults, options);
	
		return this.each(function() {
			var $target = $(this);
			var $thumbCells = $(this).find(options.thumbCellSelector);
			var $titleTxt = $(this).find(options.titleCellSelector);
			
			//最初のサムネイルをカレント表示設定
			setCurrent($thumbCells.filter(":first").find("a"));
			
			//写真が一枚の時は、サムネイル部分を非表示
			$thumbs = $thumbCells.find("img");
			if($thumbs.size() == 1) {
				$thumbCells.parent("tr").hide();
				$target.find("th").addClass("no_thumb"); //高さ調整用のクラス付加
			}
			
			//すべての写真にキャプションがなければ、キャプションのエリアを非表示
			if($thumbs.filter("[alt='']").size() == $thumbs.size()) {
				$titleTxt.parent("tr").hide();
			}
		
			$thumbCells.find("a").each(function(i) {
				//クリック、ロールオーバー／アウトアクション
				$(this).click(function(e) {
					//直前の選択セルからカレント表示クラスを除去、透明度を元に戻す
					$thumbCells.filter("." + options.thumbCellCurrentClass)
					.removeClass(options.thumbCellCurrentClass)
					.find("img").animate({ opacity: 1 }, (options.animation? options.duration: 0));
					
					//クリックされたセルにカレント表示クラスを付加し、画像の透明度を設定
					var $currentThumb = setCurrent($(this));
					
					//次に表示する画像のパス、タイトル
					var currentImgSrc = $(this).attr("href");
					var currentImgTitle = $currentThumb.attr("alt");
					
					//切り替え処理
					if(options.animation) {  //アニメーションする場合クロスフェードする
						var $prevImg = $target.find("th img");
						//imgタグを生成し、現在の画像のあとに挿入(透明にしておく)後、フェードイン
						var $currentImg = $("<img>").attr({
							src: currentImgSrc,
							alt: currentImgTitle
						})
						.insertAfter($prevImg)
						.css("opacity", 0)
						.animate({ opacity: 1 }, (options.animation? options.duration: 0), "linear", function() {
							$target.find("th img").not(":last").remove();
						});
					} else {  //アニメーションしない場合は単に画像切り替え
						$target.find("th img").attr("src", currentImgSrc);
					}
					
					//タイトルを切り替え
					//$titleTxt.text(currentImgTitle);
					
					e.preventDefault();
					return false;
				})
				.hover(
					//ロールオーバー
					function(e) {
						var $td = $(this).parent("td");
						//カレント表示クラスがついていなければ、ロールオーバークラスを付加
						if(!$td.hasClass(options.thumbCellCurrentClass)) {
							$td.addClass(options.thumbCellRollOverClass);
						}
					},
					//ロールアウト
					function(e) {
						//ロールオーバークラスを除去
						$(this).parent("td").removeClass(options.thumbCellRollOverClass);
					}
				);
			});
		});
		
		//サムネイルにカレント表示設定
		function setCurrent($thumbBtn) {
			return $thumbBtn.parent("td")
						.addClass(options.thumbCellCurrentClass)
						.removeClass(options.thumbCellRollOverClass)
						.find("img").animate({ opacity: options.thumbCurrentOpacity }, (options.animation? options.duration: 0));
		}
	}
	
	//デフォルトオプション	
	$.fn.swapPhoto.defaults = {
		animation: true,                                           //フェードインアウトをするかどうか
		duration: 200,                                              //アニメーション時間[ms]
		thumbCellSelector: ".cell_tnail",                //サムネイルのセルのセレクタ
		thumbCellRollOverClass: "hover",              //サムネイルロールオーバー時にセルに付加するクラス名
		thumbCellCurrentClass: "current",            //選択状態のサムネイルのセルに付加するクラス名
		thumbCurrentOpacity: 0.5,                        //選択状態の画像の透明度
		titleCellSelector: ".swap_photo_catch"   //タイトルを表示するセルのセレクタ
	};
})(jQuery);


