/*
This file is part of pDAF.
Copyright (c) 2005-2010 by Gregorcic & Helger IT systems OEG.
All rights reserved.
http://www.phloc.com
 */
  
// constructor
function pDAFUIExtClass() {
  this.m_nMinWaitingTime = 700;
  this.m_nImmediateWaitingTime = 200;
  this.m_nModalDialogHeight = 250;
  this.m_nModalDialogWidth = 350;
  this.m_bProgress = false;
}

pDAFUIExtClass.prototype = {
  startProgress : function(sMsg) {
    if (sMsg && sMsg != "") {
      this.m_bProgress = true;  
      window.setTimeout(function () { pDAFUIExt._startProgressInternal(sMsg) }, 
                        this.m_nMinWaitingTime);
    }
  },
  
  startProgressNow : function(sMsg) {
    if (sMsg && sMsg != "") {
      this.m_bProgress = true;  
      window.setTimeout(function () { pDAFUIExt._startProgressInternal(sMsg) }, 
                        this.m_nImmediateWaitingTime);
    }
  },
  
  modalImagePopup : function(sXHTML, sCloseMsg, nWidth, nHeight) {
    if (sXHTML && sXHTML != "") 
    {
      var aExtent = this.limitExtentToScreen(nWidth, nHeight);
      var aContainer = document.createElement("div");
      var aPreload = jphloc.getPreloadedImage(sXHTML);
      if (aPreload)
      {
        var aImgExtent = this.limitExtentToScreen(aPreload.width, aPreload.height);
        aPreload.setAttribute('width', aImgExtent.width);
        aPreload.setAttribute('height', aImgExtent.height);      
        aContainer.appendChild(aPreload); 
      }

      $.modal(aContainer, {minHeight: aExtent.height,
                           minWidth: aExtent.width,
                           autoResize:true,
                           autoPosition:true,
                           closeHTML: "<a class=\"modalCloseImg simplemodal-close\" title=\"" + sCloseMsg + "\"/>"});    
    }
  },
  
  modalPopup : function(sXHTML, sCloseMsg, nWidth, nHeight) {
    if (sXHTML && sXHTML != "") 
    {
      var aExtent = this.limitExtentToScreen(nWidth, nHeight);
      var aContainer = document.createElement("div");
      aContainer.innerHTML = sXHTML;
      $.modal(aContainer, {minHeight: aExtent.height,
                           minWidth: aExtent.width,
                           autoResize:true,
                           autoPosition:true,
                           closeHTML: "<a class=\"modalCloseImg simplemodal-close\" title=\"" + sCloseMsg + "\"/>"});    
    }
  },
  
  /**
   * checks if the passed width and height fit into the available window 
   * size.If not, the width and height are reduced (maintaining aspect ratio) 
   * and returned as an object with the property 'width' and 'height'. 
   */
  limitExtentToScreen : function (nWidth, nHeight)
  {
    var aExtent = {width:nWidth,
                   height:nHeight};    
    var aMaxExtent = this.getAvailableExtent(true);
    var aHeightFactor = aMaxExtent.height / aExtent.height;
    var aWidthFactor = aMaxExtent.width / aExtent.width;
    if (aHeightFactor < 1 || aWidthFactor < 1)
    {
      if (aHeightFactor < aWidthFactor)
      {
        aExtent.width = aExtent.width * aHeightFactor;
        aExtent.height = aExtent.height * aHeightFactor;
      }
      else
      {
        aExtent.width = aExtent.width * aWidthFactor;
        aExtent.height = aExtent.height * aWidthFactor;
      }
    }    
    return aExtent;
  },
  
  /**
   * gets the available window size to display pop-ups by calculating a little 
   * buffer at the edge. 
   */
  getAvailableExtent : function (bWithBuffer)
  {
    var nBuffer = 55;
    var nW = jphloc.getPageWidth();
    var nH = jphloc.getPageHeight();
    if (bWithBuffer)
    {
      nW -= nBuffer;
      nH -= nBuffer;
    }
    var aExtent = {width:nW,
                   height:nH};
    return aExtent;
  },
  
  appendTextWithBR : function(aParent, sText) {
    if (sText && sText != '') {
      var aLines = sText.split('\n');
      var nIndex = 0;
      for (var nLine in aLines) {
        if (nIndex > 0)
          aParent.appendChild(document.createElement("br"));          
        aParent.appendChild(document.createTextNode(aLines[nLine]));
        nIndex++;
      }
    }
  },

  _startProgressInternal : function(sMsg) {
    if (this.m_bProgress) {
      var aContainer = document.createElement("div");
      aContainer.setAttribute("id", "modal_progress");
      var aTable = document.createElement("table");
      var aTBody = document.createElement("tbody");
      aTable.appendChild(aTBody);
      aContainer.appendChild(aTable);
      var aRow = document.createElement("tr");
      aTBody.appendChild(aRow);
      var aCellIndicator = document.createElement("td");
      aRow.appendChild(aCellIndicator);
      var aCellMessage = document.createElement("td");
      aRow.appendChild(aCellMessage);
      jphloc.addClass(aCellIndicator, "pdaf_progress_indicator");
      jphloc.addClass(aCellMessage, "pdaf_progress_info");
      this.appendTextWithBR(aCellMessage, sMsg);
      $.modal(aContainer, {minHeight: 30,
                           maxHeight: this.m_nModalDialogHeight,
                           minWidth: 200,
                           maxWidth: this.m_nModalDialogWidth,
                           closeHTML: ''});
    }
  },  
  
  stopProgress : function () {
    if (this.m_bProgress) {
      $.modal.close();
    }
    this.m_bProgress = false;
  }
}

var pDAFUIExt = window.pDAFUIExt = new pDAFUIExtClass();
