// create a namespace

YAHOO.namespace("project");
			
// define the namespace object
YAHOO.project.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 = [];
	var list_item_featured_array = [];
	
	var list_item_nodes = [];
	
	var list_data_table;
	var list_data_table_featured;
		
	var filter_type;
	
	// default page being shown is 1
	var filter_page_number;	
				
	var pagination_rows_per_page;
	
	var featured_projects_count;
	
	var list_item_featured_count;
	var list_item_count;
	
	// used for link styling
	var link_style_element;
	
	
	
	return {									
		// initialise function
		init: function(){
			
			YAHOO.project.list_filter.list_item_count = 0;
			YAHOO.project.list_filter.list_item_featured_count = 0;
									
			// store the currently loaded document id
			YAHOO.project.list_filter.current_doc_href = location.href;
					
			// iniitialise the filter state variables from the hidden inputs
			YAHOO.project.list_filter.get_filter_state();
			
			YAHOO.project.list_filter.set_filter_link_style();
			
			// get the list items from the page
			YAHOO.project.list_filter.get_list_items();
									
			var oElement = document.getElementById('building_type_filter_link');								
			yue.addListener(oElement,'click', YAHOO.project.list_filter.sort_list,'building_type');
			
			var oElement = document.getElementById('name_filter_link');								
			yue.addListener(oElement,'click', YAHOO.project.list_filter.sort_list,'name');
			
			var oElement = document.getElementById('date_filter_link');								
			yue.addListener(oElement,'click', YAHOO.project.list_filter.sort_list,'date');
			
			var oElement = document.getElementById('featured_projects_filter_link');								
			yue.addListener(oElement,'click', YAHOO.project.list_filter.sort_list,'featured_projects');
			
			// set initial pagination link listeners
			//YAHOO.project.list_filter.set_pagination_link_listeners();								
															
		},
		
		// get list items
		get_list_items: function(){
		
			YAHOO.project.list_filter.list_item_nodes = yud.getElementsByClassName('list_link');
										
			YAHOO.project.list_filter.list_item_array = [];
			YAHOO.project.list_filter.list_item_featured_array = [];
			
			var list_item_count = 0;
			var list_item_featured_count = 0;
			var today = new Date();
									
			// loop through all list items, get attributes & push an object onto the array					
			for(var i=0;i<YAHOO.project.list_filter.list_item_nodes.length;i++){
				
				var doc_id 		= YAHOO.project.list_filter.list_item_nodes[i].id;
				var date 		= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('date');				
				var featured_project 	= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('featured_project');
				var building_type 	= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('building_type');
				var href 		= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('href');
				var inner_html		= YAHOO.project.list_filter.list_item_nodes[i].innerHTML;
				var name 		= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('alt');
				var link_to_document	= YAHOO.project.list_filter.list_item_nodes[i].getAttribute('link_to_document');								
						
				if(date == '1970, 01, 01'){					
					date = today.getFullYear() + ', 01, 01';
				}
				
				if(featured_project=='1'){				
					YAHOO.project.list_filter.list_item_featured_array.push({
								doc_id: doc_id, 
								date: date.substring(0,4), 
								featured_project:featured_project, 
								building_type:building_type, 
								href:href, 
								inner_html:inner_html, 
								name:name,
								link_to_document:link_to_document
					});
					YAHOO.project.list_filter.list_item_featured_count++;
				}
				YAHOO.project.list_filter.list_item_array.push({
							doc_id: doc_id, 
							date: date.substring(0,4), 
							featured_project:featured_project, 
							building_type:building_type, 
							href:href, 
							inner_html:inner_html, 
							name:name,
							link_to_document:link_to_document
				});
				YAHOO.project.list_filter.list_item_count++;
				
								
			}	
			YAHOO.project.list_filter.initialise_data_table();	
			YAHOO.project.list_filter.initialise_data_table_featured();
			
			if(YAHOO.project.list_filter.filter_type == 'featured_projects'){
				yud.setStyle(document.getElementById('list_datatable'),'display','none');
				yud.setStyle(document.getElementById('list_datatable_featured'),'display','block');
			}else{
				yud.setStyle(document.getElementById('list_datatable'),'display','block');
				yud.setStyle(document.getElementById('list_datatable_featured'),'display','none');
			}
		},
		
		initialise_data_table: function(){
			
			// make a DataTable aout of the array
			var list_data_source = new YAHOO.util.DataSource(YAHOO.project.list_filter.list_item_array);
			list_data_source.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			list_data_source.responseSchema = {
				fields: ["doc_id","date","featured_project","building_type","href","inner_html","name","link_to_document"]
			};

			var list_colum_defs = [
				{key:"doc_id", label:"doc_id", className:"hidden_col"},
				{key:"date", label:"date",className:"hidden_col", sortable:true,sortOptions:{sortFunction:YAHOO.project.list_filter.sort_by_date,defaultOrder:"desc"}},				
				{key:"building_type", className:"hidden_col", label:"building_type",sortable:true,sortOptions:{sortFunction:YAHOO.project.list_filter.sort_by_building_type,defaultOrder:"asc"}},
				{key:"featured_project",className:"hidden_col", label:"featured_project"},
				{key:"href", className:"hidden_col",label:"href"},
				{key:"inner_html", className:"", label:"inner_html"},
				{key:"link_to_document", className:"hidden_col",label:"link"},
				{key:"name", className:"hidden_col",label:"name",sortable:true,sortOptions:{sortFunction:YAHOO.project.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.project.list_filter.list_item_count > YAHOO.project.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.project.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.project.list_filter.pagination_rows_per_page // Show up to 500 rows per page 
				} 
			}; 
						
			// create the data table
			YAHOO.project.list_filter.list_data_table = new YAHOO.widget.DataTable(
				"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.project.list_filter.list_data_table.getColumnSet().getColumn(YAHOO.project.list_filter.filter_type);																		
			YAHOO.project.list_filter.list_data_table.sortColumn(sort_column);
													
			// this is required as the sort triggers a table refresh.. which resets to page 1!			
			YAHOO.project.list_filter.list_data_table.showPage(YAHOO.project.list_filter.filter_page_number);
			/* ----------------------------------------------------------------------------------------------------------------------- */

			// subscribe to the table refresh event the handlers for pagination
			YAHOO.project.list_filter.list_data_table.subscribe("refreshEvent",YAHOO.project.list_filter.set_pagination_link_listeners);
			
			// refresh the view
			YAHOO.project.list_filter.list_data_table.refreshView();
		},
		
		initialise_data_table_featured: function(){
			
			// make a DataTable aout of the array
			var list_data_source = new YAHOO.util.DataSource(YAHOO.project.list_filter.list_item_featured_array);
			list_data_source.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			list_data_source.responseSchema = {
				fields: ["doc_id","date","featured_project","building_type","href","inner_html","name","link_to_document"]
			};

			var list_colum_defs = [
				{key:"doc_id", label:"doc_id", className:"hidden_col"},
				{key:"date", label:"date",className:"hidden_col", sortable:true,sortOptions:{sortFunction:YAHOO.project.list_filter.sort_by_date,defaultOrder:"desc"}},				
				{key:"building_type", className:"hidden_col", label:"building_type",sortable:true,sortOptions:{sortFunction:YAHOO.project.list_filter.sort_by_building_type,defaultOrder:"asc"}},
				{key:"featured_project",className:"hidden_col", label:"featured_project"},
				{key:"href", className:"hidden_col",label:"href"},
				{key:"inner_html", className:"", label:"inner_html"},
				{key:"link_to_document", className:"hidden_col",label:"link"},
				{key:"name", className:"hidden_col",label:"name",sortable:true,sortOptions:{sortFunction:YAHOO.project.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.project.list_filter.list_item_featured_count > YAHOO.project.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.project.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.project.list_filter.pagination_rows_per_page // Show up to 500 rows per page 
				} 
			}; 
						
			// create the data table
			YAHOO.project.list_filter.list_data_table_featured = new YAHOO.widget.DataTable(
				"list_datatable_featured", 
				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.project.list_filter.list_data_table_featured.getColumnSet().getColumn('name');																		
			YAHOO.project.list_filter.list_data_table_featured.sortColumn(sort_column);
													
			// this is required as the sort triggers a table refresh.. which resets to page 1!			
			YAHOO.project.list_filter.list_data_table_featured.showPage(YAHOO.project.list_filter.filter_page_number);
			/* ----------------------------------------------------------------------------------------------------------------------- */

			// subscribe to the table refresh event the handlers for pagination
			YAHOO.project.list_filter.list_data_table.subscribe("refreshEvent",YAHOO.project.list_filter.set_pagination_link_listeners);
			
			// refresh the view
			YAHOO.project.list_filter.list_data_table.refreshView();
			
		},
		
		/* -------------------------------------------------------------------------------------------------------------- */
		/* event handlers */
		/* -------------------------------------------------------------------------------------------------------------- */
		
						
		sort_list: function(e,selected_filter_type){

			if(e){
				yue.stopEvent(e);
			}
			
			if(YAHOO.project.list_filter.filter_type != selected_filter_type){
			
				if(selected_filter_type == 'featured_projects'){
					
					if(YAHOO.project.list_filter.filter_type != 'featured_projects'){
						yud.setStyle(document.getElementById('list_datatable'),'display','none');
						yud.setStyle(document.getElementById('list_datatable_featured'),'display','block');
					}				
				}
				
				if(selected_filter_type != 'featured_projects'){
					
					if(YAHOO.project.list_filter.filter_type == 'featured_projects'){
						yud.setStyle(document.getElementById('list_datatable'),'display','block');
						yud.setStyle(document.getElementById('list_datatable_featured'),'display','none');
					}
				}
				
				// set the filter type status var						
				YAHOO.project.list_filter.filter_type = selected_filter_type;
				
				YAHOO.project.list_filter.set_filter_link_style();
				
				// make sure all items are shown, in case last filter was featured projects
				YAHOO.project.list_filter.show_list_items();
				
				if(selected_filter_type != 'featured_projects'){
				
					// get the column set				
					var my_column_set = YAHOO.project.list_filter.list_data_table.getColumnSet();
							
					//get the column we're sorting by		
					var sort_column = my_column_set.getColumn(selected_filter_type);	
												
					// sort the column hey presto
					YAHOO.project.list_filter.list_data_table.sortColumn(sort_column);
				}
										
				// set the hidden input			
				document.getElementById('filter_type').value = YAHOO.project.list_filter.filter_type;
					
			}
		},
		
		load_project_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.project.list_filter.filter_type = document.getElementById('filter_type').value;
			YAHOO.project.list_filter.filter_page_number = parseInt(document.getElementById('filter_page_number').value);	
			YAHOO.project.list_filter.pagination_rows_per_page = parseInt(document.getElementById('filter_pagination_rows_per_page').value);
									
		},
		
		set_filter_link_style: function(){
			
			var project_list_filter  = document.getElementById('project_list_filter');												
			var project_filter_links = yud.getElementsByClassName('project_filter_type_link','a',project_list_filter);
			
			for(var i=0;i<project_filter_links.length;i++){
				
				if(yud.hasClass(project_filter_links[i],'current_filter_type_link')){				
					yud.removeClass(project_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.project.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.project.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 link_to_document_cells = yud.getElementsByClassName('yui-dt-col-link_to_document','td');
			var doc_id_cells = yud.getElementsByClassName('yui-dt-col-doc_id','td');
						
			for(var i=0;i<links.length;i++){
				
				if(link_to_document_cells[i].innerHTML == 'Yes'){
					yue.addListener(links[i],'click', YAHOO.project.list_filter.load_project_page, href_cells[i].innerHTML);
					yue.addListener(links[i],'mouseover', YAHOO.project.list_filter.set_project_link_over_style, links[i].id);
					yue.addListener(links[i],'mouseout', YAHOO.project.list_filter.set_project_link_out_style, links[i].id);
					yud.removeClass(links[i] , 'project_link_not_clickable');
				}else{
					yud.addClass(links[i] , 'project_link_not_clickable');
					yue.purgeElement(links[i]);					
				}
				
				// 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	
			if(YAHOO.project.list_filter.filter_type != 'featured_projects'){
				YAHOO.project.list_filter.show_sub_headings();
			}
						
			// add the word page to the pagination links
			/*YAHOO.project.list_filter.show_paginator_title();*/
						
		},
		
		set_project_link_over_style: function(e,node_id){

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

			yue.stopEvent(e);
			
			YAHOO.project.list_filter.link_style_element  = yud.getElementsByClassName('row_text','div',document.getElementById(node_id))[0];
				
			yud.removeClass(YAHOO.project.list_filter.link_style_element, 'project_link_hover_style');
		},
		
		// set the 
		set_pagination_state: function(e,obj){
						
			//alert(obj['page']);
			
			var hidden_input = document.getElementById('filter_page_number');
			
			YAHOO.project.list_filter.filter_page_number = obj['page'];
			hidden_input.value = obj['page'];						
				
		},
						
		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_building_type: 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('building_type'), b.getData('building_type'), 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(){
			
			var today = new Date();
			
			var list_tbody_node = document.getElementById('yui-dt0-table');
			
			// 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');	
			}
													
			// only show sub headings for some filter types
			switch(YAHOO.project.list_filter.filter_type){
				
				case "date":
				
					// 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_' + YAHOO.project.list_filter.filter_type,'div',row_nodes[i])[0];
									
						var sub_heading_year = sub_heading_node.innerHTML.substring(0,4);
							
						// handle not set dates that default to 01 01 1970
						if(sub_heading_year == '1970'){												
							sub_heading_year = today.getFullYear();
						}
						
						// if the value of this cell is 
						if((sub_heading_year !=  previous_sub_heading) || (previous_sub_heading == '')){
							
							if(sub_heading_year != ''){
								sub_heading_node.innerHTML = sub_heading_year;
							
								yud.setStyle(sub_heading_node,'display','block');
							}
						}
						previous_sub_heading = sub_heading_year;
					}
				
				break;
				case "building_type":
				
					// 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_' + YAHOO.project.list_filter.filter_type,'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;
			}
			
		},
		
		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.project.list_filter.init, YAHOO.project.list_filter, true );
