if (typeof bsn == "undefined")_b = bsn = {};
if (typeof _b.Autosuggest == "undefined")_b.Autosuggest = {};
_b.AutoSuggest = function(a, b) {
    if (!document.getElementById)return 0;
    this.searchTimeout = null;
    this.fld = _b.DOM.gE(a);
    if (!this.fld)return 0;
    this.sInp = "";
    this.nInpC = 0;
    this.aSug = [];
    this.iHigh = 0;
    this.oP = b ? b : {};
    var c,d = {minchars:1,meth:"get",varname:"input",className:"autosuggest",timeout:2500,delay:500,offsety:-5,shownoresults:true,noresults:"No results!",maxheight:250,cache:true,maxentries:25,noSubmit:false};
    for (c in d)if (typeof this.oP[c] != typeof d[c])this.oP[c] = d[c];
    var f = this;
    this.fld.onkeypress = function(e) {
        return f.onKeyPress(e)
    };
    this.fld.onkeyup =
        function(e) {
            return f.onKeyUp(e)
        };
    this.fld.setAttribute("autocomplete", "off")
};
_b.AutoSuggest.prototype.onKeyPress = function(a) {
    var b = 1;
    switch (window.event ? window.event.keyCode : a.keyCode) {
        case 13:
            var c = _b.DOM.gE("as_ul");

            this.setHighlightedValue();

            if (this.oP.noSubmit && c && this.iHigh) {
                if (a && a.preventDefault) { a.preventDefault(); }
            }
            b = 0;
            break;
        case 27:
            this.clearSuggestions();
            break;
    }
    return b
};
_b.AutoSuggest.prototype.onKeyUp = function(a) {
    a = window.event ? window.event.keyCode : a.keyCode;
    var b = 1;

    if (typeof this.searchTimeout != 'undefined') {
        window.clearTimeout(this.searchTimeout);
    }

    switch (a) {
        case 38:
            this.changeHighlight(a);
            b = 0;
            break;
        case 40:
            this.changeHighlight(a);
            b = 0;
            break;
        /* do not show suggesstion list again on enter */
        case 13:
            b = 0;
            break;
        default:
            this.getSuggestions(this.fld.value);
            break;
    }
    return b
};
_b.AutoSuggest.prototype.getSuggestions = function(a) {
    if (a == this.sInp)return 0;
    this.clearSuggestions();
    this.sInp = a;
    if (a.length < this.oP.minchars) {
        this.aSug = [];
        this.nInpC = a.length;
        return 0
    }
    var b = this.nInpC;
    this.nInpC = a.length ? a.length : 0;
    var c = this.aSug.length;
    if (this.nInpC > b && c && c < this.oP.maxentries && this.oP.cache) {
        b = [];
        for (var d = 0; d < c; d++)if (this.aSug[d].value.substr(0, a.length).toLowerCase() == a.toLowerCase() || this.aSug[d].info == "as_header")b.push(this.aSug[d]);
        this.aSug = b;
        this.createList(this.aSug);
        return false
    } else {
        var f = this,e = this.sInp;
        clearTimeout(this.ajID);
        this.ajID = setTimeout(function() {
            f.doAjaxRequest(e)
        }, this.oP.delay)
    }
    return false
};
_b.AutoSuggest.prototype.doAjaxRequest = function(a) {
    if (a != this.fld.value)return false;
    var b = this,c = typeof this.oP.script == "function" ? this.oP.script(encodeURIComponent(this.sInp)) : this.oP.script + this.oP.varname + "=" + encodeURIComponent(this.sInp);
    if (!c)return false;
    var d = this.oP.meth;
    a = this.sInp;
    typeof this.oP.onAjax == "function" && this.oP.onAjax(this.fld);
    (new _b.Ajax).makeRequest(c, d, function(f) {
        b.setSuggestions(f, a);
        typeof b.oP.onAjaxComplete == "function" && b.oP.onAjaxComplete(b.fld);
    }, function(f) {
        typeof b.oP.onAjaxError == "function" && b.oP.onAjaxError(b.fld);
        //alert("AJAX error: " + f)
    })
};
_b.AutoSuggest.prototype.setSuggestions = function(a, b) {
    if (b != this.fld.value)return false;
    this.aSug = [];
    if (this.oP.json)for (var c = eval("(" + a.responseText + ")"),d = 0; d < c.results.length; d++)this.aSug.push(c.results[d]); else {
        c = a.responseXML.getElementsByTagName("results")[0].childNodes;
        for (d = 0; d < c.length; d++)if (c[d].hasChildNodes()) {
            var f = {};
            f.value = c[d].childNodes[0].nodeValue;
            var e = c[d].attributes;
            if (e && e.length)for (var g = 0; g < e.length; g++) {
                var h = e.item(g).nodeName,i = e.item(g).nodeValue;
                if (typeof h != "undefined")f[h] =
                    i
            }
            this.aSug.push(f)
        }
    }
    this.idAs = "as_" + this.fld.id;
    this.createList(this.aSug)
};
_b.AutoSuggest.prototype.createList = function(a) {
    var b = this;
    typeof this.oP.onChange == "function" && this.oP.onChange(this.aSug);
    this.clearSuggestions();
    if (a.length == 0 && !this.oP.shownoresults)return false;
    var c = _b.DOM.cE("div", {id:this.idAs,className:this.oP.className}),d = _b.DOM.cE("div", {className:"as_corner"}),f = _b.DOM.cE("div", {className:"as_bar"}),e = _b.DOM.cE("div", {className:"as_header"});
    e.appendChild(d);
    e.appendChild(f);
    c.appendChild(e);
    d = _b.DOM.cE("ul", {id:"as_ul"});
    for (f = 0; f < a.length; f++) {
        if (a[f].info == "plugin_header") {
            e = _b.DOM.cE("li", {className:"as_header"},
                a[f].value);
            d.appendChild(e);
        } else if (a[f].info == 'plugin_all') {
            var g = _b.DOM.cE("a", {href:'javascript:;', className:"as_all"}, a[f].value);
            g.name = f + 1;
            g.onclick = function () {
                b.setHighlightedValue();
                return false
            };
            g.onmouseover = function() {
                b.setHighlight(this.name)
            };
            e = _b.DOM.cE("li", {className:"as_all"}, g);
            d.appendChild(e);
        } else {
            e = a[f].value;
            var g = e.toLowerCase().indexOf(this.sInp.toLowerCase());
            e = e.substring(0, g) + "<em>" + e.substring(g, g + this.sInp.length) + "</em>" + e.substring(g + this.sInp.length);
            e = _b.DOM.cE("span", {}, e, true);
            if (a[f].info != "") {
                g = _b.DOM.cE("br", {});
                e.appendChild(g);
                g = _b.DOM.cE("small", {}, a[f].info);
                e.appendChild(g)
            }
            g = _b.DOM.cE("a", {href:"javascript:;"});
            var h = _b.DOM.cE("span", {className:"tl"}, " "),i = _b.DOM.cE("span", {className:"tr"}, " ");
            g.appendChild(h);
            g.appendChild(i);
            g.appendChild(e);
            g.name = f + 1;
            g.onclick = function() {
                b.setHighlightedValue();
                return false
            };
            g.onmouseover = function() {
                b.setHighlight(this.name)
            };
            e = _b.DOM.cE("li", {}, g);
            d.appendChild(e)
        }
    }
    if (a.length == 0 && this.oP.shownoresults) {
        e = _b.DOM.cE("li", {className:"as_warning"}, this.oP.noresults);
        d.appendChild(e)
    }
    c.appendChild(d);
    d = _b.DOM.cE("div", {className:"as_corner"});
    f = _b.DOM.cE("div", {className:"as_bar"});
    e = _b.DOM.cE("div", {className:"as_footer"});
    e.appendChild(d);
    e.appendChild(f);
    c.appendChild(e);
    d = _b.DOM.getPos(this.fld);
    c.style.left =
        d.x + "px";
    c.style.top = d.y + this.fld.offsetHeight + this.oP.offsety + "px";
    c.style.width = this.fld.offsetWidth + "px";
    this.fld.onfocus = function() {
        b.killTimeout()
    };
    this.fld.onblur = function() {
        b.resetTimeout()
    };
    this.oP.onDisplayResult && this.oP.onDisplayResult(a.length);
    document.getElementsByTagName("body")[0].appendChild(c);
    this.iHigh = 0;
    b = this;
    this.toID = setTimeout(function() {
        b.clearSuggestions()
    }, this.oP.timeout)
};
_b.AutoSuggest.prototype.isHeader = function(a) {
    var b = _b.DOM.gE("as_ul");
    if (!b)return false;
    a = Number(a);
    if (b.childNodes.length >= a)if (b.childNodes[a - 1].className == "as_header")return true;
    return false
};
_b.AutoSuggest.prototype.changeHighlight = function(a) {
    var b = _b.DOM.gE("as_ul");
    if (!b)return false;
    var c;
    if (a == 40) {
        c = this.iHigh + 1;
    } else if (a == 38) {
        c = this.iHigh - 1;
        if (c < 0) {
            c = b.childNodes.length;
            if (this.isHeader(c))c -= 1
        }
    }
    if (this.isHeader(c))if (a == 40)c += 1; else if (a == 38)c -= 1;
    if (c > b.childNodes.length) {
        c = 1;
        if (this.isHeader(c))c += 1
    }
    if (c < 1) {
        c = b.childNodes.length;
        if (this.isHeader(c))c -= 1
    }
    this.setHighlight(c)
};
_b.AutoSuggest.prototype.setHighlight = function(a) {
    var b = _b.DOM.gE("as_ul");
    if (!b)return false;
    this.iHigh > 0 && this.clearHighlight();
    this.iHigh = Number(a);
    if (b.childNodes[this.iHigh - 1].className != "as_header") {
        if (b.childNodes[this.iHigh - 1].className.indexOf('as_all') > -1) {
            b.childNodes[this.iHigh - 1].className = b.childNodes[this.iHigh - 1].className + " as_highlight";
        } else {
            b.childNodes[this.iHigh - 1].className = " as_highlight";
        }
    }
    this.killTimeout()
};
_b.AutoSuggest.prototype.clearHighlight = function() {
    var a = _b.DOM.gE("as_ul");
    if (!a)return false;
    if (this.iHigh > 0) {
        if (a.childNodes[this.iHigh - 1].className != "as_header") {
            if (a.childNodes[this.iHigh - 1].className.indexOf('as_all') > -1) {
                a.childNodes[this.iHigh - 1].className = a.childNodes[this.iHigh - 1].className.replace(" as_highlight", "");
            } else {
                a.childNodes[this.iHigh - 1].className = "";
            }
        }
        this.iHigh = 0
    }
};
_b.AutoSuggest.prototype.setHighlightedValue = function() {
    if (this.iHigh) {
        this.fld.focus();
        this.fld.selectionStart && this.fld.setSelectionRange(this.sInp.length, this.sInp.length);
        this.clearSuggestions();
        typeof this.oP.onSetHighlight == "function" && this.oP.onSetHighlight(this.aSug[this.iHigh - 1], this.fld)
        this.sInp = this.fld.value = this.aSug[this.iHigh - 1].value;
    }
};
_b.AutoSuggest.prototype.killTimeout = function() {
    clearTimeout(this.toID)
};
_b.AutoSuggest.prototype.resetTimeout = function() {
    clearTimeout(this.toID);
    var a = this;
    this.toID = setTimeout(function() {
        a.clearSuggestions()
    }, 500)
};
_b.AutoSuggest.prototype.clearSuggestions = function(a) {
    this.killTimeout();
    this.oP.onHideResult && this.oP.onHideResult();
    var b = _b.DOM.gE(this.idAs),c = this;
    if (b)a ? new _b.Fader(b, 1, 0, 250, function() {
        _b.DOM.remE(c.idAs)
    }) : _b.DOM.remE(this.idAs)
};
if (typeof _b.Ajax == "undefined")_b.Ajax = {};
_b.Ajax = function() {
    this.req = {};
    this.isIE = false
};
_b.Ajax.prototype.makeRequest = function(a, b, c, d) {
    if (b != "POST")b = "GET";
    this.onComplete = c;
    this.onError = d;
    var f = this;
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest;
        this.req.onreadystatechange = function() {
            f.processReqChange()
        };
        this.req.open("GET", a, true);
        this.req.send(null)
    } else if (window.ActiveXObject)if (this.req = new ActiveXObject("Microsoft.XMLHTTP")) {
        this.req.onreadystatechange = function() {
            f.processReqChange()
        };
        this.req.open(b, a, true);
        this.req.send()
    }
};
_b.Ajax.prototype.processReqChange = function() {
    if (this.req.readyState == 4)this.req.status == 200 ? this.onComplete(this.req) : this.onError(this.req.status)
};
if (typeof _b.DOM == "undefined")_b.DOM = {};
_b.DOM.cE = function(a, b, c, d) {
    a = document.createElement(a);
    if (!a)return 0;
    for (var f in b)a[f] = b[f];
    b = typeof c;
    if (b == "string" && !d)a.appendChild(document.createTextNode(c)); else if (b == "string" && d)a.innerHTML = c; else b == "object" && a.appendChild(c);
    return a
};
_b.DOM.gE = function(a) {
    var b = typeof a;
    if (b == "undefined")return 0; else if (b == "string")return(a = document.getElementById(a)) ? typeof a.appendChild != "undefined" ? a : 0 : 0; else return typeof a.appendChild != "undefined" ? a : 0
};
_b.DOM.remE = function(a) {
    return(a = this.gE(a)) ? a.parentNode.removeChild(a) ? true : 0 : 0
};
_b.DOM.getPos = function(a) {
    var b = a = this.gE(a),c = 0;
    if (b.offsetParent)for (; b.offsetParent;) {
        c += b.offsetLeft;
        b = b.offsetParent
    } else if (b.x)c += b.x;
    b = a;
    a = 0;
    if (b.offsetParent)for (; b.offsetParent;) {
        a += b.offsetTop;
        b = b.offsetParent
    } else if (b.y)a += b.y;
    return{x:c,y:a}
};
if (typeof _b.Fader == "undefined")_b.Fader = {};
_b.Fader = function(a, b, c, d, f) {
    if (!a)return 0;
    this.e = a;
    this.from = b;
    this.to = c;
    this.cb = f;
    this.nDur = d;
    this.nInt = 50;
    this.nTime = 0;
    var e = this;
    this.nID = setInterval(function() {
        e._fade()
    }, this.nInt)
};
_b.Fader.prototype._fade = function() {
    this.nTime += this.nInt;
    var a = Math.round(this._tween(this.nTime, this.from, this.to, this.nDur) * 100),b = a / 100;
    if (this.e.filters)try {
        this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = a
    } catch(c) {
        this.e.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + a + ")"
    } else this.e.style.opacity = b;
    if (this.nTime == this.nDur) {
        clearInterval(this.nID);
        this.cb != undefined && this.cb()
    }
};
_b.Fader.prototype._tween = function(a, b, c, d) {
    return b + (c - b) * (a / d)
};
