/*

-初期設定
Flash    : dipslay: none;
FlashAlt : dipslay: block;


-バージョン判定後（OKの場合）
Flash    : dipslay: block;
FlashAlt : dipslay: none;


-バージョン判定後（ダメな場合）
Flash    : dipslay: none;
FlashAlt : dipslay: block;


-JSがOFFの場合（noscript用表示が必要な場合）
Flash    : dipslay: none;
FlashAlt : dipslay: none;
noscript : 表示


-JSがOFFの場合（noscript用表示が特に不要な場合）
Flash    : dipslay: none;
FlashAlt : dipslay: block;
noscript : 不要


* 確認事項

-印刷対応は？
-FirefoxではFlashは印刷できない？ということは、やはり印刷用代替画像が必要か？



----------------------------------------------------------------------------------------*/


function SWFLoader(param){

	this.so = null;//swfoInstance
	this.playerVersion = null;

	// SWFObject : 初期設定必須
	this.src              = '';
	this.id               = '';
	this.width            = '100%';
	this.height           = '100%';
	this.requiredVersion  = '8';
	this.bgcolor          = '#ffffff';

	// config : 初期設定必須
	this.targetId         = '';
	this.altBlock         = '';
	this.noCache          = true;

	// swf options
	this.otherParam       = {};

	if( param && typeof(param) == 'object')
		this.init( param );

	/*
		this.installedVersion;
		this.requiredVersion;
		this.outputFlashOk;
	*/
}



SWFLoader.prototype ={

	// 初期化処理。SWFObjectのインスタンスを生成
	// バージョン判定も行い、その後の描画パターンを決定
	init: function( param ){
		this.setParameter( param );
		
		// 既にクエリが付いている場合への対応
		if( this.noCache ){
			var query = "noCache=" + (new Date()).getTime();
			this.src =  ( this.src.match(/\?$/) )?  this.src +       query :
						( this.src.match(/\?.*/) )? this.src + "&" + query :
													this.src + "?" + query;
		}

		if(!this.so)
			this.so = new SWFObject( this.src, this.id, this.width, this.height, this.requiredVersion, this.bgcolor );

		if( this.otherParam )
			this.setOtherParameter( this.otherParam );

/*
		// required version
		if(!document.all) console.log(this.so.getAttribute('version'));
		// installed version
		if(!document.all) console.log(this.so.installedVer);

		if(!document.all) console.log(this.requiredVersion);
		if(!document.all) console.log(this.so.getAttribute('version').versionIsValid(this.requiredVersion));
		if(!document.all) console.log(this.so.installedVer.versionIsValid(this.so.getAttribute('version')));
*/


		// 要求バージョンよりもユーザのインストール・バージョンが高ければ、alternate blockを非表示に。
		if(this.so.installedVer.versionIsValid(this.so.getAttribute('version')))
			this.hideFlashAlternate();
		else
			this.showFlashAlternate();
	},


	// Flashの元々もつパラメーターベースではなく、あくまで入力されたパラメーターをを
	setParameter: function( param ){
		for( var p in param ){
			var value = param[p];
			switch( p ){
				case 'src':
							this.src = value;
							break;
				case 'id':
							this.id = value;
							break;
				case 'width':
							this.width = value;
							break;
				case 'height':
							this.height = value;
							break;
				case 'requiredVersion':
							this.requiredVersion = new Number(value);// 別途パラメータを持てるように
							break;
				case 'bgcolor':
							this.bgcolor = value;
							break;

				case 'targetId':
							this.targetId = value;
							break;
				case 'altBlock':
							this.altBlock = value;
							break;
				case 'noCache':
							this.noCache = value;
							break;

				default :
							this.otherParam[p] = value;
							break;
			}
		}
	},


	setOtherParameter: function( param ){
		for( var p in param ){
			this.so.addVariable( p, param[p] );
		}
	},



	// プラグイン判定成功時、alternateブロックを非表示に設定
	hideFlashAlternate: function(){
		document.write( '<style type="text/css" media="screen">' );
		document.write( '#' + this.altBlock + '{display: none;}' );
		document.write( '#' + this.targetId + '{display: block;}' );
		document.write( '</style>' );

		document.write( '<style type="text/css" media="none">' );
		document.write( '#' + this.targetId + '{display: none;}' );
		document.write( '</style>' );
	},

	// alternateブロックをデフォルト非表示にしてある場合（<noscript>追加時など）も考え、
	// 改めてalternateブロックの表示指定を行う。
	showFlashAlternate: function(){
		document.write( '<style type="text/css" media="all">' );
		document.write( '#' + this.altBlock + '{display: block;}' );
		document.write( '#' + this.targetId + '{display: none;}' );
		document.write( '</style>' );
	},



	// 書き出し
	write: function(){
		if(this.so.installedVer.versionIsValid(this.requiredVersion))
			this.so.write(this.targetId);
	}

};


