jQuery(document).ready(function(){
	jQuery("#cancelBTN").click(function(){
		cancelPdfDownload = true;
	});
});

var cancelPdfDownload = false;

function requestAllPdf(start,filename,requestData){
	var separator = '?';
  	var stamp = new Date();

	if (url.indexOf('?') != -1) {
		separator = '&';
	}

	centerPopup();
	loadPopup(); 

	var requestUrl = url + separator + 'tx_tdreference_pi1[request]=' + 'get_all_pdf' + '&tx_tdreference_pi1[q]=' + 'query' + '&tx_tdreference_pi1[qStart]=' + start + '&tx_tdreference_pi1[filename]=' + filename + '&hash=' + stamp.getTime() + '&no_cache=1';

  	jQuery.post(requestUrl, 'records='+requestData, function(data){
		var separator = '?';

		// progress bar calculations
  		jQuery("#contactArea").html('Document ' + data.nextStart + ' of ' + data.allCount);
		var perc = data.nextStart * 100 / data.allCount;
  		jQuery("#fortschritt").css('width', perc + '%'); 

  		if(data.nextStart != -1) {
  			if(cancelPdfDownload == false) {
  				requestAllPdf(data.nextStart, data.filename, data.records);
  			} else {
  				disablePopup();
  			}
  		} else {
  			jQuery("#contactArea").html('merge PDF');
			jQuery("#fortschritt").css('width', '100' + '%');

			disablePopup();

			if (url.indexOf('?') != -1) {
				separator = '&';
			}

			if(data.pdfDownload != '') {
				top.document.location.href = url + separator + 'tx_tdreference_pi1[finished]=1&tx_tdreference_pi1[docurl]=' + data.pdfDownload + '&no_cache=1';
	  		}
		}
  	}, "json");
}

var popupStatus = 0;

function loadPopup(){

	if(popupStatus==0){
	jQuery("#backgroundPopup").css({
	"opacity": "0.7"
	});
	jQuery("#backgroundPopup").fadeIn("slow");
	jQuery("#popupPDFload").fadeIn("slow");
	popupStatus = 1;
	}
} 

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupPDFload").height();
	var popupWidth = jQuery("#popupPDFload").width();
	//centering
	jQuery("#popupPDFload").css({
	"position": "absolute",
	"top": windowHeight/2-popupHeight/2,
	"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery("#backgroundPopup").css({
	"height": windowHeight
	});
}

function disablePopup(){
	cancelPdfDownload = false;
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupPDFload").fadeOut("slow");
		popupStatus = 0;
	}
} 

