function Searcher(a) {
    this.inflect = a.inflect || false;
    this.locale = a.locale || "";
    this.url = a.url || "";
    this.imageClickUrl = a.imageClickUrl || "";
    this.prev = $(a.prev);
    this.next = $(a.next);
    this.state = null;
    this.imageContainer = $(a.imageContainer);
    this.requestOptions = a.requestOptions || {
        method: "get",
        data: "",
        onSuccess: this._success
    };
    this.supportOrDownload = ($("support") ? "/support_and_downloads/support/" : "/support_and_downloads/downloads/")
}
Searcher.prototype.populate = function (b, a) {
    this.select_req = $(b);
    this.select_tar = $(a);
    this.controller_req = "categories/index_list2";
    this.controller_tar = "products/index_list2";
    this.controller_thumb = "products/get_thumb";
    (this.select_req) && this.select_req.addEvent("change", this._change_req.bind(this));
    (this.select_tar) && this.select_tar.addEvent("change", this._change_tar.bind(this));
    (this.prev) && this.prev.addEvent("click", this._previous.bind(this));
    (this.next) && this.next.addEvent("click", this._next.bind(this));
    return this
};
Searcher.prototype._previous = function (c) {
    c.preventDefault();
    var b = this.select_tar.get("value");
    var a = this.select_tar.getElements("option[value=" + b + "]");
    if (typeof a == "object") {
        if (a.getPrevious("option") && a.getPrevious("option").get) {
            this.select_tar.set("value", a.getPrevious("option").get("value"));
            this.select_tar.fireEvent("change");
            this.next.removeClass("off")
        } else {
            this.prev.addClass("off")
        }
    }
};
Searcher.prototype._next = function (c) {
    c.preventDefault();
    var b = this.select_tar.get("value");
    var a = this.select_tar.getElements("option[value=" + b + "]");
    if (typeof a == "object") {
        if (a.getNext("option") && a.getNext("option").get) {
            this.select_tar.set("value", a.getNext("option").get("value"));
            this.select_tar.fireEvent("change");
            this.prev.removeClass("off")
        } else {
            this.next.addClass("off")
        }
    }
};
Searcher.prototype._getSelect = function (a) {
    if (typeof a === "string") {
        return $(a).getElement("select")
    }
    return a.getElement("select")
};
Searcher.prototype._request = function (a, c, d) {
    this.requestOptions.url = this.url + "/" + this.locale + "/" + a + (c ? "/" + c : "");
    this.requestOptions.onSuccess = this._success.bind(d);
    var b = new Request(this.requestOptions);
    b.send(this.requestOptions.data)
};
Searcher.prototype._success = function (a, b) {
    if (typeof a == "string") {
        if (this.get("tag") == "select") {
            this.set("html", a);
            this.fireEvent("change", this);
        } else {
            if (this.get("tag") == "img") {
				a = JSON.decode(a);
				if (!a) {
					$('preview').set('opacity', 0).set('src', Searcher.url + "/img/not_available.jpg").tween('opacity', 1);

				} else {
					var image = new Asset.image(Searcher.url + "/" + a.uri, {
						onload: function(){
							$('preview').set('opacity', 0).set('src', this.src).tween('opacity', 1);
						}
					});
				}	
                if (this.getParent("a")) {
                    if (Searcher.imageClickUrl) {
                        this.getParent("a").set({
                            href: Searcher.url + "/" + Searcher.locale + Searcher.supportOrDownload + Searcher.select_tar.get("value")
                        });
                        if (this.getParent().getNext("a.goLink")) {
                            this.getParent().getNext("a.goLink").set({
                                href: Searcher.url + "/" + Searcher.locale + Searcher.supportOrDownload + Searcher.select_tar.get("value")
                            })
                        }
                    } else {
                        this.getParent("a").set({
                            href: Searcher.url + "/" + Searcher.locale + "/products/view/" + Searcher.select_tar.get("value")
                        })
                    }
                }
				
            }
        }
    }
};
Searcher.prototype._change_req = function (a) 
{
if ($('previous_product'))	$('previous_product').removeClass('off');
if ($('next_product'))	$('next_product').removeClass('off');
	
    if (this.select_req.get("value") == -1) {
        this.select_tar.empty().set("html", "<option value='-1'>--</option>");
        this.imageContainer.set("src", this.url + "/img/no_select.jpg").getParent("a").set("href", "");
        return this;
    }
    this._request(this.controller_tar, this.select_req.get("value") + "/" + 1, this.select_tar)
};
Searcher.prototype._change_tar = function (a) {
    this._request(this.controller_thumb, this.select_tar.get("value"), this.imageContainer);
};
window.addEvent("domready", function () {});
