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


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('Scannar alla kapitel. Åtgärden kan ta lång tid. Vill du ändå genomföra den?')){
				loaded=true;
				$("#load-bt").html("Stoppa 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('Scannar det här kapitlet. Åtgärden kan ta lång tid. Vill du ändå genomföra den?')){
				loaded=true;
				$("#load-bt").html("Stoppa 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>Alla "+loopNum+" filer:"+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>Alla "+cloopNum+" filer:"+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>Scanningen har avslutats.<br />Tryck på utskriftsknappen längst ner för att börja skriva ut.<\/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('Vill du avbryta det här jobbet?')){
		location.reload();
	}		
}