//==============================================================================
//var theFlushArea=null; var theFlushArea2=null;
var theStartDate=new Date();
var iClientStart=theStartDate.valueOf();
// ==== site ===================================================================
var theSiteTime=null; var theSiteBar=null;
var iSiteLatest=0;
function wd_gen_ShowSiteStatus() {
	var thePagesBar=document.getElementById("wd_genbar_pages");
	if (thePagesBar && theSiteBar) {
		var iPages=parseInt(thePagesBar.style.width);
		var iPercents=Math.ceil((iPages*iPercPages)/100);
		if (iPercents>100) iPercents=100;
		if (iPercents!==iSiteLatest) {
			var sPercents=iPercents+"%";
			theSiteBar.style.width=sPercents;
			theSiteBar.innerHTML=sPercents;
			theSiteBar.title=" "+sPercents+" ";
			iSiteLatest=iPercents;
		}
	}
	//
	if (theSiteTime) {
		var theCurrDate=new Date();
		var iClientCurr=Math.ceil((theCurrDate.valueOf()-iClientStart)/1000);
		theSiteTime.innerHTML=iClientCurr;
	}
	//
	if (iPagesGenCompleted) {
		if (document.getElementById('wd_result_pgsgened')) document.getElementById('wd_result_pgsgened').innerHTML=thePagesCurract.innerHTML;
		if (document.getElementById('wd_result_srvtime')) document.getElementById('wd_result_srvtime').innerHTML=(parseInt(thePagesTime.innerHTML))+" "+LBL_SEC;
		if (document.getElementById('wd_result_clnttime')) document.getElementById('wd_result_clnttime').innerHTML=parseInt(theSiteTime.innerHTML)+" "+LBL_SEC;
		if (document.getElementById('wd_result_title')) document.getElementById('wd_result_title').innerHTML=LBL_SSRCH_IDXING_COMPLETED;
		if (document.getElementById('wd_genbar_site_title')) {
			document.getElementById('wd_genbar_site_title').innerHTML=LBL_SSRCH_IDXING_COMPLETED.toLowerCase();
		}
		//
		var theResulter=document.getElementById('wd_publish_result_text');
		var bWereErrors=0;
		if (theResulter) {
			var sPagesResult='';
			var sErrHash='';
			for (sErrHash in arrPagesErrors) {
				sPagesResult+="<SPAN STYLE=\"color:#990000;\">&bull;&nbsp;"+LBL_SSRCH_IDXING_WEREERRS+"</SPAN>";
				bWereErrors=1;
				break;
			}
			if (!sPagesResult.length) sPagesResult+="<SPAN STYLE=\"color:#006600;\">&bull;&nbsp;"+LBL_SSRCH_IDXING_NOERRS+"</SPAN>";
			theResulter.innerHTML=sPagesResult;
		}
		//
		if (bWereErrors) {
			if (document.images['wd_genimage']) document.images['wd_genimage'].src="/webdirector/mdl_ssearch/images/genoops.gif";
			//document.getElementById('wd_result_repeatbtt').style.display="";
		} else {
			if (document.images['wd_genimage']) document.images['wd_genimage'].src="/webdirector/mdl_ssearch/images/genok.gif";
		}
		if (document.getElementById('wd_result_exitbtt')) document.getElementById('wd_result_exitbtt').style.display="";
	}
}
// ==== pages ==================================================================
var iPagesGenCompleted=0;
//
var arrPagesWarnings=new Array();
var arrPagesErrors=new Array();
//
var thePagesTimer=null;
//
var thePagesCurract=null; var thePagesCurracttext=null;
var thePagesMemory=null; var thePagesTime=null; var thePagesBar=null;
var thePagesProcessor=null;
var sPagesLatestRec="";
//
var iPagesCursor=0;
var iPagesTime=0;
//
var fMaxProcPages=0;
//
function wd_gen_ShowPagesStatus(sFlowText, bPartFin) {
	var iPagesRecStart=sFlowText.lastIndexOf('pages rec ');
	if (iPagesRecStart!==-1) {
		var sPagesRec=sFlowText.substr(iPagesRecStart+10); // 10 = 'pages rec '.length
		var iPagesRecFin=sPagesRec.indexOf(' recfin');
		if (iPagesRecFin!==-1) {
			sPagesRec=sPagesRec.substr(0, iPagesRecFin);
			if (sPagesRec!==sPagesLatestRec) {
				var arrPagesRec=sPagesRec.split('::');
				if (arrPagesRec.length>5) {
					if (thePagesCurracttext) thePagesCurracttext.innerHTML=arrPagesRec[5]; // sMessage
					var iGened=thePagesCurract.innerHTML=iPagesCursor+parseInt(arrPagesRec[1]);
					if (thePagesCurract) thePagesCurract.innerHTML=iGened; // sGened
					if (thePagesMemory) thePagesMemory.innerHTML=arrPagesRec[2]; // sMemory
					if (thePagesTime) {
						var iTime=parseInt(arrPagesRec[3]);
						thePagesTime.innerHTML=(iPagesTime+iTime); // sTime
						if (bPartFin) iPagesTime+=iTime;
					}
					if (thePagesProcessor) {
						var fProcessor=parseFloat(arrPagesRec[4].replace(",", "."));
						if (fProcessor>fMaxProcPages) fMaxProcPages=fProcessor;
						thePagesProcessor.innerHTML=fProcessor+" (max "+fMaxProcPages+") %"; // processor usage
					}
					if (thePagesBar) {
						var iPercent=Math.ceil(iGened/(iPagesToIdx/100));
						thePagesBar.style.width=iPercent+"%"; // sPercent
						thePagesBar.innerHTML=iPercent+"%"; // sPercent
						thePagesBar.title=iPercent+"%"; // sPercent
						//
						if (iPercent>=100) thePagesCurracttext.innerHTML=LBL_PROCCOMPLSUCC;
					}
				}
				wd_gen_ShowSiteStatus();
				sPagesLatestRec=sPagesRec;
			}
		}
	}
}
var arrPagesStats=new Array();
function wd_gen_PickPagesStats(sFlowText) {
	// hash::gened::memory::time::cpu::message
	var arrMsgs=wd_PickupWDRecs(sFlowText, 'pages rec ', ' recfin', '<tr><td>%1%</td><td>%3% sec</td><td>%f4% %</td><td>%f2% M</td><td>%5%</td></tr>');
	var sHash="";
	for (sHash in arrMsgs) {
		arrPagesStats[sHash]=arrMsgs[sHash];
		if (document.images['wd_pages_stats']) document.images['wd_pages_stats'].src="/webdirector/mdl_ssearch/images/genstatsoops.gif";
	}
}
function wd_gen_PickPagesWarns(sFlowText) {
	// hash::errno::errstr::errfile::errline
	var arrWarns=wd_PickupWDRecs(sFlowText, 'warning rec ', ' recfin', '%3% %4%<br/>%2%');
	var sHash="";
	for (sHash in arrWarns) {
		arrPagesWarnings[sHash]=arrWarns[sHash];
		if (document.images['wd_pages_warns']) document.images['wd_pages_warns'].src="/webdirector/mdl_ssearch/images/genwarnsoops.gif";
	}
}
function wd_gen_PickPagesErrs(sFlowText, sStatusCode, iErrCode) {
	var arrErrors=new Array();
	//
	if (sStatusCode!=="200" && sStatusCode!==200) arrErrors[new Date().valueOf()+'']="invalid server response "+sStatusCode;
	// hash::errno::errstr::errfile::errline
	arrErrors=wd_Concat(arrErrors, wd_PickupWDRecs(sFlowText, 'error rec ', ' recfin', '%3% %4%<br/>%2%'));
	//
	arrErrors=wd_Concat(arrErrors, wd_PickupRecs(sFlowText, '<b>Parse error</b>:', '<br />'));
	arrErrors=wd_Concat(arrErrors, wd_PickupRecs(sFlowText, 'Parse error:', '\n'));
	arrErrors=wd_Concat(arrErrors, wd_PickupRecs(sFlowText, 'Fatal error:', '\n'));
	var sHash="";
	for (sHash in arrErrors) {
		arrPagesErrors[sHash]=arrErrors[sHash];
		if (document.images['wd_pages_errs']) document.images['wd_pages_errs'].src="/webdirector/mdl_ssearch/images/generrsoops.gif";
		if (document.images['wd_pages_errs']) document.images['wd_pages_errs'].src="/webdirector/mdl_ssearch/images/generrsoops.gif";
	}
}
function wd_idx_PagesFlow () {
	/*
	ajax callback function for indexing backreference
	*/
	if (thePagesFlow.Req.readyState === 3) {
		var sFlowText=thePagesFlow.Req.responseXML.text;
		//
		wd_gen_ShowPagesStatus(sFlowText, 0);
	}
	if (thePagesFlow.Req.readyState === 4) {
		var sFlowText=thePagesFlow.Req.responseText;
		//
		wd_gen_ShowPagesStatus(sFlowText, 1);
		//
		var sStatusCode=thePagesFlow.Req.status;
		var iErrCode=thePagesFlow.Req.responseXML.parseError.errorCode;
		//
		if (thePagesTimer) {
			window.clearInterval(thePagesTimer);
			thePagesTimer=null;
		}
		//
		sPagesLatestRec="";
		thePagesFlow.Req.abort();
		//
		wd_gen_PickPagesErrs(sFlowText, sStatusCode, iErrCode);
		wd_gen_PickPagesWarns(sFlowText);
		wd_gen_PickPagesStats(sFlowText);
		//
		iPagesCursor+=Math.ceil(fPubSpeed*WD_STEPQUANT_PAGES);
		if (iPagesCursor<arrPagesToIdx.length) {
			wd_gen_StartGenPages();
		} else {
			iPagesGenCompleted=1;
			wd_gen_ShowSiteStatus();
		}
	}
}
// ajax object for pages generation backreference:
var thePagesFlow=new wd_HttpReq(wd_idx_PagesFlow, 1); // async
function wd_gen_StartGenPages() {
	// prepare pages generation backreference:
	thePagesCurract=document.getElementById('wd_pages_curraction');
	thePagesCurracttext=document.getElementById('wd_pages_curraction_text');
	thePagesMemory=document.getElementById('wd_pages_memory');
	thePagesProcessor=document.getElementById('wd_pages_processor');
	thePagesTime=document.getElementById('wd_pages_time');
	thePagesBar=document.getElementById('wd_genbar_pages');
	//
	if (!theSiteTime) theSiteTime=document.getElementById('wd_site_time');
	if (!theSiteBar) theSiteBar=document.getElementById('wd_genbar_site');
	// starting pages generation
	var arrHeaders=new Array();
	arrHeaders['Accept-Encoding']=" ";
	arrHeaders['Content-Type']="application/x-www-form-urlencoded";
	var sBody="speed="+escape(fPubSpeed);
	var iSid=iPagesCursor;
	var iNextLimit=iPagesCursor+Math.ceil(fPubSpeed*WD_STEPQUANT_PAGES);
	if (iNextLimit>arrPagesToIdx.length) iNextLimit=arrPagesToIdx.length;
	for (iSid=iPagesCursor; iSid<iNextLimit; iSid++) {
		var sNextField=escape("uids["+(iSid-iPagesCursor)+"]")+"="+escape(arrPagesToIdx[iSid]);
		//sBody+=(sBody.length?"&":"");
		sBody+="&"+sNextField;
	}
	sBody+="&latestuid="+escape(arrPagesToIdx[arrPagesToIdx.length-1]);
	sBody+="&allquant="+escape(arrPagesToIdx.length);
	thePagesFlow.request('POST', "http://"+WD_HOST+"/webdirector/mdl_ssearch/shell/idxpages.php", "", "", arrHeaders, sBody);
	// timer to control flow (ajax calls callback foo one time for each state only)
	thePagesTimer=window.setInterval('wd_idx_PagesFlow();', 500);
	//
	return 2;
}