pyaLightBox = new function(){
	var OverlayID = "PYALIGHTBOX_OVERLAY";
	var IframeID = "PYALIGHTBOX_IFRAME";
	var FW = 0;//フレームサイズ幅
	var FH = 0;//フレームサイズ高さ
	var TH = 24;//閉じるボタン帯の高さ
	var isIE = '\v'=='v';
	var initFlag = false;
	initialize = function(){
		if( document.getElementById( IframeID ) ) document.body.removeChild( document.getElementById( IframeID ) );
		if( document.getElementById( OverlayID ) ) document.body.removeChild( document.getElementById( OverlayID ) );

		var overLayObj = document.createElement("div");
		overLayObj.setAttribute( "id", OverlayID );
		overLayObj.className = "pyaOverLayLay";
		document.body.appendChild( overLayObj );

		var iframeDivObj = document.createElement("div");
		iframeDivObj.setAttribute( "id", IframeID );
		iframeDivObj.className = "pyaIframeIfmY";
		var xDivObj = document.createElement("div");
		xDivObj.className = "pyaIframeIfmX";
		var barObj = document.createElement("div");
		barObj.className = "pyaIframeClose";
		var aTag = document.createElement("a");
		aTag.setAttribute( "href", "javascript:void(0)" );
		aTag.style.color = "#006400";
		aTag.style.fontSize = "15px";
		aTag.style.height = TH + "px";
		aTag.innerHTML = "<b>×閉じる</b>";
		barObj.appendChild( aTag );
		xDivObj.appendChild( barObj );
		iframeDivObj.appendChild( xDivObj );
		document.body.appendChild( iframeDivObj );
		if( !isIE ){
			aTag.addEventListener( "click", end, false );
			//window.addEventListener( "resize", setOverLay, false );
			overLayObj.addEventListener( "click", end, false );
			iframeDivObj.addEventListener( "click", end, false );
			xDivObj.addEventListener( "click", function(event){ event.stopPropagation(); }, false );
			document.body.addEventListener( "keydown", function( event ){ keyEvent( event ); }, false );
		}else{
			aTag.attachEvent( "onclick", end );
			window.attachEvent( "onresize", setOverLay );
			overLayObj.attachEvent( "onclick", end );
			iframeDivObj.attachEvent( "onclick", end );
			xDivObj.attachEvent( "onclick", function( event ){ event.cancelBubble=true; } );
			document.body.attachEvent( "onkeydown", function( event ){ keyEvent( event ); } );
		}
		var _addOnClick = function( myOBJ ){
			var myURL = myOBJ.getAttribute("href");
			myOBJ.getAttribute("rev").match(/width\s*:\s*([0-9]+)/i);
			var myW = ( RegExp.$1 ) ? parseInt( RegExp.$1 ) : 100;
			myOBJ.getAttribute("rev").match(/height\s*:\s*([0-9]+)/i);
			var myH = ( RegExp.$1 ) ? parseInt( RegExp.$1 ) : 100;
			var myTYPE = myOBJ.getAttribute("rel");
			myOBJ.onclick = function(){ pyaLightBox.start( myURL, myW, myH, myTYPE ); return false; };
		};
		var w, h;
		var aTagObj = document.body.getElementsByTagName("a");
		for( var i = aTagObj.length - 1; i >= 0; i-- ){
			if( aTagObj[ i ].getAttribute("rev") ){
				_addOnClick( aTagObj[ i ] );
			}
		}
		initFlag =  true;
	};
	this.start = function( url, fw, fh, rel ){
		if( !initFlag ) return;
		FW = fw;
		FH = fh;

		var iframeDivObj, xDivObj, iframeObj;

		if( rel === "image" ){
			var _reSizeImage = function( e ){
				if( e ){
					( !isIE ) ? iframeObj.removeEventListener( "load", arguments.callee, false ) : iframeObj.detachEvent( "onload", arguments.callee );
				}
				iframeDivObj.style.height = iframeObj.height + TH + "px";
				xDivObj.style.width = iframeObj.width + "px";
				xDivObj.style.height = iframeObj.height + TH + "px";
				iframeObj.style.width = iframeObj.width + "px";
				iframeObj.style.height = iframeObj.height + "px";
				xDivObj.appendChild( iframeObj );
			};

			iframeDivObj = document.getElementById( IframeID );
			iframeDivObj.style.height = "100px";

			xDivObj = iframeDivObj.firstChild;
			xDivObj.style.width = "100px";
			xDivObj.style.height = "100px";

			iframeObj = document.createElement("img");
			iframeObj.setAttribute( "src", url );
			iframeObj.className = "pyaIframeIfm";

			if( !iframeObj.width ){
				( !isIE ) ? iframeObj.addEventListener( "load", _reSizeImage, false ) : iframeObj.attachEvent( "onload", _reSizeImage );
			}else{
				_reSizeImage( 0 );
			}
		}else{
			iframeDivObj = document.getElementById( IframeID );
			iframeDivObj.style.height = fh + TH + "px";

			xDivObj = iframeDivObj.firstChild;
			xDivObj.style.width = fw + "px";
			xDivObj.style.height = fh + TH + "px";

			iframeObj = document.createElement("iframe");
			iframeObj.setAttribute( "src", url );
			iframeObj.className = "pyaIframeIfm";
			iframeObj.style.width = fw + "px";
			iframeObj.style.height = fh + "px";
			xDivObj.appendChild( iframeObj );
		}

		var iframePosY = parseInt( document.body.scrollTop || document.documentElement.scrollTop ) + 20;
		iframeDivObj.style.top = iframePosY + "px";

		setOverLay();
		toggleSelects( 0 );

		var overlayObj = document.getElementById( OverlayID );
		iframeDivObj.style.display = "block";
		overlayObj.style.display = "block";
	};
	end = function(){
		if( !initFlag ) return;
		var iframeDivObj = document.getElementById( IframeID );
		var overlayObj = document.getElementById( OverlayID );
		if( iframeDivObj.firstChild.getElementsByTagName("iframe").length ){
			iframeDivObj.firstChild.removeChild( iframeDivObj.firstChild.getElementsByTagName("iframe")[0] );
		}else if( iframeDivObj.firstChild.getElementsByTagName("img").length ){
			iframeDivObj.firstChild.removeChild( iframeDivObj.firstChild.getElementsByTagName("img")[0] );
		}
		iframeDivObj.style.display = "none";
		overlayObj.style.display = "none";
		toggleSelects( 1 );
	};
	setOverLay = function(){
		var window_H = ( document.documentElement.clientHeight > document.documentElement.scrollHeight ) ? document.documentElement.clientHeight : document.documentElement.scrollHeight;
		var overlayObj = document.getElementById( OverlayID );
		overlayObj.style.height = ( FH > window_H ) ? FH + "px" : window_H + "px";
	};
	keyEvent = function( e ){
		if( e.keyCode === 27 ){
			end();
		}
	};
	toggleSelects = function( flag ){
		if( isIE ){
			var selects = document.getElementsByTagName("select");
			var cnt = selects.length;
			for( var i=0; i<cnt; i++ ){
				selects[ i ].style.visibility = ( flag ) ? "visible" : "hidden";
			}
		}
	};
	onload = new function(){
		if( typeof( window.addEventListener ) != "undefined" ){
			window.addEventListener("load", initialize, false );
		}else if( typeof( window.attachEvent ) != "undefined" ){
			window.attachEvent("onload", initialize);
		}else{
			window.onload = function(){ initialize(); };
		}
	};
};
