
				// check to see if a variable is stored in an array
				function contains(a, obj) {
					var i = a.length;
					while (i--) {
						//alert (a[i]);
						if (a[i] === obj) {
							return true;
						} else {
						//	alert ("Fail" + a[i] + "FOO" + obj + "BAR");
						}
					}
					return false;	
				}
				
			// Resets form
			// makes 'Name' the selected option
			// makes the form visible or hidden depending on whether or not 
			// sufficient building entries are visible to require sorting as an option
			function resetSortForm() {
				if ($('form#buildingListSort').length > 0) {
					$('form#buildingListSort option[name="Name"]').attr({selected: 'selected'});
					// reset to visible if it was hidden
					$('form#buildingListSort').show();
					// hide if there are not many buildings visible
					if ($('div.buildingListEntry:visible').length < 3) {
						$('form#buildingListSort').hide();
					}
				}
			}

				//show navigation for filtered building lists
				function showBuildingListFilterNav(filterTerm, filterType) {
					// generate message template
					if (filterType == 'location')  {
						filterMessage = 'Displaying buildings located in <span>' + filterTerm + '</span>.';
					} else {
						filterMessage = 'Displaying <span>' + filterTerm + '</span> buildings.';
					}
					
					// insert message that you are looking at a subset of the building list into the document
				
					if ($('p#filterMsg').length < 1) {
						$('div#buildingListNav').prepend('<p id="filterMsg">' + filterMessage + ' <a href="#" id="removeBldgFilter">See all buildings.</a></p>');
					} else {
						$('p#filterMsg').html(filterMessage + ' <a href="#" id="removeBldgFilter">See all buildings.</a>');
					}
						// event handler that resets the view of the building list to its full set					
						$('a#removeBldgFilter').click( function () {	
								//remove existing paging
								removePaging();
        				// reset visibility of all building list items
      	  			$('div.buildingListEntry').show();
      	  			// sort and repage building list
 			   				sortBuildingList();
 			   				resetSortForm();
 			   				$('p#filterMsg').remove();
 			   				return false;
						});
										
				}

        // building list paging
        function personnelPagingFunction()
        {
          var numOfBuildings = $('div.buildingListEntry:visible').length;
          var count = 0;
          while (count < numOfBuildings)
          {
              $('div.buildingListEntry:visible').slice(count,count+10).wrapAll('<div class="pager"></div>');
              count=count+10;
          }  
          
          $('#personnelBuildingList').pager('div.pager', {
         // not compatible with IE7
        	height: '0',
          prevText: '&lt; Previous',
          nextText: 'Next &gt;'
          });
          
          $('a[class=""]').removeAttr('class');


          //triggers scroll on click of bottom set of paging links.
          $("div.nav").children().click(function () {
          	scrollTo('#personnelViewsNavigation');
          	return false;
          });
        }
             
            
        function scrollTo(selector) {
            var targetOffset = $(selector).offset().top;
            $('html,body').animate({scrollTop: targetOffset}, 250);
        }
        
        //function to strip paging on sort
        function removePaging()
        {
           $("div.pager").children().appendTo("div#personnelBuildingList");
           $("div.pager").remove();
           $("div.nav").remove();
        }
        
        // sorts building list by type
       	function sortBuildingList(sortType) 
				{
					removePaging();
					$("div.buildingListEntry").tsort("h3.sortBuildingName");
					if (sortType == 'Date') {
						$("div.buildingListEntry").tsort("h3.sortDate");
					}
					if (sortType == 'Location') {
						$("div.buildingListEntry").tsort("h3.sortLocation");
					}					
					personnelPagingFunction();	
				}  

$(document).ready(function(){
  			
			$('div#personnelBuildingList').prepend('<div id="buildingListNav"><div class="clear"></div></div>');
     	
	    // sort building list by name
        $("div.buildingListEntry").tsort("h3.sortBuildingName");
         
        // draw building list sort ui 
        if ($('div.buildingListEntry').length > 5)
        {
	        $('div#buildingListNav').prepend('<form id="buildingListSort" method="" action=""><fieldset>Sort Building List by: <select onchange="sortBuildingList(this.options[this.selectedIndex].value);"><option value="Name" name="Name">Name</option><option value="Location" name="Location">County</option><option value="Date" name="Date">Date</option></select></fieldset></form>');
        }
   

  
  // number of list items to show when list is truncated
  var list_item_count = 4;


      //show more/less for workLocations list  
      if ($('td.workLocations ul li').size() > list_item_count+2)  //change 4 to 1 to always show toggle at 7
        {
          $('td.workLocations ul li:gt(' + list_item_count + ')').hide();
          $("td.workLocations ul").after("<h6><a id='toggleWorkLocations' href='#'>Show All</a></h6>");
          
          
          $("a#toggleWorkLocations").click(function () {
            $("td.workLocations ul li:gt(" + list_item_count + ")").toggle();
						var toggleTextLabel = $('a#toggleWorkLocations').text();
						
						if (toggleTextLabel == 'Show All') {
            	$('a#toggleWorkLocations').text('Show Fewer');
            } else {
            	$('a#toggleWorkLocations').text('Show All');
            }
            return false;
          });
          

        }
        
        
      //show more/less for residenceHeadquarters list  
      if ($('td.residenceHeadquarter ul li').size() > list_item_count+2)  //change 4 to 1 to always show toggle at 7
        {
          $('td.residenceHeadquarter ul li:gt(' + list_item_count + ')').hide();
          $("td.residenceHeadquarter ul").after("<h6><a id='toggleResidenceHeadquarter' href='#'>Show All</a></h6>");
          
          
          $("a#toggleResidenceHeadquarter").click(function () {
            $("td.residenceHeadquarter ul li:gt(" + list_item_count + ")").toggle();
						var toggleTextLabel = $('a#toggleResidenceHeadquarter').text();
						
						if (toggleTextLabel == 'Show All') {
            	$('a#toggleResidenceHeadquarter').text('Show Fewer');
            } else {
            	$('a#toggleResidenceHeadquarter').text('Show All');
            }
            return false;
          });
          

        }

        //initial call to paging function
        if ($('div.buildingListEntry').length > 10)
        {
             personnelPagingFunction();
             
        }

    //tab views
   	var $tabs = $("ul#personnelViewsNavigation").tabs();
    $("ul#personnelViewsNavigation").tabs();
  

         
      
         
        // make links to building names open up in facebox popups
        // must happen before facebox is called
        $('div#personnelDescription a.activeRecord[href^=#B]').attr('rel','facebox');
        
        // inactive people links show a coming soon message
        // must happen before facebox is called
        $('a.inactiveRecord').attr({rel:'facebox', href:'#comingSoon'});
        
        // set up facebox. any links with that rel attribute will open in a facebox popover
        $('a[rel*=facebox]').facebox();   
        
        // open picture attribution links in a new window
        $('table.imageMetadata a').attr('target', '_blank');
        $('div#imageAttribution a').attr('target', '_blank');
        
        //ie6 hack -- sometimes this does not happen
				$('.inactiveRecord').css('color', 'red');
        




        
				// building list filter by building type
				buildingListTypes = new Array();
				$('ul.buildingType > li').each( function () { 
					//alert ($(this).text());
					rightnowType = $.trim($(this).text());
					if (!contains(buildingListTypes, rightnowType)) {
						buildingListTypes.push(rightnowType);	
						}
				});
				
				$('td.principalBuildingType > ul > li').each( function () {
					currentType = $(this).text();
					currentType = currentType.replace(";", "");
					currentType = $.trim(currentType);
					
//					alert (currentType);
					if (contains(buildingListTypes, currentType)) {
						//alert ('There is a building of type ' + currentType + ' in hte building list.');
						$(this).wrapInner('<a href="#"></a>');
					} 
					
					
				});
				
     //   $('td.principalBuildingType li > a').attr('href', '#');
        $('td.principalBuildingType li > a').click(function() {
        		var filterTerm = $.trim($(this).text());
        		filterTerm = filterTerm.replace(";", "");
        		//alert ("F00" + filterTerm + "F00");
        		$tabs.tabs('select', 1);
        		// prepare to parse full building list by removing paging
        		removePaging();
        		// reset visibility of all building list items
        		$('div.buildingListEntry').show();
    			//	 selects all building list items
    			//	 then removes the building list items that contain the selected attribute
    			//	 then hides the remaining building list items
 				$('div.buildingListEntry').not($('div.buildingListEntry:has(ul.buildingType:contains(' + filterTerm + '))')).hide();
    		sortBuildingList();
    			scrollTo('#personnelViewsNavigation');	
    			showBuildingListFilterNav(filterTerm, 'buildingtype');
    			resetSortForm();
    			return false;
        });
        
				
				// building list filter by work location
				workLocations = new Array();
				$('td.workLocation').each( function () { 
					//alert ($(this).text());
					
					rightnowLocation = $(this).text();
					tempLocations = rightnowLocation.split(',');
				for ( var i in tempLocations )
 					{
 						//alert(foo);
 						foo = $.trim(tempLocations[i]);
 					//	alert(foo);
 						if (!contains(workLocations, foo)) {
 							workLocations.push(foo);
 						//	alert("Added_" + foo + "_to array.");							
 						}
 					}				
				});				
				
				$('td.workLocations > ul > li').each( function () {
					currentLocation = $(this).text();
					currentLocations = currentLocation.split(',');
					var loc_link_str = '';
					for ( var i in currentLocations ) 
						{
							foo = $.trim(currentLocations[i]);
							if (i > 0){
								loc_link_str += ', ';
							}
							if (contains(workLocations, foo)) {
								//alert ('There is a location_' + foo + '_in hte building list.');
								loc_link_str += '<a href="#">' + foo + '</a>';
								$(this).wrapInner('<a href="#"></a>');
							} else {
									loc_link_str += foo;
								//alert("There are no buildings located in_" + foo + ".");
							}
						}
						$(this).html(loc_link_str);
					
//					alert (currentType);

					
				});
				
				

    
        // building list filter by work location
      //  $('td.workLocations li > a').attr('href', '#');
        $('td.workLocations li > a').click(function() {
        		var filterTerm = $.trim($(this).text());
        		filterTerm = filterTerm.replace(";", "");
        		$tabs.tabs('select', 1);
        		// prepare to parse full building list by removing paging
        		removePaging();
        		// reset visibility of all building list items
        		$('div.buildingListEntry').show();
    				// selects all building list items
    				// then removes the building list items that contain the selected attribute
    				// then hides the remaining building list items
 				$('div.buildingListEntry').not($('div.buildingListEntry:has(td.workLocation:contains(' + filterTerm + '))')).hide();
    			
    			sortBuildingList();
    			scrollTo('#personnelViewsNavigation');
    			showBuildingListFilterNav(filterTerm, 'location');
    			resetSortForm();
    			return false;
        });        
        
        

        






  });
