// Adobe 社提供のディテクションキットを含めたSWF埋め込みの書き出しを行います。

// 初期設定
var requiredMajorVersion = 6;
var requiredMinorVersion = 0;
var requiredRevision = 65;

// Flash Player のバージョンを指定したい場合に使用する。
// ※requiredPlayerVersion(メジャーバージョン,マイナーバージョン,リビジョンナンバー);
function requiredPlayerVersion(mj,mi,rv) {
	// requiredPlayerVersion() 実行しない場合、または実行時に何も引数を渡さなかった場合、初期設定を使用します。
	if(mj != "" || mj != null || mj != 'undefined') requiredMajorVersion = mj;
	if(mi != "" || mi != null || mi != 'undefined') requiredMinorVersion = mi;
	if(rv != "" || rv != null || rv != 'undefined') requiredRevision = rv;
}

// flashのidに番号を振る。
// ※JavaScript、CSSによる操作を可能にする。ex. flash1,flash2...（HTMLでのJavaScript実行順）
var flashNum = 0;

// VBScriptの書き出し
// Flash Player ActiveX コントロールのバージョン情報を検出するには、Visual Basic ヘルパーが必要です。
document.write('<script language="VBScript" type="text/vbscript">');
document.write('Function VBGetSwfVer(i) \n');
document.write('	on error resume next \n');
document.write('	Dim swControl, swVersion \n');
document.write('	swVersion = 0 \n');
document.write('	');
document.write('	set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i)) \n');
document.write('	if (IsObject(swControl)) then \n');
document.write('		swVersion = swControl.GetVariable("$version") \n');
document.write('	end if \n');
document.write('	VBGetSwfVer = swVersion \n');
document.write('End Function \n');
document.write('</script>');

// クライアントのブラウザのタイプを検出する。
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
// Flash Player プラグインのバージョン情報を検出するには、JavaScript ヘルパーが必要です。
function JSGetSwfVer(i){
	// プラグイン配列内の Flash プラグインについて NS/Opera バージョンが 3 以上かどうかを確認します
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 は Flash 4 をサポートしています
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 は Flash 3 をサポートしています
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// それ以前のバージョンの WebTV は Flash 2 をサポートしています
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// 他のすべての状況では検出できません
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 
// パラメータを指定せずに呼び出した場合、この関数は浮動小数値を返します。
// これは、Flash Player のバージョンまたは 0.0 になります。
// 例 : Flash Player 7r14 の場合は 7.14 を返します
// reqMajorVer を指定して呼び出した場合、reqMinorVer、reqRevision は、それ以上のバージョンが使用可能なときには true を返します
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
	// 最新のバージョンを見つけるまで、バージョンを遡ってループします
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray = versionStr.split(" ");
				tempString = tempArray[1];
				versionArray = tempString .split(",");
			} else {
				versionArray = versionStr.split(".");
			}
			versionMajor = versionArray[0];
			versionMinor = versionArray[1];
			versionRevision = versionArray[2];
			
			versionString = versionMajor + "." + versionRevision; // 7.0r24 == 7.24
			versionNum = parseFloat(versionString);
			// メジャーバージョンは要求されたメジャーバージョン以上であり、かつマイナーバージョンは要求されたマイナーバージョン以上の場合
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
			}
		}
	}	
	return (reqVer ? false : 0.0);
}

// Flash ムービーの埋め込み
// embedSWF('swf','サイズX','サイズY','背景色'[,'代替画像拡張子','キャッシュ','変数','ウィンドウモード'])
// ※[] 内はオプション。
function embedSWF(swf,sizeX,sizeY,bg,ext,cache,vars,wm) {
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion) { // 使用可能なバージョンが検出された場合。
		flashNum++;
		var varsItem = "?";
		if(vars != "" || vars != null || vars != 'undefined') {
			vars = varsItem + vars;
		}
		else {
			vars = varsItem;
		}
		if((cache != "" || cache != null || cache != 'undefined') && cache == true) {
			var loadTime = new Date();
			if(vars == (varsItem + 'undefined')) {
				vars = varsItem;
				vars += 'cache=' + loadTime.getTime();
			}
			else {
				vars += '&cache=' + loadTime.getTime();
			}
		}
		else {
			if(vars == (varsItem + '') || vars == (varsItem + 'null') || vars == (varsItem + 'undefined')) vars = "";
		}
		document.write('<div id="flash' + flashNum + '">');
		document.write('<object data="' + swf + '.swf' +vars+ '" type="application/x-shockwave-flash" width="' + sizeX + '" height="' + sizeY + '" id="flashid' + flashNum + '">');
		document.write('<param name="movie" value="' + swf + '.swf' + vars + '" />');
		document.write('<param name="bgcolor" value="' + bg + '" />');
		if(vars != "" || vars != null || vars != 'undefined') document.write('<param name="flashvars" value="' + vars + '" />');
		document.write('<param name="allowScriptAccess" value="sameDomain" />');
		document.write('<param name="loop" value="true" />');
		document.write('<param name="menu" value="false" />');
		document.write('<param name="quality" value="high" />');
		if(isWin && (wm == "" || wm == null || wm == 'undefined')) document.write('<param name="wmode" value="opaque" />');
		else document.write('<param name="wmode" value="' + wm + '" />');
		document.write('<param name="devicefont" value="true" /></object>');

	} else { // Flash Player のバージョンが古すぎるか、プラグインを検出できない場合。
	if(ext == "" || ext == null || ext == 'undefined') ext = "gif";
	else ext = ext;
	document.write('<a href="http://www.macromedia.com/go/getflash/"><img src="' + swf +'.' + ext +'" width="' + sizeX + '" height="' + sizeY + '" alt="Flashコンテンツの代替イメージを表示しています。このコンテンツには Macromedia Flash Player が必要です。" title="Flashコンテンツの代替イメージを表示しています。このコンテンツには Macromedia Flash Player が必要です。" /></a>');
	}
	document.write('</div>');
}