COMP.LoadStylesheet('rws_bulletins');

RWS.Bulletins=
{
	
	Request : function()
	{
		COMP.SendAjaxCommand('rws_request_bulletins', {}, this.Add, this);
	}
	
	,Clear : function()
	{
		Ext.getCmp('bulletinList').removeAll();
	}
	
	,Add : function(o)
	{
		var bulletinList = Ext.getCmp('bulletinList');
		
		for (var i=0; i<o.length; i++)
		{
			bulletinList.add({
				xtype:'panel'
				,cls:'bulletinPanel'
				,collapsible:true
				,collapsed:true
				,titleCollapse: true
				,border:true
				,title: o[i].title
				,autoLoad:{url: '/bulletin/' + o[i].url, callback:function(){bulletinList.doLayout(); RWS.Bulletins.convertAnchors();}}
			});
		}
		bulletinList.doLayout();
	} 
	
	,convertAnchors : function()
	{
		// Parse through the body and modify anchors.
		var anchors = Ext.getCmp('bulletinList').el.dom.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++)
		{
			var a = anchors[i];
			var url = a.innerHTML; 
			if(a.hostname == window.location.hostname)
				a.href = 'http:/' + a.pathname;
			a.target = "_blank";

		}
	}
};

// Copy of the old COMP.Bulletins which used to also be used for maps. This could well be simplified in the future.
// There is a contentViewer in the LWS we should consider using.
RWS.Maps=
{
	bulletinPanel:
	{
		xtype: 'panel'
		,title: L(3639) // 'Bulletin Board'
		,id:'bulletinList'
		,collapsible: true
		,cls:'bluePanel'
		,animCollapse: true
		,defaultType: 'button'
	}
	
	,Request : function()
	{
		COMP.SendAjaxCommand('rws_request_bulletins');
	}
	
	,Clear : function()
	{
		//Ext.getCmp('bulletinList').clear();
	}
	
	,Add : function(o)
	{
		RWS.Maps.Clear();
		
		var bulletinList = Ext.getCmp('bulletinList');
		for (var i=0; i<o.length; i++)
		{
			bulletinList.add({
				xtype:'panel'
				,cls:'bluePanel'
				,items:[{
					xtype:'button'
					,text:o[i][1]
					,cls:'navButton'
					,url:'/bulletin/' + o[i][0]
					,onClick: function() { RWS.Maps.Show(this);	}
				}]
			});
		}
		bulletinList.doLayout();
		StylePanelLightDark(bulletinList);
	}
	
	,Show : function(button)
	{
		var url = COMP.AssembleURL(typeof(button)=='string' ? button : button.url); // innocent hack to jam in maps
		var me = RWS.Maps;
		me.dialog = new Ext.Window({
			title: ''
			,modal: true
			,width:800
			,height:527
			,bodyStyle:'background-color: #FFFFFF;'
			,closeAction: 'close'
			,layout:'fit'
			,border:true
			,autoScroll:true
			,resizable:false
			,listeners:
			{
				beforeclose:function() {me.dialog=null; ResetFocus();}
			}
			,autoLoad:
			{
				url:url
				,callback:me.convertAnchors
			}
		});
		me.dialog.show();
	}
	
	,convertAnchors : function()
	{
		// Parse through the body and modify anchors.
		var me = RWS.Maps;
		var anchors = me.dialog.body.dom.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++)
		{
			var a = anchors[i];
			var url = a.pathname;
			a.href = null;
			a.onclick = function() 
			{
				me.dialog.load({
					url:url
					,callback:me.convertAnchors
				});
				return false; 
			}
		}
	}
};
if (window.COMP) COMP.Broadcast('scriptLoaded_rws_bulletins');