﻿var search_isAjaxHistoryNav = false;

function f_scrollTop() {
    return f_filterResults(
		        window.pageYOffset ? window.pageYOffset : 0,
		        document.documentElement ? document.documentElement.scrollTop : 0,
		        document.body ? document.body.scrollTop : 0
	        );
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function initializeRequest(sender, args) {
    search_isAjaxHistoryNav = false;
    if (document.getElementById("Header1_MgomdCtl1_lblIframe")) {
        // Clear iframe to prevent FF page reload.
        document.getElementById("Header1_MgomdCtl1_lblIframe").innerHTML = "";
    }
    var searchActionType = document.getElementById("__EVENTARGUMENT").value.substring(0, 3);
    if (searchActionType == 'FIL') {
        var scroll = f_scrollTop();
        if (scroll > 150) {
            document.getElementById("divOverlaygphc").style.top = (scroll - 100) + 'px';
        }
        document.getElementById("divOverlaygphc").style.display = "block";
        document.getElementById("divOverlayload").style.display = "block";
    } else if (searchActionType == "LBX") {
        if (search_isLightboxVisible() == false) {
            document.getElementById("navlghtbx").style.display = "none";
        }
        search_showLBX(true);
    } else if (searchActionType == "SRT") {
        search_showItemsLoading();
    } else { // ajax history navigation.
        try {
            search_showPageLoading();
            search_isAjaxHistoryNav = true;
        } catch (e) {
            window.location.assign(window.location.href.split('#')[0]);
        }
    }
}
function endRequest(sender, args) {
    if (args.get_error() != undefined) {
        args.set_errorHandled(true);
        window.location.assign(window.location.href.split('#')[0]);
    } else {
        document.getElementById("divOverlaygphc").style.display = "none";
        document.getElementById("divOverlayload").style.display = "none";

        // ensure safari doesn't cache the value of the .net hidden input fields
        document.getElementById("__EVENTVALIDATION").setAttribute("autocomplete", "off");
        document.getElementById("__VIEWSTATE").setAttribute("autocomplete", "off");

        if (search_isLightboxVisible()) {
            search_showLBX(false);
        } else {
            if (location.href.toLowerCase().indexOf("productcategory.aspx") >= 0) {
                PC_OnLoad();
            }
            if (search_isAjaxHistoryNav == false) {
                window.scrollTo(0, 0);
            }
            if (typeof (s) != 'undefined') {
                s.linkTrackVars = "eVar1,eVar2,eVar3,eVar4";
                var mercadoQueryStatsString = document.getElementById("lblMercadoQueryStats").innerHTML;
                mercadoQueryStatsString = mercadoQueryStatsString.substring(mercadoQueryStatsString.indexOf(">") + 1, mercadoQueryStatsString.lastIndexOf("<"));
                window.eval(mercadoQueryStatsString);
                mercado_vars();
                void (s.t());
            }
        }
    }
}
function search_showItemsLoading() {
    var divUpdatePanel = document.getElementById("SearchResultsGrid1_UpdatePanel1");
    var divs = divUpdatePanel.getElementsByTagName("div");
    var loopTimes = divs.length;
    for (var i = 0; i < loopTimes; i++) {
        var e = divs[i];
        if (e.id.indexOf("_divItemGphc") >= 0) {
            e.style.display = "";
            document.getElementById(e.id.replace("_divItemGphc", "_lnkImage")).style.visibility = "hidden";
        }
    }
}
function search_getVisibleWindowSize() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    arrayPageSize = [w, h];
    return arrayPageSize;
}
function search_positionOverlay() {
    var scroll = f_scrollTop();
    var posTop = ((search_getVisibleWindowSize()[1] - 341) / 2) + scroll; // 341 = height of LBX.
    if (posTop < 0) {
        posTop = 0;
    }
    document.getElementById("overlaycont").style.top = posTop + "px";
}
function search_hideSelects() {
    if (isIE6OrLower()) {
        var tags = document.all.tags("SELECT");
        var loopTimes = tags.length;
        for (var m = 0; m < loopTimes; ++m) {
            var e = tags[m];
            if (e.id.indexOf("ctrlLightboxFilters1") == -1) {
                e.style.visibility = 'hidden';
            }
        }
    }
}
function search_showLBX(isShowLBXLoading) {
    search_hideSelects();

    document.getElementById("overlay").style.display = "none";
    if (!search_isLightboxVisible() || !isShowLBXLoading) {
        search_positionOverlay();
    }
    
    document.getElementById("ctrlLightboxFilters1_spanLightbox").style.display = "";
    search_underlayIE6Hack();

    if (isShowLBXLoading) {
        document.getElementById("divLBXOverlayload").style.display = "block";
        document.getElementById("divLBXOverlaygphc").style.display = "block";
    } else {
        document.getElementById("navlghtbx").style.display = "";
        document.getElementById("divLBXOverlayload").style.display = "none";
        document.getElementById("divLBXOverlaygphc").style.display = "none";
    }
    document.getElementById("overlay").style.display = "";
}
function search_underlayIE6Hack() {
    var underlay = document.getElementById("underlay");
    if (isIE6OrLower()) {
        if (search_isLightboxVisible()) {
            underlay.style.display = "none";
            document.body.style.height = "100%";
            underlay.style.position = "absolute";
            underlay.style.height = document.body.scrollHeight + "px";
            underlay.style.width = document.body.scrollWidth + "px";
            underlay.style.display = "";
        } else {
            underlay.style.position = "fixed";
        }
    }
}
function search_refreshUnderlay() {
    if (isIE6OrLower() && search_isLightboxVisible()) {
        document.getElementById("underlay").style.width = document.body.scrollWidth + "px";
    }
    return false;
}
function search_isLightboxVisible() {
    if (document.getElementById("ctrlLightboxFilters1_spanLightbox") && document.getElementById("ctrlLightboxFilters1_spanLightbox").style.display == "") {
        return true;
    }
    return false;
}
function search_showPageLoading() {
    search_hideSelects();
    search_positionOverlay();
    
    document.getElementById("overlay").style.display = "none";
    document.getElementById("ctrlLightboxFilters1_spanLightbox").style.display = "";
    search_underlayIE6Hack();
    document.getElementById("divLBXOverlaygphc").style.display = "block";
}
function search_scrollToElement(elementId) {

    if (window.location.hash == "" || window.location.hash.indexOf("#&&") != 0) {
        return true;
    }
    var e = document.getElementById(elementId);
    var posY = 0;
    if (e) {
        while (e != null) {
            posY += e.offsetTop;
            e = e.offsetParent;
        }
        window.scrollTo(0, posY);
    }
    return false;
}