/* search.js
This module implements all dynamic functionality for search.html, by providing
various functions used in callbacks.
External objects:
init_function() - Function that is executed while search.html
smod(event) - Function executed when the search term changes.
change_index(URI) - Change the current index used for searching to the given URI.
*/
var chart;
var q = ""; // global of last run query
var cache_base = "http://search.isc.swlabs.org";
var un = false; //username global
var tok = false; // token global
/* Function executed while loading search.html*/
function init_function()
{
read_cookies();
set_login();
hasher();
window.setInterval("hasher()",500);
}
/* Change search term to that specified in "mq" */
function smod(e)
{
//$("sr-results").innerHTML += "Loading more results for '"+$("mq").value.escapeHTML();
cache_base = "http://search.isc.swlabs.org";
perform_full_search($("mq").value);
if(e) e.returnValue = false;
try {if(e) e.preventDefault();} catch (ex) {}
return false;
}
/* Change the indexer URL to base */
function change_index(base, c_base)
{
if (typeof c_base == 'undefined' ) c_base = "http://search.isc.swlabs.org";
cache_base = c_base;
$("sr-results").innerHTML = "Loading results for '"+q+"'";
var s = document.createElement('script');
s.src = base + "?query="+ q +"&hitsPerSite=2&lang=en&hitsPerPage=10&type=json&start=0";
document.getElementsByTagName('head')[0].appendChild(s);
return false;
}
function set_login()
{
if (un && tok)
{
$("li-welcome").innerHTML = "Hello " + un;
$("li-box").style.display = "block";
$("lo-box").style.display = "none";
$("tabs").style.display = "block";
}
else
{
$("lo-box").style.display = "block";
$("tabs").style.display = "none";
}
}
function read_cookies()
{
un = getCookie("wikicitiesUserName");
tok = getCookie("wikicitiesToken");
}
function getCookie(name)
{
var C = document.cookie.split("; ");
for (i=0; i < C.length; i++)
{
kv = C[i].split("=");
if(kv[0] == name){ return kv[1]; }
}
return false;
}
function comma(number) {
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0)) {
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
} else {
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
}
return (output);
}
else return number;
}
function srfetch(query, start)
{
q = query;
if(q == ""){ return; }
if(document.location.hash != "#"+q && document.location.hash != "#"+encodeURIComponent(q)) document.location.hash = "#"+encodeURIComponent(q);
$("q").value = q;
$("mq").value = q;
$("query").innerHTML = q.escapeHTML();
$("result-q").innerHTML = q.escapeHTML();
$("discuss-result-link").innerHTML = '(discuss these results)';
$("mini-title").innerHTML = q.escapeHTML();
var head = document.getElementsByTagName('head')[0];
cache_base = "http://search.isc.swlabs.org";
// get results
//alert("http://search.isc.swlabs.org/nutchsearch?query="+ encodeURIComponent(q) +"&hitsPerSite=2&lang=en&hitsPerPage=10&type=json&start=" + start);
var s = document.createElement('script');
s.src = "http://not-quite-ready-yet.index.swlabs.org/nutchsearch?query="+ encodeURIComponent(q) +"&hitsPerSite=1&lang=en&hitsPerPage=10&type=json&start=" + start;
head.appendChild(s);
// also get any mini article
var s = document.createElement('script');
s.src = "http://search.wikia.com/index.php?action=ajax&rs=wfGetArticleJSON&rsargs%5B%5D=Mini:" + encodeURIComponent(q);
head.appendChild(s);
// also get any people
var s = document.createElement('script');
s.src = "http://alpha.search.wikia.com:8983/solr/select/?start=0&row=20&wt=json&json.wrf=processPeople&indent=true&q=" + encodeURIComponent(q);
head.appendChild(s);
}
var lasthash = "";
function hasher()
{
if(document.location.hash == lasthash)
{
return;
}
lasthash = document.location.hash;
try
{
var newq = document.location.hash.substr(1);
}
catch (e)
{
throw "Error failed to decode URI:" + document.location.hash.substr(1);
}
perform_full_search(newq);
}
function perform_full_search(query)
{
RedirectQuery.handle(query);
$("sr-results").innerHTML = "Loading results for '"+query.escapeHTML()+"'";
srfetch(query,0);
DictionaryQuery.handle(query);
MathQuery.handle(query);
IntegralQuery.handle(query);
// DictionaryQuery.handle(query) coming soon.
}
// break out results since we accidentially (and temporarily) overloaded this function
function processJSON(j)
{
if(j)
{
if(j.search)
{
processResult(j.search);
}else if(j.html){
processMini(j);
}
}else{
// only mini article so far doesn't return anything
$("sr-mini-tr").innerHTML = '';
$("sr-mini-controls").innerHTML = "";
$("sr-mini-content").innerHTML = 'We do not have a mini article about "'+q.escapeHTML()+'". Improve the search results for everyone by starting this article!';
}
}
function processPeople(r)
{
var docs = r.response.docs;
var p = $("sr-people-content");
p.innerHTML = '';
if (docs.length != 0) {
for(var i=0; i < docs.length; i++) {
if (docs[i]) {
if((docs[i].defaultPhotoSet)) {
p.innerHTML += '
';
} else {
p.innerHTML += '
';
}
if (((i+1)%4==0) || (i==docs.length-1)) {
p.innerHTML += '
' + htm + ''; } last1 = host[0]; // for next result indentation sr.innerHTML += htm; } var hitsPerPage = 10; if(search.end - search.start >= hitsPerPage) { sr.innerHTML += ''; } else { sr.innerHTML += '
Edit';
if (j.html!="") {
$("sr-mini-controls").innerHTML = '';
$("sr-mini-controls").innerHTML += ' - ';
$("sr-mini-controls").innerHTML += 'History - Full Article';
}
}
function context(e)
{
var kw = $(e);
var more;
if(kw.next() && kw.next().tagName == "SPAN")
{
var stub = kw.next().innerHTML.match(/(.+?)\W{2,}/);
more = (stub)?stub[0]:kw.next().innerHTML;
if(more.length > 3)
{
kw.style.textDecoration = "underline";
kw.next().style.textDecoration = "underline";
kw.onmouseout = function (){ this.style.textDecoration = ""; this.next().style.textDecoration = ""; }
kw.onclick = function () {
$("sr-results").innerHTML = "";
srfetch(kw.innerHTML + more.unescapeHTML(),0);
}
}
}
return false;
}
function boost(amount)
{
amount -= 0;
amount = (Math.round(amount*100))/100;
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
function toggleMini() {
if ($("sr-mini-content").hasClassName("collapsed")) {
$("sr-mini-content").removeClassName("collapsed");
$("sr-mini-content").addClassName("expanded");
$("sr-mini-toggle-button").innerHTML = 'Collapse';
} else {
$("sr-mini-content").removeClassName("expanded");
$("sr-mini-content").addClassName("collapsed");
$("sr-mini-toggle-button").innerHTML = 'Expand';
}
}
function load_search_terms()
{
window.location.href= '#' + encodeURIComponent(document.forms[0].q.value);
}
/* Perform testing for all modules. */
function test()
{
Calculate.test();
}
//test();