/*
	Author:
		Hubert Seales
	Creation Date:
		11/20/2007
	Description:
		This file checks for notifications by executing a GET on the ajaxControllerPHP.
	Changelog
		0.1:	Initial release
		0.2:	Checking for inbox id existence BEFORE polling
		0.3:	XML removed, new ajaxController now in use
*/


/* ##### <CONFIG> */
var maxMinutes  = 10;
/* ##### </CONFIG> */

var doNotify	= 1;
var mytime= null;

function doClick() {
	js.executeMe();
}

function startNotify() {
	// If the inbox div doesn't exist, DO NOT poll, this means that the page has no leftnav
	var inboxDiv	= document.getElementById('inbox');
	if( inboxDiv != null) {
		mytime=setInterval('loadController()', 10000); // 10 seconds
		var stopTime	= (maxMinutes * 60) * 1000;
		setTimeout('stopNotify()', stopTime);
	}
}

function loadController() {
    if (myAjaxObject.ajaxRequest.readyState == 0 || myAjaxObject.ajaxRequest.readyState == 4){
        myAjaxObject.sendRequest(
            ajaxControllerPHP,
            {
                method: 'GET',
                parameters: {
                    action: 'notifications'
                },
                callback: 'loadControllerCallback'
            }
        );
    }
}

function loadControllerCallback() {
    if ( myAjaxObject.success == 1 ) {
        var notifications  = myAjaxObject.details['notifications'];
		for( var key in notifications) {
			var val	= notifications[key];
			if (document.getElementById(key)!=='undefined' && document.getElementById(key)!==null){
				if( val > 0 ) {		
						document.getElementById(key).style.display = '';	
				} else {
					document.getElementById(key).style.display = "none";
				}
			}
		}
    }
}

function stopNotify() {
	doNotify	= 0;
	clearInterval(mytime);
}


