/***************************************************
//                                                           //
//  Modal CSS Dialog Javascript Code                         //
//  Copyright© BrandsPatch LLC                               //
//  http://www.explainth.at                                  //
//                                                           //
//  All Rights Reserved                                      //
//                                                           //
//  Permission is granted to use, modify and redistribute    //
//  this code on the condition that this notice is retained  //
//  unchanged.                                               //
//                                                           //
//  BrandsPatch  declines all responsibility for any losses, //
//  direct or indirect, that may arise  as a result of using //
//  this code.                                               //
**************************************************************/
var CRLF = String.fromCharCode(13) + String.fromCharCode(10);

function CloseDialog()
{
 var main = document.getElementById('learnit_main');
 e = document.getElementById('overlay');
 main.removeChild(e);
 var e = document.getElementById('diadiv');
 main.removeChild(e);

}

function BuildButton(ACaption,AClick)
{
 var buf = new StringBuffer();
 buf.xAppend(['<button ','onclick="javascript:',AClick,'">',ACaption,'</button>']);
 return buf.toString();
}

function BuildHRef(ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['href="javascript:',ARef,'"']);
 return buf.toString();
}

function BuildLink(AId,AClass,ACapt,ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['<a ',BuildHRef(ARef),' id="',AId,'">',ACapt,'</a>']);
 return buf.toString();
}

function BuildSpan(AId,AClass,AText)
{
 var buf = new StringBuffer();
 buf.xAppend(['<span class="',AClass,'" id="',AId,'">',AText,'</span>']);
 return buf.toString();
}

function BuildDiv(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<div ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</div>']);
 return buf.toString();
}

function BuildPara(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<p ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</p>']);
 return buf.toString();
}

function ShowDialog(ActionCMD)
{
 var newdiv = document.createElement('div');
 var main = document.getElementById('learnit_main');
 newdiv.className = 'overlay';
 newdiv.id = 'overlay';
 var h1 = document.body.clientHeight+158 + 'px';
 newdiv.style.height = h1;
 var w1 = '1480px';
 newdiv.style.width = w1;
 main.appendChild(newdiv);
 
 newdiv = document.createElement('div');
 newdiv.className = 'diadiv';
 newdiv.id = 'diadiv';
// var h2 = document.body.clientHeight-158 + 'px';
 var h2 = '100%';
 newdiv.style.height = h2;
// var w2 = document.body.clientWidth-100 + 'px';
 var w2 = '100%';
 newdiv.style.width = w2;
 newdiv.innerHTML = MakeDialog();
 main.appendChild(newdiv); 
 
 AdjustSizes();
 
 populateDialogContent(ActionCMD);
 
}

function AdjustSizes()
{
 var d = document.getElementById('dlg');
 if (d == null) return;
 var b = document.getElementById('dbody');
 var c = document.getElementById('capt');
 //var t = document.getElementById('dtxt');
 /*var emPerpxX = 25/d.offsetWidth;
 var emPerpxY = 13/d.offsetHeight;
 
 b.style.height = (d.offsetHeight - c.offsetHeight)*emPerpxY + 'em';
 var w = d.offsetWidth*emPerpxX + 'em';
 b.style.width = w;
 c.style.width = w;
 t.style.height = b.offsetHeight*emPerpxY - 2 + 'em';
 t.style.width = (b.offsetWidth - 101)*emPerpxX + 'em';
 */
 b.style.height = '520px';
 b.style.width = '680px';
 c.style.width = '700px';
 //t.style.height = b.offsetHeight*emPerpxY - 2 + 'em';
 //t.style.width = (b.offsetWidth - 101)*emPerpxX + 'em';
 
 	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#dlg").height();
	var popupWidth = $("#dlg").width();
	d.style.top = '-100px';
	d.style.left = '220px';	
}

function populateDialogContent(ActionCMD)
{
	sendRequest("/cteach/register_ajax.php",ActionCMD,"post");
}
function MakeDialog()
{
 var buf = new StringBuffer();
 /*buf.xAppend(BuildLink('close','diaqbut','X','CloseDialog()'));*/
 buf.xAppend([BuildLink('close','diaqbut','x','CloseDialog()'),
              BuildSpan('caption','capt','')]);
 var s = buf.toString();
 buf.cleanUp();
 var capt = BuildDiv('capt','diacapt',s);


/* s  = BuildPara('para','diapara','CSS Modal Dialog recipe from ExplainThat!') +
      BuildDiv('btns','diaclose',BuildButton('Close','CloseDialog()'));

 s =    BuildDiv('dtxt','diatxt',s) +
 BuildDiv('dimg','diaimg','<img width=101 height=94 src="/images/dlgimg.png"');
*/
 s = BuildDiv('dcontent','diacontent','<br><br>Loading...');
 s = BuildDiv('dbody','diabody',s);

 return BuildDiv('dlg','diaframe',capt + s);
}
/**********************************************************************
/
/
/    StringBuffer is a simple utility object which delivers
/     faster string concatenation than the Javascript + operator
/
***********************************************************************/
function StringBuffer() {this.buffer = []} 
StringBuffer.prototype.append = function append(string) {this.buffer.push(string)} 
StringBuffer.prototype.xAppend = 
function xAppend(strings)
{
 var i,ALen;
 ALen = strings.length;
 for (i=0;i<ALen;i++) this.buffer.push(strings[i]);
}
StringBuffer.prototype.cleanUp = function cleanUp(){this.buffer = []}
StringBuffer.prototype.toString = function toString(){return this.buffer.join("")}
  

