<?php
header('Content-Type: text/javascript');
require_once '../lib/nocache.php';
?>
DialogBox = {};

DialogBox.titleBarBackgroundColor = '#189CCE';
DialogBox.titleBarColor           = 'white';
DialogBox.titleBarFontFamily      = 'Arial,Helvetica,Sans-Serif';
DialogBox.titleBarFontSize        = '16px';
DialogBox.titleBarFontWeight      = 'bold';

DialogBox.alertColor              = 'black';
DialogBox.alertFontFamily         = 'Arial,Helvetica,Sans-Serif';
DialogBox.alertFontSize           = '16px';
DialogBox.alertFontWeight         = 'normal';

DialogBox.bgcolor                 = 'white';

DialogBox.closeIcon    = 'http://facemoo.com/image/cleveland/dialog/closeicon.gif';
DialogBox.waitImage    = 'http://facemoo.com/image/cleveland/dialog/wait2.gif';
DialogBox.overlayImage = 'http://facemoo.com/image/cleveland/dialog/overlay.png';

DialogBox.stack        = new Array();

DialogBox.ticking      = false;
DialogBox.tick = function() {
   if(DialogBox.stackSize() < 1) {
      DialogBox.ticking = false;
      return;
   }
   DialogBox.ticking = true;
   DialogBox.resize();
   setTimeout('DialogBox.tick()', 200);
}

DialogBox.fly = function() {
   var div = document.getElementById('bluebird');
   var left = parseInt(div.style.left);
   var top  = parseInt(div.style.top);
   left -= 25;

   if(left > 300) { top += 10; } else
   { top -= 10; }

   div.style.left = left + 'px';
   div.style.top  = top + 'px';
   if(left > -300) {
      setTimeout('DialogBox.fly()', 150);
   } else {
      DialogBox.flying = false;
      (document.getElementsByTagName('body')[0]).removeChild(div);
   }
}

DialogBox.flying = false

DialogBox.bluebird = function() {
   if(DialogBox.flying) { return; }
   DialogBox.flying = true;
   var div = document.createElement('div');
   div.id = 'bluebird';
   div.style.width  = '258px';
   div.style.height = '219px';
   div.style.position = 'absolute';
   div.style.top  = '10px';
   div.style.left = '800px';
   div.style.zIndex = 5000;
   div.innerHTML = "<img width='258' height='219' src='http://facemoo.com/image/cleveland/dialog/anibluebird.gif' />";
   (document.getElementsByTagName('body')[0]).appendChild(div);
   DialogBox.fly();
}

DialogBox.box = function(width, height) {

   if(DialogBox.stack.length) {
      var data = DialogBox.stack[DialogBox.stack.length-1];
      data.ol.style.visibility = 'hidden';
   }

   DialogBox.hideThings(true);

   var box = document.createElement('div');
   box.style.zIndex     = (DialogBox.stack.length + 1) * 1000;
   box.style.width      = width + 'px';
   box.style.height     = height + 'px';
   box.style.position   = 'absolute';
   box.style.display    = 'block';
   box.style.visibility = 'hidden';
   box.style.top  = '0px';
   box.style.left = '0px';
   box.id = 'box' + box.style.zIndex;
   //box.style.border = '1px solid black';

   var container = document.getElementById('pageContainer');

   var ol = document.createElement('div');
   ol.style.zIndex = box.style.zIndex - 1;
   ol.style.position = 'absolute';
   ol.style.top    = '0px';
   ol.style.left   = '0px';

   //ol.style.width  = '100%';
   //ol.style.width  = (container.offsetWidth-40) + 'px';
   //ol.style.width  = (document.getElementsByTagName('body')[0]).offsetWidth + 'px';

   ol.style.width  = container.offsetWidth  + 'px';
   ol.style.height = container.offsetHeight + 'px';

   ol.style.backgroundRepeat = 'repeat';
   ol.style.display = 'block';

   if(DialogBox.isExplorer()) {
      ol.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" +
                        "(src='" + DialogBox.overlayImage + "', sizingMethod='scale');}";
   } else { ol.style.backgroundImage = 'url(' + DialogBox.overlayImage + ')'; }

   var data = {};
   data.box    = box;
   data.ol     = ol;
   data.width  = width;
   data.height = height;

   DialogBox.stack.push(data);

   (document.getElementsByTagName('body')[0]).appendChild(ol);
   (document.getElementsByTagName('body')[0]).appendChild(box);

   return box;
}

DialogBox.close = function() {
   if(!DialogBox.stack.length) {
      DialogBox.hideThings(false);
      return;
   }
   var data = DialogBox.stack.pop();
   data.box.innerHTML = '';
   document.getElementsByTagName('body')[0].removeChild(data.box);
   document.getElementsByTagName('body')[0].removeChild(data.ol);
   if(!DialogBox.stack.length) {
      DialogBox.hideThings(false);
   } else {
      var data = DialogBox.stack[DialogBox.stack.length-1];
      data.ol.style.visibility = 'visible';
   }
}

DialogBox.resize = function() {
   if(!DialogBox.stackSize()) { return; }
   var container = document.getElementById('pageContainer');
   for(var i=0; i<DialogBox.stack.length; i++) {
      var data = DialogBox.stack[i];
      var top  = DialogBox.scrollTop();
      var left = DialogBox.scrollLeft();
      left  = ((DialogBox.pageWidth() - data.width)/2) + left + 'px';
      data.box.style.left = left;
      top   = ((DialogBox.pageHeight() - data.height)/2) + top + 'px';
      data.box.style.top = top;
      data.ol.style.width = container.offsetWidth + 'px';
      //data.ol.style.width = DialogBox.pageWidth() + 'px';
      //data.ol.style.width =
   }
}

DialogBox.titleBar = function(title, width, callback) {
   if(!callback) { callback = "DialogBox.close()"; }

   var style = '';
   style += "height:26px;";
   style += "width:" + width + "px;";
   style += "background-color:" + DialogBox.titleBarBackgroundColor + ";";
   style += "color:white;";
   style += "line-height:26px;";
   style += "padding-left:10px;";
   style += "padding-right:10px;";
   style += "font-weight:bold;";
   style += "font-family:Arial;";
   style += "font-size:16px;";
   style += "text-align:left;";
   style += "margin-bottom:0px;";

   var table = "";
   table += "<table style='" + style + "' cellpadding='0' cellspacing='0' border='0'><tr>";
   table += "<td align='left'>" + title + "</td>";
   table += "<td align='right'><img ";
   table += "onmouseover=\"this.style.cursor='pointer'\" ";
   table += "onclick=\"" + callback + "\" ";
   table += "src='" + DialogBox.closeIcon + "' /></td>";
   table += "</tr></table>";

   return table;
}

DialogBox.closeAlert = function(id) {
   DialogBox.close();
   if(id) {
      (document.getElementById(id)).focus();
   }
}

DialogBox.alertBox = function(title, text, callback) {

   if(!callback) { callback = 'DialogBox.close()'; }
   if(!title)    { title    = 'Alert Message'; }

   var width = Term.getStringWidth(title, '') + 150;
   if(width < 300) { width = 300; }
   
   var html = '';

   html += "<div class='dboxTitleBar' style='line-height:35px;height:35px;width:100%;'>";
   html += "<div style='width:35px;height:35px;float:left;margin-right:20px;' class='dboxIcon'></div>";
   html += "<span class='dboxTitle'>" + title + "</span>";
   html += "</div>";

   html += "<div id='alertbox' style='padding:10px;'>";
   html += "<div class='dboxContent' style='margin-bottom:20px;'>" + text + "</div>";
   html += "<div style='text-align:right;'><button class='dboxButton' id='alertbox_close' onclick=\"" +  callback + "\">Close</button></div>";
   html += "</div>";

   DialogBox.domBox(width, html);
}

DialogBox.confirmBox = function(title, text, callbackYes, callbackNo) {
   if(!callbackYes) { callbackYes = 'DialogBox.close()'; }
   if(!callbackNo)  { callbackNo  = 'DialogBox.close()'; }
   if(!title)       { title       = 'Alert Message'; }

   var width = Term.getStringWidth(title, '') + 150;
   if(width < 300) { width = 300; }

   var html = '';

   html += "<div class='dboxTitleBar' style='line-height:35px;height:35px;width:100%;'>";
   html += "<div style='width:35px;height:35px;float:left;margin-right:20px;' class='dboxIcon'></div>";
   html += "<span class='dboxTitle'>" + title + "</span>";
   html += "</div>";

   html += "<div id='alertbox' style='padding:10px;'>";
   html += "<div class='dboxContent' style='margin-bottom:20px;'>" + text + "</div>";
   html += "<div style='text-align:right;'>"
   html += "<button class='dboxButton' id='confirmBoxYes' onclick=\"" +  callbackYes + "\">Yes</button>\n";
   html += "<button class='dboxButton' id='confirmBoxNo' onclick=\"" +  callbackNo + "\">No</button>\n";
   html += "</div>";
   html += "</div>";

   DialogBox.domBox(width, html);
}

DialogBox.focus = function(id) {
   var el = document.getElementById(id);
   if(!el) { return; }
   el.focus();
}


DialogBox.dialogBox = function(title, content, width, focusId) {
   if(!title)    { title    = 'Dialog Box'; }
   if(!width)    { width    = 300; }

   var html = '';
   html += "<div class='dboxTitleBar' style='line-height:35px;height:35px;width:100%;'>";
   html += "<div style='width:35px;height:35px;float:left;margin-right:20px;' class='dboxIcon'></div>";
   html += "<span class='dboxTitle'>" + title + "</span>";
   html += "</div>";
   html += "<div id='alertbox' style='padding:10px;'>";
   html += "<div class='dboxContent'>" + content + "</div>";
   html += "</div>";

   DialogBox.domBox(width, html);

   if(focusId) {
      setTimeout("DialogBox.focus('" + focusId + "')", 500);    
   } 
}


DialogBox.squareBox = function(width, height, content, bgcolor) {
   var box = DialogBox.box(width, height);
   box.innerHTML = content;
   if(bgcolor) {
      box.style.backgroundColor = bgcolor;
   }
   DialogBox.resize();
   box.style.visibility = 'visible';
   if(!DialogBox.ticking) { DialogBox.tick(); }

}

/**
This function is used by both alertBox and dailogBox 
functions. Important!
*/
DialogBox.domBox = function(width, content, fadein) {
   height = 0;
   content = content.replace(/[ ]+$/, '');
   content = content.replace(/^[ ]+/, '');
   height  = Term.getContentHeight(width, content);
   var box = DialogBox.box(width, height);
   box.className = 'dboxDiv';
   box.innerHTML = content;
   DialogBox.resize();
   //if(fadein) { box.style.display    = none; }
   box.style.visibility = 'visible';
   //if(fadein) { $('#' + box.id).fadein(); }
   if(!DialogBox.ticking) { DialogBox.tick(); }
}


DialogBox.getContentHeight = function(width, content) {
   var height = 0;
   var div    = document.getElementById('content.height');
   if(!div) {
      div = document.createElement('div');
      div.id = 'content.height';
      div.style.display = 'none';
      div.style.visibility = 'hidden';
      div.style.overflow = 'scroll';
      (document.getElementsByTagName('body')[0]).appendChild(div);
   }
   div.style.width   = width + 'px';
   div.style.display = 'block';
   div.innerHTML = content;

   height = div.scrollHeight;

   /*
   if(document.defaultView) {
      //alert('using default view');
      height = parseInt(document.defaultView.getComputedStyle(div, null).getPropertyValue("height").replace(/px/, ''));
   } else if(div.clientHeight) {
      //height = div.clientHeight;
   } else if(div.offsetHeight) {
      //height = div.offsetHeight;
   }
   */

   div.style.display = 'none';
   (document.getElementsByTagName('body')[0]).removeChild(div);

   return height;
}

DialogBox.getContentWidth = function(html, style) {
   var fontMetrics = document.getElementById('font_metrics');
   if(!fontMetrics) {
      fontMetrics    = document.createElement('span');
      fontMetrics.id = 'font_metrics';
      fontMetrics.style.visibility = 'hidden';
      document.getElementsByTagName('body')[0].appendChild(fontMetrics);
   }
   fontMetrics.innerHTML = html;
   fontMetrics.className = style;
   return parseInt(fontMetrics.offsetWidth);
}


DialogBox.waitBox = function() {

   var box = DialogBox.box(200, 50);
   DialogBox.resize();
   box.className = 'dboxWait';
   box.style.lineHeight = '50px';
   box.innerHTML = 'One Moment Please ...';
   box.style.visibility = 'visible';
}

DialogBox.stackSize = function() {
   return DialogBox.stack.length;
}

DialogBox.busy = function() {
   return DialogBox.stackSize() == 0 ? false : true;
}

DialogBox.closeAll = function() {
   while(DialogBox.stackSize()) {
      DialogBox.close();
   }
}

DialogBox.isExplorer = function() {
   var version = 0;
   if(navigator.appVersion.indexOf("MSIE")!=-1) {
      var temp = navigator.appVersion.split("MSIE");
      version = parseFloat(temp[1]);
      return version ? true : false;
   }
   return false;
}

DialogBox.scrollTop = function() {
   var pos = 0;
   if(window.pageYOffset) {
      pos = window.pageYOffset;
   } else if(document.documentElement && document.documentElement.scrollTop) {
      pos = document.documentElement.scrollTop;
   } else if(document.body) {
      pos = document.body.scrollTop;
   }
   return pos;
}

DialogBox.scrollLeft = function() {
   var pos = 0;
   if(window.pageXOffset) {
      pos = window.pageXOffset;
   } else if(document.documentElement && document.documentElement.scrollLeft) {
      pos = document.documentElement.scrollLeft;
   } else if(document.body) {
      pos = document.body.scrollLeft;
   }
   return pos;
}

DialogBox.pageWidth = function() {
   return window.innerWidth != null? window.innerWidth:
   document.documentElement && document.documentElement.clientWidth ?
   document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}

DialogBox.pageHeight = function() {
   return window.innerHeight != null? window.innerHeight:
   document.documentElement && document.documentElement.clientHeight ?
   document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

DialogBox.hideThings = function(yorn) {

   // This fuction is only necessary for IE,
   // so return if it isn't.
   //if(!document.all) { return; }

   var v = 'visible';
   if(yorn) { v = 'hidden'; }

   /*
   var tag=document.getElementsByTagName('select');
   for(i=tag.length-1;i>=0;i--) tag[i].style.visibility=v;
   */

   /*
   tag=document.getElementsByTagName('iframe');
   for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=v;
   */


   /*
   tag=document.getElementsByTagName('object');
   for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=v;

   tag=document.getElementsByTagName('embed');
   for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=v;
   */

}


