// create a namespace

YAHOO.namespace("newspress");
			
// define the namespace object
YAHOO.newspress.list_filter = function() {
	
	/* shorthand dor YUI libraries */ 
	var yue = YAHOO.util.Event;
	var yud = YAHOO.util.Dom;
		
	/* vars */	
	
	
	var data_table_array = [];
	
	// array of list items
			
	var list_item_array_news_and_press = [];
	var list_item_array_news = [];
	var list_item_array_press = [];
	
	var list_item_nodes = [];
	
	var list_data_table_news_and_press;
	var list_data_table_news;
	var list_data_table_press;
	
	var list_data_table_news_and_press_initial_page;
	var list_data_table_news_initial_page;
	var list_data_table_press_initial_page;
	
	// current filter setting	
	var filter_type;
	
	var doc_id;
	
	// default page being shown is 1
	var filter_page_number;	
			
	// paginaiton setting	
	var pagination_rows_per_page;
	
	var number_of_news_items_to_show;
	var number_of_press_items_to_show;
	
	// counts of items
	var news_count;
	var press_count;		
	var news_and_press_count;
	
	return {									
		// initialise function
		init: function(){
			
			// ge the doc id of the news/press document
			YAHOO.newspress.list_filter.doc_id = document.getElementById('doc_id').value;
			
			YAHOO.newspress.list_filter.news_and_press_count = 0;
			YAHOO.newspress.list_filter.news_count = 0;
			YAHOO.newspress.list_filter.press_count = 0;
																
			// iniitialise the filter state variables from the hidden inputs
			YAHOO.newspress.list_filter.get_filter_state();
			
			YAHOO.newspress.list_filter.set_filter_link_style();
			
			// get the list items from the page
			YAHOO.newspress.list_filter.get_list_items();
								
			var oElement = document.getElementById('news_and_press_filter_link');								
			yue.addListener(oElement,'click', YAHOO.newspress.list_filter.sort_list,'news_and_press');
			
			var oElement = document.getElementById('news_by_year_filter_link');								
			yue.addListener(oElement,'click', YAHOO.newspress.list_filter.sort_list,'news_by_year');
			
			var oElement = document.getElementById('press_by_year_filter_link');								
			yue.addListener(oElement,'click', YAHOO.newspress.list_filter.sort_list,'press_by_year');
			
			var oElement = document.getElementById('news_by_project_filter_link');								
			yue.addListener(oElement,'click', YAHOO.newspress.list_filter.sort_list,'news_by_project');
			
			var oElement = document.getElementById('press_by_project_filter_link');								
			yue.addListener(oElement,'click', YAHOO.newspress.list_filter.sort_list,'press_by_project');
			
			// set initial pagination link listeners
			//YAHOO.newspress.list_filter.set_pagination_link_listeners();								
															
		},
		
		// get list items
		get_list_items: function(){
		
			YAHOO.newspress.list_filter.list_item_nodes = yud.getElementsByClassName('list_link');
										
			YAHOO.newspress.list_filter.list_item_array_news_and_press = [];
			YAHOO.newspress.list_filter.list_item_array_news = [];
			YAHOO.newspress.list_filter.list_item_array_press = [];
										
			// loop through all list items, get attributes & push an object onto the array					
			for(var i=0;i<YAHOO.newspress.list_filter.list_item_nodes.length;i++){
				
				var doc_id 		= YAHOO.newspress.list_filter.list_item_nodes[i].id;
				var date 		= YAHOO.newspress.list_filter.list_item_nodes[i].getAttribute('date');				
				var news_or_press 	= YAHOO.newspress.list_filter.list_item_nodes[i].getAttribute('news_or_press');			
				var href 		= YAHOO.newspress.list_filter.list_item_nodes[i].getAttribute('href');
				var inner_html		= YAHOO.newspress.list_filter.list_item_nodes[i].innerHTML;
				var name 		= YAHOO.newspress.list_filter.list_item_nodes[i].getAttribute('alt');
													
				// get the projects this item applies to																
				var project_array = yud.getElementsByClassName('row_project','div',YAHOO.newspress.list_filter.list_item_nodes[i]);
				
				// if no projects associated still push on
				if(project_array.length == 0){
					
					if(news_or_press=='News'){				
						YAHOO.newspress.list_filter.list_item_array_news.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,							
							project:'', 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_count++;
					}
					
					if(news_or_press=='Press'){				
						YAHOO.newspress.list_filter.list_item_array_press.push({
									doc_id: doc_id, 
									date:date.substring(date.length-4, date.length), 		
									news_or_press:news_or_press,
									project:'', 
									href:href, 
									inner_html:inner_html, 
									name:name
						});
						YAHOO.newspress.list_filter.press_count++;
					}
					
					if(news_or_press=='Press' && YAHOO.newspress.list_filter.press_count <=  YAHOO.newspress.list_filter.number_of_press_items_to_show ){
						YAHOO.newspress.list_filter.list_item_array_news_and_press.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,
							project:'', 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_and_press_count++;
					}
					if(news_or_press=='News' && YAHOO.newspress.list_filter.news_count <=  YAHOO.newspress.list_filter.number_of_news_items_to_show ){
						YAHOO.newspress.list_filter.list_item_array_news_and_press.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,
							project:'', 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_and_press_count++;
					}
					
				}
				for(var j=0;j<project_array.length;j++){
													
					if(news_or_press=='News'){				
						YAHOO.newspress.list_filter.list_item_array_news.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,							
							project:project_array[j].innerHTML, 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_count++;
					}
					
					if(news_or_press=='Press'){				
						YAHOO.newspress.list_filter.list_item_array_press.push({
									doc_id: doc_id, 
									date:date.substring(date.length-4, date.length), 		
									news_or_press:news_or_press,
									project:project_array[j].innerHTML, 
									href:href, 
									inner_html:inner_html, 
									name:name
						});
						YAHOO.newspress.list_filter.press_count++;
					}
					
					if(news_or_press=='Press' && YAHOO.newspress.list_filter.press_count <=  YAHOO.newspress.list_filter.number_of_press_items_to_show ){
						YAHOO.newspress.list_filter.list_item_array_news_and_press.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,
							project:project_array[j].innerHTML, 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_and_press_count++;
					}
					if(news_or_press=='News' && YAHOO.newspress.list_filter.news_count <=  YAHOO.newspress.list_filter.number_of_news_items_to_show ){
						YAHOO.newspress.list_filter.list_item_array_news_and_press.push({
							doc_id: doc_id, 
							date:date.substring(date.length-4, date.length), 	
							news_or_press:news_or_press,
							project:project_array[j].innerHTML, 
							href:href, 
							inner_html:inner_html, 
							name:name
						});						
						YAHOO.newspress.list_filter.news_and_press_count++;
					}
				}
												 												
			}	
			YAHOO.newspress.list_filter.initialise_data_table_news_and_press();
			YAHOO.newspress.list_filter.initialise_data_table_news();	
			YAHOO.newspress.list_filter.initialise_data_table_press();
			
			if(YAHOO.newspress.list_filter.filter_type == 'news_and_press'){								
				yud.setStyle(document.getElementById('news_list_datatable'),'display','none');
				yud.setStyle(document.getElementById('press_list_datatable'),'display','none');
				yud.setStyle(document.getElementById('news_press_list_datatable'),'display','block');								
			}
			
			if((YAHOO.newspress.list_filter.filter_type == 'news_by_year')||(YAHOO.newspress.list_filter.filter_type == 'news_by_project')){								
				yud.setStyle(document.getElementById('news_list_datatable'),'display','block');
				yud.setStyle(document.getElementById('press_list_datatable'),'display','none');
				yud.setStyle(document.getElementById('news_press_list_datatable'),'display','none');				
			}
			
			if((YAHOO.newspress.list_filter.filter_type == 'press_by_year')||(YAHOO.newspress.list_filter.filter_type == 'press_by_project')){								
				yud.setStyle(document.getElementById('news_list_datatable'),'display','none');
				yud.setStyle(document.getElementById('press_list_datatable'),'display','block');
				yud.setStyle(document.getElementById('news_press_list_datatable'),'display','none');				
			}
			
		},
		
		initialise_data_table_news_and_press: function(){
			
			// make a DataTable aout of the array
			var list_data_source = new YAHOO.util.DataSource(YAHOO.newspress.list_filter.list_item_array_news_and_press);
			list_data_source.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			list_data_source.responseSchema = {
				fields: ["doc_id","news_or_press","date","project","href","inner_html","name"]
			};

			var list_colum_defs = [
				{key:"doc_id", label:"doc_id", className:"hidden_col"},
				{key:"news_or_press", className:"hidden_col",label:"news_or_press", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_news_or_press,defaultOrder:"asc"}},
				{key:"date", label:"date",className:"hidden_col", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_date,defaultOrder:"desc"}},												
				{key:"project",className:"hidden_col", label:"project", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_project,defaultOrder:"asc"}},
				{key:"href", className:"hidden_col",label:"href"},
				{key:"inner_html", className:"", label:"inner_html"},
				{key:"name", className:"hidden_col",label:"name",sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_name,defaultOrder:"asc"}}
			];

			// only enable pagination if we need to ie the number of rows exceeds the rows per page setting
			if(YAHOO.newspress.list_filter.news_and_press_count > YAHOO.newspress.list_filter.pagination_rows_per_page){
				var paginated = true;
			}else{
				var paginated = false;
			}				
			
			var list_data_table_configs = { 
				paginated:paginated, // Enables built-in client-side pagination 
				paginator:{ // Configurable options 
				containers: null, // Create container DIVs dynamically 
				currentPage: YAHOO.newspress.list_filter.filter_page_number, // Show page  
				dropdownOptions: null, // Show these in the rows-per-page dropdown 
				pageLinks: 0, // Show links to all pages 
				rowsPerPage: YAHOO.newspress.list_filter.pagination_rows_per_page // Show up to 500 rows per page 
				} 
			}; 
						
			// create the data table
			YAHOO.newspress.list_filter.list_data_table_news_and_press = new YAHOO.widget.DataTable(
				"news_press_list_datatable", 
				list_colum_defs,
				list_data_source, 
				list_data_table_configs,{sortedBy:{key:'name', dir:"asc"}}
			); 
																				
			/* ----------------------------------------------------------------------------------------------------------------------- */
			/* IMPORTANT - both the sortColumn and showPage calls cause the refreshEvent to be invoked, this will obviously 
			/* cause anything subscribed to this event to also be called - so only place the subscribe statement above these if the
			/* consequences of the subscribed function running twice are desirable! I am putting it inbetween so it triggers once */
			
			// initial sort - NOTE performing this resets pagination MUCHOS IMPORTANT
			var sort_column = YAHOO.newspress.list_filter.list_data_table_news_and_press.getColumnSet().getColumn('news_or_press');																		
			YAHOO.newspress.list_filter.list_data_table_news_and_press.sortColumn(sort_column);
			
			// if the page number has not been received, set it so that we go to the page the news/press item is on
					
			if(YAHOO.newspress.list_filter.filter_page_number == 0){
				YAHOO.newspress.list_filter.list_data_table_news_and_press_initial_page = YAHOO.newspress.list_filter.get_initial_pagination_page('news_and_press');
			}else{
				YAHOO.newspress.list_filter.list_data_table_news_and_press_initial_page = YAHOO.newspress.list_filter.filter_page_number;
			}
																										
			// this is required as the sort triggers a table refresh.. which resets to page 1!			
			YAHOO.newspress.list_filter.list_data_table_news_and_press.showPage(YAHOO.newspress.list_filter.list_data_table_news_and_press_initial_page);
			/* ----------------------------------------------------------------------------------------------------------------------- */

			// subscribe to the table refresh event the handlers for pagination
			YAHOO.newspress.list_filter.list_data_table_news_and_press.subscribe("refreshEvent",YAHOO.newspress.list_filter.set_pagination_link_listeners);
			
			if(YAHOO.newspress.list_filter.filter_type=='news_and_press'){
				// refresh the view only for this type of filtering
				YAHOO.newspress.list_filter.list_data_table_news_and_press.refreshView();
			}
			
		},
		
		initialise_data_table_news: function(){
			
			// make a DataTable aout of the array
			var list_data_source = new YAHOO.util.DataSource(YAHOO.newspress.list_filter.list_item_array_news);
			list_data_source.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			list_data_source.responseSchema = {
				fields: ["doc_id","news_or_press","date","project","href","inner_html","name"]
			};

			var list_colum_defs = [
				{key:"doc_id", label:"doc_id", className:"hidden_col"},
				{key:"news_or_press", className:"hidden_col",label:"news_or_press", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_news_or_press,defaultOrder:"asc"}},
				{key:"date", label:"date",className:"hidden_col", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_date,defaultOrder:"desc"}},												
				{key:"project",className:"hidden_col", label:"project", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_project,defaultOrder:"asc"}},
				{key:"href", className:"hidden_col",label:"href"},
				{key:"inner_html", className:"", label:"inner_html"},
				{key:"name", className:"hidden_col",label:"name",sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_name,defaultOrder:"asc"}}
			];

			// only enable pagination if we need to ie the number of rows exceeds the rows per page setting
			if(YAHOO.newspress.list_filter.news_count > YAHOO.newspress.list_filter.pagination_rows_per_page){
				var paginated = true;
			}else{
				var paginated = false;
			}				
			
			var list_data_table_configs = { 
				paginated:paginated, // Enables built-in client-side pagination 
				paginator:{ // Configurable options 
				containers: null, // Create container DIVs dynamically 
				currentPage: 1, // Show page  
				dropdownOptions: null, // Show these in the rows-per-page dropdown 
				pageLinks: 0, // Show links to all pages 
				rowsPerPage: YAHOO.newspress.list_filter.pagination_rows_per_page // Show up to 500 rows per page 
				} 
			}; 
						
			// create the data table
			YAHOO.newspress.list_filter.list_data_table_news = new YAHOO.widget.DataTable(
				"news_list_datatable", 
				list_colum_defs,
				list_data_source, 
				list_data_table_configs,{sortedBy:{key:'name', dir:"asc"}}
			); 
			
			/* ----------------------------------------------------------------------------------------------------------------------- */
			/* IMPORTANT - both the sortColumn and showPage calls cause the refreshEvent to be invoked, this will obviously 
			/* cause anything subscribed to this event to also be called - so only place the subscribe statement above these if the
			/* consequences of the subscribed function running twice are desirable! I am putting it inbetween so it triggers once */
			
			// initial sort - NOTE performing this resets pagination MUCHOS IMPORTANT
			switch(YAHOO.newspress.list_filter.filter_type){
			
				case 'news_by_project' : 
					var sort_column = YAHOO.newspress.list_filter.list_data_table_news.getColumnSet().getColumn('project');
					// do the sort
					YAHOO.newspress.list_filter.list_data_table_news.sortColumn(sort_column);
			
					// if the page number has not been received, set it so that we go to the page the news/press item is on
					if(YAHOO.newspress.list_filter.filter_page_number == 0){
						YAHOO.newspress.list_filter.list_data_table_news_initial_page = YAHOO.newspress.list_filter.get_initial_pagination_page('news_by_project');
					}else{
						YAHOO.newspress.list_filter.list_data_table_news_initial_page = YAHOO.newspress.list_filter.filter_page_number;
					}					
					break;
					
				case 'news_by_year' : 			
					var sort_column = YAHOO.newspress.list_filter.list_data_table_news.getColumnSet().getColumn('date');
			
					// do the sort
					YAHOO.newspress.list_filter.list_data_table_news.sortColumn(sort_column);
			
					// if the page number has not been received, set it so that we go to the page the news/press item is on
					if(YAHOO.newspress.list_filter.filter_page_number == 0){
						YAHOO.newspress.list_filter.list_data_table_news_initial_page = YAHOO.newspress.list_filter.get_initial_pagination_page('news_by_year');
					}else{
						YAHOO.newspress.list_filter.list_data_table_news_initial_page = YAHOO.newspress.list_filter.filter_page_number;
					}					
					break;
					
				default :
					var sort_column = YAHOO.newspress.list_filter.list_data_table_news.getColumnSet().getColumn('date');
			
					// do the sort
					YAHOO.newspress.list_filter.list_data_table_news.sortColumn(sort_column);
					
					YAHOO.newspress.list_filter.list_data_table_news_initial_page = 1;
					break;
					
			}
											
			// this is required as the sort triggers a table refresh.. which resets to page 1!			
			YAHOO.newspress.list_filter.list_data_table_news.showPage(YAHOO.newspress.list_filter.list_data_table_news_initial_page);
			/* ----------------------------------------------------------------------------------------------------------------------- */

			// subscribe to the table refresh event the handlers for pagination
			YAHOO.newspress.list_filter.list_data_table_news.subscribe("refreshEvent",YAHOO.newspress.list_filter.set_pagination_link_listeners);
			
			//if(YAHOO.newspress.list_filter.filter_type=='news_by_year' || YAHOO.newspress.list_filter.filter_type=='news_by_project' ){
				// refresh the view only for this type of filtering
				YAHOO.newspress.list_filter.list_data_table_news.refreshView();
			//}
			
		},
		
		initialise_data_table_press: function(){
			
			// make a DataTable aout of the array
			var list_data_source = new YAHOO.util.DataSource(YAHOO.newspress.list_filter.list_item_array_press);
			list_data_source.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			list_data_source.responseSchema = {
				fields: ["doc_id","news_or_press","date","project","href","inner_html","name"]
			};

			var list_colum_defs = [
				{key:"doc_id", label:"doc_id", className:"hidden_col"},
				{key:"news_or_press", className:"hidden_col",label:"news_or_press", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_news_or_press,defaultOrder:"asc"}},
				{key:"date", label:"date",className:"hidden_col", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_date,defaultOrder:"desc"}},												
				{key:"project",className:"hidden_col", label:"project", sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_project,defaultOrder:"asc"}},
				{key:"href", className:"hidden_col",label:"href"},
				{key:"inner_html", className:"", label:"inner_html"},
				{key:"name", className:"hidden_col",label:"name",sortable:true,sortOptions:{sortFunction:YAHOO.newspress.list_filter.sort_by_name,defaultOrder:"asc"}}
			];

			// only enable pagination if we need to ie the number of rows exceeds the rows per page setting
			if(YAHOO.newspress.list_filter.press_count > YAHOO.newspress.list_filter.pagination_rows_per_page){
				var paginated = true;
			}else{
				var paginated = false;
			}				
			
			var list_data_table_configs = { 
				paginated:paginated, // Enables built-in client-side pagination 
				paginator:{ // Configurable options 
				containers: null, // Create container DIVs dynamically 
				currentPage: 1, // Show page  
				dropdownOptions: null, // Show these in the rows-per-page dropdown 
				pageLinks: 0, // Show links to all pages 
				rowsPerPage: YAHOO.newspress.list_filter.pagination_rows_per_page // Show up to 500 rows per page 
				} 
			}; 
						
			// create the data table
			YAHOO.newspress.list_filter.list_data_table_press = new YAHOO.widget.DataTable(
				"press_list_datatable", 
				list_colum_defs,
				list_data_source, 
				list_data_table_configs,{sortedBy:{key:'name', dir:"asc"}}
			); 
			
			/* ----------------------------------------------------------------------------------------------------------------------- */
			/* IMPORTANT - both the sortColumn and showPage calls cause the refreshEvent to be invoked, this will obviously 
			/* cause anything subscribed to this event to also be called - so only place the subscribe statement above these if the
			/* consequences of the subscribed function running twice are desirable! I am putting it inbetween so it triggers once */
			
			// initial sort - NOTE performing this resets pagination MUCHOS IMPORTANT
			switch(YAHOO.newspress.list_filter.filter_type){
			
				case 'press_by_project' : 
					var sort_column = YAHOO.newspress.list_filter.list_data_table_press.getColumnSet().getColumn('project');
					
					// do the sort																
					YAHOO.newspress.list_filter.list_data_table_press.sortColumn(sort_column);
			
					// if the page number has not been received, set it so that we go to the page the news/press item is on
					if(YAHOO.newspress.list_filter.filter_page_number == 0){
						YAHOO.newspress.list_filter.list_data_table_press_initial_page = YAHOO.newspress.list_filter.get_initial_pagination_page('press_by_project');
					}else{
						YAHOO.newspress.list_filter.list_data_table_press_initial_page = YAHOO.newspress.list_filter.filter_page_number;
					}					
					break;
					
				case 'press_by_year' : 				
					var sort_column = YAHOO.newspress.list_filter.list_data_table_press.getColumnSet().getColumn('date');
					
					// do the sort																
					YAHOO.newspress.list_filter.list_data_table_press.sortColumn(sort_column);
			
					// if the page number has not been received, set it so that we go to the page the news/press item is on
					if(YAHOO.newspress.list_filter.filter_page_number == 0){
						YAHOO.newspress.list_filter.list_data_table_press_initial_page = YAHOO.newspress.list_filter.get_initial_pagination_page('press_by_year');
					}else{
						YAHOO.newspress.list_filter.list_data_table_press_initial_page = YAHOO.newspress.list_filter.filter_page_number;
					}					
					break;
					
				default :
					var sort_column = YAHOO.newspress.list_filter.list_data_table_press.getColumnSet().getColumn('date');
			
					// do the sort
					YAHOO.newspress.list_filter.list_data_table_press.sortColumn(sort_column);
					
					YAHOO.newspress.list_filter.list_data_table_press_initial_page = 1;
					break;
			}
									
			// this is required as the sort triggers a table refresh.. which resets to page 1!			
			YAHOO.newspress.list_filter.list_data_table_press.showPage(YAHOO.newspress.list_filter.list_data_table_press_initial_page);
			/* ----------------------------------------------------------------------------------------------------------------------- */

			// subscribe to the table refresh event the handlers for pagination
			YAHOO.newspress.list_filter.list_data_table_press.subscribe("refreshEvent",YAHOO.newspress.list_filter.set_pagination_link_listeners);
			
			//if(YAHOO.newspress.list_filter.filter_type=='press_by_year' || YAHOO.newspress.list_filter.filter_type=='press_by_project' ){
				// refresh the view only for this type of filtering
				YAHOO.newspress.list_filter.list_data_table_press.refreshView();
			//}
			
		},
		
		// gets the page number of the link list that the current doc appears on
		// to work it requires the datatable to already have been sorted as required
		get_initial_pagination_page: function(filter_type){
								
			switch(filter_type){
			
				case 'news_and_press' :
				
					var records = YAHOO.newspress.list_filter.list_data_table_news_and_press.getRecordSet().getRecords();						
					break;
					
				case 'news_by_year' :
				case 'news_by_project' :
				
					var records = YAHOO.newspress.list_filter.list_data_table_news.getRecordSet().getRecords();						
					break;
																								
				case 'press_by_year' :
				case 'press_by_project' :
				
					var records = YAHOO.newspress.list_filter.list_data_table_press.getRecordSet().getRecords();						
					break;																							
				
			}
			for(k=0;k<records.length;k++){
												
				if(YAHOO.newspress.list_filter.doc_id == records[k].getData('doc_id')){
					var initial_pagination_page = Math.ceil((k+1) / YAHOO.newspress.list_filter.pagination_rows_per_page);
					return initial_pagination_page;
					break;
				}				
			}
			
												
		},
		
		/* -------------------------------------------------------------------------------------------------------------- */
		/* event handlers */
		/* -------------------------------------------------------------------------------------------------------------- */
								
		sort_list: function(e,selected_filter_type){
															
			if(e){
				yue.stopEvent(e);
			}
							
			if(selected_filter_type == 'news_and_press'){
				
				if(YAHOO.newspress.list_filter.filter_type != 'news_and_press'){
					yud.setStyle(document.getElementById('news_list_datatable'),'display','none');
					yud.setStyle(document.getElementById('press_list_datatable'),'display','none');
					yud.setStyle(document.getElementById('news_press_list_datatable'),'display','block');
				}				
			}
			
			if((selected_filter_type == 'news_by_year')||(selected_filter_type == 'news_by_project')){
				
				if((YAHOO.newspress.list_filter.filter_type != 'news_by_year')||(YAHOO.newspress.list_filter.filter_type != 'news_by_project')){
					yud.setStyle(document.getElementById('news_list_datatable'),'display','block');
					yud.setStyle(document.getElementById('press_list_datatable'),'display','none');
					yud.setStyle(document.getElementById('news_press_list_datatable'),'display','none');
				}
			}
			
			if((selected_filter_type == 'press_by_year')||(selected_filter_type == 'press_by_project')){
				
				if((YAHOO.newspress.list_filter.filter_type != 'press_by_year')||(YAHOO.newspress.list_filter.filter_type != 'press_by_project')){
					yud.setStyle(document.getElementById('news_list_datatable'),'display','none');
					yud.setStyle(document.getElementById('press_list_datatable'),'display','block');
					yud.setStyle(document.getElementById('news_press_list_datatable'),'display','none');
				}
			}
												
			// if we need to do a re-sort then do so
			if((selected_filter_type == 'news_by_project')&&(YAHOO.newspress.list_filter.filter_type != 'news_by_project')){
			
				// get the column set				
				var my_column_set = YAHOO.newspress.list_filter.list_data_table_news.getColumnSet();
						
				//get the column we're sorting by		
				var sort_column = my_column_set.getColumn('project');	
											
				// sort the column hey presto
				YAHOO.newspress.list_filter.list_data_table_news.sortColumn(sort_column);
			}
			
			if((selected_filter_type == 'press_by_project')&&(YAHOO.newspress.list_filter.filter_type != 'press_by_project')){
			
				// get the column set				
				var my_column_set = YAHOO.newspress.list_filter.list_data_table_press.getColumnSet();
						
				//get the column we're sorting by		
				var sort_column = my_column_set.getColumn('project');	
											
				// sort the column hey presto
				YAHOO.newspress.list_filter.list_data_table_press.sortColumn(sort_column);
			}
			
			if((selected_filter_type == 'news_by_year')&&(YAHOO.newspress.list_filter.filter_type != 'news_by_year')){
			
				// get the column set				
				var my_column_set = YAHOO.newspress.list_filter.list_data_table_news.getColumnSet();
						
				//get the column we're sorting by		
				var sort_column = my_column_set.getColumn('date');	
											
				// sort the column hey presto
				YAHOO.newspress.list_filter.list_data_table_news.sortColumn(sort_column);
			}
			
			if((selected_filter_type == 'press_by_year')&&(YAHOO.newspress.list_filter.filter_type != 'press_by_year')){
			
				// get the column set				
				var my_column_set = YAHOO.newspress.list_filter.list_data_table_press.getColumnSet();
						
				//get the column we're sorting by		
				var sort_column = my_column_set.getColumn('date');	
											
				// sort the column hey presto
				YAHOO.newspress.list_filter.list_data_table_press.sortColumn(sort_column);
			}
			
			// set the filter type status var and styles (i.e current link)						
			YAHOO.newspress.list_filter.filter_type = selected_filter_type;
			
			YAHOO.newspress.list_filter.set_filter_link_style();
									
			// set the hidden input			
			document.getElementById('filter_type').value = YAHOO.newspress.list_filter.filter_type;
			
			YAHOO.newspress.list_filter.show_sub_headings();
								
		},
		
		load_news_page: function(e,selected_href){
						
			// set the 'action' of the hidden form to be the current project doc id			
			var list_filter_form_node = document.getElementById('list_filter_form');						
			list_filter_form_node.setAttribute('action',selected_href);
			
			// submit the form
			document.list_filter_form.submit();
			
		},
						
		// pull the post vars off elements on the page - previously written by PHP
		get_filter_state: function(){
			
			YAHOO.newspress.list_filter.filter_type = document.getElementById('filter_type').value;
			YAHOO.newspress.list_filter.filter_page_number = parseInt(document.getElementById('filter_page_number').value);
			
			YAHOO.newspress.list_filter.pagination_rows_per_page = parseInt(document.getElementById('filter_pagination_rows_per_page').value);
			YAHOO.newspress.list_filter.number_of_press_items_to_show = parseInt(document.getElementById('filter_pagination_rows_per_page').value);
			YAHOO.newspress.list_filter.number_of_news_items_to_show = parseInt(document.getElementById('filter_pagination_rows_per_page').value);
		},
		
		set_filter_link_style: function(){
			
			var news_press_list_filter  = document.getElementById('news_press_list_filter');												
			var news_press_filter_links = yud.getElementsByClassName('news_press_filter_type_link','a',news_press_list_filter);

			for(var i=0;i<news_press_filter_links.length;i++){
				
				if(yud.hasClass(news_press_filter_links[i],'current_filter_type_link')){				
					yud.removeClass(news_press_filter_links[i], 'current_filter_type_link');
				}								
			}

			// while we're here, set a class on the filter links so that the 'current' one can be styled
			var current_filter_link = document.getElementById(YAHOO.newspress.list_filter.filter_type + '_filter_link');			
			yud.addClass(current_filter_link, 'current_filter_type_link');
		},
		
		set_pagination_link_listeners: function(){
			
			// set listeners for the pagination links
			var bottom_paginator = document.getElementById('yui-dt0-paginator1');
			var pagination_links = yud.getElementsByClassName('yui-dt-page','a',bottom_paginator);
			
			for(var i=0;i<pagination_links.length;i++){
				
				yue.addListener(pagination_links[i],'click', YAHOO.newspress.list_filter.set_pagination_state,{page: pagination_links[i].innerHTML},false);
			}
						
			// set listner to each project page link
			var links = yud.getElementsByClassName('yui-dt-col-inner_html','td');
			var href_cells = yud.getElementsByClassName('yui-dt-col-href','td');
			var doc_id_cells = yud.getElementsByClassName('yui-dt-col-doc_id','td');
			
			for(var i=0;i<links.length;i++){
				
				yue.addListener(links[i],'click', YAHOO.newspress.list_filter.load_news_page, href_cells[i].innerHTML);
				yue.addListener(links[i],'mouseover', YAHOO.newspress.list_filter.set_project_link_over_style, links[i].id);
				yue.addListener(links[i],'mouseout', YAHOO.newspress.list_filter.set_project_link_out_style, links[i].id);
				
				// set the link of the currently being viewed page
				current_doc_id = document.getElementById('doc_id').value;
				
				if(current_doc_id == doc_id_cells[i].innerHTML){
					yud.addClass(links[i] , 'current_project');
				}else{
					yud.removeClass(links[i] , 'current_project');
				}
			}
			
			// set the sub headings			
			YAHOO.newspress.list_filter.show_sub_headings();
									
			// add the word page to the pagination links
			/*YAHOO.newspress.list_filter.show_paginator_title();*/
						
		},
		
		set_project_link_over_style: function(e,node_id){

			yue.stopEvent(e);
			
			var link_text = yud.getElementsByClassName('row_text','div',document.getElementById(node_id))[0];
						
			yud.addClass(link_text, 'project_link_hover_style');
		},
		
		set_project_link_out_style: function(e,node_id){

			yue.stopEvent(e);
			
			var link_text = yud.getElementsByClassName('row_text','div',document.getElementById(node_id))[0];
						
			yud.removeClass(link_text, 'project_link_hover_style');
		},
		
		// set the 
		set_pagination_state: function(e,obj){
						
			//alert(obj['page']);
			
			var hidden_input = document.getElementById('filter_page_number');
			
			YAHOO.newspress.list_filter.filter_page_number = obj['page'];
			hidden_input.value = obj['page'];						
				
		},
		
		sort_by_news_or_press: function(a, b, desc) { 
									
			// Deal with empty values 
			if(!YAHOO.lang.isValue(a)) { 
				return (!YAHOO.lang.isValue(b)) ? 0 : 1; 
			} 
			else if(!YAHOO.lang.isValue(b)) { 
				return -1; 
			} 
	 						
			// First compare by state 
			var comp = YAHOO.util.Sort.compare; 
			var compState = comp(a.getData('news_or_press'), b.getData('news_or_press'), false); 
	 
			// If states are equal, then compare by name 									
			return (compState !== 0) ? compState : comp(a.getData("name"), b.getData("name"), false); 
		},
		
		sort_by_date: function(a, b, desc) { 
									
			// Deal with empty values 
			if(!YAHOO.lang.isValue(a)) { 
				return (!YAHOO.lang.isValue(b)) ? 0 : 1; 
			} 
			else if(!YAHOO.lang.isValue(b)) { 
				return -1; 
			} 
	 						
			// First compare by state 
			var comp = YAHOO.util.Sort.compare; 
			var compState = comp(a.getData('date'), b.getData('date'), true); 
	 
			// If states are equal, then compare by name 									
			return (compState !== 0) ? compState : comp(a.getData("name"), b.getData("name"), false); 
		},
		
		sort_by_project: function(a, b, desc) { 
									
			// Deal with empty values 
			if(!YAHOO.lang.isValue(a)) { 
				return (!YAHOO.lang.isValue(b)) ? 0 : 1; 
			} 
			else if(!YAHOO.lang.isValue(b)) { 
				return -1; 
			} 
	 						
			// First compare by state 
			var comp = YAHOO.util.Sort.compare; 
			var compState = comp(a.getData('project'), b.getData('project'), false); 
	 
			// If states are equal, then compare by name 
			return (compState !== 0) ? compState : comp(a.getData("name"), b.getData("name"), false); 
		},
		
		sort_by_name: function(a, b, desc) { 
									
			// Deal with empty values 
			if(!YAHOO.lang.isValue(a)) { 
				return (!YAHOO.lang.isValue(b)) ? 0 : 1; 
			} 
			else if(!YAHOO.lang.isValue(b)) { 
				return -1; 
			} 
	 						
			// First compare by state 
			var comp = YAHOO.util.Sort.compare; 
			var compState = comp(a.getData('name'), b.getData('name'), false); 
	 
			// If states are equal, then compare by name 
			return (compState !== 0) ? compState : comp(a.getData("name"), b.getData("name"), false); 
		},
		
		// show list items
		show_list_items: function(){
			
			var even_rows = yud.getElementsByClassName('yui-dt-even');
			var odd_rows = yud.getElementsByClassName('yui-dt-odd');
			
			// hide all elements
			for(var i=0;i<even_rows.length;i++){
				yud.setStyle(even_rows[i],'display','block');								
			}
			for(var i=0;i<odd_rows.length;i++){
				yud.setStyle(odd_rows[i],'display','block');								
			}			
		},
			
		// show list items
		hide_list_items: function(){
			
			var even_rows = yud.getElementsByClassName('yui-dt-even');
			var odd_rows = yud.getElementsByClassName('yui-dt-odd');
			
			// hide all elements
			for(var i=0;i<even_rows.length;i++){
				yud.setStyle(even_rows[i],'display','none');								
			}
			for(var i=0;i<odd_rows.length;i++){
				yud.setStyle(odd_rows[i],'display','none');								
			}			
		},
															
		show_sub_headings: function(){
				
			// target a specific table based on the type of filtering we are doing
			switch(YAHOO.newspress.list_filter.filter_type){
				case 'news_and_press':
					var list_tbody_node = document.getElementById('yui-dt0-table');
				break;
				case 'press_by_year':
				case 'press_by_project':
					var list_tbody_node = document.getElementById('yui-dt2-table');
				break;
				case 'news_by_year':
				case 'news_by_project':
					var list_tbody_node = document.getElementById('yui-dt1-table');
				break;
			}
			
			
			
			// hide all sub headings first
			var sub_heading_nodes = yud.getElementsByClassName('row_sub_heading','div',list_tbody_node);
			
			for(var i=0;i<sub_heading_nodes.length;i++){
				yud.setStyle(sub_heading_nodes[i],'display','none');	
			}
				
			
			
			// news_and_press
			// press_by_year - year
			// news_by_year - year
			// news_by_project - project sub heading
			// news_by_project - project sub heading
			
			// only show sub headings for some filter types		
			switch(YAHOO.newspress.list_filter.filter_type){
				
				case 'news_and_press':
				
					var row_nodes = yud.getElementsByClassName('yui-dt-col-inner_html','td',list_tbody_node);
						
					var news_or_press_cells = yud.getElementsByClassName('yui-dt-col-news_or_press','td',list_tbody_node);
					
					var previous_sub_heading = '';						
																			
					// loop through
					for(var i=0;i<row_nodes.length;i++){
					
						if(news_or_press_cells[i].innerHTML == 'News'){
							// get the sub heading for this cell
							var sub_heading_node = yud.getElementsByClassName('row_latest_news','div',row_nodes[i])[0];
						}
						if(news_or_press_cells[i].innerHTML == 'Press'){
							// get the sub heading for this cell
							var sub_heading_node = yud.getElementsByClassName('row_latest_press','div',row_nodes[i])[0];
						}
														
						// if the value of this cell is 
						if((sub_heading_node.innerHTML !=  previous_sub_heading) || (previous_sub_heading == '')){
							
							if(sub_heading_node.innerHTML != ''){
								yud.setStyle(sub_heading_node,'display','block');
							}
						}
						previous_sub_heading = sub_heading_node.innerHTML;
						
					}
				
					
				
					break;
				
				case 'press_by_year':
				
				case 'news_by_year':

					// get each row in the table
					var row_nodes = yud.getElementsByClassName('yui-dt-col-inner_html','td',list_tbody_node);
						
					
					var previous_sub_heading = '';						
																			
					// loop through
					for(var i=0;i<row_nodes.length;i++){
					
						// show the row - incase the list was viewed as news_by_project, in which case the row nodes may be hidden
						yud.setStyle(row_nodes[i],'display','block');
						
						// get the sub heading for this cell
						var sub_heading_node = yud.getElementsByClassName('row_date','div',row_nodes[i])[0];
					
						var sub_heading_year = sub_heading_node.innerHTML.substring(sub_heading_node.innerHTML.length-4, sub_heading_node.innerHTML.length);
						
						//alert(sub_heading_node.innerHTML +'!=' +  previous_sub_heading + '::' + sub_heading_node);
						
						// if the value of this cell is 
						if((sub_heading_year !=  previous_sub_heading) || (previous_sub_heading == '')){
							
							sub_heading_node.innerHTML = sub_heading_year;
							
							if(sub_heading_year != ''){
								yud.setStyle(sub_heading_node,'display','block');
							}
						}
						previous_sub_heading = sub_heading_year;
						
					}
				break;
				case 'press_by_project':
				
				case 'news_by_project':
					// get each row in the table
					var row_nodes = yud.getElementsByClassName('yui-dt-col-inner_html','td',list_tbody_node);
											
					var previous_sub_heading = '';						
																			
					// loop through
					for(var i=0;i<row_nodes.length;i++){
					
						
						// get the sub heading for this cell
						var sub_heading_node = yud.getElementsByClassName('row_project','div',row_nodes[i])[0];
						
						// if theres no sub heading node then this item has no associated project - hence do not show in this list at all
						if(!sub_heading_node){
							yud.setStyle(row_nodes[i],'display','none');
						}else{
						
							yud.setStyle(row_nodes[i],'display','block');
							
							// if the value of this cell is 
							if((sub_heading_node.innerHTML !=  previous_sub_heading) || (previous_sub_heading == '')){
								
								if(sub_heading_node.innerHTML != ''){
									yud.setStyle(sub_heading_node,'display','block');
								}
							}
							previous_sub_heading = sub_heading_node.innerHTML;
						}
						
					}
				break;
				
			}

			
		},
		
		show_paginator_title: function(){
			
			// add the word page infront of the pagination links
			var bottom_paginator = document.getElementById('yui-dt0-paginator1','span');			
			bottom_paginator.innerHTML = "<span id='pagination_title'>PAGE</span>" + bottom_paginator.innerHTML;
			
		}
		
		
	};
}();

// intialise page object once the page has loaded
YAHOO.util.Event.on(window, 'load', YAHOO.newspress.list_filter.init, YAHOO.newspress.list_filter, true );

