/**
 * MORRICH Player起動関数
 * 
 * @auther  ---
 * @version 5.0, 2005.3.14
 * @comment 有料コンテンツ直リンクフロー改造
 */

/* プレイヤー共通関数の読み込み（グローバル変数はこのJSを参照） */
document.write("<script language='JavaScript' src='/MORRICH/js/flibrary.js'></script>");

/* OCXチェックスクリプト */
var isSwfObject;
var isWmObject;
document.write("<script language='VBScript' src='/MORRICH/js/check_ocx.js'></script>");

/* ポップアップブロックエラーページ */
var mPopUpErrorURL = "/MORRICH/popup_error.html";

/**
 * 指定の直リンクを別ウィンドウで起動（SMOJサイト内での直リンク仕様）
 * @param URL			直リンクのURL
 */
function doMPlay(URL){
	var ok_url = "";
	if (URL.indexOf("?") == -1) {
		ok_url = URL + "?ref=1";
	} else {
		ok_url = URL;
	}
	doMPlayCore(ok_url);
}

/**
 * 購入ダイヤログを表示する（SMOJサイト内での直リンク仕様）
 * @param URL			直リンクのURL
 * @param cid			品番
 */
function doMPayPlay(URL, cid){
	// 視聴環境チェック
	if (getOperationType() == "win") {
		if (getBrowserType().indexOf("ie") != -1 && getBrowserType().indexOf("ie4") == -1 && getMWmVersion() && getMFlaVersion()) {
			error = 0;
		} else {
			/* japanease or english */
			if (URL.indexOf("/MORRICH/english/") != -1) { error_url = mEgErrorURL; error = 2; }
			else { error_url = mWErrorURL; error = 1; };
			
		}
	} else {
		/* japanease or english */
		if (URL.indexOf("/MORRICH/english/") != -1) { error_url = mEgErrorURL; error = 2; }
		else { error_url = mMErrorURL; error = 1; };
	}
	if (error) {
		/* japanease or english */
		if (error == 1) {
			win = window.open(mBlankURL,dwName,"width="+dwWidth+",height="+dwHeight+",toolbar=no,resizable=no,menubar=no,scrollbars=yes,status=yes");
		} else if (error == 2) {
			win = window.open(mBlankURL,owName);
		}
		win.location.href = error_url;
		return;
	}
	// 購入処理完了フラグ初期化
	var payFlg = false;
	// ISPフラグのセット（デフォルト）
	dObject.ISP = param_isp;
	// 購入ダイヤログの表示
	var strURL = "/MORRICH/common/dialog.html" + "?flg=5&cid="+cid;
	var sRet = window.showModalDialog(strURL,dObject,"resizable:no;status:no;dialogHeight:"+dwHeight+"px;dialogWidth:"+dwWidth+"px;") ;
	if( typeof( sRet ) != "undefined" ){
		if( sRet.length > 0 ){
			if (sRet.indexOf(".asf") != -1 || sRet.indexOf(".wmv") != -1 || sRet.indexOf(".asx") != -1) {
				val = "get=" + sRet;
				setCookie(cid, val, 1);
				payFlg = true;
			}
		}
	}
	// 購入が正常に完了したら、プレイヤーを起動
	if (payFlg) { doMPlay(URL); }
}

/**
 * プレイヤーの起動処理（直リンク仕様）
 * 1. このメソッドでは以下の点を判断
 *  ・配信が終了している
 *  ・座布団ページを表示させるかどうか
 * 2. プレイヤーURLをセット。このとき必ずISP情報は最後にセットする
 */
function checkPlayer(){
	playerURL = wwwRoot + playerFileName;
	var ok_url = "";
	if (! bc_status) { location.replace(mEndURL); return false; }
	if (! param_ref) { location.replace(ispBaseUrls[param_isp]); }
	if (action == 2) {		/* ConnecteD対応 */
		param_cnd = 1;
	} else {							/* ConnecteD非対応 */
		param_cnd = 0;
	}
	ok_url = playerURL
				 + "?cha=" + param_channel 
				 + "&cid=" + param_cid 
				 + "&cnd=" + param_cnd 
				 + "&cat=" + param_category 
				 + "&ovj=" + param_ovj 
				 + "&hst=" + param_hist 
				 + "&isp=" + param_isp;
	doMPlayCore(ok_url);
}

/**
 * 1. 視聴環境チェック
 * 2. 指定のURLに遷移 or 別ウィンドウで起動
 *  ・プレイヤー起動URL
 *  ・直リンクURL
 *  ・DRM発行URL
 *  ・エラー
 * @param OK_URL			指定のURL
 */
function doMPlayCore(OK_URL){
	var url = "";
	var error = 0;
	/* ポップアップブロックのチェック用 */
	if (winName != pwName && getOperationType() == "win" && getBrowserType().indexOf("ie") != -1) {
		chkWin = window.open(mBlankURL, "PopUpCheck", "width=50,height=50");
		if (chkWin != null) {
			chkWin.close();
		} else {
			if (winName == pwName) {
				window.opener.top.location.href = mPopUpErrorURL + "?err=" + error + "&url=" + escape(OK_URL);
				window.close();
			} else {
				window.top.location.href = mPopUpErrorURL + "?err=" + error + "&url=" + escape(OK_URL);
			}
			return;
		}
	}
	/* 視聴環境チェック */
	if (getOperationType() == "win") {
		if (getBrowserType().indexOf("ie") != -1 && getBrowserType().indexOf("ie4") == -1 && getMWmVersion() && getMFlaVersion()) {
			url = OK_URL;
		} else {
			/* japanease or english */
			if (OK_URL.indexOf("/MORRICH/english/") != -1) { url = mEgErrorURL; error = 2; }
			else { url = mWErrorURL; error = 1; };
			
		}
	} else {
		/* japanease or english */
		if (OK_URL.indexOf("/MORRICH/english/") != -1) { url = mEgErrorURL; error = 2; }
		else { url = mMErrorURL; error = 1; };
	}
	/* 指定のURLに遷移 */
	if (! error && winName == pwName) {
		location.href = OK_URL;
	}
	/* 別ウィンドウ起動 */
	else {
		if (error) {
			/* japanease or english */
			if (error == 1) {
				win = window.open(mBlankURL,dwName,"width="+dwWidth+",height="+dwHeight+",toolbar=no,resizable=no,menubar=no,scrollbars=yes,status=yes");
			} else if (error == 2) {
				win = window.open(mBlankURL,owName);
			}
			win.location.href = url;
		} else {
			var tmpURL = mGetDrmURL + "?" + url;
			win = window.open(tmpURL,pwName,"width="+pwWidth+",height="+pwHeight+",toolbar=no,resizable=no,menubar=no,scrollbars=no,status=no");
		}
		if (win != null) {
			//win.location.href = url;		/* プレイヤーの場合は、getDrmURLにプレイヤー遷移スクリプトを追加 */
			win.focus();
			win.moveTo(0,0);
		} else {
			window.top.location.href = mPopUpErrorURL + "?err=" + error + "&url=" + escape(OK_URL);
		}
	}
}

/**
 * Check windows media player 7.1 or more later version
 * @return returnValue		true：OK / false：NG
 */
function getMWmVersion(){
	returnValue = false;
	if (getOperationType() == "win" && getBrowserType().indexOf("ie") != -1) {
		if (isWmObject) {
			WMPObj = new ActiveXObject( "WMPlayer.OCX" );
			if (WMPObj && WMPObj.versionInfo.indexOf("7.0.0") == -1) {
				returnValue = true;
			}
		}
	} else {
		var pName_Wmt="Windows Media";
		for(i=0;i<navigator.plugins.length;i++){
			if(navigator.plugins[i].name.indexOf(pName_Wmt,0)!=-1){
				returnValue = true;
			}
		}
	}
	return returnValue;
}

/**
 * Check flash player 6 or more later version
 * @return returnValue		true：OK / false：NG
 */
function getMFlaVersion(){
	returnValue = false;
	if (getOperationType() == "win" && getBrowserType().indexOf("ie") != -1) {
		returnValue = isSwfObject;
	} else {
		var pName_Fla="Shockwave Flash 6.0";
		var pName_Fla_Core = "Shockwave Flash ";
		var pName_Fla_Ver = 0;
		for(i=0;i<navigator.plugins.length;i++){
			if(navigator.plugins[i].description.indexOf(pName_Fla_Core,0)!=-1){
				if (navigator.plugins[i].description.substring(pName_Fla_Core.length) != pName_Fla_Ver) {
					pName_Fla_Ver = navigator.plugins[i].description.substring(pName_Fla_Core.length);
				}
			}
		}
		if (parseInt(pName_Fla_Ver) >= 6) {
			returnValue = true;
		}
	}
	return returnValue;
}

