// FMBox.js
// Javascript Behaviour for the FMBox Control
// Idea by Matthias Hertel, http://www.mathertel.de
// ----- 
// 01.03.1009 CKE

// a singelton Behaviour !
var FMBox = {
  oldHead: null,
  oldMessage: null,
  ie: true,
  transparency: 100,
  transparencystep: 2,
  msg: null,
  head: null,
  obj: null, // a pointer to the FMBox overlay
  dlg: null, // the current displayed dialog element
  
  init: function() {
    var elem;
    
    if (this.obj != null)
      alert("The FMBox behavior can only be included once!");
    FMBox.obj = this;

    elem = document.getElementById("ct100_myFMB_message_head");
    oldHead = elem.innerHTML;
    elem = document.getElementById("ct100_myFMB_message");
    oldMessage = elem.innerHTML;
    transparency = 100;
  }, // init


  term: function() {
    FMBox.dlg = null;
    FMBox.obj = null;
  },

  
  // open the current Dialog
  open: function(dlg, message, headline) {
    var elem;
    head = headline; // external headline
    msg = message; // external message

    if (FMBox.dlg != null)
      FMBox.hide();
      
    if ((dlg != null) && (dlg.constructor == String))
      dlg = document.getElementById(dlg);

    if(head != null && msg != null && head.length > 0 && msg.length > 0) {
      elem = document.getElementById("ct100_myFMB_message_head");
      elem.innerHTML = head;
      elem = document.getElementById("ct100_myFMB_message");
      elem.innerHTML = msg;
    } else {
      // standardtext from *.master-page or RESET!
      elem = document.getElementById("ct100_myFMB_message_head");
      elem.innerHTML = oldHead;
      elem = document.getElementById("ct100_myFMB_message");
      elem.innerHTML = oldMessage;
    } // if

    FMBox.dlg = dlg;
    FMBox.show();
  }, // open


  // show the current or a new dialog element
  show: function() {
    var obj = FMBox.obj;
    var dlg = FMBox.dlg;

    obj.style.height = "75px"; // 50
    obj.style.width = "180px";

    window.scrollTo(0,0); 
    obj.style.zIndex = 98;
    obj.style.display = "block";

    transparency = 100;

    if (dlg != null) {
      dlg.style.zIndex = 99;
      dlg.style.display = "block";
      dlg.style.position = "absolute";
  	  dlg.style.filter = "alpha(opacity=" + transparency + ")";
      dlg.style.top = (document.documentElement.clientHeight - dlg.scrollHeight)/2 + 100 + "px";
      dlg.style.left = (document.documentElement.offsetWidth- dlg.scrollWidth)/2 + 40 + "px";
    } // if
    
    this.transparency = 100;
    FMBox.autoHide(2200);
  }, // show


  // hide the current Dialog in msec milliseconds
  autoHide: function(msec) {
    window.setTimeout(FMBox.fi, msec);
  }, // autoHide


  fi: function() {
    var dlg = FMBox.dlg;
    var elem = document.getElementById("ct100_myFMB_message");

    if(transparency > 0) {
      transparency -= 2;
      dlg.style.filter = "alpha(opacity=" + this.transparency + ")";
      window.setTimeout(FMBox.fi, 2);
    } else {
      FMBox.hide();
    }
	}, // fi
  

  // hide the current Dialog
  hide: function() {
    var obj = FMBox.obj;
    var dlg = FMBox.dlg;
    
    obj.style.display = "none";
    
    if (dlg != null) {
      dlg.style.zIndex = 0;
      dlg.style.display = "none";
      dlg.style.position = "";
      FMBox.dlg = null;
    } // if
    
    this.transparency = 100;
  }, // hide
  
 
  // show the current Dialog
  openUrl: function(url) {
    FMBox.dlg.innerHTML = "<p>loading..." + url + "</p>";
    var f = FMBox.frame;
    f.src = url;
    f.onreadystatechange = FMBox.ready;
    f.onload = FMBox.ready;
    FMBox.show();
  }, // openUrl

  ready: function () {
    var f = FMBox.frame;
    if ((f.readyState == null) || (f.readyState == "complete")) {
      var s = f.contentWindow.document.body.innerHTML;
      FMBox.dlg.innerHTML = s;"<p>loading...<br /><br /><br /><br />....</p>";

      var dlg = FMBox.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";
      }
    
  } // ready
} // FMBox
