var top_photogallery_date;
var top_photogallery_title;
var top_photogallery_url = new Array();

$(function(){
	//フォトギャラリー
	var today = new Date();
	var Year = today.getFullYear();
	var photo_count = 0;
	$.ajax({
		url:"./xml/photogallery_"+Year+".xml",//RSSファイル名
		async:false,
		cache:false,
		dataType:"xml",
		success: function(xml){
			$(xml).find('photo').each(function(){
				top_photogallery_date = $(this).find('date').text();
				top_photogallery_title = $(this).find('title1').text()+'&nbsp;'+$(this).find('title2').text();

				$(this).find('photo_url').each(function(){
					top_photogallery_url[photo_count] = $(this).text();
					photo_count++;
					if(photo_count >3){
						return false;
					}
				});
				return false;
			});
		},
		error: function(xml){
			//alert("xmlファイルの読み込みに失敗しました");
		}
	});
	
	
	if(photo_count==0){
		
		Year--;
		$.ajax({
			url:"./xml/photogallery_"+Year+".xml",//RSSファイル名
			async:false,
			cache:false,
			dataType:"xml",
			success: function(xml){
				$(xml).find('photo').each(function(){
					top_photogallery_date = $(this).find('date').text();
					top_photogallery_title = $(this).find('title1').text()+'&nbsp;'+$(this).find('title2').text();

					$(this).find('photo_url').each(function(){
						top_photogallery_url[photo_count] = $(this).text();
						photo_count++;
						if(photo_count >3){
							return false;
						}
					});
					return false;
				});
			},
			error: function(xml){
				//alert("xmlファイルの読み込みに失敗しました");
			}
		});
	}
	
	
	$('#photo_textArea').append('<p>'+top_photogallery_date+'&nbsp;'+top_photogallery_title+'</p>');
	$('#photo_textArea').append('<div class="photo_main"><img src="funzone/'+top_photogallery_url[0]+'"></div>');
	photosizeFix($('#photo_textArea > .photo_main > img'),268,201)

	for(var i = 0; i <top_photogallery_url.length; i++){
		$('#photo_textArea').append('<div class="photo_thumb'+(i+1)+'"></div>');
		$('.photo_thumb'+(i+1)).append('<a onClick="thumbClick('+i+')"></a>');
		$('.photo_thumb'+(i+1)+' > a').append('<img src="funzone/'+top_photogallery_url[i]+'">');

		photosizeFix($('#photo_textArea > .photo_thumb'+(i+1)+' > a > img'),60,45)
	}
});

//写真サイズFIXメソッド
function photosizeFix(targetObj,targetW,targetH){
	var w = targetObj.width();
	var h = targetObj.height();
	if(w > h){
		targetObj.width(targetW);
		//縦位置をセンターへ
		var ySabun = (targetH-targetObj.height())/2;
		targetObj.css("margin-top",ySabun);
	}else{
		targetObj.height(targetH);
	}
}

//クリックメソッド
function thumbClick(targetID){
	$('.photo_main').html('<img src=funzone/'+top_photogallery_url[targetID]+'>');
	photosizeFix($('.photo_main > img'),268,201)
}
