var loupe = '<img src="/common/img/ico_loupe.gif" alt="拡大する" width="20" height="20" class="loupe" />';
var thisId;
$(function(){
	$(window).load(function(){ makePhotoGallery(); });
	$(window).scroll(function(){ movePhotoGallery(); });
	$(window).resize(function(){ resizePhotoGallery(); });
});

function makePhotoGallery(){
	// ID及びClassNameの付与
	var i = 1 ;
	$(".entryReadSection .pgallery:has(img)").attr("cuID",function(){
		return i++;
	})
	.append(loupe)
	.addClass("thumbnail")
	.click(function(){
		setPhotoGallery($(this).attr("cuID"));
	});
	$(".entryBodySection .pgallery:has(img)").attr("cuID",function(){
		return i++;
	})
	.append(loupe)
	.addClass("thumbnail")
	.click(function(){
		setPhotoGallery($(this).attr("cuID"));
	});
}

function deletePhotoGallery(){
	// slideshowの復帰
	$("#slideshow").css("display","block");
	// div#deletePhotoGalleryの消去
	$("#photoGallery").remove();
}

function movePhotoGallery(){
	if($("body #photoGallery").html() == null){
		return ;
	}
	var scrollTop;
	if(document.documentElement.scrollTop){
		scrollTop = document.documentElement.scrollTop;
	}else{
		scrollTop = document.body.scrollTop;
	}
	$("body #photoGallery div").css("top",scrollTop+"px");
}
function resizePhotoGallery(){
	if($("body #photoGallery").html() == null){
		return ;
	}
	var resizeHeight;
	if(document.documentElement.clientHeight){
		resizeHeight = document.documentElement.clientHeight;
	}else{
		resizeHeight = document.body.clientHeight;
	}
	var resizeWidth;
	if(document.documentElement.clientWidth){
		resizeWidth = document.documentElement.clientWidth;
	}else{
		resizeWidth = document.body.clientWidth;
	}
	$("body #photoGallery div").css("height",resizeHeight+"px");
	$("#flashcontent").css("width",resizeWidth+"px");
	$("#flashcontent").css("height",resizeHeight+"px");
	var flconW = $("#flashcontent").css("width").replace(/px/,"");
	var flconH = $("#flashcontent").css("height").replace(/px/,"");
	if(flconW < 950){  $("#flashcontent").css("width","950px"); }
	if(flconH < 560){  $("#flashcontent").css("height","560px"); }
}

function setPhotoGallery(id){
	// slideshowの削除
	$("#slideshow").css("display","none");
	// div#deletePhotoGalleryの設置
	$("body").prepend("<div id=\"photoGallery\"><div id=\"flashArea\"></div></div>");
	var contentsHeight;
	var windowHeight;
	if(document.documentElement.scrollHeight > document.body.scrollHeight){
		contentsHeihgt = document.documentElement.scrollHeight;
	}else{
		contentsHeihgt = document.body.scrollHeight;
	}
	if(document.documentElement.clientHeight){
		windowHeight = document.documentElement.clientHeight;
	}else{
		windowHeight = document.body.clientHeight;
	}
	$("body #photoGallery").css("height",contentsHeihgt+"px");
	$("body #photoGallery div#flashArea").css("height",windowHeight+"px");
	// Ajax
	getPhotoGallery(id);
	// 表示領域の設定
	movePhotoGallery();
}
function getPhotoGallery(id){
		var xmlName = "xml/photogallery.xml";
		var url  = '/include/photogallery.php?';
		var path = location.href.replace(/index.php/,xmlName);
		if(!path.match(xmlName)){
			path = path+xmlName;
		}
		thisId = id ;
		var params = "cuID="+id+"&xmlPath="+path;
		$.ajax({
			type: "post",
			url: url,
			data: params,
			dataType: "html",
			success: function(msg){ $("body #photoGallery div#flashArea").html(msg); },
			error: function(XMLHttpRequest,textStatus,errorThrown){ alert("XMLHttpRequest: "+XMLHttpRequest+"\ntextStatus: "+textStatus+"\nerrorThrown: "+errorThrown); }
		});
}
