/*---------------------------------------


2011/12/10

Use the load function of jquery to scan #main on each page.

Scans array pfile in search_data_edit.js.

------------------------------------------*/

var loaded
function cload(mode) {

	if(mode==0){
		if(loaded==true){
			cancel_load()
		}else{
			if(window.confirm('扫描所有章节。操作可能会变慢。是否还要执行？')){
				loaded=true;
				$("#load-bt").html("Stop scanning.");
				$("#load-bt").css("color","#fff");
				$("#percent").show("slow");
				$("#percent").css("background-image","url(../images/loading.gif)");
				loadpagesall()
			}	
		}
	}else{
		if(loaded==true){
			cancel_load()
		}else{
			if(window.confirm('扫描本章。操作可能会变慢。是否还要执行？')){
				loaded=true;
				$("#load-bt").html("Stop scanning.");
				$("#load-bt").css("color","#fff");
				$("#percent").show("slow");
				$("#percent").css("background-image","url(../images/loading.gif)");
				loadpages(mode)
			}	
		}		
	}
}


function formatNum(keta, num) {
  var src = new String(num);
  var cnt = keta - src.length;
  if (cnt <= 0) return src;
  while (cnt-- > 0) src = "0" + src; return src;
}

function loadpagesall(u) {	
	var n = 1
	var loopNum=pfile.length-1;
	
	var k = 1
	setTimeout(function callback(){

		if(!(n<=loopNum)) return printbt();
		
		a=formatNum(3,n)
		idName=pfile[n].replace(".htm","")

			$("#scroll-area").append("<div id='load"+ idName +"' class='cont'></div>");
			$("#load"+ idName +"").load(""+ idName +".htm #main",function() {
				$("#percent-box p").html("<p>All "+loopNum+" file:"+parseInt(k/loopNum*100)+"%<\/p>");
				//$("#percent").css("width",parseInt(k/loopNum*100)+"%");
				$("#load"+ idName +"").append("<p class='page-file-name'>"+idName+".htm</p>");
			});	

		k++;
		n++;
		setTimeout(callback);
	 })	
}

function loadpages(mode) {
	var n = 0
	var loopNum=pfile.length-1;
	var cloopNum=0
	var loopcategoryNo = "0"+ mode + "-"
	//alert(loopcategoryNo)
	cloopFile = new Array();
	
	for (i=0;i<=loopNum;i++){	
		idName=pfile[i].replace(".htm","")
		//document.write(idName+"("+i+")"+"<br />")
		if(idName.indexOf(loopcategoryNo)!=-1){
			cloopNum=cloopNum+1
			cloopFile[cloopNum]=i
		}
	}

	n = cloopFile[1]
	loopNum = cloopFile[1]+cloopNum-1
	//alert(n)
	//alert(loopNum)
	var k = 0
	setTimeout(function callback(){

		if(!(n<=loopNum)) return printbt();
		
		a=formatNum(3,n)
		idName=pfile[n].replace(".htm","")
		if(idName.indexOf(loopcategoryNo)!=-1){
			$("#scroll-area").append("<div id='load"+ idName +"' class='cont'></div>");
			$("#load"+ idName +"").load(""+ idName +".htm #main",function() {
				$("#percent-box p").html("<p>All "+cloopNum+" file:"+parseInt(k/cloopNum*100)+"%<\/p>");
				//$("#percent").css("width",parseInt(k/cloopNum*100)+"%");
				$("#load"+ idName +"").append("<p class='page-file-name'>"+idName+".htm</p>");
			});	
		}
		k++;
		n++;
		setTimeout(callback);
	 })
		
}

function printbt() {
	$("#print-bt2").show();
	$("#print-bt2").bind('click', function() {
		window.print();
		window.location.reload();
	})
	$("#load-bt").css("display","none");
	$("#percent").css("display","none");
	$("#percent-box p").html("<p>扫描结束。<br />按下面的打印按钮开始打印。.<\/p>");

}

function loadpages1() {
	$("#scroll-area").append("<div id='load01-001' class='cont'></div>");
	$("#load01-001").load("01-001.htm #main",function() {
	$("#loading").fadeOut(function() {
		 $("#load01-001").show();
		});
	});		
}
function cancel_load() {
	if(window.confirm('是否想要取消该作业？')){
		location.reload();
	}		
}