var req;

function loadPage(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open('GET', url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open('GET', url, true);
            req.send();
        }
    } else {
		updateMessage('cannot check mail');
	}
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    switch (req.readyState) {
		case 1:
			updateMessage('checking mail ...');
			break;

		case 4:
			// only if "OK"
			if (req.status == 200) {
				updateMessage(req.responseText);
				if (isIE) {
					/*loadPopup();*/
				}
			} else {
				// Don't do anything on errors; probably a transient failure,
				// and we'll try again after the next interval.
			}
			break;
    }
}

function updateMessage(message)
{
    if (document.all) {
        document.all.checkemail.innerHTML = message;
    } else {
        document.getElementById('checkemail').innerHTML = message;
    }
}

function updateEmail()
{
	try {
		loadPage("http://hub.ou.edu/email/check_mail.php");
	} catch (e) {
		// Don't do anything on errors; probably a transient failure,
		// and we'll try again after the next interval.
	}

	// Schedule the next update for 5 minutes and 5 seconds.
	window.setTimeout('updateEmail();', 5.09 * 60 * 1000);
}



/************************************************************/

var oPopup = null;
var idFlyPopup = -1;

function loadPopup()
{
	oPopup = window.createPopup();
	AnimateNotify();
}

function flyInit()
{
	flyMove.curH = 0;
	if (window.document.dir === "rtl")
		flyMove.curX = 0;
	else {
		flyMove.curX = window.screen.availWidth - flyMove.flyWidth;
		flyMove.curY = window.screen.availHeight;
	}
}

function flyMove()
{
	flyMove.curH += 2;
	flyMove.curY -= 2;
	oPopup.show(flyMove.curX, flyMove.curY, flyMove.flyWidth, flyMove.curH);
	var oPopupBody = oPopup.document.body;
	
	if (flyMove.curH >= flyMove.flyHeight)
	{
		window.clearInterval(idFlyPopup);
		idFlyPopup = -1;
		/*window.setTimeout('closePopup()', 15000);*/
	}
}

flyMove.curH = 0;
flyMove.curY = 0;
flyMove.curX = 0;
flyMove.flyHeight = 126;
flyMove.flyWidth = 130;

function AnimateNotify()
{
	if (null == oPopup)
		return;

	var message = "You have 2 new emails.";

	var oPopupBody = oPopup.document.body;
	var szHTML = "<TABLE height=100% width=100% cellpadding=4 cellspacing=0 style=\"font-family: Arial;font-size:100%; color: #fff;\">"+
				 "<TR style=\"background-image:url(newitem.gif);background-repeat:no-repeat;background-position:99% 99%\">"+
				 "<TD align=middle >"+ message +"</TD></TR></TABLE>";

	oPopupBody.innerHTML = szHTML;
	oPopupBody.style.fontSize = document.body.currentStyle.fontSize;
	oPopupBody.style.cursor="hand";
	oPopupBody.style.color = "black";
	oPopupBody.style.borderWidth='3px';
	oPopupBody.style.borderStyle='window-inset';
	oPopupBody.style.borderColor='activeborder';
	oPopupBody.style.backgroundImage="url(nin-bg.gif)";
	oPopupBody.style.backgroundRepeat="no-repeat";
	oPopupBody.style.backgroundPosition="center center";
	oPopupBody.style.backgroundColor="#8D9FB7";

	flyInit();
	
	if (idFlyPopup == -1)
		idFlyPopup = window.setInterval(flyMove,10);
}

function closePopup()
{
	if (null != oPopup)
		oPopup.hide();
}
