﻿/*
広告エリア１の操作　 ( 2009.05.30 )
*/



//
//
function ad00(){				// 広告エリア１の定義
	//
	//
	this.timer01Id=0;			// このエリアの管理用のタイマー
	this.timer01Count=0;
	this.opacity01=0.0;
	//
	this.nowTimer0=500;
	this.openTimer0=15000;
	this.nowTimerCnt=0;
	this.openOpacity=1.0/(this.openTimer0/this.nowTimer0);
	//
	this.timer02Id=0;			// ＪＳＯＮのコールバック管理のタイマー
	this.nowAdCount=0;			// このカウントの一つ手前まで読み込みが完了している
	this.nowTimeout=0;
	//
	this.timer03Id=0;			// 広告表示用のタイマー
	this.timer03Stage=0;		// タイマーステージ
	this.nowDspCount=0;			// このテーブルの一つ手前までの表示が完了している
	this.nowAreaCount=0;		// これからこの枠の広告を変更する。
	//
	//
	this.areaStart=true;
	//
	//
	this.areaNo	 = 26;			// 表示枠の数
	this.srcTblNo= 52;			// リソースの読み込み枠数
	//
	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;
	this.offsetTop=100;
	this.areaWidth =kycBlogWidth-240;
	this.areaHeight=kycBlogTop-this.offsetTop;
	//
	//
	//
	this.randomComp=new Array(kycAdCount);
	//
	this.ramdomNoComp=kycAdCount;
	//
	for( i=0; i<kycAdCount; i++ ){
		this.randomComp[i]=false;
	}
	//
	//
	//
	//  メソッド
	//
	/**********************************************************
		ランダム用テーブルの番号決定
	***********************************************************/
	//
	this.selectRandom=ad00_selectRandom;
	//
	function ad00_selectRandom(){
		//
		var len=this.ramdomNoComp;
		//
		if( len<1 ){
			this.ramdomNoComp=kycAdCount;
			//
			for( i=0; i<kycAdCount; 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<kycAdCount) ){
			if(this.randomComp[i]==false){
				n++;
				if(n>=a){
					 this.ramdomNoComp--;
					 this.randomComp[i]=true;
					 r=i;
				}
			}
			i++;
		}
		//
		return( r );
		//
	}
	//
	//
	//
	/**********************************************************
		表示エリアの初期設定
	***********************************************************/
	//
	this.setArea=ad00_setArea;
	//
	function ad00_setArea(){
		//
		var pos0=new kycPosition();
		pos0.width=120;
		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;
		//
		//
		for( i=0; i<this.areaNo; i++ ){
			//
			//
			var u=0;
			//
			if( i<6 ){
				pos0.left=L0+(pos0.width+4)*i;
				pos0.top =t0;
			}
			//
			if( (i>=6)&&(i<13)){
				u=6;
				pos0.left=r0-pos0.width;
				pos0.top=t0+pos0.height*(i-(u-1));
				//alert( 't0='+t0+'  height='+pos0.height+' pos0.top='+pos0.top );
			}
			//
			if( (i>=13)&&(i<19) ){
				u=13;
				pos0.left=r0-pos0.width-(i-u)*(pos0.width+4);
				pos0.top =b0-pos0.height;
				//alert( 'b0='+b0+'  height='+pos0.height+' pos0.top='+pos0.top );
			}
			//
			if( (i>=19)&&(i<26) ){
				u=19;
				pos0.left=L0;
				pos0.top =b0-pos0.height*((i-u)+2);
			}
			//
			//
			//
			var elm0=document.getElementById('myheader');
			//
			//
			//
			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.0 );
			//
			this.backelm[i].setAttribute('id',bs);
			elm0.appendChild(this.backelm[i]);
			//
			//
		}
		//
		return;
	}

	/**********************************************************
		各タイマーの開始
	***********************************************************/
	//
	this.startTimer=ad00_startTimer;
	//
	function ad00_startTimer(){
		//
		this.timer01Count=0;
		this.opacity01=0;
		timer01Id=setTimeout( ad00_timer01CallBack,this.nowTimer0);		// 2000
		//
		timer02Id=setTimeout( ad00_timer02CallBack,500);
		//
		//++++++++??????? timer03Id=setTimeout( ad00_timer03CallBack,1);
		//
	}



	/**********************************************************************
	 *
	 *　ＪＳＯＮＰによるリクエスト
	 *
	 **********************************************************************/

	this.loadAdReq=ad00_loadAdReq;

	function ad00_loadAdReq(){
		//
		try{
			//var url = kycMainUrl+"Ad00/"+"ad_00_000_"+formatNum(4,nowCount)+".js";
			//
			//alert('Req start!!');
			var len=kycAdCount;			// 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+"Ad00/"+kycAdFname[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 loadAd(data) {
	ad0.adCmpFg[ad0.nowAdCount]=true;
	ad0.adTbl[ad0.nowAdCount]=data;
}






//
//
//
/**********************************************************

	広告表示用のタイマー

***********************************************************/
//
function ad00_timer03CallBack(){
	//
	clearTimeout( ad0.timer03Id );
	//
	var intv=200;
	//
	if( ad0.areaStart==true ){
		if( ad0.timer03Stage<2)	ad0.timer03Stage=2;
		intv=1;
	}
	//
	switch( ad0.timer03Stage ){
		case 0 :
				//
			　　ad0.timer03Stage=1;
				break;
				//
		case 1 :
				ad0.adOpacity[ad0.nowAreaCount]-=0.1;
				if(ad0.adOpacity[ad0.nowAreaCount]<0.0){
					ad0.adOpacity[ad0.nowAreaCount]=0.0;
					ad0.timer03Stage=2;
				}
				kycSetOpacity( ad0.element[ad0.nowAreaCount], ad0.adOpacity[ad0.nowAreaCount] );
				break;
				//
		case 2 : 
				var n=0;
				var fg0=false;
				var u=ad0.nowDspCount-1;
				if( u<0 ) u=kycAdCount-1;
				//
				while( (n<kycAdCount)&&(fg0==false)){
					if(ad0.adCmpFg[u]==true){
						 fg0=true;
					}
					else{
						u=u-1;
						if( u<0 ) u=kycAdCount-1;
					}
					n++;
				}
				//
				if( fg0==true ){		// 入居者が見つかった
					if( ad0.areaStart==true ){
						u=ad0.nowAreaCount%2;
					}
					//
					ad0.element[ad0.nowAreaCount].innerHTML=ad0.adTbl[u].innerHTML;
					//
					if( ad0.areaStart==false )	ad0.adCmpFg[u]=false;
					//
					if( u>=kycAdCount )		 u=0;
					//
					ad0.nowDspCount=u;
					//
					ad0.timer03Stage=3;	
					//
				}
				break;
				//
		case 3 : 						// 次のサイクルへ
				//
				if( grSeenNo01>=4 ){
					ad0.adOpacity[ad0.nowAreaCount]=1.0;
					kycSetOpacity( ad0.element[ad0.nowAreaCount], ad0.adOpacity[ad0.nowAreaCount] );
				}
				//
				ad0.nowAreaCount++;
				if( ad0.nowAreaCount>=ad0.areaNo ){
					 ad0.nowAreaCount=0;
					 ad0.areaStart=false;
				}
				//
				intv=2000;
				//
				ad0.timer03Stage=0;				// 次のサイクルへ
				//
				break;
	}
	//
	if(grSeenNo01>=4){
		ad0.timer03Id=setTimeout( ad00_timer03CallBack, intv);
	}
	//
}


//
//
//
/**********************************************************
	ＪＳＯＮのコールバック管理のタイマー
***********************************************************/
//
function ad00_timer02CallBack(){
	//
	//
	clearTimeout( ad0.timer02Id );
	//
	try{
		//
		ad0.nowTimeout++;
		//
		if(ad0.adCmpFg[ad0.nowAdCount]==true){
			//　ＪＳＯＮによる受信が完了した場合と前回の検索で空き家が
			//　見つからなかった場合にここに入ってくる。
			var n=0;
			var fg0=false;
			//
			// 空き家を探す（通常は、隣が空いているはず）
			while((n<kycAdCount)&&(fg0==false)){
				ad0.nowAdCount++;
				if( ad0.nowAdCount>=kycAdCount ) ad0.nowAdCount=0;
				if(ad0.adCmpFg[ad0.nowAdCount]==false)	fg0=true;
				n++;
			}
			//
			if(fg0==true){
				ad0.loadAdReq();		// 次のリクエスト
			}
			//
		}else{
			if(	ad0.nowTimeout>4 ){
				ad0.loadAdReq();		// タイムアウトをしたので、次のリクエストを
			}							// 次のリクエストを行う
		}
	}catch( ex ){
		alert( ex );
	}
	//
	//
	if( grSeenNo01<4 ){
		timer02Id=setTimeout( ad00_timer02CallBack,500);
	}else{
		timer02Id=setTimeout( ad00_timer02CallBack,2000);
	}
	//
}



//
//
//
/**********************************************************
	このエリアの管理用のタイマー
***********************************************************/
//
function ad00_timer01CallBack(){
	//
	clearTimeout( ad0.timer01Id );
	//
	//
	ad0.timer01Count++;
	if(ad0.timer01Count>1000) ad0.timer01Count=0;
	//
	//
	var z=grSeenNo01;
	//
	if(grSeenNo01==0){
		ad0.opacity01=ad0.opacity01+ad0.openOpacity;
		//
		if( ad0.opacity01>1.0 ){
			 ad0.opacity01=1.0;
			grSeenNo01=1;
		}
	}
	//
	//
	//
	//
	//
	//
	if(grSeenNo01==3){
		//
		if(ad0.nowAdCount>=ad0.areaNo){
			//
			//alert( 'A'+grSeenNo01 );
			while(ad0.areaStart==true){
				ad00_timer03CallBack();
			}
			//
			//alert( 'B'+grSeenNo01 );
			//
			for( i=0; i<ad0.areaNo; i++ ){
				//
				ad0.adOpacity[i]=1.0;
				kycSetOpacity( ad0.element[i], ad0.adOpacity[i] );
			}
			//
			//alert( 'C'+grSeenNo01 );
			//
			var elm0=document.getElementsByTagName('body')[0];
			// elm0.style.backgroundColor='#87ceeb';		// skyblue
			//elm0.style.backgroundColor='#9400d3';		// darkviolet
			//elm0.style.backgroundColor='#ffc0cb';		// pink
			//elm0.style.backgroundColor='#b0e0e6';		// pownderblue
			//elm0.style.backgroundColor='#ffa500';		// orange
			//elm0.style.backgroundColor='#c0c0c0';		// silver
			//
			var cc=Math.floor(Math.random()*15);
			//
			if(cc>15)	cc=15;
			elm0.style.backgroundColor=kycBkColor[cc];		//'#ffd700';
			//
			//
			//
			//alert( 'D'+grSeenNo01 );
			//
			elm0=document.getElementById('mytitle0');
			elm0.style.visibility='visible';
			//
			//alert( 'E'+grSeenNo01 );
			//
			elm0=document.getElementById('mytitle1');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'F'+grSeenNo01 );
			//
			elm0=document.getElementById('myrss0');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'G'+grSeenNo01 );
			//
			elm0=document.getElementById('myrss1');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'H'+grSeenNo01 );
			//
			elm0=document.getElementById('myreport0');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'I'+grSeenNo01 );
			//
			elm0=document.getElementById('myreport1');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'J'+grSeenNo01 );
			//
			elm0=document.getElementById('container');
			elm0.style.visibility='visible';
			//
			//
			//alert( 'K'+grSeenNo01 );
			//
			timer03Id=setTimeout( ad00_timer03CallBack,5000);
			//
			grSeenNo01=4;
			//		
			//alert( 'Z'+grSeenNo01 );
			//
		}
	}
	//
	//
	//
	if( grSeenNo01>100 )	ad0.nowTimer0=5000;
	//
	//
	//alert( ad0.areaNo );
	//
	for( i=0; i<ad0.areaNo; i++ ){
		//alert( i );
		ad0.backelm[i].style.backgroundColor=kycColor[(ad0.timer01Count+i)%16];
		if(z==0)	kycSetOpacity( ad0.backelm[i], ad0.opacity01 );
	}
	//
	timer01Id=setTimeout( ad00_timer01CallBack, ad0.nowTimer0);
	//
}




var ad0;


//
//
//
function kycAd0000Start()
{
	//
	//alert('kycAd0000Start start!!');
	//
	ad0=new ad00();
	ad0.setArea();
	ad0.startTimer();
	//
	//alert('kycAd0000Start end!!');
	//
	return;
}
