function initialize()
{
	dispImageFromXmlList('./xml/albmTopPhotoImageList.xml', 'photo', 100, 'm');
	dispImageFromXmlList('./xml/albmTopAdviceAlbumImageList.xml', 'adviceAlbum', 80, 'm');
	//resizeImage("document.album_img", 80);
}

function dispImageFromXmlList( xml_name, my_id , max_image_size, size){

	var xml = new JKL.ParseXML ( xml_name );
	var data = xml.parse();
	
	var cnt = 0;
	while(true)
	{	
//		var lnk = eval( "document.all." + my_id + 'lnk' + cnt);
//		var image = eval( "document.all." + my_id + 'Image' + cnt);
//		var authorName = eval( "document.all." + my_id + 'Atr' + cnt);
		var lnk = document.getElementById(my_id + 'lnk' + cnt);
		var image = document.getElementById(my_id + 'Image' + cnt);
		var authorName = document.getElementById(my_id + 'Atr' + cnt);

		if(
			data == undefined 
			|| data.Images.Image[cnt] == undefined
		)
		{
			break;
		}

		var imgUrl = (data.Images.Image[cnt].ImgUrl + '&tntype=' + size);
		var linkUrl = data.Images.Image[cnt].LinkUrl;

		//var href = "http://www.myalbum.jp/Pc/" + linkUrl;
		//var var1 = "'http://www.myalbum.jp/Pc/" +  imgUrl + "'";
		var href = linkUrl;
		var var1 = "'" + imgUrl + "'";

		var title = data.Images.Image[cnt].Title;
		var author = data.Images.Image[cnt].Author;

		var image_id = my_id + cnt + "Image";

		// 画像の設定
		image.href = href;
//		image.innerHTML  = ("<img src=" + var1 + " oncontextmenu='return false;' name='" + image_id + "' border=0 id='" + image_id + "' class='photo' onload='loadImage(this.src, this.name, " + max_image_size + ")' style='display:none'>");
		image.innerHTML  = ("<img src=" + var1 + " oncontextmenu='return false;' name='" + image_id + "' border=0 id='" + image_id + "' class='photo photo"+max_image_size+"')'>");

		// リンクの設定
		lnk.href = href;
		//lnk.innerHTML = ( title + '<br>' + author);
		lnk.innerHTML = ( title );
		
		// 作者名
		authorName.innerHTML = ( author );
		
		// 画像を設定内に収める
		//resize( eval("document.all." + image_id) , max_image_size);

		++cnt;
	}
}

// 引数の名前に、連番をつけて、リサイズを行っていく
// 無効なオブジェクト名になった時点で終了
function resizeImage(_name, size)
{
	var cnt = 0;
	while( true )
	{
		var image = eval( _name + cnt);
		if( image == undefined || cnt > 1000)  // 無限ループに陥らないように
		{
			break;
		}
		if( image.width > size || image.height > size)
		{
			resize(image, size);
			image.style.visibility = 'visible';
		}
		++cnt;
	}	
}

function resize(img, size)
{

	// 画像を引数のサイズに収める
	var _width = img.width;
	var _height = img.height;

	if( _width <= size && _height <= size)
	{
		return;
	}

	var rate = 1;

	if( _width >= _height )
	{
		rate = size/_width;
	}
	else
	{
		rate = size/_height;
	}
	
	img.style.zoom = rate;
	img.style.visibility = 'visible';
	
	//img.style.visibility = 'visible';
	//img.style.width = _width * rate;
	//img.style.height = _height * rate;
	//img.style.visibility = 'visible';
}

// 画面のトップに移動する
function backToTop() {
  var x1 = x2 = x3 = 0;
  var y1 = y2 = y3 = 0;
  if (document.documentElement) {
      x1 = document.documentElement.scrollLeft || 0;
      y1 = document.documentElement.scrollTop || 0;
  }
  if (document.body) {
      x2 = document.body.scrollLeft || 0;
      y2 = document.body.scrollTop || 0;
  }
  x3 = window.scrollX || 0;
  y3 = window.scrollY || 0;
  var x = Math.max(x1, Math.max(x2, x3));
  var y = Math.max(y1, Math.max(y2, y3));
  window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
  if (x > 0 || y > 0) {
      window.setTimeout("backToTop()", 25);
  }
}

/*アルバムのテンプレートを変更する*/
function change_album_template(template_no,display)
{ 
  switch(template_no){
    case 0: /*白*/
		change_white(display)
	  break;
    case 1: /*黒*/
		change_black(display)
	  break;
  }
  /*
  if( document.images["prvimage"] != undefined )
  {
   resizeDisplay01(document.images["prvimage"].src);
  }
  else if( document.images["pubimage"] != undefined )
  {
	resizeDisplay01(document.images["pubimage"].src);
  }	
  */
}

/* 画像とその名前 */
var img, imgName;

/* 画像読み込みタイマー、画像読み込みチェック回数とその最大値 */
var imgTimerID;
var checkCount, checkCountMax = 10;

function resizeDisplay01(_src)
{
  img = new Image();
  img.src = _src;

  /* タイマー起動 */
  checkCount = 0;
  clearInterval(imgTimerID);
  imgTimerID = setInterval("loadImageWait()", 500);

}

/* 画像ロードを待つための関数 */
function loadImageWait()
{
  checkCount++;
  /* 画像読み込み完了または一定回数実行後は次の処理へ */
  if (img.complete || (checkCount > checkCountMax)) {
    if (img.complete) {
		//alert(checkCount);
    } else {		
      var prvimage=document.images["prvimage"];
      if(prvimage != undefined)
      {
		prvimage.width = 600;
		prvimage.style.display = 'inline';
	  }
      var pubimage=document.images["pubimage"];
      if(pubimage != undefined)
      {
		pubimage.width = 600;
		pubimage.style.display = 'inline';        
      }
      clearInterval(imgTimerID);
      return;
    }
    clearInterval(imgTimerID);
    loadImageExec();
  }
}

function loadImageExec()
{
  var rate = 1;
  if( img.width > img.height )
  {
	rate = 600/img.width;
  }
  else
  {
    rate = 600/img.height;
  }
  if( rate > 1 )
  {
	rate = 1;
  }
  var prvimage=document.images["prvimage"];
  if( prvimage != undefined)
  {
	prvimage.width = rate*img.width;
	prvimage.height = rate*img.height;
	prvimage.style.display = 'inline';
  }
  var pubimage=document.images["pubimage"];
  if( pubimage != undefined)
  {
	pubimage.width = rate*img.width;
	pubimage.height = rate*img.height;
	pubimage.style.display = 'inline';
  }  
}

/*白のアルバムのテンプレート*/
function change_white(display)
{
  var i=0;
  
  document.getElementById("album_back").className = "album_backcolor0";
  document.getElementById("album_back_bottom").className = "album_bottom_backcolor0";

  while( true )
  {
		if( document.getElementById("shadow_top" + i) == undefined )
		{
			break;
		}
	    document.getElementById("image_title" + i).className = "image_title0";		
	    document.getElementById("shadow_top" + i).className = "shadow_top";
	    document.getElementById("shadow_left" + i).className = "shadow_left";
	    document.getElementById("shadow_right" + i).className = "shadow_right";
	    document.getElementById("shadow_bottom_left" + i).className = "shadow_bottom_left";
	    document.getElementById("shadow_bottom_right" + i).className = "shadow_bottom_right";
	    ++i;
  }
  if( i == 0 )
  {
		if( document.images["modimage"] != undefined )
		{
  			document.getElementById("shadow_top1").className = "shadow_big_top";
			document.getElementById("shadow_left1").className = "shadow_big_left";
			document.getElementById("shadow_right1").className = "shadow_big_right";
			document.getElementById("shadow_bottom_left1").className = "shadow_big_bottom_left";
			document.getElementById("shadow_bottom_right1").className = "shadow_big_bottom_right";
		}
		else
		{
			if( document.images["prvimage"] != undefined || document.images["pubimage"] != undefined)
			{
  				document.getElementById("shadow_top").className = "shadow_big_top";
				document.getElementById("shadow_left").className = "shadow_big_left";
				document.getElementById("shadow_right").className = "shadow_big_right";
				document.getElementById("shadow_bottom_left").className = "shadow_big_bottom_left";
				document.getElementById("shadow_bottom_right").className = "shadow_big_bottom_right";
				document.getElementById("photo_title_big").className = "photo_title_big0";
			}
			else
			{
				return;
			}
		}
  }
  else
  {
    document.getElementById("tab_noselected").className = "tab_display0";
    document.getElementById("tab_selected").className = "tab_display0_selected";
  }

/*
  switch(display){
    case 54: //54枚表示
      for(i=1;i<55;i++)
      {
		  if( document.getElementById("image_title" + i) == undefined)
		  {
			break;
		  }
		  
	    document.getElementById("image_title" + i).className = "image_title0";
		
	    document.getElementById("shadow_top" + i).className = "shadow_top";
	    document.getElementById("shadow_left" + i).className = "shadow_left";
	    document.getElementById("shadow_right" + i).className = "shadow_right";
	    document.getElementById("shadow_bottom_left" + i).className = "shadow_bottom_left";
	    document.getElementById("shadow_bottom_right" + i).className = "shadow_bottom_right";
      }
	  
	  break;
    case 15: //15枚表示
      for(i=1;i<16;i++)
      {
		  if( document.getElementById("image_title" + i) == undefined)
		  {
			break;
		  }
		  
	    document.getElementById("image_title" + i).className = "image_title0";
		
	    document.getElementById("shadow_top" + i).className = "shadow_top";
	    document.getElementById("shadow_left" + i).className = "shadow_left";
	    document.getElementById("shadow_right" + i).className = "shadow_right";
	    document.getElementById("shadow_bottom_left" + i).className = "shadow_bottom_left";
	    document.getElementById("shadow_bottom_right" + i).className = "shadow_bottom_right";
      }
	  
	  break;
    case 1: //写真詳細
  	  document.getElementById("shadow_top").className = "shadow_big_top";
	  document.getElementById("shadow_left").className = "shadow_big_left";
	  document.getElementById("shadow_right").className = "shadow_big_right";
	  document.getElementById("shadow_bottom_left").className = "shadow_bottom_big_left";
	  document.getElementById("shadow_bottom_right").className = "shadow_bottom_big_right";
	  
	  break;
  }
*/

  document.getElementById("page_top").className = "page0";
  document.getElementById("page_bottom").className = "page0";
  
  /*
  if (display == 1) //写真詳細の場合
  {
    document.getElementById("photo_title_big").className = "photo_title_big0";
  }
  else //写真詳細以外の場合
  {
    document.getElementById("tab_noselected").className = "tab_display0";
    document.getElementById("tab_selected").className = "tab_display0_selected";
  }
  */
}

/*黒のアルバムのテンプレート*/
function change_black(display)
{
  var i=0;
  
  document.getElementById("album_back").className = "album_backcolor1";
  document.getElementById("album_back_bottom").className = "album_bottom_backcolor1";
  
  while( true )
  {
		if( document.getElementById("shadow_top" + i) == undefined )
		{
			break;
		}
	    document.getElementById("image_title" + i).className = "image_title1";		
	    document.getElementById("shadow_top" + i).className = "no_shadow";
	    document.getElementById("shadow_left" + i).className = "no_shadow";
	    document.getElementById("shadow_right" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_left" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_right" + i).className = "no_shadow";
	    ++i;
  }
  if( i == 0 )
  {
  		if( document.images["modimage"] != undefined )
		{
  			document.getElementById("shadow_top1").className = "no_shadow";
			document.getElementById("shadow_left1").className = "no_shadow";
			document.getElementById("shadow_right1").className = "no_shadow";
			document.getElementById("shadow_bottom_left1").className = "no_shadow";
			document.getElementById("shadow_bottom_right1").className = "no_shadow";
		}
		else
		{
			if( document.images["prvimage"] != undefined || document.images["pubimage"] != undefined)
			{
  				document.getElementById("shadow_top").className = "no_shadow";
				document.getElementById("shadow_left").className = "no_shadow";
				document.getElementById("shadow_right").className = "no_shadow";
				document.getElementById("shadow_bottom_left").className = "no_shadow";
				document.getElementById("shadow_bottom_right").className = "no_shadow";
				document.getElementById("photo_title_big").className = "photo_title_big1";
			}
			else
			{
				return;
			}
		}
  }
  else
  {
    document.getElementById("tab_noselected").className = "tab_display1";
    document.getElementById("tab_selected").className = "tab_display1_selected";
  }
  
  /*
  switch(display){
    case 54: //54枚表示
      for(i=1;i<55;i++)
      {
		  if( document.getElementById("image_title" + i) == undefined)
		  {
			break;
		  }
		  
	    document.getElementById("image_title" + i).className = "image_title1";
		
	    document.getElementById("shadow_top" + i).className = "no_shadow";
	    document.getElementById("shadow_left" + i).className = "no_shadow";
	    document.getElementById("shadow_right" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_left" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_right" + i).className = "no_shadow";		
      }
	  
	  break;
    case 15: //15枚表示
      for(i=1;i<16;i++)
      {
		  if( document.getElementById("image_title" + i) == undefined)
		  {
			break;
		  }
		  
	    document.getElementById("image_title" + i).className = "image_title1";
		
	    document.getElementById("shadow_top" + i).className = "no_shadow";
	    document.getElementById("shadow_left" + i).className = "no_shadow";
	    document.getElementById("shadow_right" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_left" + i).className = "no_shadow";
	    document.getElementById("shadow_bottom_right" + i).className = "no_shadow";
      }
	  
	  break;
    case 1: //写真詳細
  	  document.getElementById("shadow_top").className = "no_shadow";
	  document.getElementById("shadow_left").className = "no_shadow";
	  document.getElementById("shadow_right").className = "no_shadow";
	  document.getElementById("shadow_bottom_left").className = "no_shadow";
	  document.getElementById("shadow_bottom_right").className = "no_shadow";
	  
	  break;
  }
  */
  
  document.getElementById("page_top").className = "page1";
  document.getElementById("page_bottom").className = "page1";
  
  /*
  if (display == 1) //写真詳細の場合
  {
    document.getElementById("photo_title_big").className = "photo_title_big1";
  }
  else //写真詳細以外の場合
  {
    document.getElementById("tab_noselected").className = "tab_display1";
    document.getElementById("tab_selected").className = "tab_display1_selected";
  } 
  */
}


var picImage;
var maxWidth=100;
var maxHeight=100;
var fileTypes=["jpg","jpeg"];
var spacer="images/spacer.gif";
var filePrefix = "Album_Upload_loFile";
var imageId;

// 参照ファイル切り替え時に呼び出されます
function preview(that){
  var source = that.value;
  // 拡張子のチェック
  var ext = source.substring( source.lastIndexOf(".") + 1, source.length ).toLowerCase() ;
  for ( var i=0 ; i < fileTypes.length ; i++ ) if ( fileTypes[ i ] == ext ) break ;
  
  // 画像ソースの設定
  picImage = new Image();
  if ( i < fileTypes.length ) 
  {
  	picImage.src = source ;
  }
  else {
    picImage.src = spacer;
  }
  imageId = that.id ; 
  setTimeout( 'applyChanges( imageId )', 200 ) ;
}

// 画像の切り替え
function applyChanges( id ){
  var field = document.getElementById( "thumbnail" + id.replace( filePrefix , "" ) );
  var x = parseInt( picImage.width ) ;
  var y = parseInt( picImage.height ) ;
  
  var ratio = getRatio( x, y )
  x *= ratio ;
  y *= ratio ;
  
  field.src = picImage.src ;
  field.width = x ;
  field.height = y ;
}

// 拡大/縮小率の取得
function getRatio( x, y )
{
   var ratio ;
   
  if (x > y) {
    ratio = maxWidth / x ;
  } else {
    ratio = maxHeight / y ;
  }
  return ratio ;
}