// search.js Jason Boyer 7/07
// convienience functions for search boxes: watermarks and Google CSE searches.

function onGSearch(id)
{
    var q = document.getElementById(id);
    var l = window.location;
    var loc = l.protocol + "//" + l.hostname + '/search/?cx=010541689445484624643%3A-haz5q5tsym&cof=FORID%3A11&q={searchTerms}&sa=Search';
    
    if (q && q.value !== '')
    {
        window.location = loc.replace(/\{searchTerms\}/, escape(q.value.replace(/ /g, "+")));
    }

    return false;
}

function onCSearch(id)
{
	var t = document.getElementById(id);
    var loc = "http://evergreen.lib.in.us/opac/en-US/skin/default/xml/rresult.xml?rt=keyword&tp=keyword&t={searchTerms}&l=17&d=0&f=";

	// without this search munging Evergreen won't find things with 's in the name, or \'s. taking out spaces just keeps it neat.
    if (t && t.value !== '')
    {
        t.value = t.value.replace(/'/g, " ");
		t.value = t.value.replace(/\\/g, " ");
		t.value = t.value.replace(/^\s*/, "");
		t.value = t.value.replace(/\s*$/, "");
		window.location = loc.replace(/\{searchTerms\}/, escape(t.value));
    }

    return false;
}

function pSearchBlur(elm, url)
{
    if (elm && elm.value === '')
    {
        elm.style.background = '#FFFFFF url(' + url + ') left no-repeat';
    }
}

function gSearchBlur(elm)
{
    pSearchBlur(elm, 'http://www.google.com/coop/images/google_custom_search_watermark.gif');
}

function cSearchBlur(elm)
{
    var l = window.location;
    pSearchBlur(elm, l.protocol + "//" + l.hostname + '/images/SearchWatermark.gif');
}

function searchFocus(elm)
{
    if (elm)
    {
        elm.style.background = '#FFFFFF';
    }
}

function init()
{
    var q = document.getElementById("q");
    var t = document.getElementById("t");
    

    if (q)
    {
        gSearchBlur(q);
        addEvent(q, "focus", function () {
            searchFocus(q); 
        }, false);
        
        addEvent(q, "blur", function () {
            gSearchBlur(q); 
        }, false);
    }
    
    if (t)
    {
        cSearchBlur(t);
        addEvent(t, "focus", function () {
            searchFocus(t); 
        }, false);

        addEvent(t, "blur", function () {
            cSearchBlur(t); 
        }, false);
    }
}

addEvent(window, "load", init, false);
