/*
**Scripts by Mark Wales**
http://neo-sophistry.blogspot.com
Originally used on: http://www.philosopherprofiles.com
Tested on: Safari 3.0, FireFox 2.0 - 3.0, IE 6.0 - 8.0, Opera 9
Known Bugs:
------------
None
License:
--------
Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported
http://creativecommons.org/licenses/by-nc-sa/3.0/
--------
You must keep these comments intact if you want to reuse any of this code - a minor price I think you'll agree
*/
onload = pageLoaded; // set the initialisation behaviour
function pageLoaded () {
testForAJAX(); // check for AJAX - means we can avoid AJAX controls being displayed if they're not supported
/* ===Cancel Button on Search Field=== */
// Insert cancel button only if javascript is working
if (document.getElementById("searchform")) {
document.getElementById("searchform").innerHTML += '
';
}
// Rollover effect
document.getElementById("cancel").onmouseover = function() {
if (document.getElementById("cancel")) {
document.getElementById("cancel").src = "/resources/cross_over.gif";
}
}
document.getElementById("cancel").onmouseout = function() {
if (document.getElementById("cancel")) {
document.getElementById("cancel").src = "/resources/cross.gif";
}
}
// Clear the search field, give it focus, remove the results menu
document.getElementById("cancel").onclick = function() {
if (document.getElementById("Search")) {
document.getElementById("Search").focus();
document.getElementById("Search").value = "";
hideMenu();
}
}
// Stop the default behaviour: it stops the focus() function from working
document.getElementById("cancel").onmouseup = function() {
document.getElementById("cancel").style.top = "4px";
document.getElementById("cancel").style.right = "3px";
return false;
}
document.getElementById("cancel").onmousedown = function() {
document.getElementById("cancel").style.top = "5px";
document.getElementById("cancel").style.right = "2px";
return false;
}
/* ===Setting up event handlers=== */
if (document.getElementById("Writers")){ // check for writers element - only appears on 'The Philosophers' page
document.getElementById("Writers").onchange = refreshPhilosophers;
}
if (document.getElementById("AdminMode")) { // "AdminMode" ID is placed by PHP only if a cookie exists
document.getElementById("main").onclick = editProfile;
document.getElementById("sidecolumn").onclick = editProfile;
}
if (document.getElementById("Search")){ // check for 'Search' element - should appear on all pages
document.getElementById("Search").onkeyup = smartSearch;
document.getElementById("Search").onfocus = smartSearch;
document.getElementById("Search").onblur = hideMenu;
document.getElementById("Search").onkeydown = checkForUnexpected;
document.getElementById("Search").onkeypress = checkForUnexpected;
document.getElementById("Search").setAttribute("autocomplete", "off");
selectedResult = 0; // global variable to keep track of which result has been selected. First results selected by default
mouseOverMenu = false;
document.getElementById("searchResults").onmouseover = function() { mouseOverMenu=true }; // set it so that hideMenu knows that the mouse is over the menu so it should not be hidden
document.getElementById("searchResults").onmouseout = function() { mouseOverMenu=false }; // set it so that hideMenu knows that the mouse is not over the menu so it can be hidden
}
}
//-----------Smart-Search----------//
//---------------------------------//
function checkForUnexpected(e) { // stops keydowns/keypresses doing unexpected behaviour
var keyPressed = e || window.event;
var keyCode = 0;
if (keyPressed.keyCode) { keyCode = keyPressed.keyCode };
if (keyPressed.which) { keyCode = keyPressed.which };
switch(keyCode) {
case 13: // avoid enter doing the wrong thing (i.e. submitting the form when it should be going to a selected result)
case 38: // avoid up key doing the wrong thing (i.e. going to the beginning of what's being typed)
case 40: // avoid down key doing the wrong thing (i.e. going to end of what's being typed)
return false;
break;
}
}
function smartSearch(e) {
var keyPressed = e || window.event; // ppk's method for getting the event - e is passed by W3C, window.event is for Microsoft
var keyCode = 0; // set the key code to 0 just to be careful
if (keyPressed.keyCode) { keyCode = keyPressed.keyCode }; // support for normal browsers
if (keyPressed.which) { keyCode = keyPressed.which }; // support for Mozilla
switch(keyCode) {
case 27: // escape key
hideMenu(); // hide the menu when escape key is pressed
break;
case 37: //left arrow
case 39: //right arrow
selectedResult = -1; // make all results unselected
colouriseResults(); // display so that no results are selected
break;
case 38: //up arrow
if (selectedResult > 0) { // go up list by one result
selectedResult -= 1;
} else if (selectedResult == 0) {
selectedResult = numberOfResults-1; // go to the bottom of the list if at first entry- for circular movement (i.e. up on the top takes you to the bottom and down on the bottom takes you to the top)
}
colouriseResults(); // display the selected result properly
// alert(selectedResult);
return false; // make sure the default behaviour does not happen
break;
case 40: //down arrow
if (selectedResult < numberOfResults-1) { // go down the list by one result
selectedResult += 1;
} else if (selectedResult == numberOfResults-1) { // go to top of the list if at last entry - for circular movement
selectedResult = 0;
}
colouriseResults(); // display the selected result properly
// alert(selectedResult);
return false; // make sure the default behaviour does not happen
break;
case 13: //enter
if (selectedResult >= 0 && selectedResult < numberOfResults) { // go to the link with in the result if one is selected
window.location = document.getElementById("result"+selectedResult).getElementsByTagName("a")[0].href;
}
else {
document.getElementById("searchform").submit(); // if no result is selected or there are no results submit the form in the normal way
}
return false; // make sure the default behaviour does not happen
break;
default:
var tempArray = [["Descartes", "René", "1596", "1650", "descartes_r"], ["Berkeley", "George", "1685", "1753", "berkeley_g"], ["Gettier", "Edmund", "1927", "Current", "gettier_el"], ["Gödel", "Kurt", "April 28, 1906 ", "January 14, 1978", "godel_k"], ["Hume", "David", "April 26, 1711", "August 25, 1776", "hume_d"], ["Kant", "Immanuel", "22 April, 1724", "12 February, 1804", "kant_i"], ["Nagel", "Thomas", "July 4, 1936", "Current", "nagel_t"], ["Nietzsche", "Friedrich", "October 15, 1844", "August 25, 1900", "nietzsche_f"], ["Plato", "", "c. 424 BC", "347 BC", "plato"], ["Putnam", "Hilary", "July 31, 1926", "Current", "putnam_h"], ["Quine", "Willard Van Orman", "June 25, 1908", "December 25, 2000", "quine_wvo"], ["Reid", "Thomas", "1710", "1796", "reid_t"], ["Wittgenstein", "Ludwig", "April 26, 1889", "April 29, 1951", "wittgenstein_l"], ["Lewis", "David Kellogg", "September 28, 1941", "October 14, 2001", "lewis_d"], ["Foucault", "Michael", "October 15, 1926", "June 25, 1984", "foucault_m"], ["Frege", "Gottlob", "8 November, 1848", "26 July, 1925", "frege_g"], ["Hobbes", "Thomas", "1588", "1679", "hobbes_t"], ["Bentham", "Jeremy", "1748", "1832", "bentham_j"], ["Mill", "John Stuart", "1806", "1873", "mill_j"], ["Sartre", "Jean-Paul", "June 21st, 1905", "April 15th, 1980", "sartre_j"], ["Leibniz", "Gottfried", "1646", "1716", "leibniz_g"], ["Empiricus", "Sextus", "c. 200 A.D.", "??", "empiricus_s"], ["Rousseau", "Jean-Jacques", "1712", "1788", "rousseau_j"], ["Heraclitus", "", "c. 535", "475 B.C.", "heraclitus"], ["Camus", "Albert", "7th November, 1913", "4th January, 1960", "camus_a"], ["Dewey", "John", "October 20, 1859", "June 1, 1952", "dewey_j"], ["Thales of Miletus", "", "624 BC", "546 BC", "thales"], ["Folk", "The", "252,004 B.C.", "Current", "folk_t"], ["Freud", "Sigmund", "May 6th 1856", "September 23rd 1939", "freud_s"]];
// set searchValue to contents of the search field
var searchValue = document.getElementById("Search").value;
// create an array to hold the results
var resultArray = new Array();
// create a check: if it stays false not results were found
var check = false;
// keeps track of how many results there were
numberOfResults = 0;
lastNameResults = 0;
// if the search field is not blank
if ( searchValue != "") {
var searchLength = searchValue.length; // get the size of the typed search value
var spacePresent = false;
for (var i=0; i < searchValue.length; i++) { // check if there's a space - no point doing lastName/firstName checks if there is
if (searchValue.substring(i,i+1) == ' '){
spacePresent = true;
}
}
if (spacePresent) { // if there's a space you want to look for a full name
for (var i=0; i < tempArray.length; i++) { // go through every name in the database
var foreignCharsVersion = checkForForeignCharacters(tempArray[i][1]).toLowerCase() + ' ' + checkForForeignCharacters(tempArray[i][0]).toLowerCase();
var reverseForeignCharsVersion = checkForForeignCharacters(tempArray[i][0]).toLowerCase() + ' ' + checkForForeignCharacters(tempArray[i][1]).toLowerCase();
var fullNameValue = tempArray[i][1].toLowerCase() + ' ' + tempArray[i][0].toLowerCase(); // take the last name of the current philosopher and convert to lower case
var lastNameFirstNameValue = tempArray[i][0].toLowerCase() + ' ' + tempArray[i][1].toLowerCase(); // the last name followed by the first name, just in case
if (tempArray[i][1].indexOf(' ') != -1) { // check the first name for spaces
var tempFirstName = tempArray[i][1].substring(0,tempArray[i][1].indexOf(' ')); // get rid of the middle name
}
else {
var tempFirstName = tempArray[i][1]; // just use regular first name as there isn't a middle name
}
var firstNameLastNameValue = tempFirstName.toLowerCase() + ' ' + tempArray[i][0].toLowerCase(); // the full name without a middle name
if ( fullNameValue.substring(0,searchLength) == searchValue.toLowerCase() || lastNameFirstNameValue.substring(0,searchLength) == searchValue.toLowerCase() || firstNameLastNameValue.substring(0,searchLength) == searchValue.toLowerCase() || foreignCharsVersion.substring(0,searchLength) == searchValue.toLowerCase() || reverseForeignCharsVersion.substring(0,searchLength) == searchValue.toLowerCase()) { // if what has been typed matches the first x letters of the philosopher's full name
resultArray.push(tempArray[i]); // add matches to an array
check = true; //Result found
numberOfResults += 1; // increase the count for number of results
}
}
}
else {
for (var i=0; i < tempArray.length; i++) { // go through every name in the database
var foreignCharsVersion = checkForForeignCharacters(tempArray[i][0]).toLowerCase();
var lastNameValue = tempArray[i][0].toLowerCase(); // take the last name of the current philosopher and convert to lower case
if ( lastNameValue.substring(0,searchLength) == searchValue.toLowerCase() || foreignCharsVersion.substring(0,searchLength) == searchValue.toLowerCase()) { // if what has been typed matches the first x letters of the philosopher's last name
resultArray.push(tempArray[i]); // add matches to an array
check = true; //Result found
numberOfResults += 1; // increase the count for number of results
lastNameResults += 1; // increase count for number of last names
}
}
for (var i=0; i < tempArray.length; i++) { // go through every name in the database
var foreignCharsVersion = checkForForeignCharacters(tempArray[i][1]).toLowerCase();
var firstNameValue = tempArray[i][1].toLowerCase();
if ( firstNameValue.substring(0,searchLength) == searchValue.toLowerCase() || foreignCharsVersion.substring(0,searchLength) == searchValue.toLowerCase() ) { // if what has been typed matches the first x letters of the philosopher's first name
resultArray.push(tempArray[i]); // add matches to an array
check = true; //Result found
numberOfResults += 1; // increase the count for number of results
}
}
}
if (check) { // if results have been found
var result = "";
for (var i=0; i < resultArray.length; i++) { // go through the results and add a
for each one if (!spacePresent) { // put headings for non-full results if (i == 0 && lastNameResults != 0) { // check if there are any last name results and insert heading if there are result += '
Last Name Matches:
'; } if (i == lastNameResults) { // insert first name results result += 'First Name Matches:
'; } } else { // put headings for full results if (i == 0) { result += 'Full Matches:
'; } } if (resultArray[i][1] != "") { //Check for first name if (i < lastNameResults) { // last name matches display in "LastName, FirstNames" order result += ''; } else { // first name matches display in "FirstName LastName" order result += ''; } } else { //For philosophers with only last names result += '' + resultArray[i][0] + '
(' + resultArray[i][2] + ' - ' + resultArray[i][3] + ')
s added together go inside the div
}
document.getElementById("searchResults").style.visibility = "visible"; // show the results div
selectedResult = 0; // set default search result to the first in the list
colouriseResults(); // colour-in the selected boxes
for (var i=0; i