//
// global variables
//
var isMozilla;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;
var twidth = 0;
var theight = 0;
var dimPage = false;
var displayIt = true;

function displayWindow() {
	var w, h, l, t;
	w=450; h=275; 
	//l=screen.width/2; alert ("l="+l); //t=screen.height/2 ; alert ("t="+t);
	l=getWidth()/2 -w/2; t=getHeight()/2 -h/2; 
	displayFloatingDiv('alert',alertTitle, w, h, l, t,'alertlink');
	if ( ! displayIt ) hideFloatingDiv('alert','alertlink');
}

function getWidth() {
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth ;
}

function getHeight() {
	var myHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && document.body.clientHeight ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight ;
}

function displayFloatingDiv(divId, title, width, height, left, top, linkname) {
	DivID = divId;
	if ( thisAlert == "" ) return; // only display this when something exists
	document.getElementById(linkname).style.display='none';
	if ( dimPage ){
		document.getElementById('dimmer').style.visibility = "visible";
		document.getElementById('dimmer').style.display='block';
	}

	twidth = parseInt(document.getElementById(divId).style.width);
	theight = parseInt(document.getElementById(divId).style.height);
	//alert ("twidth="+twidth+" theight="+theight);
    document.getElementById(divId).style.left = left + 'px';
    document.getElementById(divId).style.top = top + 'px';
	if ( displayIt ){ 
		document.getElementById(divId).style.visibility = "visible";
		document.getElementById(divId).style.display = "block";
	}
	growFloatingDiv(divId, width, height);

	var addHeader;
	
	if (originalDivHTML == "")  originalDivHTML = document.getElementById(divId).innerHTML;
	
	//addHeader = '<table style="width:' + width + 'px" class="floatingHeader">' +
	addHeader = '<table style="width:100%" class="floatingHeader">' +
	            '<tr><td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px">' + title + '</td>' + 
	            '<td style="width:18px" align="right"><a href="javascript:hideFloatingDiv(\'' + divId + '\',\'alertlink\');void(0);">' + 
	            '<img alt="Close..." title="Close..." src="/images/close.jpg" border="0"></a></td></tr></table>';
	
    // add header to your div  	
	document.getElementById(divId).innerHTML = addHeader + originalDivHTML;
	
}

function growFloatingDiv(divId, width, height) {
	twidth+=5;
	theight+=5;
	if ( twidth > width ) twidth = width ;
	if ( theight > height ) theight = height;
	var moving = false;
	if ( twidth < width ) {moving = true;}
 	if ( theight < height ) {moving = true;}
	document.getElementById(divId).style.width = twidth + 'px';
	document.getElementById(divId).style.height = theight + 'px';
	if ( moving )window.setTimeout("growFloatingDiv('" +divId+"', "+width+", "+height+")",10);
	else addAlertContent(divId);
}

function addAlertContent(divId){
	var DivHTML = document.getElementById(divId).innerHTML;
	DivHTML = DivHTML + thisAlert;
	document.getElementById(divId).innerHTML = DivHTML;
}

function hideFloatingDiv(divId,linkname) {
	//document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.visibility='hidden';
	document.getElementById(divId).style.display='none';
	document.getElementById(linkname).style.visibility='visible';
	document.getElementById(linkname).style.display='inherit';
	if ( dimPage ){
		document.getElementById('dimmer').style.visibility = 'hidden'; // to dim the page
		document.getElementById('dimmer').style.display = 'none'; // to dim the page
	}
	DivID = "";
}

function recallFloatingDiv(divId,linkname) {
	//document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.visibility='visible';
	document.getElementById(divId).style.display='block';
	document.getElementById(linkname).style.visibility='hidden';
	document.getElementById(linkname).style.display='inherit';
	if ( dimPage ){
		document.getElementById('dimmer').style.visibility = 'visible'; // to dim the page
		document.getElementById('dimmer').style.display = 'block'; // to dim the page
	}
	DivID = divId;
}

function buildDimmerDiv(){
    document.write('<div id="dimmer" class="dimmer" style="width:'+ window.screen.width + 'px; height:' + window.screen.height +'px"></div>');
}

function MouseDown(e) {
    if (over) {
        if (isMozilla) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        } else {
            objDiv = document.getElementById(DivID);
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

function MouseMove(e) {
    if (objDiv) {
        if (isMozilla) {
            objDiv.style.top = (e.pageY-Y) + 'px';
            objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        } else {
            objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;
            return false;
        }
    }
}

function MouseUp() {
    objDiv = null;
}

function init() {
    // check browser
    isMozilla = (document.all) ? 0 : 1;

    if (isMozilla) {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }

    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
	
	if ( dimPage ) buildDimmerDiv();

}