//  Jargon Buster

//  Global variables:
//  check [true|false];
//  jbNewPage - jump to a new page for results
//  newWindow - jump to a new page in new window for results

window.onload = function() {
  if (location.search) {
  
      	var words = getRequestParam("words");
      	var letter = getRequestParam("letter");
  
      if (typeof words != "undefined") {
  	       var ls = location.search;
  	       while (ls.indexOf("+") != -1) {
  		            ls = ls.substring(0,ls.indexOf("+")) + " " + ls.substring(ls.indexOf("+")+1);
  	       }
  	       ls = (ls.indexOf("&") > 7?ls.substring(7,ls.indexOf("&")):ls.substring(7));
         document.JBSearch.words.value = ls;
  		     multiSearch();
  	     } 	else if (typeof letter != "undefined") {
  		     letterSearch(letter);
  		   }
  }  
}

function getRequestParam(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

if (location.search && location.search.indexOf("width") == 1)
	width = parseInt(location.search.substring(7));
if (typeof width == "undefined")
	width = 160;
else if (typeof width != "number" || width < 160 || isNaN(width))
	width = 160;
else if (width > 350)
	width = 350;

var NN4,NN7,IE4;
if (document.layers) {
	NN4 = true;
	origWidth = innerWidth;
	origHeight = innerHeight;
	onresize = reArrange;
} else if (document.all)
	IE4 = true;
else if (document.getElementById)
	NN7 = true;

function reArrange() {
	if (innerWidth != origWidth || innerHeight != origHeight)
		location.reload();
}

function getObject(obj) {
	return (IE4?document.all[obj]:(NN7?document.getElementById(obj):eval("document." + obj)));
}

function openJargonBuster(str) {
	var loco = "";
	if (location.href.indexOf("core.vnunet.com") != -1 || (location.port && location.port == "8888")) {
		if (location.href.indexOf("/ca") != -1)
			loco += "/ca"
		if (location.href.indexOf("/wa") != -1)
			loco += "/wa"
		if (location.href.indexOf("/ah") != -1)
			loco += "/ah"
	}
	loco += (location.href.indexOf("/ah") == -1 && location.href.indexOf("activehome.co.uk") == -1?"/interactive":"") + "/jargonbuster/popup?text=" + escape(str);
	var jbwin = window.open(loco,"jbwin","HEIGHT=320,WIDTH=250,STATUS=NO,DIRECTORIES=NO,LOCATION=NO,buttons=no,TOOLBAR=NO,SCROLLBARS=NO,MENUBAR=NO,RESIZABLE=NO");
	return false;
}

// SEARCHING AND SORTING ALGORYTHMNS //
function checkSearchLength(str) {
	var temparr = [];
	if (str.indexOf(" ") != -1)
		temparr = str.split(" ");
	else
		temparr[0] = str;
	for (var i = 0; i < temparr.length; i++) {
		if (!temparr[i] || temparr[i].length < 2)
			return false;
	}
	return true;
}

var results = [];
function handleResults(index,n) {
	for (var i = 0; i < results.length; i++) {
		if (index == results[i][0]) {
			results[i][1] += n;
			return;
		}
	}
	results[results.length] = [index,n];
}

function sortResults() {
	var tmp;
	for (var i = 0; i < results.length; i++) {
		for (var j = i+1; j < results.length; j++) {
			if (results[i][1] < results[j][1]) {
				tmp = results[i];
				results[i] = results[j];
				results[j] = tmp;
			}
		}
	}
}

var str = "";
function displayResults() {
	if (results[0] == null) // no matches, so set response as a result
		results = [[jargonresults.length-1,100]];
	if (typeof handheld != "undefined") { // it's the handheld, so populate the fields with data
		if (results[0][0] != jargonresults.length-1)
			document.JBSearch.words.value = (jargonresults[results[0][0]].word).replace(/&eacute;/g,"e");
		document.JBSearch.def.value = (jargonresults[results[0][0]].text).replace(/&eacute;/g,"e");
		if (results.length > 1)
			getStyleObject("moreresults").visibility = "visible";
		else 
			getStyleObject("moreresults").visibility = "hidden";
	} else { // it's the main search page so build the results list
		str = "<DIV CLASS=\"bigresults\">Results</DIV>\n<OL>";
		for (var i = 0; i < results.length; i++) {
			str += "<DIV CLASS=\"smallspace\"><B><LI><I>" + jargonresults[results[i][0]].word + "<BR></I></B>" + jargonresults[results[i][0]].text + "</DIV>";
		}
		str += "<DIV CLASS=\"jbred\"><A HREF=\"#search\">&laquo; Back to search and browse</A></DIV>\n</OL>";
		if (IE4 || NN7)
			getObject("results").innerHTML = str;
//	NN4 results written directly on the page, using global variable str, having been given its value here
	}
}

function actualSearch(str,x) {
	var jr,jr2;
	for (var i = 0; i < jargonresults.length-1; i++) { // it's length-1 cos no results is on the end
		jr = jargonresults[i].word.toLowerCase();
		jr2 = jr;
		while (jr2.indexOf("-") != -1) {
			jr2 = jr2.substring(0,jr2.indexOf("-")) + " " + jr2.substring(jr2.indexOf("-")+1);
		}
		if (str == jr) {
			if (x == null)
				handleResults(i,500);
			else
				handleResults(i,(x?100-(x*6):100));
		} else if (x == null && str == jr2)
				handleResults(i,300);
		else if (jr.indexOf(" " + str + " ") != -1 || jr.indexOf(str + " ") == 0 || (jr.indexOf(" " + str) != -1 && ((jr.length-1) == (jr.indexOf(" " + str)+str.length))))
			handleResults(i,(x?60-(x*6):60));
		else if (jr.indexOf("(" + str + ")") != -1 || jr.indexOf(" " + str + ")") != -1 || jr.indexOf("(" + str + " ") != -1)
			handleResults(i,(x?50-(x*5):50));
		else if (jr.indexOf("-" + str + "-") != -1 || jr.indexOf(" " + str + "-") != -1 || jr.indexOf("-" + str + " ") != -1 || jr.indexOf(str + "-") == 0 || (jr.indexOf("-" + str) != -1 && ((jr.length-1) == (jr.indexOf("-" + str)+str.length))))
			handleResults(i,(x?40-(x*4):40));
		else if (jr.indexOf(str) != -1)
			handleResults(i,(x?20-(x*2):20));
	}
}


function newSearch(s) {
	var sarr;
	results = [];
	actualSearch(s);
	if (s.indexOf(" ") != -1)
		sarr = s.split(" ");
	else if (s.indexOf(",") != -1)
		sarr = s.split(",");
	if (sarr) {
		for (var i = 0; i < sarr.length; i++) {
			actualSearch(sarr[i],i);
		}
	}
	sortResults();
	displayResults();
}

function multiSearch() {
	var words = document.JBSearch.words.value;
	if (checkSearchLength(words)) { // can't search on single characters
		if (typeof jbNewPage != "undefined") {
		    //Find out if we are live or not	    
		    var site = "";
		    if (location.port == "8888" || location.href.indexOf("core.vnunet.com") != -1) {
				site = location.pathname.substring(1);
				site = "/" + site.substring(0,site.indexOf("/"));
		    }
			if (typeof newWindow != "undefined" && newWindow) {
			    var newWin = window.open(site+jbNewPage+'?words='+words, '_blank');
			    newWin.focus();
		        void(0);
				return false;
			} else {
		        window.location = site+jbNewPage+'?words='+words;
			    return false;
			}
		} else if (typeof check == "undefined") { // it's the embedded search, so open the handheld and pass the query
			openJargonBuster(words);
			return false;
		} else if (typeof handheld != "undefined") { //  Handheld never does reload search - puts values in form fields
			document.JBSearch.def.value = "Searching...";
			setTimeout("newSearch(document.JBSearch.words.value.toLowerCase())",1);
			return false;
		} else if ((IE4 || NN7) || typeof check == "undefined") { // IE4 never does reload searches, only main search (check=true) with NN4.
			newSearch(words.toLowerCase());
			return false;
		} else if (NN4 && ls) {
			newSearch(ls);
			ls = "";
			return false; 
		} else if (NN4) {
			return true;
		}
	} else {
		alert("Every search term you use must be at least two characters \nlong. If you want to browse the database letter by letter, \nclick on the list of letters below \"Browse for a definition\".");
		return false;
	}

}

function letterSearch(x) {
    if (typeof jbNewPage != "undefined") { // if jbNewPage url specified - jump to another window

	    //Find out if we are live or not	    
	    var site = "";
	    if (location.port == "8888" || location.href.indexOf("core.vnunet.com") != -1) {
			site = location.pathname.substring(1);
			site = "/" + site.substring(0,site.indexOf("/"));
	    }
		
		if(typeof newWindow != "undefined" && newWindow) {
		   var newWin = window.open(site+jbNewPage+'?letter='+x, '_blank');
		   newWin.focus();
	       void(0);
		} else {
	       window.location = site+jbNewPage+'?letter='+x;
		}
	} else {
       results = [];
	   if(x && x !== "ALL" && x.length == 1) {
		  for(var i = 1; i < jargonresults.length-1; i++) {
			  if(jargonresults[i].word.charAt(0).toLowerCase() == x.toLowerCase())
				 results[results.length] = [i,100];
		  }
	   } else if (x && x == "ALL") {
	      for (var i = 1; i < jargonresults.length-1; i++) {
			results[results.length] = [i,100];
		  }
	   }
		displayResults();
	}
}
// END SEARCHING/SORTING
