var unreadCount = 0;

function refreshTreeView(){

	try {
		// use DWR to build the tree view contents
		InboxEventManager.getInboxTreeNodes(function(data) {
			
			d = new dTree('d');
		
			for(var i=0;i<data.length;i++){
				var node=data[i];
				d.add(node.id,node.parentId,node.text,node.url,node.hoverText,node.image);
			}
			document.getElementById('tree').innerHTML=d.toString();
		});
		
		InboxEventManager.getUnreadCount(function(count){
			if(unreadCount>count){
				// new item - create sound
			}
			unreadCount=count;
		});
	} catch (e) {}
}

addLoadEvent(refreshTreeView);

function viewEvent(urlPath,tableId,eventId){
	InboxEventManager.markInboxEntryRead(eventId,function(){
		location.href = document.getElementById('ctx').value+'/'+urlPath+'?id='+tableId;
	});
}

function deleteEvent(eventId){
	InboxEventManager.deleteInboxEntry(eventId,function(){
		refreshTreeView();
	});
}

// function to refresh the inbox after a minute if the page has not been refreshed
window.setInterval(refreshTreeView,60000);

// function to flash the inbox in red if there are unread items
window.setInterval(flashRed,2000);

function flashRed(){
	if(unreadCount==0)
		return;
		
	var box = document.getElementById('tree_frame');
	if(box!=undefined){
		if(box.className=='grey'){
			box.className='red';
			window.setTimeout(restoreGrey,1000);
		}
	}
}

function restoreGrey(){
	var box = document.getElementById('tree_frame');
	if(box!=undefined){
		if(box.className=='red')
			box.className='grey';
	}
}

function playSound(){
	var soundFile = document.getElementById('ctx').value+'/sounds/ringin.wav';
	Sound.play(soundFile,{replace:true});
	alert('playing '+soundFile);
}