﻿/*
広告エリア２の操作　 ( 2009.06.20 )
*/



//
//
function ad01(){				// 広告エリア２の定義
	//
	//
	this.timer01Id=0;			// このエリアの管理用のタイマー
	this.timer01Count=0;
	this.opacity01=0.0;
	//
	this.timer02Id=0;			// ＪＳＯＮのコールバック管理のタイマー
	this.nowAdCount=0;			// このカウントの一つ手前まで読み込みが完了している
	this.nowTimeout=0;
	//
	this.timer03Id=0;			// 広告表示用のタイマー
	this.timer03Stage=0;		// タイマーステージ
	this.nowDspCount=0;			// このテーブルの一つ手前までの表示が完了している
	this.nowAreaCount=0;		// これからこの枠の広告を変更する。
	//
	//
	this.areaNo	 = 3;			// 表示枠の数
	this.srcTblNo= 6;			// リソースの読み込み枠数
	//
	this.areaStart=true;
	//
	this.adTbl  =new Array( this.srcTblNo );	// 広告の読み込み枠
	this.adCmpFg=new Array( this.srcTblNo );	// 広告の読み込み済みフラグ
	//
	this.adOpacity=new Array( this.areaNo );	// 広告枠の透過度
	//
	for( i=0; i<this.areaNo; i++ ){
		this.adOpacity[i]=0.0;				// 広告枠の透過度
	}
	//
	for( i=0; i<this.srcTblNo; i++ ){
		this.adCmpFg[i]=false;				// 広告の読み込み済みフラグ
	}
	//
	this.element=new Array(this.areaNo);
	this.backelm=new Array(this.areaNo);
	//
	this.offsetLeft=kycBlogLeft+200+120;
	this.offsetTop=100+60;
	this.areaWidth =120*4+4*5;
	this.areaHeight=60*3-26;
	//
	//
	//
	//
	//
	this.randomComp=new Array(kycAdCount1);
	//
	this.ramdomNoComp=kycAdCount1;
	//
	for( i=0; i<kycAdCount1; i++ ){
		this.randomComp[i]=false;
	}
	//
	//
	//  メソッド
	//
	//
	//
	/**********************************************************
		ランダム用テーブルの番号決定
	***********************************************************/
	//
	this.selectRandom=ad01_selectRandom;
	//
	function ad01_selectRandom(){
		//
		var len=this.ramdomNoComp;
		//
		if( len<1 ){
			this.ramdomNoComp=kycAdCount1;
			//
			for( i=0; i<kycAdCount1; i++ ){
				this.randomComp[i]=false;
			}
			//
			len=this.ramdomNoComp;
			//
		}
		//
		var a = Math.floor(Math.random()*len)+1;
		//
		//
		var i=0;
		var n=0;
		var r=0;
		//
		while( (n<a)&&(i<kycAdCount1) ){
			if(this.randomComp[i]==false){
				n++;
				if(n>=a){
					 this.ramdomNoComp--;
					 this.randomComp[i]=true;
					 r=i;
				}
			}
			i++;
		}
		//
		return( r );
		//
	}
	//
	//
	//
	/**********************************************************
		表示エリアの初期設定
	***********************************************************/
	//
	this.setArea=ad01_setArea;
	//
	function ad01_setArea(){
		//
		var pos0=new kycPosition();
		pos0.width=468;
		pos0.height=60;
		//
		w0=6;
		h0=10;
		//
		L0=this.offsetLeft;
		t0=this.offsetTop;
		r0=this.offsetLeft+(pos0.width+4)*(w0-1)+pos0.width;
		b0=t0+pos0.height*h0-pos0.height;
		//
		//
		//
		//
		var elm0=document.getElementById('myheader');
		//
		//
		//
		for( i=0; i<this.areaNo; i++ ){
			//
			switch( i ){
				case 0 : pos0.left=this.offsetLeft+18;
						 pos0.top=this.offsetTop+12;
						 break;
						//
				case 1 : pos0.left=this.offsetLeft+18;
						 pos0.top=this.offsetTop+12+60+12;
						 break;
						// 
				case 2 : pos0.left=this.offsetLeft+120*5+25-468-5;
						 pos0.top=36;
						 break;
			}
			//
			var ss='myadarea'+kycFormatNum(4,i);
			this.element[i]=kycMakeElement('div',pos0);
			this.element[i].style.backgroundColor="#ffffff";
			//
			this.element[i].style.zIndex=20;
			kycSetOpacity( this.element[i], 0.0 );
			//
			this.element[i].setAttribute('id',ss);
			elm0.appendChild(this.element[i]);
			//
			var bs='myadback'+kycFormatNum(4,i);
			this.backelm[i]=kycMakeElement('div',pos0);
			this.backelm[i].style.backgroundColor=kycColor[i%16];
			//
			this.backelm[i].style.zIndex=10;
			kycSetOpacity( this.backelm[i], 0.5 );
			//
			this.backelm[i].setAttribute('id',bs);
			elm0.appendChild(this.backelm[i]);
			//
			//
		}
		//
		return;
	}

	/**********************************************************
		各タイマーの開始
	***********************************************************/
	//
	this.startTimer=ad01_startTimer;
	//
	function ad01_startTimer(){
		//
		this.timer01Count=0;
		this.opacity01=0;
		timer01Id=setTimeout( ad01_timer01CallBack,2000);
		//
		timer02Id=setTimeout( ad01_timer02CallBack,1000);
		//
		timer03Id=setTimeout( ad01_timer03CallBack,3000);
		//
	}



	/**********************************************************************
	 *
	 *　ＪＳＯＮＰによるリクエスト
	 *
	 **********************************************************************/

	this.loadAdReq=ad01_loadAdReq;

	function ad01_loadAdReq(){
		//
		try{
			//
			//alert('Req start!!');
			var len=kycAdCount1;			// kycAdFname.length;
			//alert('len='+len);
			//var a = Math.floor(Math.random()*len);
			var a=this.selectRandom();
			if(a>=len) a=len-1;
			var url = kycMainUrl+"Ad01/"+kycAdFname1[a];
			//alert('url='+url );
			//
			this.nowTimeout=0;
			//
			var script = document.createElement('script');
			script.setAttribute('src', url);
			document.getElementById('myheader').appendChild(script);
		}catch( ex ){
			alert( ex );
		}
		//
	}
}
//
//
//



/**********************************************************************
 *
 *　ＪＳＯＮＰからのコールバック
 *
 **********************************************************************/
//　コールバック：
//  広告パーツ部分を受け取った
//
function loadAd01(data) {
	//alert('Receive');
	ad1.adCmpFg[ad1.nowAdCount]=true;
	ad1.adTbl[ad1.nowAdCount]=data;
}






//
//
//
/**********************************************************

	広告表示用のタイマー

***********************************************************/
//
function ad01_timer03CallBack(){
	//
	clearTimeout( ad1.timer03Id );
	//
	var intv=200;
	//
	switch( ad1.timer03Stage ){
		case 0 :
				//
			　　ad1.timer03Stage=1;
				break;
				//
		case 1 :
				ad1.adOpacity[ad1.nowAreaCount]-=0.1;
				if(ad1.adOpacity[ad1.nowAreaCount]<0.0){
					ad1.adOpacity[ad1.nowAreaCount]=0.0;
					ad1.timer03Stage=2;
				}
				kycSetOpacity( ad1.element[ad1.nowAreaCount], ad1.adOpacity[ad1.nowAreaCount] );
				break;
				//
		case 2 : 
				var n=0;
				var fg0=false;
				var u=ad1.nowDspCount-1;
				if( u<0 ) u=kycAdCount1-1;
				//
				while( (n<kycAdCount1)&&(fg0==false)){
					if(ad1.adCmpFg[u]==true){
						 fg0=true;
					}
					else{
						u=u-1;
						if( u<0 ) u=kycAdCount1-1;
					}
					n++;
				}
				//
				if( fg0==true ){		// 入居者が見つかった
					ad1.element[ad1.nowAreaCount].innerHTML=ad1.adTbl[u].innerHTML;
					ad1.adCmpFg[u]=false;
					u=u+1;
					if( u>=kycAdCount1 ) u=0;
					ad1.nowDspCount=u;
					//
					ad1.timer03Stage=3;	
					//
				}
				break;
				//
		case 3 : 						// 次のサイクルへ
				//
				ad1.adOpacity[ad1.nowAreaCount]=1.0;
				kycSetOpacity( ad1.element[ad1.nowAreaCount], ad1.adOpacity[ad1.nowAreaCount] );
				ad1.nowAreaCount++;
				if( ad1.nowAreaCount>=ad1.areaNo ){
					 ad1.nowAreaCount=0;
					ad1.areaStart=false;
				}
				//
				if( ad1.areaStart==false )	intv=60000;
				else						intv=3000;
				//
				ad1.timer03Stage=0;				// 次のサイクルへ
				//
				break;
	}
	//
	ad1.timer03Id=setTimeout( ad01_timer03CallBack, intv);
	//
}


//
//
//
/**********************************************************
	ＪＳＯＮのコールバック管理のタイマー
***********************************************************/
//
function ad01_timer02CallBack(){
	//
	//
	clearTimeout( ad1.timer02Id );
	//
	try{
		//
		ad1.nowTimeout++;
		//
		if(ad1.adCmpFg[ad1.nowAdCount]==true){
			//　ＪＳＯＮによる受信が完了した場合と前回の検索で空き家が
			//　見つからなかった場合にここに入ってくる。
			var n=0;
			var fg0=false;
			//
			// 空き家を探す（通常は、隣が空いているはず）
			while((n<kycAdCount1)&&(fg0==false)){
				ad1.nowAdCount++;
				if( ad1.nowAdCount>=kycAdCount1 ) ad1.nowAdCount=0;
				if(ad1.adCmpFg[ad1.nowAdCount]==false)	fg0=true;
				n++;
			}
			//
			if(fg0==true){
				ad1.loadAdReq();		// 次のリクエスト
			}
			//
		}else{
			if(	ad1.nowTimeout>2 ){
				ad1.loadAdReq();		// タイムアウトをしたので、次のリクエストを
			}							// 次のリクエストを行う
		}
	}catch( ex ){
		alert( ex );
	}
	//
	//
	timer02Id=setTimeout( ad01_timer02CallBack,1000);
	//
}



//
//
//
/**********************************************************
	このエリアの管理用のタイマー
***********************************************************/
//
function ad01_timer01CallBack(){
	//
	clearTimeout( ad1.timer01Id );
	//
	//
	ad1.timer01Count++;
	//
	if(ad1.timer01Count>1000) ad1.timer01Count=0;
	//
	ad1.opacity01=ad1.opacity01+0.1;
	//
	if( ad1.opacity01>1.0 ) ad1.opacity01=1.0;
	//
	//
	//alert( ad1.areaNo );
	//
	for( i=0; i<ad1.areaNo; i++ ){
		//alert( i );
		ad1.backelm[i].style.backgroundColor=kycColor[(ad1.timer01Count+i)%16];
		kycSetOpacity( ad1.backelm[i], ad1.opacity01 );
	}
	//
	timer01Id=setTimeout( ad01_timer01CallBack,2000);
	//
}




var ad1;


//
//
//
function kycAd0001Start()
{
	//
	//alert('kycad0100Start start!!');
	//
	ad1=new ad01();
	ad1.setArea();
	ad1.startTimer();
	//
	//alert('kycad0100Start end!!');
	//
	return;
}
