var naLib= new naLib();
var cTtc_swf= new cTtc_swf();

naLib.append_event(window, 'load', cTtc_swf.init);

function externalCall(vle){
	var func= vle.split(";")[0];
	var value= vle.split(";")[1];
	
	switch(func){
		case "getViewType": cTtc_swf.setViewType(); break;
		case "setWindowH": cTtc_swf.setWindowH(value); break;
		case "selectedHorse": cTtc_swf.selectedHorse(value); break;
		case "alert": alert(value); break;
		case 'locate':cTtc_swf.locate(value); break;
		
	}
}

function cTtc_swf(){
	var _this=this;
	var viewType;
	
	this.init=function(){
		
		_this.viewType = _this.getViewType();
		
		//Flashを貼り付け
		var flashStatus={};
		flashStatus.path= "swf/flash_ttc.swf";
		flashStatus.width= "100%";
		flashStatus.height= "100%";
		flashStatus.id= "swf1";
		flashStatus.fvs= "&pageNo="+ naLib.$("PAGE_NO").value +"&viewType="+_this.viewType;
		
		//Flashのバージョンを調べる
		if( naLib.get_flash_version() >= 8){
			naLib.$("DIV_SWF").innerHTML = _this.setDivSwf(flashStatus);
			naLib.$("DIV_SWF").style.height = "600px";
			
			//ボタンイベントを追加
			//naLib.append_event(naLib.$("BTN_FARM_VIEW"), 'click', _this.changeSwfViewToBokujo);
			//naLib.append_event(naLib.$("BTN_DATA_VIEW"), 'click', _this.changeSwfViewToData);
			
		}else{
			naLib.$("DIV_SWF").innerHTML =_this.getNoFlashHtml();
		}
	}

	this.setDivSwf=function(flashStatus){
		var inner_html="";
		inner_html +="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'";
		inner_html +=" codebase='https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab'";
		inner_html +=" id='" +flashStatus.id +"'";
		inner_html +=" width='" +flashStatus.width +"'";
		inner_html +=" height='" +flashStatus.height +"'>";
		
		inner_html +="<param name='allowscriptaccess' value='sameDomain'>";//非ローカル
		//inner_html +="<param name='allowScriptAccess' value='always'>";//ローカル
		
		inner_html +="<param name='movie' value='" +flashStatus.path +"'>";
		inner_html +="<param name='quality' value='high'>";
		if(flashStatus.fvs ) inner_html +="<param name='FlashVars' value='" + flashStatus.fvs +"'>";
		inner_html +="<embed src='" +flashStatus.path +"'";
		
		inner_html +=" allowscriptaccess='sameDomain'";//非ローカル
		//inner_html +=" allowscriptaccess='always'";//ローカル
		
		inner_html +=" name='" +flashStatus.id +"'";
		inner_html +=" quality='high'";
		inner_html +=" width='" + flashStatus.width +"'";
		inner_html +=" height='" + flashStatus.height +"'";
		inner_html +=" type='application/x-shockwave-flash'";
		if(flashStatus.fvs ) inner_html +=" FlashVars='" + flashStatus.fvs +"'";
		inner_html +=" pluginspage='https://www.macromedia.com/go/getflashplayer' >";
		inner_html +="</embed>";
		inner_html +="</object>";
		
		return inner_html;
	}
	
	this.getNoFlashHtml=function(){
		var swfHtml="";
		var imgPath = "/common/images/";
		
		var width= "100%";
		var height= "600";
		swfHtml += '<div id="getFlash" style="width:' + width + 'px; height:' + (height - Math.round(height / 2 - 50)) + 'px; padding-top:' + Math.round(height / 2 - 50) + 'px;">';
		
		swfHtml += '<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&Lang=Japanese&promoid=BPCKX">';
		swfHtml += '<img src="' + imgPath + 'flash_dl.png" width="158" height="39" alt="Get ADOBE Flash Player"><br /><br />';
		swfHtml += '最新のFlash Playerをインストールしてください。';
		swfHtml += '</a>';
		swfHtml += '</div>';
		return swfHtml;
	}
	
	this.setWindowH=function(value){
		naLib.$("DIV_SWF").style.height = value + "px";
	}
	
	this.locate=function(url){
		window.location.href=url;
	}
	
	this.selectedHorse=function(value){//http://tokyo-tc.com/
		//window.location.href="http://tokyo-tc.com/syozoku/"+value+"/";
		window.location.href="syozoku/"+value+"/";
	}
	
	this.changeSwfViewToBokujo=function(){
		_this.setUrlValue("farm");
		naLib.$("BTN_FARM_VIEW").className = "on";
		naLib.$("BTN_DATA_VIEW").className = "";
		
	}
	
	this.changeSwfViewToData=function(){
		_this.setUrlValue("data");
		naLib.$("BTN_FARM_VIEW").className = "";
		naLib.$("BTN_DATA_VIEW").className = "on";
	}
	
	this.updateSwfStatus=function(value){
		document.swf1.commUpdtFromJS(value);
	}
	
	this.setUrlValue=function(value){
		if(_this.viewType == value)return;
		
		var ar =document.URL.split("?");
		//alert(ar[0]+"?v="+value);
		window.location.href= ar[0]+"?v="+value;
		return false;
	}
	
	this.getViewType=function(){
		var ar =document.URL.split("?");
		
		if(ar.length <= 1){
			var value="farm";
			_this.setUrlValue(value);
		}else{
			var value= naLib.getURLValue('v');
		}
		
		switch(value){
			case "farm":{
				naLib.$("BTN_FARM_VIEW").className="on";
				naLib.$("BTN_DATA_VIEW").className="";
			}break;
			case "data":{
				naLib.$("BTN_FARM_VIEW").className="";
				naLib.$("BTN_DATA_VIEW").className="on";
			}break;
		}
		return value;
	}
}

/* Version 8.10b */
function naLib(){
	var _this=this;
	
	this.append_event= function(e, type, handler){
		if (e.addEventListener) e.addEventListener(type, handler, false);
		else e.attachEvent('on' + type, handler);
	}
	
	this.remove_event=function(eventTarget, eventType, eventListener){
		if(eventTarget.removeEventListener){
			eventTarget.removeEventListener(eventType, eventListener, false);
		}else if(eventTarget.detachEvent){
			eventTarget.detachEvent("on"+eventType, eventListener);
		}
	}

	this.loadJsDynamically= function( url , charset){
		var script  = document.createElement( 'script' );
		script.type = 'text/javascript';
		script.src  = url;
		if(charset)script.charset = charset;
		var head    = document.getElementsByTagName( 'head' )[0];
		head.appendChild( script );
	}

	this.$= function(id){
		return document.getElementById(id);
	}

	this.getURLValue= function(elm, key){
		if(! key) var key="?";
		var ar= (location.href).split(key);
		if(ar.length <= 1) return null;
		var ob= _this.parseToGetStr(ar[1]);
		return ob[elm];
	}
	
	this.parseToGetStr= function(str){
		var ob= {};
		var ar= str.split("&");
		for(var i in ar){
			ar2= ar[i].split("=");
			ob[ar2[0]]= ar2[1];
		}
		return ob;
	}

	this.sendAndloadText= function(strReqURI, strReqMethod, strReqQueries, strOnLoadFunc){
		try {var xmlhttp =new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try {var xmlhttp =new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
				if(typeof XMLHttpRequest != "undefined"){
					var xmlhttp = new XMLHttpRequest();
				}
			}
		}
		var thisObj=this;
		if(xmlhttp){
			xmlhttp.onreadystatechange =function(){
				if(xmlhttp.readyState < 4) thisObj.confLoginResponse('loading:' +xmlhttp.readyState, '', '', strOnLoadFunc);
				if(xmlhttp.readyState == 4){
					var strResTxt, strResHeader;
					if(xmlhttp.status == 200){
						strResTxt =xmlhttp.responseText;
						strResHeader =xmlhttp.getAllResponseHeaders();
					}
					if(xmlhttp.status == 404) strResTxt =null;
					if(strOnLoadFunc) thisObj.confLoginResponse('completed:' +xmlhttp.status, strResHeader, strResTxt, strOnLoadFunc);
				}
			};
			xmlhttp.open(strReqMethod, strReqURI, true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.send(strReqQueries);
		}
	}

	this.confLoginResponse= function(ldSt, resHd, rsp, strOnLoadFunc){
		if(ldSt == "completed:200"){
			if(rsp){
				strOnLoadFunc(rsp);
			}
		}
	}
	
	this.strReplace= function (before, affter, str){
		var ar= str.split(before);
		return ar.join(affter);
	}
	
	
	this.getStrOfEmbedForSwf= function(flashStatus){
		var inner_html="";
		inner_html +="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'";
		inner_html +=" codebase='https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab'";
		inner_html +=" id='" +flashStatus.id +"'";
		inner_html +=" width='" +flashStatus.width +"'";
		inner_html +=" height='" +flashStatus.height +"'>";
		inner_html +="<param name='allowScriptAccess' value='always'>";
		inner_html +="<param name='movie' value='" +flashStatus.path +"'>";
		inner_html +="<param name='quality' value='high'>";
		if(flashStatus.fvs ) inner_html +="<param name='FlashVars' value='" + flashStatus.fvs +"'>";
		inner_html +="<embed src='" +flashStatus.path +"'";
		inner_html +=" allowscriptaccess='always'";
		inner_html +=" name='" +flashStatus.id +"'";
		inner_html +=" quality='high'";
		inner_html +=" width='" + flashStatus.width +"'";
		inner_html +=" height='" + flashStatus.height +"'";
		inner_html +=" type='application/x-shockwave-flash'";
		if(flashStatus.fvs ) inner_html +=" FlashVars='" + flashStatus.fvs +"'";
		inner_html +=" pluginspage='https://www.macromedia.com/go/getflashplayer' >";
		inner_html +="</embed>";
		inner_html +="</object>";
		return inner_html;
	}
	
	this.get_flash_version=function() {
		var result;
		var BT= _this.getBrowserType();
		
		var hasFlashPlagin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
		if (hasFlashPlagin) {
			result = parseInt(hasFlashPlagin.description.substring(hasFlashPlagin.description.indexOf("h ")+2 , hasFlashPlagin.description.indexOf(".")));
		} else if (BT.isWinIE) {
			try {
				hasFlashPlagin = Math.floor(new ActiveXObject("ShockwaveFlash.ShockwaveFlash").FlashVersion() / 0x10000);
				result= parseInt(hasFlashPlagin)
			} catch (e) {
						chkResult = false;
				}
		}
		return result;
	}
	
	this.getBrowserType=function(){
		var ob={};
		ob.isSafari  = (navigator.appVersion.indexOf("Safari") != -1) ? true : false;
		ob.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
		ob.isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
		ob.isWinIE = (ob.isIE == true && ob.isWin == true) ? true : false;
		ob.isMacSafari = (ob.isSafari == true && ob.isWin == false) ? true : false;
		
		return ob;
	}
	
}