var jbglobalpath = (typeof thisjb != "undefined"?"http://www.webactivemagazine.co.uk/":"http://www.computeractive.co.uk/");
var jbimagepath = "http://images.vnunet.com";

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,IE4;
if (document.layers) {
	NN4 = true;
	origWidth = innerWidth;
	origHeight = innerHeight;
	onresize = reArrange;
} else if (document.all)
	IE4 = true;

function reArrange() {
	if (innerWidth != origWidth || innerHeight != origHeight)
		location.reload();
}

function openJargonBuster(str) {
//	var loco = jbglobalpath + (typeof thisjb != "undefined"?"WA":"CA") + "/jargonbuster_results.htm?text=" + escape(str);
	var loco = "";
//	if (location.href.indexOf("computeractive.co.uk") != -1 || location.href.indexOf("webactivemagazine.co.uk") != -1 || (location.port && location.port == "8888")) {
	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"
	} else if (location.href.indexOf("computeractive.co.uk") == -1 && location.href.indexOf("webactivemagazine.co.uk") == -1)
		loco += jbglobalpath.substring(0,jbglobalpath.length-1);
	loco += "/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;
}

function openHelp() {
	var helpwin = window.open(jbglobalpath + (typeof thisjb != "undefined"?"WA":"CA") +  "/add_help_2.htm","helpwin","HEIGHT=390,WIDTH=418,STATUS=NO,DIRECTORIES=NO,LOCATION=NO,TOOLBAR=NO,SCROLLBARS=NO,MENUBAR=NO,RESIZABLE=NO");
}

function findImage(obj,d) {
	var img;
	if (!d)
		d = document;
	if (!(img = d[obj]) && d.all)
		img = d.all[obj];
	for (var i = 0; !img && d.layers && i < d.layers.length; i++) {
		img = findImage(obj,d.layers[i].document);
	}
	return img;
}


var addButtons = new Array();
addButtons[0] = new Image();
addButtons[1] = new Image();
addButtons[0].src = jbimagepath + "/v6_image/ca/jargon/add_off.gif";
addButtons[1].src = jbimagepath + "/v6_image/ca/jargon/add_on.gif";


var browseButtons = new Array();
browseButtons[0] = new Image();
browseButtons[1] = new Image();
browseButtons[0].src = jbimagepath + "/v6_image/ca/jargon/browse_off.gif";
browseButtons[1].src = jbimagepath + "/v6_image/ca/jargon/browse_on.gif";

function pressMouse(name,arr) {
	var img = findImage(name);
	if (img.src.indexOf(arr[0].src) == -1)
		img.src = arr[0].src;
	else
		img.src = arr[1].src;
}

function IE4Repair() {
	if (document.all)
		document.all["addsite"].src = addButtons[0].src;
}

// 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;
		document.JBSearch.def.value = jargonresults[results[0][0]].text;
		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)
			document.all.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() {
	if (checkSearchLength(document.JBSearch.words.value)) { // can't search on single characters
		if (typeof check == "undefined") { // it's the embedded search, so open the handheld and pass the query
			openJargonBuster(document.JBSearch.words.value);
			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 || typeof check == "undefined") { // IE4 never does reload searches, only main search (check=true) with NN4.
			newSearch(document.JBSearch.words.value.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) {
	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
//(typeof thisjb != "undefined"?"webactivemagazine":"computeractive")
function renderJargonbuster() {
	var jb = "";
	if ((typeof check == "undefined" && typeof computeractive == "undefined") || (typeof computeractive != "undefined" && !NN4)) {
		jb += "<STYLE TYPE=\"text/css\">\n";
		jb += ".JBInput { font-size:13px; background-color:#F7E7C7 }\n";
		jb += ".jargonbuster { font-size:11px; font-family:verdana,arial,helvetica,sans-serif; color:#DF002B }\n";
		jb += "</STYLE>\n";
	}
	var extrawidth = Math.round((width - 148)/2);
	jb += "<FORM NAME=\"JBSearch\" onSubmit=\"return multiSearch()\">\n";
	jb += (NN4 && typeof compare != "undefined"?"<DIV ALIGN=CENTER>":"");
	jb += "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 BGCOLOR=\"#F7E7C7\" WIDTH=" + width + " HEIGHT=49>\n";
	jb += "<TR>\n";
	jb += "<TD WIDTH=" + extrawidth + " BACKGROUND=\"" + jbimagepath + (typeof thisjb != "undefined"?"/v6_image/wa/jargon/jb_top_3_bg_wa.gif":"/v6_image/ca/jargon/jb_top_3_bg_ca.gif") + "\">&nbsp;</TD>";
	jb += "<TD ALIGN=CENTER WIDTH=148><A HREF=\"" + jbglobalpath + "\" TARGET=\"newwin\"><IMG SRC=\"" + jbimagepath + (typeof thisjb != "undefined"?"/v6_image/wa/jargon/wa_jb_top_3.gif":"/v6_image/ca/jargon/jb_top_3.gif") + "\" WIDTH=\"148\" HEIGHT=\"49\" BORDER=\"0\" ALT=\"Computeractive Jargon Buster\"></A></TD>\n";
	jb += "<TD WIDTH=" + (width - 148 - extrawidth) + " BACKGROUND=\"" + jbimagepath + (typeof thisjb != "undefined"?"/v6_image/wa/jargon/jb_top_3_bg_wa.gif":"/v6_image/ca/jargon/jb_top_3_bg_ca.gif") + "\">&nbsp;</TD>";
	jb += "</TR>\n";
	jb += "</TABLE>\n";
	jb += "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 BGCOLOR=\"#F7E7C7\" WIDTH=" + width + ">\n";
	jb += "<TR>\n";
	jb += "<TD ROWSPAN=3 WIDTH=6><IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=6 HEIGHT=1 BORDER=0 ALT=\"\"><BR></TD>\n";
	jb += "<TD COLSPAN=2><DIV CLASS=\"jargonbuster\">Type in any computer-related word or phrase you don't really understand and click \"Go\".</DIV></TD>\n";
	jb += "<TD ROWSPAN=3 WIDTH=6><IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=6 HEIGHT=1 BORDER=0 ALT=\"\"><BR></TD>\n";
	jb += "</TR>\n";
	jb += "<TR>\n";
	jb += "<TD><IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR>\n";
	jb += "<INPUT TYPE=\"text\" NAME=\"words\" SIZE=\"" + (Math.round((width - 150)/(NN4?8:7)) + (NN4?8:11)) + "\" CLASS=\"JBInput\">\n";
	jb += "<BR>\n";
	jb += "<IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR></TD>\n";
	jb += "<TD><IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR><INPUT TYPE=\"IMAGE\" SRC=\"" + jbimagepath + "/v6_image/ca/jargon/jb_go_2.gif\" BORDER=0 NAME=\"jb_submit\" VALUE=\"Go\" onFocus=\"this.blur()\"><BR>\n";
	jb += "<IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR></TD>\n";
	jb += "</TR>\n";
	jb += "<TR>\n";
	jb += "<TD COLSPAN=2 ALIGN=CENTER>\n";
	jb += "<A onMousedown=\"pressMouse('browse',browseButtons)\" onMouseup=\"pressMouse('browse',browseButtons)\" HREF=\"" + jbglobalpath +  (typeof thisjb != "undefined"?"WA":"CA") + "/jargonbuster.jsp\" "
	jb += (typeof computeractive == "undefined"?"TARGET=\"microwin\"":"");
	jb += " onFocus=\"this.blur()\"><IMG NAME=\"browse\" SRC=\"" + jbimagepath + "/v6_image/ca/jargon/browse_off.gif\" WIDTH=\"119\" HEIGHT=\"21\" BORDER=\"0\" ALT=\"Browse the entire Jargon Buster database\"></A>\n";
	jb += "<A onMousedown=\"pressMouse('addsite',addButtons)\" onMouseup=\"pressMouse('addsite',addButtons)\" onMouseout=\"IE4Repair()\" HREF=\"javascript:openHelp()\" \"javascript:openHelp()\"onFocus=\"this.blur()\"><IMG NAME=\"addsite\" SRC=\"" + jbimagepath + "/v6_image/ca/jargon/add_off.gif\" WIDTH=\"119\" HEIGHT=\"21\" BORDER=\"0\" ALT=\"Add the Jargon Buster to your site\"></A>\n";
	jb += "</TD></TR>\n";
	if (typeof computeractive == "undefined" || !computeractive) {
		jb += "<TR><TD COLSPAN=4 ALIGN=CENTER>\n";
		jb += "<IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR>";
		jb += "<A HREF=\"" + jbglobalpath + "\" TARGET=\"newwin\" onFocus=\"this.blur()\"><IMG SRC=\"" + jbimagepath + (typeof thisjb != "undefined"?"/v6_image/wa/jargon/wa_logo_s.gif":"/v6_image/ca/jargon/ca_logo_s.gif") + "\" WIDTH=120 HEIGHT=33 BORDER=0 ALT=\"\"></A><BR>";
		jb += "<IMG SRC=\"" + jbimagepath + "/v6_image/ca/jargon/px.gif\" WIDTH=1 HEIGHT=3 BORDER=0 ALT=\"\"><BR>";
		jb += "</TD>\n";
		jb += "</TR>\n";
	}
	jb += "</TABLE>\n";
	jb += (NN4 && typeof compare != "undefined"?"</DIV>":"");
	jb += "</FORM>\n";
	document.writeln(jb);
}

function renderReducedJB() {
	var jb = "<FORM NAME=\"JBSearch\" onSubmit=\"return multiSearch()\">\n";
	jb += "<INPUT TYPE=\"text\" NAME=\"words\" SIZE=\"" + (navigator.appName == "Netscape"?13:16) + "\">\n";
	jb += "<INPUT TYPE=\"submit\" value=\"Go\">\n</FORM>";
	document.writeln(jb);
}

if (typeof check == "undefined") {
	if (typeof jb_reduced == "undefined")
		renderJargonbuster();
	else
		renderReducedJB();
}
