///////////////////////////////////////////////////////////////////////////////
//
//  3D SHOWROOM CO制御JavaScript
//  Copyright 2006 NTT DATA SANYO SYSTEM CORPORATION
//
///////////////////////////////////////////////////////////////////////////////

/***** HTMLロード時の処理 *****/
function onHTMLLoad() {

	// ブラウザのチェック
	top.checkBrowser();

	// ウィンドウの大きさをXGAに
	resizeTo(1024,768);
	top.onResize();

	// ３Ｄエリア画像を変更
	if ( BG_mid1 != null && BG_mid1_bc != null ) changeBG( BG_mid1, BG_mid1_bc );

	// 1秒後、デフォルトCOを密かに読み込み開始
	setTimeout("changeCO(\""+co_default+"\");",1000);

}

/***** シーン開始時の処理 *****/
function onSceneFinished() {

	// ブラウザの判別
	if (typeof(top.f_mid1.document.embeds["CultObject"])=="object") {
		Cult3D = new Cult3D_Object(top.f_mid1.document.embeds["CultObject"]);
		this.Type = "NS";
	} else {
		Cult3D = new Cult3D_Object(eval("top.f_mid1.CultObject"));
		this.Type = "IE";
	}
	Cult3DLoaded = true;

	// モデル初期化
	if ( firstOp ) {
		// カラー選択されている場合、すぐに反映
		if ( menSel==1 ) {
			changeColor(colSel);
		}
	}

}

/***** COロード完了時の処理 *****/
function onLoadFinished(){
	// すでにボタン操作後ならすぐにCO表示
	if ( firstOp ) {
		top.f_mid1.showCO();
	}
}

/***** Cult3Dからのメッセージを受け取る *****/
function onSendMessage(){

	if (this.Type=="IE") {
		mes=Cult3D.LastMessage;
	} else if (this.Type=="NS") {
		mes=Cult3D.getLastMessage();
	}
}

/***** Cult3Dからメッセージを処理する *****/
function onMessageSent(mes) {
	loadingWorldFlg = false;
}

/***** ブラウザがリサイズされたときの処理 *****/
function onResize() {

	if ( top.f_mid1.document.all.img_main.style.visibility=="hidden" ) return;

	// 3Dエリア画像を、縦横比を保持したまま拡大・縮小
	var cw = top.f_mid1.document.body.clientWidth;
	var ch = top.f_mid1.document.body.clientHeight;
//alert(w +","+ h );
	if ( cw/ch < 790/441 ) {
		var h = (cw*441/790);
		var t = (441-h)/2;
		top.f_mid1.document.all.img_main.style.marginLeft="0px";
		top.f_mid1.document.all.img_main.style.marginTop=t+"px";
		top.f_mid1.document.all.img_main.style.width=cw+"px";
		top.f_mid1.document.all.img_main.style.height=h+"px";
	} else {
		var w = (ch/441*790);
		var l = (790-w)/2;
		top.f_mid1.document.all.img_main.style.marginLeft=l+"px";
		top.f_mid1.document.all.img_main.style.marginTop="0px";
		top.f_mid1.document.all.img_main.style.width=w+"px";
		top.f_mid1.document.all.img_main.style.height=ch+"px";
	}
}

/***** HTMLアンロード時の処理 *****/
function onUnload() {
}

/***** 対応環境以外でアラートを表示する *****/
function checkBrowser() {

	flg = false;

	// OS
	str = navigator.userAgent.toUpperCase();
	if (str.indexOf("WIN") >= 0) {

		// ブラウザ
		str  = navigator.appName.toUpperCase();
		if (str.indexOf("MICROSOFT") >= 0) {

			// ブラウザバージョン
			str  = navigator.userAgent;
			i = str.indexOf("MSIE ",0) + 5;
			j = str.indexOf(";",i);
			if ( eval(str.substring(i,j)) >= 5.0 ) {
				flg=true;
			} 
		}
	}

	if ( !flg ) {
		alert( "このページは、お客様の環境では正しく動作しない可能性があります。\n詳しくは「動作環境について」をご覧下さい。" );
	}

}

/***** カラー変更をCult3Dに伝える *****/
function changeColor(id) {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerAction("ChangeBodyTexture",daCol[id][1]);
}

/***** ３Ｄエリアの画像を変更する *****/
function changeBG(filename,color) {
	onResize();
	BG_mid1=filename;
	BG_mid1_bc=color;
//	top.f_mid1.document.body.style.backgroundImage="url(images/"+filename+")";
	top.f_mid1.document.all.img_main.src="images/"+filename;
	top.f_mid1.document.body.style.backgroundColor="#"+color;
}

/***** COを変更する *****/
function changeCO(filename) {

	if ( Cult3DFilename==filename ) return;
	Cult3DFilename=filename;

	Cult3D=null;
	Cult3DLoaded=false;

	if ( top.f_mid1 != null )
		setTimeout("changeCO2(\""+filename+"\");",20);

}

function changeCO2() {

	var ih="";
	ih+="<OBJECT id='CultObject' name='CultObject' codeBase='http://www.cult3d.com/download/cult.cab' width='0' height='0' align='absMiddle' border='0' classid='clsid:31B7EB4E-8B4B-11D1-A789-00A0CC6651A8'>";
    ih+="<PARAM name='src' value='../co/"+Cult3DFilename+".co'>";
    ih+="    <PARAM name='axis' value='X'>";
    ih+="    <PARAM name='speed' value='0.1'>";
    ih+="    <PARAM name='antialiasing' value='"+aaSel+"'>";
    ih+="    <PARAM name='antialiasingdelay' value='1'>";
    ih+="    <PARAM name='viewfinished' value='1'>";
    ih+="    <PARAM name='disablepb' value='0'>";
    ih+="    <PARAM name='disablehw' value='"+disablehw+"'>";
    ih+="    <PARAM name='pbcolor' value='606060'>";
    ih+="    <EMBED type='application/x-cult3d-object' src='../co/"+Cult3DFilename+".co' name='CultEmbed' id='CultEmbed' axis='X' speed='0.1' width='100%' height='100%' border='0' ";
    ih+="		align='absmiddle' antialiasing='1' ";
    ih+="		pluginspage='http://www.cult3d.com/newuser/index.html' ";
    ih+="	onloadfinished='onLoadFinished();' onsendmessage='messageSent();'>";
    ih+="    </EMBED> ";
    ih+="</OBJECT>";

	top.f_mid1.document.all.cult3d_area.innerHTML = ih;

}

/***** CO読み込み中の場合は中止する *****/
function unloadCO(filename) {

	Cult3DFilename="";
	if ( top.f_mid1.document.all.cult3d_area != null ) 
		top.f_mid1.document.all.cult3d_area.innerHTML = "";

	top.f_mid1.hideCO();

}


/***** Cult3D操作ヘルプ・コントロールを表示 *****/
function showCOControl() {
	top.f_down1.document.all.btnF3D.style.visibility = "visible";
	top.f_down1.document.all.btnP3D.style.visibility = "visible";
	writeAa();
	top.f_down1.document.body.style.backgroundImage="url(images/gui_S01_05_mouseop.gif)";
	top.f_down2.document.body.style.backgroundImage="url(images/gui_S01_06_mouseop.gif)";
}


/***** Cult3D操作ヘルプ・コントロールを非表示 *****/
function hideCOControl() {
	top.f_down1.document.all.btnF3D.style.visibility = "hidden";
	top.f_down1.document.all.btnP3D.style.visibility = "hidden";
	top.f_mid2.document.all.list_aa.innerHTML = "";
	top.f_down1.document.body.style.backgroundImage="url(images/gui_S01_05.gif)";
	top.f_down2.document.body.style.backgroundImage="url(images/gui_S01_06.gif)";

}

/***** ３Ｄ表示エリア拡大／元に戻す *****/
function switchF3D() {

	F3DFlg=(!F3DFlg);
	top.f_down1.document.all.btnF3D.src=MenF3DURL(0);
	if ( F3DFlg ) {
		// 拡大する
		top.frame_row.rows="33,*,41";
		top.frame_col1.cols="*,9";
		top.frame_col2.cols="20,*,9";
		top.frame_col3.cols="20,*,9";
		top.frame_out_row.rows="0,*,0";
		top.frame_out_col.cols="0,*,0";
		top.f_down2.document.all.btnEnv.style.visibility="hidden";
		top.f_down1.document.all.list.style.visibility="hidden";
	} else {
		// 元に戻す
		top.frame_row.rows="33,439,111";
		top.frame_col1.cols="811,178";
		top.frame_col2.cols="20,791,178";
		top.frame_col3.cols="20,791,178";
		top.frame_out_row.rows="*,583,*";
		top.frame_out_col.cols="*,989,*";
		top.f_down2.document.all.btnEnv.style.visibility="visible";
		top.f_down1.document.all.list.style.visibility="visible";
	}
}



/***** 印刷 *****/
function print3D() {

	if( window.confirm("印刷を実行します。\nよろしいですか？") ) {
		// 確認ダイアログ自体が印刷されてしまうことを防ぐため、100ミリ秒後に印刷する
		setTimeout("print3D2();",100);
	}

};

function print3D2() {

	top.f_mid1.CaptCtrl.CaptAreaLeft = top.f_mid1.screenLeft; 
	top.f_mid1.CaptCtrl.CaptAreaRight = top.f_mid1.screenLeft + top.f_mid1.document.body.clientWidth;
	top.f_mid1.CaptCtrl.CaptAreaTop = top.f_mid1.screenTop;
	top.f_mid1.CaptCtrl.CaptAreaBottom = top.f_mid1.screenTop + top.f_mid1.document.body.clientHeight;
	top.f_mid1.CaptCtrl.CaptFileName = "c:/windows/temp/capt.bmp";

//	top.f_mid1.CaptCtrl.CaptMode = 2;
//	top.f_mid1.CaptCtrl.Capture();

//	winPrint = window.open("c:/windows/temp/capt.bmp","印刷中です","width=1,height=1");
//	if ( winPrint != null && winPrint.closed == false ) winPrint.print();
//	if ( winPrint != null && winPrint.closed == false ) winPrint.close();

	top.f_mid1.CaptCtrl.CaptMode = 3;
	top.f_mid1.CaptCtrl.Capture();

	if(winEst == null){
		w = 600;
		h = 400;
		l = ( screen.availWidth - w ) / 2;
		t = ( screen.availHeight - h ) / 2;
		top.winEst = window.open("est.htm", null, "left=" + l + ",top=" + t + ",width=" + w + ",height=" + h );
	} else {
		winEst.focus();
	}

	top.f_down1.document.all.btnP3D.src=top.MenP3DURL(0);

}

/***** カメラ変更（左） *****/
function cameraLeft() {
	Cult3D.triggerEvent("cameraLeft");
}

/***** カメラ変更（右） *****/
function cameraRight() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("cameraRight");
}

/***** カメラ変更（前） *****/
function cameraFront() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("cameraFront");
}

/***** カメラ変更（後） *****/
function cameraBack() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("cameraBack");
}

/***** ズームイン *****/
function zoomIn() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("zoomIn");
}

/***** ズームアウト *****/
function zoomOut() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("zoomOut");
}

/***** 指定ノード以下を表示 *****/
function showNode(s) {
	if ( Cult3DLoaded == false ) return;	
	Cult3D.triggerAction("showNode",s);
}

/***** 指定ノード以下を非表示 *****/
function hideNode(s) {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerAction("hideNode",s);
}

/***** ロード済ワールドをすべてアンロード *****/
function unloadAll() {
	if ( Cult3DLoaded == false ) return;
	Cult3D.triggerEvent("unloadAll");
}

/***** ＡＡ処理の切り替え *****/
function changeAa(aa) {
	aaSel=aa;
	if ( Cult3DLoaded )	Cult3D.setAntiAliasing(aa);
}
