var unreadCount = 0;
var currentGroupName = '';

function refreshTreeView(){

	try {
		// use DWR to build the tree view contents
		InboxEventManager.getInboxTreeNodes(currentGroupName,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,null,node.image,null,null,node.a_params);
			}
			document.getElementById('tree').innerHTML=d.toString();
			d.openAll();
			RightContext.initialize();
		});
		
		InboxEventManager.getInboxAlarmStatus(function(status){
			if(status.playSound){
				$('sound1').Play();
				showInbox();
			}
			unreadCount=status.unreadCount;
			if(unreadCount>0)
				document.getElementById('expandCollapse').title = unreadCount+' unread';
			else
				document.getElementById('expandCollapse').title = '';
		});
		SessionUtilsManager.getSessionAttribute('showInbox',function(show){
			if(show=='no')
				hideInbox();
			else showInbox();
		});
	} catch (e) {}
}

addLoadEvent(refreshSelectionBox);

var selectionCountMap = {
		put : function(foo,bar) {this[foo] = bar;},
		get : function(foo) {return this[foo];}
	};

function refreshSelectionBox(){
	UserFilterManager.getSelectionsHTML(function(html){
		$('selectionsBox').innerHTML = html;
		RightContext.initialize();
	});
	UserFilterManager.getSelectionCountMap(function(list){
		for(var i=0;i<list.length;i++)
			selectionCountMap.put(list[i].label,list[i].value);
	});
}

function viewEvent(urlPath,tableId,eventId){
	InboxEventManager.markInboxEntryRead(eventId,function(){
		location.href = ctx()+'/'+urlPath+'?id='+tableId;
	});
}

function deleteEvent(eventId){
	InboxEventManager.deleteInboxEntry(eventId,function(){
		refreshTreeView();
	});
}

function expandEventGroup(groupName){
	currentGroupName = groupName;
	refreshTreeView();
}



function flashRed(){
	var tree = $('tree_frame');
	if(unreadCount==0)
		if(tree.className=='grey')
			return;
		
	var arrow = $('expandCollapse');
	if(tree){
		if(tree.className=='grey'){
			tree.className='red';
			if(arrow.src.indexOf('left')>-1)
				arrow.src = ctx() + '/images/red_arrow_left.png';
			else
				arrow.src = ctx() + '/images/red_arrow_right.png';
		} else {
			tree.className='grey';
			if(arrow.src.indexOf('left')>-1)
				arrow.src = ctx() + '/images/blue_arrow_left.png';
			else
				arrow.src = ctx() + '/images/blue_arrow_right.png';
		}
	}
}

function showHideInbox(){
	var box = document.getElementById('tree_frame');
	if(box.style.display=='none')
		showInbox();
	else hideInbox();
}

function showInbox(){
	var tree = document.getElementById('tree_frame');
	if(tree.style.display!='none')
		return;
	
	tree.style.display='block';
	document.getElementById('expandCollapse').src=ctx()+'/images/blue_arrow_left.png';
	document.getElementById('page').style.margin = '0px 260px';
	if(document.getElementById('layers_frame')){
		document.getElementById('layers_frame').style.display='block';
	}
	if(document.getElementById('scheduler_frame')){
		document.getElementById('scheduler_frame').style.display='block';
	}
	SessionUtilsManager.setSessionAttribute('showInbox','yes');
}

function hideInbox(){
	var tree = document.getElementById('tree_frame');
	if(tree.style.display=='none')
		return;
	
	tree.style.display='none';
	document.getElementById('expandCollapse').src=ctx()+'/images/blue_arrow_right.png';
	document.getElementById('page').style.margin = '0px 30px';
	if(document.getElementById('layers_frame')){
		document.getElementById('layers_frame').style.display='none';
	}
	if(document.getElementById('scheduler_frame')){
		document.getElementById('scheduler_frame').style.display='none';
	}
	SessionUtilsManager.setSessionAttribute('showInbox','no');
}

//define a menu
inboxGroupMenu = { attributes: "groupName" ,

          items: [{type:RightContext.TYPE_MENU,
					text:Labels.clearAllNotifications,
					onclick:function() {clearAllNotifications([groupName]);}},

	                {type:RightContext.TYPE_MENU,
                  	text:Labels.markAllAsRead,
                  	onclick:function() {markAllAsRead([groupName]);}}

                 ]
         };

RightContext.addMenu("inboxGroupMenu", inboxGroupMenu);

function clearAllNotifications(groupName){
	InboxEventManager.clearAllNotifications(groupName,refreshTreeView);	
}

function markAllAsRead(groupName){
	InboxEventManager.markAllAsRead(groupName,refreshTreeView);	
}

addLoadEvent(refreshTreeView);
window.setInterval(refreshTreeView,60000);
window.setInterval(flashRed,1000);




