//------------------------
// ギャラリーページ用
//
//------------------------
$(function(){

	$("#next a, #main-photo a").attr({"rel": "Appendix"});

	//リンク先が外部ならカウンターを呼び出し
	$('a').click(function(){
		if($(this).attr('href').substr(0,7) == "http://" && $(this).attr('rel') != "Appendix"){
			$(this).attr('target', "_blank");
			var img="";
			var url=encodeURIComponent(this.href);
			if($(this).children("img").attr("src")){
				img=$(this).children("img").attr("src");
			}
			else{
				img=$(this).text();
			}

			$.get("/php/counter.php", {site: "1", url: url, img: img, page: document.URL});
		}
	});

	//本体にスタイルを指定
	var mainBox = $("#main-photo").css({position: "relative"});
	var preImage = mainBox.find("img").addClass("pre");
	var browserInfo = $.support.objectAll;
	var thumbList = $("#thumb-box ul li");
	var ancorList = thumbList.find("a");

//	if(!browserInfo){	//IEフィックス
//		var positionFix = $(document).width % 2;
		//メイン画像にドロップシャドウ(IEのみ)
//		var shadowProp = {left: 0, top: 0, blur: 2, opacity: 0.75};
//		preImage.load(function(){
//			preImage.dropShadow(shadowProp);
//		});
//	}

	//オフィシャルHPのURL取得
	var official = $("#banner ul li:first a").attr("href");
	
	//サムネイルの明滅処理
	thumbList.find("img[class!='sltd']").hover(
		function(){	$(this).fadeTo("fast", 1);
		}, function(){ 
			if(!$(this).hasClass("sltd")) $(this).fadeTo("fast", 0.5);
	 });

	//PREV、NEXTの処理
	$("#prev, #next, #main-photo img").click(function(){
		if(mainBox.find("img").is(":animated")) return false;

		onImage = preImage.attr("src").replace("http://"+location.hostname, "");

		ancorList.each(function(){
			if($(this).attr("href") == onImage){
				preIndex = ancorList.index(this);
				return false;
			}
		});

		if($(this).attr("id") == "prev"){
			var newIndex = preIndex - 1;
			$("#next a, #main-photo a").attr({"rel": "Appendix"});
			if(preIndex == 1) $("#prev a").removeAttr("rel");
		}
		else{
			var newIndex = preIndex + 1;
			$("#prev a").attr({"rel": "Appendix"});
			if(newIndex == ancorList.length - 1) $("#next a, #main-photo a").removeAttr("rel");
		}
		var newImage = ancorList.eq(newIndex).attr("href");
		var newAlt = ancorList.eq(newIndex).children().attr("alt");

		//指定画像がなければオフィシャルへ
		if(!newImage){
			url=encodeURIComponent(official);
			img= $(this).attr("src") ? $(this).attr("src") : $(this).text();
			return true;
		}
		else{
			ancorList.find("img[class='sltd']").fadeTo("fast", 0.5).removeClass("sltd");
			ancorList.find("img").eq(newIndex).fadeTo("fast", 1).addClass("sltd");
			ChageImage(newImage, newAlt);
			return false;
		}
	});

	//サムネイルリストの処理
	ancorList.click(function(){
		if(mainBox.find("img").is(":animated")) return false;

		var preThumbnail = $(this);
		var newImage = preThumbnail.attr("href");
		var newAlt = preThumbnail.children().attr("alt");

		if(preImage.attr("src") == newImage) return false;

		ancorList.find("img[class='sltd']").fadeTo("fast", 0.5, function(){
			preThumbnail.children().fadeTo("fast", 1).addClass("sltd");
		}).removeClass("sltd");
		
		if(ancorList.index(this) == 0){
			$("#prev a").removeAttr("rel");
			$("#next a, #main-photo a").attr({"rel": "Appendix"});
		}
		else if(ancorList.index(this) == ancorList.length - 1){
			$("#next a, #main-photo a").removeAttr("rel");
			$("#prev a").attr({"rel": "Appendix"});
		}
		else{
			$("#prev a, #next a, #main-photo a").attr({"rel": "Appendix"});
		}

		ChageImage(newImage, newAlt);
		return false;
	});


	function ChageImage(newImage, newAlt) {
		if(preImage.attr("src") == newImage) return false;
		preImage.ImageSwitch({Type: "FlyOut", 
			NewImage:newImage,	
			Direction:"DownTop"	//動き通常は左右
		});
	}

//	function _ChageImage(newImage, newAlt) {
//		if(preImage.is(":animated") || mainBox.find("div").is(":animated") || $("#loading").html()) return false;
//
//		speed = !browserInfo ? 1000 : 2000;
//		//画像の読み込み開始
//		var image = new Image();
//		image.src = newImage;
//
//		//loadingを削除  
//		$("#loading").remove(); 
//		var load_offset = preImage.offset();
//		var load_box_left = Math.ceil(($("#main-photo").outerWidth() - 200) / 2);
//		var load_box_top = Math.ceil((preImage.outerHeight() - 80) / 2) + 15;
//		var load_propaties = {"position": "absolute", top: load_box_top, left: load_box_left, zIndex: 100, "font-size": "36px", "color": "#999999", opacity: 0.8, "text-indent": "-9999px", "background": "url('/img/com/loading.gif') no-repeat left top", width: 200, height: 80}
//		$('<div id="loading">Now Loading...</div>').appendTo("#main-photo").css(load_propaties);
//
//		preImage.after("<div class='re-img' />");
//		nextImageBox = mainBox.find("div.re-img").css({position: "absolute", top: "15px", left: "0", width: "100%", display: "none", "text-align": "center"});
//
//		//新しい画像要素を生成
//		nextImageBox.append('<img src="'+newImage+'" />').css({display: "none"});
//
//		if(!browserInfo && $.browser.version < 7){	//---- IE6用
//			timer = setInterval(function(){
//				if(image.complete){
//					$("#loading").fadeOut(speed - 300);
//					nextImageBox.css({left: "15px"}).fadeIn(speed, function(){
//						mainBox.find("div[class!='dropShadow']").remove();
//					});
//					mainBox.find("div[class!='re-img']").fadeOut(speed - 300);
//					preImage.fadeOut(speed - 300, function(){
//						mainBox.find("div.dropShadow").remove();
//						preImage.attr({src: newImage, alt: newAlt}).show().dropShadow(shadowProp);
//					});
//				}
//				clearInterval(timer);
//			}, 1000);
//		}
//		else{	//---- 通常用
//			mainBox.find("div.re-img img").load(function(){
//				$("#loading").fadeOut(speed - 300);
//				if(!browserInfo && !positionFix) nextImageBox.css({left: "1px"});
//				nextImageBox.fadeIn(speed, function(){
//					mainBox.find("div[class!='dropShadow']").remove();
//				});
//				if(!browserInfo) mainBox.find("div[class!='re-img']").fadeOut(speed - 500);
//				preImage.fadeOut(speed - 300, function(){
//					if(!browserInfo) mainBox.find("div.dropShadow").remove();
//					preImage.attr({src: newImage, alt: newAlt}).show();
//					if(!browserInfo) preImage.dropShadow(shadowProp);
//				});
//			});
//		}
//	}
});

