﻿Type.registerNamespace("DspAjax");

DspAjax.RefineResults = function(categoriesID, closeID, focusID, goID, popupID, productsID, toggleID) {
    DspAjax.RefineResults.initializeBase(this);
    this.CategoriesID = categoriesID;
    this.CloseID = closeID;
    this.FocusID = focusID;
    this.GoID = goID;
    this.PopupID = popupID;
    this.ProductsID = productsID;
    this.ToggleID = toggleID;
}

DspAjax.RefineResults.prototype = {
    initialize: function() {
        DspAjax.RefineResults.callBaseMethod(this, "initialize");

        this._popupTimer = null;
        this._popupVisible = false;

        if (typeof this._closeID != "undefined") {
            this._close = $get(this._closeID);
            $addHandler(this._close, "click", Function.createDelegate(this, this.onCloseClick));
        }

        if (typeof this._focusID != "undefined") {
            this._focus = $get(this._focusID);
        }

        if (typeof this._goID != "undefined") {
            this._go = $get(this._goID);

            $addHandler(this._go, "click", Function.createDelegate(this, this.onGoClick));
            $addHandler(this._go, "mousedown", Function.createDelegate(this, this.onMouseDownClick));
            $addHandler(this._go, "blur", Function.createDelegate(this, this.onGoBlur));
        }

        if (typeof this._popupID != "undefined") {
            this._popup = $get(this._popupID);
            $addHandler(this._popup, "keypress", Function.createDelegate(this, this.onKeyPress));
        }

        if (typeof this._toggleID != "undefined") {
            this._toggle = $get(this._toggleID);
            $addHandler(this._toggle, "click", Function.createDelegate(this, this.toggle));
        }

        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(Function.createDelegate(this, this.onEndRequest));
    },

    dispose: function() {
        this._args = null;
        DspAjax.RefineResults.callBaseMethod(this, "dispose");
    },

    get_CategoriesID: function() {
        return this._categoriesID;
    },

    set_CategoriesID: function(value) {
        this._categoriesID = value;
    },

    get_CloseID: function() {
        return this._closeID;
    },

    set_CloseID: function(value) {
        this._closeID = value;
    },

    get_FocusID: function() {
        return this._focusID;
    },

    set_FocusID: function(value) {
        this._focusID = value;
    },

    get_GoID: function() {
        return this._goID;
    },

    set_GoID: function(value) {
        this._goID = value;
    },

    get_PopupID: function() {
        return this._popupID;
    },

    set_PopupID: function(value) {
        this._popupID = value;
    },

    get_ProductsID: function() {
        return this._productsID;
    },

    set_ProductsID: function(value) {
        this._productsID = value;
    },

    get_ToggleID: function() {
        return this._toggleID;
    },

    set_ToggleID: function(value) {
        this._toggleID = value;
    },

    onCloseClick: function(e) {
        this.hide();
    },

    onEndRequest: function(sender, e) {
        var dataItems = e.get_dataItems();
        if (dataItems) {
            var query = dataItems["ctl00"]; // HACK: ctl00 = Page.Master
            if (query) {
                location.href = "NoResults.aspx" + query;
            }
        }

    },

    onGoClick: function() {
        //Hide Refine Results and do not send the beacon because results grid does not refresh in FF..
        this.hide();
    },

    onMouseDownClick: function() {
        //Send the beacon to overcome the FF issue onGoClick. 
        if (typeof window.$$dbi != "undefined") {
            var categories = document.getElementById(this._categoriesID);
            var categoryID = categories.value || "0";
            var categoryName = categories.options[categories.selectedIndex].text || categories.options[0].text;

            var products = document.getElementById(this._productsID);
            var productID = products.value || "0";
            var productName = products.options[products.selectedIndex].text || products.options[0].text;

            var freetext = "*";
            if (typeof window.$$Q != "undefined") {
                freetext = $$Q.getValue("freetext") || "*";
            }

            $$dbi.swtp({
                "cid": "15",
                "stype": "ss_rr",
                "catid": categoryID,
                "sterm": freetext + ";" + categoryName + ";" + productName,
                "prodid": productID
            });
        }


    },

    onGoBlur: function(e) {
        if (document.activeElement) {
            var el = document.activeElement;
            while (el.parentNode) {
                el = el.parentNode;
                if (el.tagName == "DIV" && el.id == this.get_PopupID()) {
                    return;
                }
            }

            this.hide();
        }
    },

    onKeyPress: function(e) {
        var target = e.target;
        var keyCode = e.charCode;
        if (keyCode == 27) {
            this.onCloseClick();
            e.preventDefault();
        } else if (keyCode == 13 && target.className.indexOf("dsp-ignore-enter") == -1) {
            __doPostBack(this.GoID, "");
            this.onGoClick();
        } else {
            return true;
        }
    },

    onMouseOver: function(e) {
        this.keepVisible();
        e.stopPropagation();
    },

    keepVisible: function() {
        if (this._popupVisible) {
            this.show();
        }
    },

    hide: function() {
        this._popupTimer = null;
        this._popupVisible = false;

        this._popup.style.display = "none";
        this._toggle.className = "dsp-refine-results";
        this._toggle.focus();
    },

    show: function() {
        if (this._popupTimer != null) {
            window.clearTimeout(this._popupTimer);
            this._popupTimer = null;
        }

        this._popupVisible = true;

        this._popup.style.display = "block";
        this._toggle.className = "dsp-refine-results-selected";
        this._focus.focus();
    },

    toggle: function() {
        var showPopup = this._popup.style.display != "block";
        if (showPopup) {
            this.show();
        } else {
            this.hide();
        }
    }
};

DspAjax.RefineResults.registerClass("DspAjax.RefineResults", Sys.Component, Sys.IDisposable);

if (typeof window.Sys != "undefined") {
    Sys.Application.notifyScriptLoaded();
}