// ==== 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;
var fMaxMemPages=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) {
						var fMem=parseFloat(arrPagesRec[2].replace(",", "."));
						if (fMem>fMaxMemPages) fMaxMemPages=fMem;
						thePagesMemory.innerHTML=fMem+" (max "+fMaxMemPages+")"; // 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/(iPagesToPublish/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_content/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_content/images/genwarnsoops.gif";
	}
}
function wd_gen_PickPagesErrs(sFlowText, sStatusCode, iErrCode, sReqUrl) {
	var arrErrors=new Array();
	//
	if (sStatusCode!=="200" && sStatusCode!==200) arrErrors[new Date().valueOf()+'']="invalid server response ["+sStatusCode+"]";// while trying to receive url ["+sReqUrl+"] (<a href=\""+sPagesServerLog+"\">server log</a>)";
	// 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_content/images/generrsoops.gif";
		if (document.images['wd_pages_errs']) document.images['wd_pages_errs'].src="/webdirector/mdl_content/images/generrsoops.gif";
	}
}
function wd_gen_PagesFlow () {
	/*
	ajax callback function for navygation generation backreference
	*/
	if (thePagesFlow.Req.readyState === 3) {
		var sFlowText=thePagesFlow.Req.responseXML.text;
		//
		wd_gen_ShowPagesStatus(sFlowText, 0);
	} else if (thePagesFlow.Req.readyState === 4) {
		var sFlowText=thePagesFlow.Req.responseText;
//alert(sFlowText.substr(sFlowText.length-1024, 1024));
		//
		//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, thePagesFlow.latestUrl);
		wd_gen_PickPagesWarns(sFlowText);
		wd_gen_PickPagesStats(sFlowText);
		//
		wd_gen_ShowPagesStatus(sFlowText, 1);
		//
		if (iPagesCursor>2) {
			iPagesCursor+=Math.ceil(fPubSpeed*WD_GENQUANT_PAGES);
		} else {
			iPagesCursor+=1; // '_init1', '_init2' and 'root' were processed separately (cause of mcrsescache, htaccess, etc)
		}
		if (iPagesCursor<arrPagesToPublish.length) {
			var iTimeout=5000;
			if (typeof(fPubSpeed)!=='undefined') {
				if (fPubSpeed>=50) {
					iTimeout=1000;
				} else if (fPubSpeed>=10) {
					iTimeout=2000;
				} else if (fPubSpeed>=5) {
					iTimeout=3000;
				} else if (fPubSpeed>=3) {
					iTimeout=4000;
				}
			}
			window.setTimeout('wd_gen_StartGenPages();', iTimeout);
		} else {
			iPagesGenCompleted=1;
			wd_gen_ShowSiteStatus();
		}
	}
}
// ajax object for pages generation backreference:
var thePagesFlow=new wd_HttpReq(wd_gen_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);
	sBody+="&nosite301="+escape(iDenySite301);
	sBody+="&nodirs301="+escape(iDenyDirs301);
	sBody+="&needlastmod="+escape(iNeedLastmod);
	var iSid=iPagesCursor;
	if (iPagesCursor>2) {
		var iNextLimit=iPagesCursor+Math.ceil(fPubSpeed*WD_GENQUANT_PAGES);
	} else {
		var iNextLimit=iPagesCursor+1; // to process '_init1', '_init2' and 'root' separately (cause of mcrsescache, htaccess, etc)
	}
	if (iNextLimit>arrPagesToPublish.length) iNextLimit=arrPagesToPublish.length;
	for (iSid=iPagesCursor; iSid<iNextLimit; iSid++) {
		var sNextField=escape("sids["+(iSid-iPagesCursor)+"]")+"="+escape(arrPagesToPublish[iSid]);
		//sBody+=(sBody.length?"&":"");
		sBody+="&"+sNextField;
	}
	thePagesFlow.request('POST', "http://"+WD_HOST+"/webdirector/mdl_content/shell/genpages.php", "", "", arrHeaders, sBody);
	// timer to control theNavyFlow (ajax calls callback foo one time for each state only)
	thePagesTimer=window.setInterval('wd_gen_PagesFlow();', 500);
	//
	return 2;
}