/* =============================================================================
* print.js
*
* iscapeJS.fontcontrol class definition for font control widgets
*
* ------------------------------------------------------------------------------
*
* USAGE:
*
* Include this script in the document it will be used by:
*   <script type="text/javascript" src="/common/js/library/print.js"></script>
*
* Create one instance of the class, using this EXACT line:
*   var print_widget = new iscapeJS.printcontrol();
*
* ------------------------------------------------------------------------------
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006
*
* Last updated 2007.05.07
*
* Reuse or modification without permission is prohibited.
* =========================================================================== */

// call in iscapeJS javascript library method object
var iscapeJS = iscapeJS || {};

// define a new class for the font control widget
iscapeJS.printcontrol = function(stylesheet, a_link, dsply) {

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._stylesheet
  *
  * This serves as storage for the link element of the current document that
  * defines style information for printed media, and should not be accessed or
  * modified from outside the object's internal methods. It is automatically
  * filled upon page load using browser-neutral techniques.
  *
  * ------------------------------------------------------------------------- */
  this._stylesheet = stylesheet;

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._link
  *
  * This serves as storage for the anchor element of the current document that
  * will be clicked on to open the printer-friendly popup, and should not be
  * accessed or modified from outside the object's internal methods. It is
  * automatically filled upon page load using browser-neutral techniques.
  *
  * ------------------------------------------------------------------------- */
  this._link = a_link;

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._output
  *
  * This serves as storage for the popup window that will contain the
  * printer-friendly version of this page, and should not be accessed or
  * modified from outside the object's internal methods. It is automatically
  * filled upon page load using browser-neutral techniques.
  *
  * ------------------------------------------------------------------------- */
  this._output = null;

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._location
  *
  * This serves as storage for the URL of the current page's printer-friendly
  * popup window, and should not be accessed or modified from outside the
  * object's internal methods. It is automatically filled upon page load using
  * browser-neutral techniques.
  *
  * ------------------------------------------------------------------------- */
  this._location = null;

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._onload
  *
  * This serves as storage for any pre-existing onload event handler, and should
  * not be accessed or modified from outside the object's internal methods. It
  * is automatically filled upon page load using browser-neutral techniques.
  *
  * ------------------------------------------------------------------------- */
  this._onload = null;

  /* == PRIVATE VARIABLE =======================================================
  *
  * (iscapeJS.printcontrol)._display
  *
  * This serves as storage for the CSS display property value to set when
  * showing this._link, and should not be accessed or modified from outside the
  * object's internal methods. It is filled from the dsply parameter passed on
  * object instantiation.
  *
  * ------------------------------------------------------------------------- */
  this._display = dsply;

  /* ===========================================================================
  *
  * (iscapeJS.printcontrol).attachPrintLink
  *
  * Attaches a print link to the popup window content, floating it in the top
  * right corner.
  *
  * USAGE:
  *
  * Usage is automatically invoked when the print popup is loaded.
  *
  * ------------------------------------------------------------------------- */
  this.attachPrintLink = function() {
    if (this._output && this._output.document) {
      var oDoc = this._output.document, oBdy = null;

      if (oDoc && oDoc.getElementById) {
        oBdy = oDoc.getElementById('header') || oDoc.getElementById('tpl-header');
      } else if (oDoc.getElementsByTagName) {
        oBdy = oDoc.getElementsByTagName('body')[0];
      } else if (oDoc.body) {
        oBdy = oDoc.body;
      } else {
        return false;
      }

      if (!oDoc || !oBdy) {
        return false;
      }

      if (oDoc.createElement) {
        var pLink = oDoc.createElement('a');

        pLink.output = this._output;

        pLink.onclick = function() {
          this.output.print();
          this.output.close();
          return false;
        };

        pLink.href = '#print';

        pLink.className = 'popupPrint';

        pLink.appendChild(oDoc.createTextNode('Print this Page'));

        oBdy.insertBefore(pLink, oBdy.childNodes[0])
      } else {
        oBdy.innerHTML =
          '<a href="#print" onclick="window.print();window.close();return false;"'
          + ' style="float:right">Print this Page</a>'
          + oBdy.innerHTML;
      }
    }
  };

  /* ===========================================================================
  *
  * (iscapeJS.printcontrol).print
  *
  * Opens a new window with the printer-friendly content in it.
  *
  * USAGE:
  *
  * Usage is automatically invoked via an anchor built in the init method. See
  * the (iscapeJS.printcontrol).init method description below for usage details.
  *
  * ------------------------------------------------------------------------- */
  this.print = function() {
    var popup_win,
        href = window.location.href;

    if (href.indexOf('#') >= 0) {
      href = href.substring(0, href.indexOf('#'));
    }

    this._location = href + (href.indexOf('?') >= 0 ? '&' : '?') + 'print=t';

    if (this._output) {
      this._output.close();
    }

    if (popup_win) {
      this._output = popup_win(this._location, { 'target' : 'printFriendly' });
    } else {
      this._output = window.open(this._location, 'printFriendly',
        'top=50,left=50,height=400,width=600,location=1,menubar=1,scrollbars=1,resizable=1');
    }

    if (this._output) {
      this._output.widget = this;

      this._output.onload = function() {
        this.widget.attachPrintLink(this);
        this.focus();
      };
    }

    return false;
  };

  /* ===========================================================================
  *
  * (iscapeJS.printcontrol).init
  *
  * Initialization of the widget
  *
  * USAGE:
  *
  * Usage is automatically invoked via the onload event handler.
  *
  * ------------------------------------------------------------------------- */
  this.init = function() {
    if (typeof (this._stylesheet) == 'string') {
      this._stylesheet = document.getElementById(this._stylesheet);
    }

    if (this._stylesheet) {
      this._stylesheet.rel = 'alternate stylesheet';
      this._stylesheet.disabled = true;
    }

    if (typeof (this._link) == 'string') {
      this._link = document.getElementById(this._link);
    }

    if (!this._link
        || typeof this._link != 'object') {
      return;
    }

    this._link.href = '#print';

    this._link.widget = this;

    this._link.onclick = function() {
      this.widget.print();
      return false;
    };

    this._link.style.display = this._display;
  };

  /* ===========================================================================
  *
  * build the onload event handler
  *
  * ------------------------------------------------------------------------- */

  this._onload = window.onload;
  window.print_widget = this;
  window.onload = function() {
    if (window.print_widget._onload
        && typeof (window.print_widget._onload) == 'function') {
      window.print_widget._onload();
    }

    window.print_widget.init();
  };
}

