//mk 8/6-2007

var should_highlight = false;
    
function highlight_next_load() {
  should_highlight = true;
}


var indexArray = index.split(",");

function sortVotes(a, b) {
  var first = a.split(" ");
  var second = b.split(" ");
  return second[1] - first[1];
}

var result = "";
var cachedResults = "";
var firstLoad = false;

function hugin_search() {
  firstLoad = true;
        var expression = document.searchform.expression.value;
	expression = expression.toLowerCase();
	var keywords = expression.split(" ");
	var indices = new Array();

	result = "";

	for (var k in keywords) {
	  keyword = keywords[k];
	  if (!(parseInt(keyword) > 0)) { //index does not support search for numbers
	    var i = -1;
	    for (var s in indexArray) {
	      i = i + 1;
	      //if (indexArray[s].match(keyword) != null) {
	      if (indexArray[s] == keyword) {
		result = result + indexArray[s] + " ";
	        for (j = i+1; j < indexArray.length && indexArray[j] > -1; j = j + 1) {
		  //result = result +  "_" + indexArray[j] + "_";
		  indices.push(indexArray[j]);
	        }
	      }
	    }
	  }
	}
	indices.sort();
	var pageVotes = new Array();
	if (indices.length > 0) {
	  var current = -1;
	  var count = 0;
	  for (var j in indices) {
	    if (indices[j] != current) {
	      if (current >= 0) {
		pageVotes.push(current + " " + count);
		current = indices[j];
		count = 1;
	      } else {
		current = indices[j];
		count = 1;
	      }
	    } else {
	      count = count + 1;
	    }
	  }
	  pageVotes.push(current + " " + count);
	  pageVotes.sort(sortVotes);
	  
	  var links = "";
	  var matches = -1;
	  for (var i in pageVotes) {
	    var pageVote = pageVotes[i].split(" ");
	    if (matches == -1) {
	      matches = pageVote[1];
	      if (matches > 1) {
		links = links + "<h1>Best results (" + matches + " keywords):</h1>";
	      } else {
		links = links + "<h1>Best results (" + matches + " keyword):</h1>";
	      }
	    }
	    if (matches != pageVote[1]) {
	      matches = pageVote[1];
	      if (matches > 1) {
		links = links + "<hr><h2>Other results (" + matches + " keywords):</h2>";
	      } else {
		links = links + "<hr><h2>Other results (" + matches + " keyword):</h2>";
	      }
	    }
	    //	    var url = pageUrl[pageVote[0]];
	    var url = "javascript:loadPage(\""+pageUrl[pageVote[0]]+"\");";
	    var title = pageTitle[pageVote[0]];
	    links = links + "<br><a href='" + url + "'>" + title + "</a>";
	    //links = links + " " + pageVotes[i];
	  }
	} else {
	  result = "none...";
	  links = "<h1>No pages found.</h1>"
	}

	cachedResults = "";
	cachedResults = cachedResults + "<html><head><LINK REL=STYLESHEET HREF='pane.css' TYPE='text/css'>\n";
	cachedResults = cachedResults + "<script language='JavaScript'>\n";
	cachedResults = cachedResults + "<!--\n";
	cachedResults = cachedResults + "function loadPage(url) {\n";
	cachedResults = cachedResults + "  try {\n";
	cachedResults = cachedResults + "    parent.frames['topbar'].highlight_next_load();\n";
	cachedResults = cachedResults + "    document.location=url;\n";
	cachedResults = cachedResults + "  } catch (error) {\n";
	cachedResults = cachedResults + "    //open result page without highlighting keywords\n";
	cachedResults = cachedResults + "    document.location=url;\n";
	cachedResults = cachedResults + "  }\n";
	cachedResults = cachedResults + "}\n";
	cachedResults = cachedResults + "-->\n";
	cachedResults = cachedResults + "</script>\n";
	cachedResults = cachedResults + "</head><body bgcolor='#ffffcc'>\n";
	cachedResults = cachedResults + "<i><b>Keywords found:</b> "+result+"</i>\n";
	cachedResults = cachedResults + links + "\n";
	cachedResults = cachedResults + "</body></html>\n";



	parent.frames[2].location="top_bar.html";
	var mainframe = parent.frames[2].document;
	mainframe.open();
	//mainframe.writeln("<html><head><LINK REL=STYLESHEET HREF='pane.css' TYPE='text/css'>");

	////mainframe.writeln("<script type='text/javascript' src='showpage.js'></script>");

	//mainframe.writeln("<script language='JavaScript'>");
	//mainframe.writeln("<!--");
	//mainframe.writeln("function loadPage(url) {");
	//mainframe.writeln("  parent.frames['topbar'].highlight_next_load();");
	//mainframe.writeln("  document.location=url;");
	//mainframe.writeln("}");
	//mainframe.writeln("-->");
	//mainframe.writeln("</script>");
	//mainframe.writeln("</head><body bgcolor='#ffffcc'>");
	//mainframe.writeln("<i><b>Keywords found:</b> "+result+"</i>");
	//mainframe.writeln(links);
	//mainframe.writeln("</body></html>");
	mainframe.writeln(cachedResults);
	mainframe.close();
	//cachedResults = parent.frames['pane'].document.getElementsByTagName("html")[0].innerHTML;
}



function redraw_page_highlighted(evt) {
  //if user click back button to view search results, the page must be "redrawn" to make the page "trusted" to firefox. haxehaxe code.
  var pageSrc = parent.frames['pane'].location + " ";
  if (typeof parent.document.getElementsByTagName('frame')[2].addEventListener != "undefined" && pageSrc.match("top_bar") != null && firstLoad == false) {
    //display page with last search results
    var mainframe = parent.frames[2].document;
    mainframe.open();
    mainframe.writeln(cachedResults);
    mainframe.close();  
    return;
  }
  firstLoad = false;
  if (should_highlight != true) {
    return;
  }
  should_highlight = false;
  var htmlDocument = parent.frames['pane'].document.getElementsByTagName("html")[0].innerHTML;


  htmlDocument = htmlDocument.replace(/</g," <");
  htmlDocument = htmlDocument.replace(/>/g,"> ");
/*
  htmlDocument = htmlDocument.replace(/<script/gi," <script");
  htmlDocument = htmlDocument.replace(/script>/gi,"script> ");
  htmlDocument = htmlDocument.replace(/<!--/gi," <!--");
  htmlDocument = htmlDocument.replace(/-->/gi,"--> ");
  htmlDocument = htmlDocument.replace(/<head/gi," <head");
  htmlDocument = htmlDocument.replace(/head>/gi,"head> ");
  */
  parent.frames['pane'].document.open();
  parent.frames['pane'].document.writeln("<html>");
  //  parent.frames['pane'].document.writeln("<html><head></head><body></body></html>");
  //  return;
  arrKeywords = result.split(" ");
  docElements = htmlDocument.split(" ");
  var ignoreBlock = 0;
  for (var i in docElements) {
    var match = false;
    if (docElements[i].toLowerCase().match("<script") != null) {
      ignoreBlock = ignoreBlock + 1;
      //alert(docElements[i]);
    }
    if (docElements[i].toLowerCase().match("</script") != null) {
      ignoreBlock = ignoreBlock - 1;
      //alert(docElements[i]);
    }
    if (docElements[i].toLowerCase().match("<!--") != null) {
      ignoreBlock = ignoreBlock + 1;
      //alert(docElements[i]);
    }
    if (docElements[i].toLowerCase().match("-->") != null) {
      ignoreBlock = ignoreBlock - 1;
      //alert(docElements[i]);
    }
    if (docElements[i].toLowerCase().match("<head") != null) {
      ignoreBlock = ignoreBlock + 1;
      //alert(docElements[i]);
    }
    if (docElements[i].toLowerCase().match("</head") != null) {
      ignoreBlock = ignoreBlock - 1;
      //alert(docElements[i]);
    }
    //    if (docElements[i].match("onload") != null) {
    //      docElements[i] = docElements[i].replace(/onload/i,"_onload");
    //    }

    if (ignoreBlock < 1) {
      for (var j in arrKeywords) {
	if (docElements[i].length > 0 && arrKeywords[j].length > 0 && docElements[i].toLowerCase().substring(0,arrKeywords[j].length) == arrKeywords[j]) {
	  //alert(docElements[i]);
	  match = true;
	}
      }
    }
    if (match == false) {
      var c = docElements[i].charAt(0).toLowerCase();
      if (c == "." || c == "," || c == "<") {
	parent.frames['pane'].document.write(docElements[i]);
      } else {
	parent.frames['pane'].document.write(" " + docElements[i]);
      }

    } else {
      parent.frames['pane'].document.write(" <FONT style='BACKGROUND-COLOR: red; COLOR: white'><b><big>" + docElements[i] + "</big></b></FONT>");
    }
  }
  parent.frames['pane'].document.writeln("</html>");
  parent.frames['pane'].document.close();
  //alert(ignoreBlock);
}

//register page load event for keyword highlighting
if (typeof parent.document.getElementsByTagName('frame')[2].addEventListener != "undefined") {
  parent.document.getElementsByTagName('frame')[2].addEventListener('load',redraw_page_highlighted,true);
 } else if (typeof parent.document.getElementsByTagName('frame')[2].attachEvent != "undefined") {
  parent.document.getElementsByTagName('frame')[2].attachEvent('onload',redraw_page_highlighted);
 }
