window.SiteSearch = function() {
    this.debug = false;
    this.debugOutput = function(a) {
        alert(a)
    };
    this.currentResultNum = 1;
    this.currentSearch = null;
    this.settings = {
        encoding: "iso-8859-1",
        fe: "ajax",
        client: "rbs_co_uk",
        host: "search.rbs.com",
        collections: [{
            label: "All",
            id: "rbs_com"
        }]
    };
    this.ui = {
        isInternetExplorer: document.all ? true: false,
        queryTextBox: null,
        contentPanel: null,
        heading1: null,
        heading2: null,
        createSearchNavigation: false,
        previousButtonText: "<< Previous",
        nextButtonText: "Next >>",
        loadingImageSource: "/images/search/rotation.gif",
        noQueryMessage: "Please enter your search terms above.",
        noResultsMessage: 'No results found matching the query "$a".',
        spellingSuggestionMessage: "Did you mean : ",
        synonymSuggestionMessage: "You could also try : ",
        heading1Text: "Searching in $a",
        heading2Text: "Results matching $a searching in $b"
    };
    this.query = {
        setText: function(a) {
            this.ui.queryTextBox.value = a
        },
        getText: function() {
            var a = new String(this.ui.queryTextBox.value);
            return a.replace(/[{}"';]*/g, "")
        },
        currentCollection: null,
        getSearchCollectionName: function() {
            var a = "";
            for (var i = 0; i < this.settings.collections.length; i++) {
                if (this.settings.collections[i].id == this.currentCollection) {
                    a = this.settings.collections[i].label
                }
            }
            return a
        }
    };
    this.query.ui = this.ui;
    this.query.settings = this.settings;
    this.addScript = function(a) {
        var b = document.createElement("script");
        b.type = "text/javascript";
        b.src = a;
        document.getElementsByTagName("head")[0].appendChild(b)
    };
    this.submit = function() {
        return this.submit(null)
    };
    this.submit = function(a) {
        if (a == null) {
            a = 0
        }
        var b = "http://" + this.settings.host + "/search?access=p&output=xml_no_dtd&getfields=description&client=";
        b += this.settings.client + "&proxystylesheet=" + this.settings.fe + "&ie=" + this.settings.encoding + "&oe=" + this.settings.encoding;
        b += "&start=" + a + "&q=" + this.query.getText() + "&site=" + this.query.currentCollection;
        if (this.debug) {
            this.debugOutput("Debug on");
            this.debugOutput("Contacting server via " + b)
        }
        this.clearAll();
        this.setHeadings();
        this.setLoadingStatus();
        this.currentResultNum = a;
        this.addScript(b);
        return false
    };
    this.getNextResults = function() {
        var a = this.currentResultNum + 10;
        this.submit(a)
    };
    this.getPreviousResults = function() {
        var a = (this.currentResultNum >= 11) ? this.currentResultNum - 10 : 0;
        this.submit(a)
    };
    this.setHeadings = function() {
        if (this.debug) {
            this.debugOutput("Setting headings")
        }
        var a = this.ui.heading1;
        var b = this.ui.heading2;
        if (a != null) {
            var c = this.ui.heading1Text.replace("$a", this.query.getSearchCollectionName());
            this.removeChildNodes(a);
            a.appendChild(document.createTextNode(c))
        }
        if (b != null) {
            this.removeChildNodes(b);
            if (this.query.getText() != "") {
                var d = this.ui.heading2Text.replace("$a", this.query.getText());
                var d = d.replace("$b", this.query.getSearchCollectionName());
                b.appendChild(document.createTextNode(d));
                b.style.display = "block"
            }
        }
    };
    this.setLoadingStatus = function() {
        if (this.debug) {
            this.debugOutput("Setting loading icon")
        }
        this.loading = this.ui.contentPanel.appendChild(document.createElement("div"));
        this.loading.style.height = "540px";
        this.loading.style.listStyleImage = "none";
        this.loading.style.listStyleType = "none";
        this.loading.style.textAlign = "center";
        this.loading.style.paddingTop = "200px";
        var a = this.loading.appendChild(document.createElement("img"));
        a.setAttribute("src", this.ui.loadingImageSource)
    };
    this.removeChildNodes = function(a) {
        if (a.childNodes.length > 0) {
            var b = a.childNodes.item(0);
            var c;
            while (b) {
                c = b.nextSibling;
                a.removeChild(b);
                b = c
            }
        }
    };
    this.setStatusText = function(a) {
        if (this.debug) {
            this.debugOutput("Setting status text")
        }
        this.ui.statusText = this.ui.contentPanel.appendChild(document.createElement("div"));
        this.ui.statusText.className = "statusText";
        var b;
        if (a.queryDetails.estimatedTotal > 0) {
            b = "Results " + a.queryDetails.startRecordNum + " to " + a.queryDetails.endRecordNum;
            if (a.queryDetails.estimatedTotal > 10) {
                b += " of about " + a.queryDetails.estimatedTotal
            }
            b += ' matching the query "' + this.query.getText() + '".'
        } else {
            if (this.query.getText() == "") {
                b = this.ui.noQueryMessage
            } else {
                b = this.ui.noResultsMessage.replace("$a", this.query.getText())
            }
        }
        this.ui.statusText.appendChild(document.createElement("p")).appendChild(document.createTextNode(b));
        this.loadSuggestions(a.spelling, this.ui.spellingSuggestionMessage);
        this.loadSuggestions(a.synonyms, this.ui.synonymSuggestionMessage)
    };
    this.loadSuggestions = function(a, b) {
        if (this.debug) {
            this.debugOutput("Loading " + a.length + " suggestions")
        }
        if (a.length > 0) {
            var c;
            var d = this.ui.statusText.appendChild(document.createElement("p"));
            d.appendChild(document.createTextNode(b));
            d.id = "spelling";
            for (var i = 0; i < a.length; i++) {
                c = d.appendChild(document.createElement("a"));
                c.href = "#";
                c.onclick = trySuggestion;
                c.appendChild(document.createTextNode(a[i].q))
            }
        }
    };
    this.createNavigation = function(a) {
        if (this.debug) {
            this.debugOutput("Creating navigation bar")
        }
        var b;
        var c;
        var d = ((a.queryDetails.startRecordNum - 1) / 10) + 1;
        var s = (Math.floor((d - 1) / 10) * 10) + 1;
        var e = s + 9;
        var f = a.queryDetails.estimatedTotal / 10;
        var g = this.ui.contentPanel.appendChild(document.createElement("div"));
        g.className = "searchNavigation";
        if (f > 1) {
            if (a.queryDetails.hasPrevious) {
                b = g.appendChild(document.createElement("button"));
                b.className = "previous";
                b.title = "Previous Page";
                b.onclick = getPreviousResults;
                b.onkeypress = getPreviousResults;
                b.appendChild(document.createTextNode(this.ui.previousButtonText))
            }
            if (this.ui.createSearchNavigation) {
                for (var i = s; i <= e && i <= f; i++) {
                    anchor = g.appendChild(document.createElement("a"));
                    anchor.href = "#" + i;
                    anchor.title = "go to results page " + i;
                    anchor.onclick = changePage;
                    anchor.onkeypress = changePage;
                    if (i == d) {
                        anchor.style.fontWeight = "bold";
                        anchor.style.textDecoration = "underline"
                    }
                    anchor.appendChild(document.createTextNode(i))
                }
            }
            if (a.queryDetails.hasNext) {
                c = g.appendChild(document.createElement("button"));
                c.className = "next";
                c.title = "Next Page";
                c.onclick = getNextResults;
                c.onkeypress = getNextResults;
                c.appendChild(document.createTextNode(this.ui.nextButtonText))
            }
        }
    };
    this.trackEvent = function(a) {
        try {
            var b = _hbEvent("search");
            b.keywords = a.queryDetails.queryText;
            b.results = a.queryDetails.estimatedTotal;
            b.attr1 = a.queryDetails.site
        } 
		catch(e) {}
		try {
			/* new search performed - update sitecatalyst */			
	        trackSearchDetails(a);
    		scSetUpPageVars();
		}
		catch(e) {}
    };
    this.loadKeyMatches = function(a) {
        if (this.debug) {
            this.debugOutput("Loading " + a.keyMatches.length + " key matches")
        }
        if (a.keyMatches.length > 0) {
            var b;
            var c;
            var d;
            var e = this.ui.contentPanel.appendChild(document.createElement("div"));
            var f = e.appendChild(document.createElement("div"));
            f.className = "keyMatchTop";
            f.appendChild(document.createElement("hr"));
            var g = e.appendChild(document.createElement("div"));
            g.className = "keyMatchContent";
            b = g.appendChild(document.createElement("ul"));
            b.className = "searchResultsList";
            b.setAttribute("id", "keyMatchResults");
            var h = e.appendChild(document.createElement("div"));
            h.className = "keyMatchBase";
            h.appendChild(document.createElement("hr"));
            e.appendChild(document.createElement("br")).className = "cb";
            for (var i = 0; i < a.keyMatches.length; i++) {
                c = b.appendChild(document.createElement("li"));
                d = c.appendChild(document.createElement("a"));
                d.setAttribute("href", a.keyMatches[i].url);
                d.appendChild(document.createTextNode(a.keyMatches[i].title));
                c.appendChild(document.createTextNode(" - (Key Match)"))
            }
        }
    };
    this.clearAll = function() {
        if (this.debug) {
            this.debugOutput("Clearing all content")
        }
        this.removeChildNodes(this.ui.contentPanel)
    };
    this.loadResults = function(a) {
        var b;
        var c;
        var d;
        var e;
        var f;
        if (this.debug) {
            this.debugOutput("Loading results")
        }
        document.title = "Search";
        this.clearAll();
        this.setStatusText(a);
        if (a.queryDetails.estimatedTotal > 0) {
            this.loadKeyMatches(a);
            if (this.debug) {
                this.debugOutput(a.results.length + " results found")
            }
            this.resultsList = this.ui.contentPanel.appendChild(document.createElement("ul"));
            this.resultsList.className = "searchResultsList";
            for (var i = 0; i < a.results.length; i++) {
                b = a.results[i];
                c = this.resultsList.appendChild(document.createElement("li"));
                d = c.appendChild(document.createElement("h4")).appendChild(document.createElement("a"));
                d.href = b.url;
                d.title = b.title;
				d.setAttribute("onclick","checkForSearchClick();");
                if (d.innerHTML) {
                    d.innerHTML = b.title
                } else {
                    d.appendChild(document.createTextNode(b.title.replace(/(<\/?b>)/g, "").replace(/&amp;/g, "&")))
                }
                e = c.appendChild(document.createElement("p"));
                f = (b.description == "") ? b.summary: b.description;
                if (d.innerHTML) {
                    e.innerHTML = f
                } else {
                    e.appendChild(document.createTextNode(f.replace(/(<\/?b>)/g, "").replace(/&amp;/g, "&")))
                }
            }
            this.createNavigation(a)
        } else {
            if (this.debug) {
                this.debugOutput("No results returned")
            }
        }
        if (typeof(this.onLoaded) == "function") {
            this.onLoaded.call()
        }
        this.currentSearch = a;
        this.trackEvent(a)
    };
    this.onLoaded = null
};
function changePage() {
    var a = (ie) ? window.event.srcElement: window.event.target;
    var b = (a.innerText) ? a.innerText: a.childNodes[0].nodeValue;
    var c = ((parseInt(b) - 1) * 10);
    search.submit(c)
}
function trySuggestion(e) {
    if (!e) {
        e = window.event
    }
    var a = (ie) ? e.srcElement: e.target;
    var b = (a.innerText) ? a.innerText: a.childNodes[0].nodeValue;
    search.query.setText(b);
    search.submit()
}
function getNextResults() {
    search.getNextResults()
}
function getPreviousResults() {
    search.getPreviousResults()
}
function loadSearch(a) {
    search.loadResults(a)
}
