/**
 * DhtmlModalDialog
 * DEPENDENCIES-: ReadWriteCommObj.js
 */
 
/***************************************/
var PROFICIENT_SM_SERVER_HOSTNAME = "sm-cdc.a.proficient.com";
var baseHrefSyntax = (location.protocol == "https:")? "https://" + PROFICIENT_SM_SERVER_HOSTNAME  : "http://" + PROFICIENT_SM_SERVER_HOSTNAME;
if (PROFICIENT_SM_SERVER_HOSTNAME == "") { baseHrefSyntax = ""; }
if (location.href.toString().lastIndexOf("|N|http") < 0) {
	document.write("<script id='PsysApplet' src='" + baseHrefSyntax + "/scripts/customer/CustomerApplet.js'>" + "</script>");
}
/***************************************/

var globalId;
var truePosY;
var dhtmlHeight;
var idCount = 0;

function DhtmlDialog(_id, _width, _height)
{
	globalId = _id;
	dhtmlHeight = _height;
	
    this.id = _id;
    this.width = _width;
    this.height = _height;
    this.dCanvas;
    this.canvasExists = false;
	
	idCount++;
}

DhtmlDialog.prototype.createDialogCanvas = function(_content, _justify)
{
    var nL;
    var dPos;
	var nHtml;
    
    // NOTE-: Determine Dialog Position
    dPos = this.getDialogPosition(_justify);
    
    // NOTE-: Write Canvas;
	nHtml = ""
            + '<div id="maskLayer" style="display: none; position: absolute; top: 0px; left: 0px; width:' + document.body.scrollWidth + '; height:' + document.body.scrollHeight + '; z-index: 1;">'
            + '<img src="' + document.psysControlF.PROFICIENT_SERVER_BASE_URL + '/graphics/spacer.gif" width="100%" height="100%" alt=""/>'
            + '</div>'
            + '<div id="' + this.id + '"'
            + ' style="z-index: 2; display: none; border: 0px solid black; left:' + dPos.x + 'px; top:' + dPos.y + 'px; width:' + this.width + 'px;height:' + this.height + 'px; position:absolute">'
            + _content
		    + '</div>';

    document.body.insertAdjacentHTML('beforeEnd', nHtml);

    this.dCanvas = document.getElementById(this.id);
}

DhtmlDialog.prototype.getDialogPosition = function(_justify)
{
    var refP;
    var trueP = new Object();
    
    switch (_justify)
    {
        default :
            refP = getWindowCenter();
    }
    
    trueP.x = refP.x - (this.width/2);
    trueP.y = refP.y - (this.height/2);
    
	truePosY = trueP.y;
	
    return trueP;
}
 
DhtmlDialog.prototype.getContent = function(_url, _display)
{
    return this.commChannel.run(_url, _display, 0);
}
 
DhtmlDialog.prototype.run = function(_content)
{
    var dCanvas;
    var dialogContent;
    
    // NOTE-: Create Canvas to show.
    if (!this.canvasExists) { 
        this.createDialogCanvas();
        this.canvasExists = true; 
    }

    // NOTE-: Get Information to show.
    this.write(_content);
    
    // NOTE-: show the content layer;
    this.showContent();
	window.focus();
}
 
DhtmlDialog.prototype.showContent = function()
{
    this.dCanvas.style.display = "inline";
	moveContent();
}

DhtmlDialog.prototype.write = function(_content)
{
    document.getElementById(this.id).innerHTML = _content;
}

function moveContent()
{
	var layerTop = document.getElementById(globalId);
    var refP;
    var trueP = new Object();
	var adjustedY;
	
	if(layerTop.style.display != "inline"){ return; }
		
	refP = getWindowCenter();
	trueP.y = refP.y - (dhtmlHeight/2);
	
	if(idCount == 1){ 
		adjustedY = trueP.y + "px"; 
		layerTop.style.top = adjustedY;
	}
	else{ return; }
	
	setTimeout('moveContent()',10);
}

/*** NOTE-: Global Methods */
function getWindowCenter()
{	
	var clientHeight;
	var clientWidth;
	var docTop;
	var docLeft;

    clientHeight = document.body.clientHeight;
    clientWidth = document.body.clientWidth;
    docTop = document.body.scrollTop;
    docLeft = document.body.scrollLeft;
    docHeight = document.body.offsetHeight;
    docWidth = document.body.offsetWidth;

    var loc = new Object();
    
    loc.clientW = clientHeight;
    loc.clientH = clientHeight;
    loc.x = docLeft + clientWidth/2;
    loc.y = docTop + clientHeight/2;
    return loc;
}

var dhtmlDialog = new Array();