﻿new function(){
galleryViewer = {
	/* ------------------------------------------------------------- */
	/* となりのインテリア　ギャラリー　更新：2008.6.03
	/* ------------------------------------------------------------- */
	galleryHandler : function(){
		//変数設定
		var ids = ['Room01','Room02','Room03','Room04']
		var len = ids.length;
		var showId = $luUtil.id(ids[0]);
		var hoverBtn = $luUtil.id('Btn'+ids[0]).firstChild;
		var key;
		var popFlag = false;
		var iconFlag = false;
		var mainAlpha = '0.6';
		var mainAlphaIe = '60';
		var mainImg;
		var pconfade;
		var popupAlpha = '0.2';
		var iconfade;
		var iconElm =  $luUtil.getClass('icon','p',showId)[0];
		var popupId = $luUtil.id('PopupContainer');
		
		//parentContainerの初期化
		//var newPopup = popupId.cloneNode(true);
		var parentContainer = popupId.parentNode;
		$luUtil.getClass('canvas','div',showId)[0].appendChild(popupId);
		//新たにPopupContainerノードを格納
		popupId = $luUtil.id('PopupContainer');
		//透過インスタンス生成
		fadeInstance();
		//closeボタンイベント処理
		closeEventListener();
		
		//ROOM01以外のブロックを非表示
		for(var i=1;i<ids.length;i++){
			if($luUtil.id(ids[i])){
				 $luUtil.id(ids[i]).style.display='none';
			};
		}

		//naviClickHandler
		var naviClickHandler = function(evt){
			//デフォルトアクション削除・伝播抑止
			$luUtil.preventDefault(evt);
			$luUtil.stopPropagation(evt);
			
			//liタグのノードを取得
			var target = $luUtil.target(evt);
			target = (target.tagName == 'IMG')?target.parentNode.parentNode:(target.tagName == 'A')?target.parentNode:target;
			//現在表示されているnewPopupを削除
			//$luUtil.getClass('canvas','div',showId)[0].removeChild(popupId);
			
			//現在表示されているgalleryを非表示
			showId.style.display="none";
			
			//クリックされたgalleryを表示
			showId = $luUtil.id(target.id.split('Btn')[1]);
			showId.style.display = 'block';
			
			//newPopupを表示
			//newPopup.style.display = 'none';
			$luUtil.getClass('canvas','div',showId)[0].appendChild(popupId);
			//新たにPopupContainerノードを格納
			popupId = $luUtil.id('PopupContainer');
			popupId.style.opacity='0';
			popupId.style.filter='alpha(opacity=0)';
			
			//新たにiconNodeを取得
			iconElm = $luUtil.getClass('icon','p',showId)[0];
			
			//ナビゲーションボタンのclassを変更
			hoverBtn.className='';
			target.firstChild.className='on';
			
			//透過インスタンス生成
			fadeInstance();
			
			//CloseContainerにイベント割り当て
			closeEventListener();
			popupClose();
			
			//表示したIDを各変数に格納
			hoverBtn = target.firstChild;
		}
		
		
		
		//thumClickHandler
		var thumClickHandler = function(evt){
			//デフォルトアクション削除・伝播抑止
			$luUtil.preventDefault(evt);
			$luUtil.stopPropagation(evt);
			
			//iconが表示されていたら非表示にする
			if(iconFlag){
				iconElm.style.display = 'none';
				iconFlag = false;
			};
			
			//メイン画像を透過する
				
			/*
			if(!popFlag){
				mainImg = $luUtil.getClass('canvasImg','img',showId)[0];
				mainImg.style.opacity = mainAlpha;
				mainImg.style.filter = 'alpha(opacity='+mainAlphaIe+')';
			}*/
			
			var target = $luUtil.target(evt);
			target = (target.tagName == 'IMG' || target.tagName == 'SPAN')?target.parentNode:(target.tagName == 'LI')?$luUtil.tag(target,'A')[0]:target;
			var imgBlockId = $luUtil.id('ImgBlock');
			
			//title要素を取得
			var tChilds = $luUtil.getClass('title','span',target)[0].childNodes;
			var tLen = tChilds.length;
			var title = '';
			if(tLen > 1){
				for(var i=0;i<tLen;i++){
					if(tChilds[i].data){
						title += tChilds[i].data;
					}
				}
			}else{
				title = tChilds[0].data
			}
			//caption要素を取得
			var cChilds = $luUtil.getClass('caption','span',target.parentNode)[0].childNodes;
			var cLen = cChilds.length;
			var caption = '';
			if(cLen > 1){
				for(var i=0;i<cLen;i++){
					if(cChilds[i].data){
						caption += cChilds[i].data;
					}else{
						caption += '<br />';
					}
				}
			}else{
				caption = cChilds[0].data
			}
			
			//画像以外を画面に出力
			$luUtil.id('GalleryTitle').innerHTML = title;
			$luUtil.id('CaptionBlock').innerHTML = caption;
			
			//getImgSize
			var getImgSize = function(){
				var w = $luUtil.tag(imgBlockId,'img')[0].offsetWidth;
				var src = 'width:'+w+'px;margin:0px auto 10px;';
				return src;
			}
			
			//イメージデータの読み込み
			var newImg = new Image();
			var imgLoaded = function(){
				if(popFlag){
					imgBlockId.style.visibility='hidden';
					imgBlockId.innerHTML = '<img src="'+newImg.src+'" alt="'+title+'">';
					imgBlockId.style.display='block';
					imgBlockId.style.cssText = getImgSize();
					imgBlockId.style.visibility='visible';
				}else{
					popupId.style.visibility='hidden';
					imgBlockId.innerHTML = '<img src="'+newImg.src+'" alt="'+title+'">';
					popupId.style.display='block';
					var w = $luUtil.tag(imgBlockId,'img')[0].offsetWidth;
					imgBlockId.style.cssText = getImgSize();
					popupId.style.visibility='visible';
					pconfade.fadeIn(popupAlpha,function(){
						popFlag = true;
					});
				}
			}
			$luUtil.addEventListener(newImg,'load',imgLoaded);
			newImg.src=target.href.split('&')[0];
		}
		
		
		
		//thumOverHandler
		var thumOverHandler = function(evt){
			if(popFlag) return;
			//デフォルトアクション削除・伝播抑止
			$luUtil.preventDefault(evt);
			$luUtil.stopPropagation(evt);
			
			var target = $luUtil.target(evt);
			target = (target.tagName == 'IMG' || target.tagName == 'SPAN')?target.parentNode:(target.tagName == 'LI')?$luUtil.tag(target,'A')[0]:target;
			var href = target.href.split('&');
			
			
			
			iconElm.style.left = href[1] + 'px';
			iconElm.style.top = href[2] + 'px';
			//iconElm.style.display = 'block';
			iconfade.fadeIn('0.1',function(){
				iconFlag = true;
			});
			
		}
		
		
		//thumOutHandler
		var thumOutHandler = function(evt){
			//デフォルトアクション削除・伝播抑止
			$luUtil.preventDefault(evt);
			$luUtil.stopPropagation(evt);
			iconElm.style.display = 'none';
			iconFlag = false;
		}
		
		//fadeInstance
		function fadeInstance(){
			pconfade = new $luUtil.Fade(popupId,'1');
			iconfade = new $luUtil.Fade(iconElm,'1');
		}
		
		//popupClose
		function popupClose(){
			if(!popFlag) return;
			popFlag = false;
			//メイン画像の透過を元にもどす
			/*
			mainImg.style.opacity = '1';
			mainImg.style.filter = 'alpha(opacity=100)';
			*/
			pconfade.fadeOut(popupAlpha,function(){
				popupId.style.display='none';
			});
			
		}
		
		
		//closeEventListener
		function closeEventListener(){
			var closes = $luUtil.getClass('closeElm','',showId);
			for(var i = 0;i<closes.length;i++){
				$luUtil.addEventListener(closes[i],'click',popupClose);
			}
		};
		
		
		//GalleryNaviにイベント割り当て
		for(var i =0;i<len;i++){
			$luUtil.addEventListener($luUtil.id('Btn'+ids[i]),'click',function(evt){naviClickHandler(evt)});
		}
		
		//canvasThumbにイベント割り当て
		var thumbs = $luUtil.getClass('thumImg','a',$luUtil.id('Gallery'));
		for(var i =0;i<thumbs.length;i++){
			$luUtil.addEventListener(thumbs[i],'click',thumClickHandler);
			$luUtil.addEventListener(thumbs[i],'mouseover',thumOverHandler);
			$luUtil.addEventListener(thumbs[i],'mouseout',thumOutHandler);
		}
	}
};
//フォトギャラリー実行
var checkCounter = 0;
var checkLocalContents = setInterval(function(){
	checkCounter++
	if($luUtil.id('PopupContainer')){
		clearInterval(checkLocalContents);
		galleryViewer.galleryHandler();
	}else if(checkCounter >= 40){
		clearInterval(checkLocalContents);
	}
},20);

};
