// イメージ配列
var m_Image = new Array();

// イメージ配列
var m_IndexImage = Array();

// イメージコメント配列
var m_ImageComment = new Array();

// 写真カレント番号
var m_Counter = 0;

// 写真最大枚数
var m_PictureCount = 0;

// スライドショー区分
var m_SlideON = false;

var m_Fe;

var m_FeidTimeOut;

var m_Interval;

var m_ScrollTimer;

var m_Preview;



function StartPreview()
{
	m_Preview = true;
}



// スクロール時表示トップ座標セット
function ScrollTop() 
{
	try
	{
		// トップ座標セット
		document.getElementById('imgLogo').style.top = (document.body.scrollTop || document.documentElement.scrollTop) + "px";
		document.getElementById('popupWindow').style.top = (document.body.scrollTop || document.documentElement.scrollTop) + "px";

		
		// タイマーセット
		m_ScrollTimer = setTimeout("ScrollTop()", 100);
	}
    catch (e)
    {
        alert(e);
    }
}

function StopTimer() 
{
	try
	{
	    clearTimeout(m_ScrollTimer);
	}
    catch (e)
    {
        alert(e);
    }
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// イメージオブジェクト配列生成
//
function SettingIndexPicture(size, margin)
{
	try
	{
		var pictureCount = 0;
		var idWord = "";
		var baseSize = 0;
		var i = 0;
		
		var dateText = GetPathName() + "mini";
//		var dateText = GetPathName();
	
		// 最大枚数取得
		pictureCount = m_PicturePath[dateText].length;

		// 写真枚数でループ
		for(i = 0; i < pictureCount; i++)
		{
			idWord = "img" + i.toString();
			
			// イメージオブジェクトのインスタンス生成
			m_IndexImage[i] = new Image();
			
			// イメージソース設定
			m_IndexImage[i].src = m_PicturePath[dateText.toString()][i];
			
		}	

		for(i = 0; i < pictureCount; i++)
		{
			idWord = "img" + i.toString();
			
			document.getElementById(idWord).src = m_IndexImage[i].src;
		}
		
		window.onload = function()
		{
			try
			{
				for (var i = 0; i < m_IndexImage.length; i++)
				{
					SetIndexPictureSize("img" + i.toString(), i, 180, 10);
				}
				
				hyouji("divPictureIndex");
				ScrollChangePicture("popupArea");
				ScrollChangePicture("popupWindow");
				Hide("popupAreaLoad");
				ScrollTop();
				
				if (m_Preview)
				{
					ShowPicture(0);
				}
				
				window.onresize = function()
				{
					ResizeShowPicture();
				}
				
//				// フッターマージンセット
//				if (getBrowserHeight() > document.body.scrollHeight)
//				{
//					document.getElementById("divFooterMargin").style.marginTop = (90 + getBrowserHeight() - document.body.scrollHeight) + "px";
//				}
				
		//		alert("Complete");	
			}
			catch (e)
			{
				alert(e);	
			}
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function SetIndexPictureSize(idWord, number, size, margin)
{
	try
	{
		var baseSize = size - margin * 2;
		var anotherWidthSize = (m_IndexImage[number].width / m_IndexImage[number].height) * baseSize;
		var anotherHeightSize = (m_IndexImage[number].height / m_IndexImage[number].width) * baseSize;
		
		// 画像サイズをセット
		// 縦＞横の場合
		if (m_IndexImage[number].height >= m_IndexImage[number].width)
		{
			document.getElementById(idWord).width = anotherWidthSize;
			document.getElementById(idWord).height = baseSize;
			document.getElementById(idWord).style.marginTop = margin + "px";
			document.getElementById(idWord).style.marginBottom = margin + "px";
			document.getElementById(idWord).style.marginLeft = (size - anotherWidthSize) / 2 + "px";
		}
		// 縦＜横の場合
		else
		{
			document.getElementById(idWord).width = baseSize;
			document.getElementById(idWord).height = anotherHeightSize;
			document.getElementById(idWord).style.marginTop = (size - anotherHeightSize) / 2 + "px";
			document.getElementById(idWord).style.marginBottom = (size - anotherHeightSize) / 2 + "px";
			document.getElementById(idWord).style.marginLeft = margin + "px";
		}
	}
	catch(e)
	{
		alert(e);	
	}
}

function ShowPictureIndex(number)
{
    try
    {
		var startIndex = m_Image[number].src.toString().length - 8 - 8;
		
		return "No. " + m_Image[number].src.toString().slice(startIndex, -8);

    }
    catch (e)
    {
        alert(e);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// イメージオブジェクト配列生成
//
function SettingShowPicture(parameter)
{
	try
	{

		
		var dateText = "";
		
		if (parameter == "")
		{
			dateText = GetPathName();
		}
		else
		{
			dateText = parameter;	
		}
		
		var i = 0;
	
		// 最大枚数取得
		m_PictureCount = m_PicturePath[dateText].length;
	
		// 写真枚数でループ
		for(i = 0; i < m_PictureCount; i++)
		{
			// イメージオブジェクトのインスタンス生成
			m_Image[i] = new Image();

			// イメージソース設定
			m_Image[i].src = m_PicturePath[dateText.toString()][i];

			// イメージコメント設定
//			m_ImageComment[i] = m_PictureComment[dateText.toString()][i];
		}	
		
		// 画面左上から適応
		document.getElementById('popupArea').style.top = "0px";
		document.getElementById('popupArea').style.left = "0px";
		document.getElementById('popupAreaLoad').style.top = "0px";
		document.getElementById('popupAreaLoad').style.left = "0px";
		
		// 透明度を設定
		document.getElementById('popupArea').style.opacity = 0.9;
		document.getElementById('popupArea').style.MozOpacity = 0.9;
		
		// 画面左上から適応
		document.getElementById('popupWindow').style.top = "0px";
		document.getElementById('popupWindow').style.left = "0px";
	}
    catch (e)
    {
        alert(e);
    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////
// 写真表示
//
function ShowPicture(number)
{
    try
    {
		HidePopup("imgShowPicture");
		
		// 引数で指定された番号をカレント番号とする
		m_Counter = number;
		
		// 写真をセット
		document.getElementById("imgShowPicture").src = m_Image[m_Counter].src;
		
		
		var startIndex = m_Image[m_Counter].src.toString().length - 8 - 8;
		
		document.getElementById("pPictureName").innerHTML = "No. " + m_Image[m_Counter].src.toString().slice(startIndex, -8);

		SetShowPictureSize();

//		// 写真番号を表示
//		document.getElementById("count").innerHTML = "[ " + (m_Counter + 1) + "/" + m_PictureCount + " ]";


//		switch(navigator.appName)
//		{
//			case "Microsoft Internet Explorer":
//				break;
//				
//			case "Netscape":
//				document.close();
//				break;
//		}
    }
    catch (e)
    {
        alert(e);
    }
}

function ResizeShowPicture()
{
	// 基準
	var baseHeight = getBrowserHeight() - 100;
	
	var topMargin = 0;
	
	var buttonMargin = 250;
	
	var imgBairitsu = getBrowserHeight() / 1000;

	// マージン初期化
	document.getElementById('imgShowPicture').style.marginTop = "0px";


	// リサイズ後の横幅がブラウザ表示横幅以上の場合は基準の高さを変更
	if (getBrowserWidth() <= (m_Image[m_Counter].width / m_Image[m_Counter].height) * baseHeight + buttonMargin)
	{
		baseHeight = (getBrowserWidth() - buttonMargin) * (m_Image[m_Counter].height / m_Image[m_Counter].width);
		
		topMargin = (getBrowserHeight() - 100 - baseHeight) / 2;
		document.getElementById('imgShowPicture').style.marginTop = topMargin + "px";
		
		imgBairitsu = getBrowserWidth() / 1965;
	}
	
	// 画像サイズをセット
	document.getElementById("imgShowPicture").width = (m_Image[m_Counter].width / m_Image[m_Counter].height) * baseHeight;
	document.getElementById("imgShowPicture").height = baseHeight;
	
	// ボタン配置をセット
	document.getElementById("divShowPictureBox").style.width = (m_Image[m_Counter].width / m_Image[m_Counter].height) * baseHeight + "px";
	
	// popupArea //////////////////////////////////////////////////////////////////////////////////////////
	// body表示領域全体をポップアップエリアに設定
	document.getElementById('popupArea').style.height = document.body.clientHeight + "px";
	document.getElementById('popupArea').style.width = document.body.clientWidth + "px";
	document.getElementById('popupAreaLoad').style.height = document.body.clientHeight + "px";
	document.getElementById('popupAreaLoad').style.width = document.body.clientWidth + "px";

	// popupWindow //////////////////////////////////////////////////////////////////////////////////////////
	// body表示領域全体をポップアップエリアに設定
	document.getElementById('popupWindow').style.width = document.body.clientWidth + "px";
	
	
	// imgLogoMovable //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('imgLogoMovable').style.height = 40 * imgBairitsu + "px";
	document.getElementById('imgLogoMovable').style.width = 245 * imgBairitsu + "px";
	
	// pPictureName //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('pPictureName').style.fontSizeAdjust = 0.5 * imgBairitsu;
	
	// but_sl //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('but_sl').style.height = 32 * imgBairitsu + "px";
	document.getElementById('but_sl').style.width = 70 * imgBairitsu + "px";

	// imgSlideStop //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('imgSlideStop').style.height = 32 * imgBairitsu + "px";
	document.getElementById('imgSlideStop').style.width = 70 * imgBairitsu + "px";

	// imgLeftButton //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('imgLeftButton').style.marginTop = ((baseHeight / 2) + topMargin) + "px";
	document.getElementById('imgLeftButton').style.marginLeft = - (30 * imgBairitsu) - (70 * imgBairitsu) + "px";
	document.getElementById('imgLeftButton').style.height = 32 * imgBairitsu + "px";
	document.getElementById('imgLeftButton').style.width = 70 * imgBairitsu + "px";

	// imgRightButton //////////////////////////////////////////////////////////////////////////////////////////
	document.getElementById('imgRightButton').style.marginTop = ((baseHeight / 2) + topMargin) + "px";
	document.getElementById('imgRightButton').style.marginLeft = ((m_Image[m_Counter].width / m_Image[m_Counter].height) * baseHeight + (30 * imgBairitsu)) + "px";
	document.getElementById('imgRightButton').style.height = 32 * imgBairitsu + "px";
	document.getElementById('imgRightButton').style.width = 70 * imgBairitsu + "px";
}

function SetShowPictureSize()
{
	try
	{
		ResizeShowPicture();

		// 表示
		ViewPopup('popupArea');
		
		ViewPopup('popupWindow');	
		
		ViewPopup("imgShowPicture");
	}
	catch (e)
	{
		alert(e);	
	}

}

function SlideShow()
{
	try
	{
		if(m_SlideON)
		{
			StopSlideShow();
		}
		else
		{
			clearInterval(m_Interval);

			NextPicture();
			
			document.getElementById("but_sl").title = "スライドショー停止";
			document.getElementById("but_sl").src = document.getElementById("but_sl").src.toString().replace("_SlideStart", "_SlideStop");
			
			m_Interval = setInterval("NextPicture();", 4000);
			
			m_SlideON = true;
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function StopSlideShow()
{
	try
	{
		m_SlideON = false;

		clearTimeout(m_Interval);
		document.getElementById("but_sl").title = "スライドショー開始";
		document.getElementById("but_sl").src = document.getElementById("but_sl").src.toString().replace("_SlideStop", "_SlideStart");
		
	}
	catch (e)
	{
		alert(e);
	}
}

function HidePopup(idWord)
{
	try
	{
		document.getElementById(idWord).style.visibility = "hidden";
	}
	catch (e)
	{
		alert(e);
	}
}

function ViewPopup(idWord)
{
	try
	{
		document.getElementById(idWord).style.visibility = "visible";
	}
	catch (e)
	{
		alert(e);
	}
}

function PrevPicture()
{
	try
	{
		if(m_Counter > 0)
		{
			m_Counter--;
		}
		else
		{
			m_Counter = m_PictureCount - 1;
		}
	
		m_Fe = 100;
		FeidOut();
	}
	catch (e)
	{
		alert(e);
	}
}

function NextPicture()
{
	try
	{
		if(m_Counter < m_PictureCount - 1)
		{
			m_Counter++;
		
			m_Fe = 100;
			FeidOut();
		}
		else
		{
			//m_Counter = 0;
			ClosePopUp();
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function ClosePopUp()
{
	try
	{
		HidePopup('imgShowPicture');
		StopSlideShow(); 
		HidePopup('popupWindow');
		
		HidePopup('popupArea');
//		HidePopup('popupAreaLoad');
	}
	catch(e)
	{
		alert(e);	
	}
}

function FeidOut()
{
	try
	{
		clearTimeout(m_FeidTimeOut);
		
		if (m_Fe > 0)
		{
			// Internet Explorer
			if(window.ActiveXObject)
			{
				document.getElementById("imgShowPicture").filters["alpha"].opacity = m_Fe;
			}
			// FireFox
			else
			{
				document.getElementById("imgShowPicture").style.opacity = m_Fe / 100;
			}
			
			m_Fe -= 100;

			m_FeidTimeOut = setInterval("FeidOut();", 20);
		}
		else
		{
			// 写真表示
			ShowPicture(m_Counter);

			clearTimeout(m_FeidTimeOut);
			m_Fe = 0;
			FeidIn();
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function FeidIn()
{
	try
	{
		clearTimeout(m_FeidTimeOut);
		
		if (m_Fe < 100)
		{
			// Internet Explorer
			if(window.ActiveXObject)
			{
				document.getElementById("imgShowPicture").filters["alpha"].opacity = m_Fe;
			}
			// FireFox
			else
			{
				document.getElementById("imgShowPicture").style.opacity = m_Fe / 100;
			}
			
			m_Fe += 100;
			m_FeidTimeOut = setInterval("FeidIn();", 20);
		}
		else
		{
			clearTimeout(m_FeidTimeOut);
	
			// Internet Explorer
			if(window.ActiveXObject)
			{
				document.getElementById("imgShowPicture").filters["alpha"].opacity = 100;
			}
			// FireFox
			else
			{
				document.getElementById("imgShowPicture").style.opacity = 1;
			}
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function ScrollChangePicture(idWord)
{
	try
	{
		ele = document.getElementById(idWord);
		
		
		//マウスホイールのイベントを追加
		if(navigator.userAgent.match("MSIE"))
		{
			ele.attachEvent("onmousewheel",mouseWheelZooming); 
		}
		
		//if( navigator.userAgent.match( "Gecko" ) ){ ele.addEventListener("DOMMouseScroll",mouseWheelZooming,false )};
		
		if(navigator.userAgent.match("Gecko") && !navigator.userAgent.match("Safari"))
		{
			ele.addEventListener("DOMMouseScroll",mouseWheelZooming,false )
		}
		
		if(navigator.userAgent.match("Safari"))
		{
			ele.onmousewheel = mouseWheelZooming; 
		}
	}
	catch(e)
	{
		alert(e);
	}
}

function mouseWheelZooming(event)
{
	try
	{
		//マウスホイールの上／下の取得と、スクロールのキャンセル
		if(navigator.userAgent.match("MSIE"))
		{ 
			var delta = event.wheelDelta; 
			event.returnValue = false; 
			var ele = event.srcElement
		} //IE
		
		if(navigator.userAgent.match("Gecko") && !navigator.userAgent.match("Safari"))
		{ 
			var delta = event.detail * -1; 
			event.preventDefault();
			var ele = event.target
		} //Firefox
		
		if(navigator.userAgent.match("Safari"))
		{ 
			var delta = event.wheelDelta; 
			event.returnValue = false;
			var ele = event.target
		} //Safari
		
		foward(ele,delta);
	}
	catch(e)
	{
		alert(e);
	}
}

//ホイールで進む
function foward(ele,delta)
{
	try
	{
		if(delta < 0)
		{
			if(m_SlideON)
			{
				SlideShow();
			} 
			else
			{
				NextPicture();
			}
		}
		else
		{
			if(m_SlideON)
			{
				SlideShow();
			} 
			else
			{
				PrevPicture();
			}
		}
	}
	catch(e)
	{
		alert(e);	
	}
	
//	ele.src =  ele.cache[ele.currentPos].src;
//	window.status = (ele.currentPos + 1) + "/" +  ele.cache.length
}

