/*
	overlayFlash2.js: Fullsize Flash Overlay
	by octech 2008

	改変したところ：
	- 自身のファイル名を変更（→overlayFlash2.js）.
	- pause関数を削除.
	- showFlash を showOverlayFlash( swfname, swfver ); というインターフェイスに変更.
	  （showOverlayFlash( "hoge.swf", "8,0,0,0" ); という形で呼べるように。）
	- showOverlayFlash9(); という簡易版インターフェイスを追加.
	  （Flashなどからも簡単に呼べるように。）
	- showOverlayFlashで作成したdiv要素のidを返すように変更.
	  （最新 id は lastFlashID にも格納）
	- hideFlash を hideOverlayFlash(); に関数名変更.
	- onclickを埋め込み、hideFlashを呼んでいた場所を削除.
	- javascriptの流儀はよく知らないが、aタグ内rel要素を見て、onclickイベントをつける、
	  というのは、ライブラリのやることではないように思うので、初期化処理を削除.
	  →addLoadEvent(func), initoverLayFlash(); 削除.
	  （簡易的にonclickで設定でき、サイトによっては、初期化ルーチンを別で書けばよいかと.）
	- Flashを表示したdivについてid管理が出来る仕組みを実装.
	- ウィンドウリサイズ時にウィンドウサイズを再度取得するようにしました.
	
	簡単な使い方の例：
	<html>
	  <head>
	    <script type="text/javascript" src="overlayFlash2.js"></script>
	  </head>
	  <body>
	    <a href="JavaScript:void(0)" onclick="showOverlayFlash9('test2.swf')">CLICK!</a>
	  </body>
	</html>
	
	----------------------------------------

	overLayFlash JS: Fullsize FLash Overlays 
	by Ken Murayama むらけん - http://www.muraken.biz

	
	本スクリプトの利用、あるいは利用できないことによって発生した損失や損害に対してむらけんならびにF-siteは一切責任を負いません。
	自由に改変、利用してもらって構いません。
	但し転載する場合は、引用元を明記し、リンクを張ってください。
*/


// --- グローバル変数 --- //

// 最後に作成したFlashを表示しているdiv要素のidパラメータ.
var lastFlashID = "";



// --- メソッド --- //

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	
	// フラッシュ表示中に呼ばれたら、
	// ウィンドウ高さにFlash自身が影響しないようにします.
	if( lastFlashID != "" ){
		var objOverlay = document.getElementById( lastFlashID );
		objOverlay.style.height = "100%";
	}
	
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



//
// showOverlayFlash()
// @param swfname [in] SWF Filename (ex.hoge.swf)
// @param swfver  [in] SWF Version (ex."9,0,0,0")
//
function showOverlayFlash( swfname, swfver ){
	if(navigator.appName.indexOf("Microsoft") != -1){
		var br = "ie";
	}else{
		var br = "other";
	}
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();	
	var objBody = document.body;
	var objOverlay = document.createElement("div");
	var myTag= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+swfver+'" width="100%" height="100%" id="overlayFlash" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+swfname+'" /><param name="quality" value="high" /><param name="salign" value="lt" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="br='+br+'" /><embed src="'+swfname+'" quality="high" salign="lt" swLiveConnect=true wmode="transparent" bgcolor="#ffffff" FlashVars="br='+br+'" width="100%" height="100%" name="overlayFlash" id="overlayFlash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	var ret_idname = "overlay_"+swfname;
	ret_idname = ret_idname.replace( ".", "_" );
	ret_idname = ret_idname.replace( "/", "_" );
	
	objOverlay.setAttribute( 'id', ret_idname );
	objOverlay.style.display = 'block';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '200';
 	objOverlay.style.width = '100%';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.innerHTML = myTag;
	objBody.insertBefore(objOverlay, objBody.firstChild);
	objOverlay.style.display = 'block';
	
	lastFlashID = ret_idname;
	
	// ウィンドウリサイズ時のハンドラを設定.
	window.onresize = resizeHandler;
	
	return ret_idname;
}


//ウィンドウのリサイズ時に高さ情報を更新する
//	objOverlay.style.height = (arrayPageSize[1] + 'px');
function resizeHandler(){
	if( lastFlashID == "" ){
		return;
	}
	
	var objOverlay = document.getElementById( lastFlashID );
	var arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
}

//
// showOverlayFlash9() -- バージョン9専用.
// @param swfname [in] SWF Filename (ex.hoge.swf)
//
function showOverlayFlash9( swfname ){
	return showOverlayFlash( swfname, "9,0,0,0" );
}


//
// hideOverlayFlash()
// @param id [in] SWFを囲んだdiv要素のidパラメータ（showOverlayFlashの返り値）
//
function hideOverlayFlash( id ){
	// 特に引数を指定していない場合は、最後に設定したウィンドウを設定.
	if( id == undefined || id == "" ){
		id = lastFlashID;
	}
	
	var objOverlay = document.getElementById( id );
	var objOverlayParent = objOverlay.parentNode
	objOverlayParent.removeChild( objOverlay );
	
	// リセット.
	if( id == lastFlashID ){
		lastFlashID = "";
	}
}

