﻿var pdfprintloaded = false;
var pdfprintshowing = false;

// This method should be called from the page onload event to construct dynamic objects.
function initializePdfPrint() 
{
  var objbody = document.getElementsByTagName("body").item(0);
	if (objbody) {
	  // Dynamically add overlay html content
	  var objoverlay = document.createElement("div");
	  objoverlay.setAttribute("id", "overlaypdfprint");
	  objoverlay.onclick = function() { hidePdfPrint(); return false; }
	  objoverlay.style.display = "none";	  
	  objbody.appendChild(objoverlay); 
	  
	  var objpdfprintiframe = document.createElement("iframe");
	  objpdfprintiframe.setAttribute("id", "pdfprintiframe");  
	  objpdfprintiframe.setAttribute("name", "pdfprintiframe");  
	  objpdfprintiframe.setAttribute("frameborder", "0");  
	  objpdfprintiframe.setAttribute("border", "0");  

	  var objpdfprintoverlay = document.createElement("div");
	  objpdfprintoverlay.setAttribute("id", "pdfprintoverlay");  
	  objpdfprintoverlay.style.display = "none";	 
	  var html = '<div id="pdfprint_topbar"></div><div id="pdfprint_header"><div id="pdfprint_close">';
	  html += '<a id="PdfPrintButton_close" onclick="closePdfPrint();">';
	  html += '<img id="PdfPrintImage_close" src="' + document.getElementById('_PdfPrintImage_close').src + '" style="border-width: 0px;"></a>';
	  html += '</div><div id="pdfprint_title">Afdrukken</div></div>';
	  objpdfprintoverlay.innerHTML = html;
	  objbody.appendChild(objpdfprintoverlay); 

	  // do not set display to none because of Safari bug
	  if (navigator.userAgent.toString().toLowerCase().indexOf('safari') > 0) {
      // instead set dimensions to zero
	    objpdfprintiframe.style.width = "0px";
	    objpdfprintiframe.style.height = "0px";
    } else {
      objpdfprintiframe.style.display = "none";
    }		  
	  
	  // setAttribute does not work for IE for properties that impact display layout
	  objpdfprintiframe.frameBorder = "0";	  
	  objpdfprintiframe.border = "0";	  	  	     
	  objbody.appendChild(objpdfprintiframe);  	 	  
	}
}

// Dynamically position overlay and iframe for pdfprint. Only works in case pdfprint is 
// visible or when force = true (for internal use).
function resizePdfPrintOverlay(force)
{
  if ((pdfprintshowing) || (force)) {
    var overlaydiv = $("overlaypdfprint");
    var overlayiframe = $("pdfprintiframe");
    var overlaypdf = $("pdfprintoverlay");

    if (overlaydiv && overlayiframe && overlaypdf) {
    //if (overlaydiv && overlayiframe) {
      var menuarea = document.getElementById("bbmenuarea");
      if (menuarea) {
        var pagesize = KMMeasurement.getPageSize();
        var divrect = KMMeasurement.getRect(menuarea);
        divrect.top += divrect.height + 1;
        divrect.left = 0;
        divrect.width = pagesize.windowWidth;
        divrect.height = pagesize.windowHeight - divrect.top;
        
        KMMeasurement.setRect(overlaydiv, divrect);
        
        var iframerect = KMMeasurement.cloneRect(divrect);
        iframerect.width = (divrect.width * 0.9);
        iframerect.height = (divrect.height * 0.9);
        
        var margin = (divrect.height - iframerect.height) / 2; 
        iframerect.top = divrect.top + margin;
        iframerect.left = divrect.width - iframerect.width - margin;
        KMMeasurement.setRect(overlayiframe, iframerect);
        
        // again for overlaypdf
        var headermargin = 20;
        var widthmargin = 4;
        var overlayrect = KMMeasurement.cloneRect(divrect);
        overlayrect.width = (divrect.width * 0.9 + widthmargin * 2);
        overlayrect.height = (divrect.height * 0.9 + headermargin + widthmargin);
        var margin = (divrect.height - overlayrect.height) / 2; 
        overlayrect.height += (widthmargin * 2);
        overlayrect.top = divrect.top + margin - headermargin + widthmargin;
        overlayrect.left = iframerect.left - widthmargin;
        KMMeasurement.setRect(overlaypdf, overlayrect);
      }   
    }
  }
}

var pdfprinturl = "";
function togglePdfPrint(url) 
{
  if (!pdfprintshowing) {
    showPdfPrint(url);
  }
  else {
    if (url != pdfprinturl) {
      showPdfPrint(url);
    }
    else {
      hidePdfPrint();
    }
  }
  pdfprinturl = url;
}

// Make pdfprint and it's overlay visible.
function showPdfPrint(url)
{  
  overlaydiv = $("overlaypdfprint");
  overlayiframe = $("pdfprintiframe");
  overlaypdf = $("pdfprintoverlay");
  
  if (!overlaydiv) {
    throw("overlaypdfprint element niet beschikbaar. initializePdfPrint() niet aangeroepen in pagina onload?");
  } else {
    resizePdfPrintOverlay(true);    
    new Effect.Appear(overlaydiv, { duration: 0.2, from: 0.0, to: 0.8, afterFinish: _callbackShowPdfPrint });    
     
    if (!pdfprintloaded) {
      var pdfprintframe = frames["pdfprintiframe"];    
      if (pdfprintframe) {
        // do not load pdf here but in _callbackShowPdfPrint
        //pdfprintframe.location.href = url;
        pdfprintloaded = true;
      }
    }
    pdfprintshowing = true;
  }
}

// Called after show overlay effect was finished to allow us to display the actual pdfprint list.
function _callbackShowPdfPrint(obj)
{
  Element.show(overlayiframe);	 
  Element.show(overlaypdf);	 
  overlayiframe.src = pdfprinturl;
}

// Hides pdfprint and it's overlay.
function hidePdfPrint()
{
  if (pdfprintshowing) {
	  Element.hide("pdfprintiframe");
	  Element.hide("pdfprintoverlay");
    new Effect.Fade("overlaypdfprint", { duration: 0.2});    
    
    pdfprintshowing = false;
  }
}


function getPrintContent() {
  // get content
  var printurl = typeof frames["iframe_content"].printurl != 'undefined' ? frames["iframe_content"].printurl : frames["iframe_content"].location;
  var printmediatype = typeof frames["iframe_content"].printmediatype != 'undefined' ? frames["iframe_content"].printmediatype : 'html';
  var ispdf = (printmediatype.toString().toLowerCase() === 'pdf');
  var url = encodeURI(printurl);
  var arrPrintdata = new Array(ispdf, url);
  return arrPrintdata;
}

function closePdfPrint()
{
  if (parent.hidePdfPrint) {
    parent.hidePdfPrint();
  }
}

function doPrint() {
  var arrPrintdata = getPrintContent();
  var ispdf = arrPrintdata[0];
  var url = arrPrintdata[1];
  if (ispdf) {
    togglePdfPrint(url);
  }
  else {
    // print content frame
    frames["iframe_content"].focus();
    // see Caremax 1544 for reason for IE7 browser detect
    if (isIE7()) {
      frames["iframe_content"].document.execCommand('print',false,null);
    }
    else {
      frames["iframe_content"].print();
    }
  }
}

function isIE7(){var ie7=false;if(window.XMLHttpRequest){if(window.ActiveXObject){ie7=true;}else{ie7=false;}}else{ie7=false;}return ie7;}

