// LightBox.js
// Javascript Behaviour for the LightBox Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 18.08.2006 18:13:08 created by Matthias Hertel
// 15.09.2006 18:13:08 DOM Leak removed.
// 16.09.2006 context on event-methods is now set to the bound object.
// 28.12.2007 documentation and minor changes


jcl.LightBoxBehavior = {
  /// <summary>Implementation of half transparent object that can cover the page.</summary>
  /// <example>
  /// A page that uses this control ist available at:<br />
  /// <a href="http://www.mathertel.de/AjaxEngine/S04_VisualEffects/LightBoxDemo.aspx">
  /// http://www.mathertel.de/AjaxEngine/S04_VisualEffects/LightBoxDemo.aspx</a></example>

obj: null, /// <summary>The reference to the lightbox overlay.</summary>
dlg: null, /// <summary>The current displayed dialog element.</summary>


init: function() {
  /// <summary>Initialize the control.</summary>
  if (this.obj != null)
    alert("The LightBox behavior can only be included once!");
  jcl.LightBoxBehavior.obj = this;
}, // init


term: function() {
  /// <summary>Terminate the control. Unlink all html elements.</summary>
  jcl.LightBoxBehavior.dlg = null;
  jcl.LightBoxBehavior.obj = null;
},


open: function(dlg) {
  /// <summary>Open a dialog.</summary>
  if (jcl.LightBoxBehavior.dlg != null)
    jcl.LightBoxBehavior.hide();

  if ((dlg != null) && (dlg.constructor == String))
    dlg = document.getElementById(dlg);
  jcl.LightBoxBehavior.dlg = dlg;
  if (dlg.show) dlg.show();
  jcl.LightBoxBehavior.show();
}, // open


show: function() {
  /// <summary>Show the current or a new dialog element.</summary>
  var obj = jcl.LightBoxBehavior.obj;
  var dlg = jcl.LightBoxBehavior.dlg;
  
  obj.style.height = Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight) + "px";
  obj.style.width = document.documentElement.scrollWidth + "px";

  window.scrollTo(0,0); 
  obj.style.zIndex = 98;
  obj.style.display = "block";

  if (dlg != null) {
    dlg.style.zIndex = 99;
    dlg.style.position = "absolute";
    dlg.style.display = "block";
    
    dlg.style.top = (document.documentElement.clientHeight - dlg.scrollHeight)/2 + "px";
    dlg.style.left = (document.documentElement.offsetWidth - dlg.scrollWidth)/2 + "px";
    dlg.focus();
  } // if
}, // show


autoHide: function(msec) {
  /// <summary>Hide the current Dialog in msec milliseconds.</summary>
  window.setTimeout(jcl.LightBoxBehavior.hide, msec);
}, // autoHide


hide: function() {
  /// <summary>Hide the current Dialog.</summary>
  var obj = jcl.LightBoxBehavior.obj;
  var dlg = jcl.LightBoxBehavior.dlg;
  
  obj.style.display = "none";
  
  if (dlg != null) {
    dlg.style.zIndex = 1;
    dlg.style.display = "none";
    dlg.style.position = "";
    jcl.LightBoxBehavior.dlg = null;
  } // if
}, // hide


openUrl: function(url) {
  /// <summary>Show the current Dialog.</summary>
  jcl.LightBoxBehavior.dlg.innerHTML = "<p>loading..." + url + "</p>";
  var f = jcl.LightBoxBehavior.frame;
  f.src = url;
  f.onreadystatechange = jcl.LightBoxBehavior.ready;
  f.onload = jcl.LightBoxBehavior.ready;
  jcl.LightBoxBehavior.show();
}, // openUrl


ready: function () {
  var f = jcl.LightBoxBehavior.frame;
  if ((f.readyState == null) || (f.readyState == "complete")) {
    var s = f.contentWindow.document.body.innerHTML;
    jcl.LightBoxBehavior.dlg.innerHTML = s;"<p>loading...<br /><br /><br /><br />....</p>";

    var dlg = jcl.LightBoxBehavior.dlg;
    dlg.style.width = dlg.scrollWidth;
    dlg.style.top = (document.documentElement.clientHeight - dlg.scrollHeight)/2 + "px";
    dlg.style.left = (document.documentElement.offsetWidth - dlg.scrollWidth)/2 + "px";
  } // if
} // ready

} // jcl.LightBoxBehavior

