/*
    $Revision$
    $Id$

    File:           PrintOptions.js
    Description:    Functies voor de PrintOptions dialoog
    Parameters:     Via dialogArguments krijgen we de laatste SLNKMinMax
                    Daarmee weten we de Extent van de tekening (DWGmin/max)
                    En ook hoe ver de gebruiker was ingezoomd op de tekening (viewmin/max)
    Context:        Printoptions.asp
    Note:           qryWhat must be set before including
*/

var SLNKMinMax = FcltMgr.dialogArguments().SLNKEvent;
//
// Deze DWG coordinaten moeten weer zichtbaar worden
//
var DWGSizeX = (SLNKMinMax.DWGmaxX - SLNKMinMax.DWGminX);
var DWGSizeY = (SLNKMinMax.DWGmaxY - SLNKMinMax.DWGminY);

var draggingBusy = false;
var sumdx = sumdy = 0; // After dragging

function calcSheets()
{
	var s = { x: 1, y: 1, r: 0 };
	var frm = document.forms["theForm"]
	var isPortrait = frm.rPortrait[0].checked;
	var papier = frm.sPaperDest.value;
	papier = parseInt(papier.substring(1));
    switch (frm.sPaperSize.value)
	{
	  case "A0": { 	 switch (papier)
	                 {
	                    case 0: s = { x: 1, y: 1, r: 0 };
	                            break;
	                    case 1: s = { x: 2, y: 1, r: 1 };
        		                isPortrait = !isPortrait;
	                            break;
	                    case 2: s = { x: 2, y: 2, r: 0 };
        		                isPortrait = !isPortrait;
	                            break;
	                    case 3: s = { x: 4, y: 2, r: 0 }
        		                isPortrait = !isPortrait;
	                            break;
	                    case 4: s = { x: 4, y: 4, r: 1 }
        		                //isPortrait = !isPortrait;
    		                    break;
    		         }
                     break;
		           }
		case "A1": {
	                 switch (papier)
	                 {
	                    case 1: s = { x: 1, y: 1, r: 0 };
	                            break;
	                    case 2: s = { x: 1, y: 1, r: 0 };
	                            break;
	                    case 3: s = { x: 2, y: 2, r: 1 }
        		                //isPortrait = !isPortrait;
	                            break;
	                    case 4: s = { x: 4, y: 2, r: 0 }
        		                isPortrait = !isPortrait;
    		                    break;
    		         }
    		         break;
		           }
	    case "A2": {
	                 switch (papier)
	                 {
	                    case 2: s = { x: 1, y: 1, r: 0 };
	                            break;
	                    case 3: s = { x: 2, y: 1, r: 0 }
        		                isPortrait = !isPortrait;
	                            break;
	                    case 4: s = { x: 2, y: 2, r: 1 }
        		                isPortrait = !isPortrait;
    		                    break;
    		         }
    		         break;
		           }
		case "A3": {
		             switch (papier)
		             {
		                case 3: s = { x: 1, y: 1, r: 0 };
                                isPortrait = !isPortrait;
                                break;
		                case 4: s = { x: 2, y: 1, r: 1 };
                                isPortrait = !isPortrait;
		                        break;
		             }
		             break;
		           }
		case "A4": { s = { x: 1, y: 1, r: 0 }
                     isPortrait = !isPortrait;
		             break;
		           }
  }
  if (!isPortrait) // Landscape
  {
    var t=s.x;
    s.x=s.y;
    s.y=t;
   }
  frm.xSheets.value = s.x;
  frm.ySheets.value = s.y;
  return s;
}

function calcParams(bForPreview)
{
    //
    // Bepaal papier grootte
    //
    var frm = document.forms["theForm"];
    var ps = $("#sPaperSize").val();
    switch (ps)
    {   // We houden aan elke kant altijd 20 mm speling
        case "A4": { paperWidth = 165; paperHeight = 260; break; }
        case "A3": { paperWidth = 260; paperHeight = 370; break; }
        case "A2": { paperWidth = 520; paperHeight = 370; break; }
        case "A1": { paperWidth = 740; paperHeight = 520; break; }
        case "A0": { paperWidth = 1040; paperHeight = 740; break; }
    }

    var mustrotate = false; // Moet de bitmap echt geroteerd worden
    if (  frm.rPortrait[0].checked && ps != "A4" && ps != "A3"
      || !frm.rPortrait[0].checked && (ps == "A4" || ps == "A3")) // switch sizes
    {
        mustrotate = true;
        var h=paperWidth;
        paperWidth=paperHeight;
        paperHeight=h;
    }

    //
    // Bepaal pixel grootte
    //
    // Zo groot in pixels gaan we het plaatje maken
    targetSX = (paperWidth / 25.4) * 96 * 2; // IE 6.0 doet 96 dpi lijkt/schijnt het
    targetSY = (paperHeight / 25.4) * 96 * 2; // Factor 2 voor extra resolutie

    if (bForPreview) // Much smaller
    {
        if (paperWidth > paperHeight)
        {
            targetSX = 500;
            targetSY = 500 / paperWidth * paperHeight;
        }
        else
        {
            targetSY = 500;
            targetSX = 500 / paperHeight * paperWidth;
        }
    }
    targetSX = Math.round(targetSX);
    targetSY = Math.round(targetSY);


    // deze view gaan we minimaal in beeld brengen
    var viewSizeX = (SLNKMinMax.viewmaxX - SLNKMinMax.viewminX);
    var viewSizeY = (SLNKMinMax.viewmaxY - SLNKMinMax.viewminY);
    var centerX   = (SLNKMinMax.viewminX + SLNKMinMax.viewmaxX) / 2;
    var centerY   = (SLNKMinMax.viewminY + SLNKMinMax.viewmaxY) / 2;

    var scl = parseFloat(frm.sScale.value); // door gebruiker gekozen
    if (scl < 0) // Maximaal
    {
        var viewSizeX = (SLNKMinMax.DWGmaxX - SLNKMinMax.DWGminX);
        var viewSizeY = (SLNKMinMax.DWGmaxY - SLNKMinMax.DWGminY);
        var centerX   = (SLNKMinMax.DWGminX + SLNKMinMax.DWGmaxX) / 2;
        var centerY   = (SLNKMinMax.DWGminY + SLNKMinMax.DWGmaxY) / 2;
    }

    // Schaal bepalen en corrigeren voor de aspect van de view
    if (scl > 0)
    {
        var currentScale = scl;
    }
    else if (targetSY / targetSX > viewSizeY / viewSizeX)
    {   // 'Horizontale' view met 'verticaal' papier==>Y uitbreiden
        var currentScale = viewSizeX / paperWidth;  // DWG units per mm
    }
    else
    {   // 'Verticale' view met 'horizontaal' papier==>X uitbreiden
        var currentScale = viewSizeY / paperHeight;  // DWG units per mm
    }

    if (currentScale > 1.0)
        $("#txtScale").html(qrySchaal + " 1:" + currentScale.toFixed(0));
    else
        $("#txtScale").html(qrySchaal + "Schaal " + (1.0/currentScale).toFixed(0) + ":1");

    var dwgScale = currentScale * paperWidth / targetSX; // dwg per mm

    //
    // Prepareer resultaat
    //
    var qryShow = "&sizeX="+targetSX
                + "&sizeY="+targetSY
                + "&paperColor="+0xFFFFFF // Altijd wit

    //
    qryShow += "&dwgScale=" + (dwgScale);
    qryShow += "&centerX=" + (centerX - dwgScale * sumdx); // Pannen verwerken
    qryShow += "&centerY=" + (centerY + dwgScale * sumdy);

    var sh = calcSheets();
    if (!bForPreview && sh.r)
        mustrotate = !mustrotate;

    if (frm.cForceBW.checked)
        qryShow += "&forceBW=1";
    var printParams = { qryShow : qryShow,
                        paperWidth : paperWidth,
                        paperHeight : paperHeight,
                        mustrotate: mustrotate,
                        //sheetsR: sh.r,
                        sheetsX: sh.x,
                        sheetsY: sh.y
                      };
    return printParams;
}

function onPrint()
{
    FcltMgr.closeDetail(window, calcParams(false))
}

function onChangeScale()
{
    // var scl = parseFloat(frm.sScale.value);
    // TODO: sumdx en sumdy aanpassen voor de schaalwijziging
    sumdx = sumdy = 0; // Reset it lazyly
    Preview();
}

function onChangePaper()
{
    var frm = document.forms["theForm"];
    frm.sPaperDest.value = frm.sPaperSize.value;

    Preview();
}

function Preview()
{
    elemIMG = document.getElementById("Eview");
    var info=calcParams(true);
    var qryAll = qryWhat + "&ForPrint=1" + info.qryShow + SLNKMinMax.qryDWGSize;
    elemIMG.src = qryAll;
}

function _onMouseDown(evt)
{
    if (!evt) evt=window.event;
    draggingBusy=true;
    elemIMG.setCapture();
    dragStartX = evt.clientX;
    dragStartY = evt.clientY;
    return false;
}

/************************************************************\
*
\************************************************************/
function _onMouseMove(evt)
{
    if (!evt) evt=window.event;
    lastMouseX = evt.clientX;
    lastMouseY = evt.clientY;

    if (draggingBusy)
    {
        var dx=(evt.clientX - dragStartX);
        var dy=(evt.clientY - dragStartY);
        elemIMG.style.left = $("#idPaper").position().left + 4 + dx + "px";
        elemIMG.style.top = $("#idPaper").position().top + 4 + dy + "px";

        var clip = "rect("+(dy<0?-dy:"0")+"px "+(targetSX-dx)+"px "+(targetSY-dy)+"px "+(dx<0?-dx:"0")+"px)";
        elemIMG.style.clip = clip;
    }
}

/************************************************************\
*
\************************************************************/
function _onMouseUp(evt)
{
    if (draggingBusy)
    {
        elemIMG.releaseCapture();
        if (!evt) evt=window.event;
        draggingBusy=false;
        sumdx += evt.clientX - dragStartX;
        sumdy += evt.clientY - dragStartY;
        Preview();
    }
}

function onload(event)
{
    elemIMG.style.left = $("#idPaper").position().left + 4 + "px";
    elemIMG.style.top = $("#idPaper").position().top + 4 + "px";
    elemIMG.style.clip = "rect(auto auto auto auto)";
    $("#idPaper").width($(elemIMG).width());
    $("#idPaper").height($(elemIMG).height());
    return;
}

function _onKeyPress(event)
{
    if (event.keyCode == 27)
        self.close();
    else
        if (event.keyCode == 13)
            onPrint();
}

function CRect(left, top, right, bottom)
{
  this.left = left;
  this.top = top;
  this.right = right;
  this.bottom = bottom;
}

function GetWindowRect(ob)
{
  var w = ob.clientWidth;
  var h = ob.clientHeight;

  var x = ob.offsetLeft;
  var y = ob.offsetTop;

  while (ob.offsetParent.tagName.toLowerCase() != 'body')
  {
    ob = ob.offsetParent;
    x += ob.offsetLeft;
    y += ob.offsetTop;
  }

  return new CRect(x, y, x + w, y + h);
}