function removeBreadcrumb(newPathAfterRemove, opt) {
    g_AJAXHelper.makeAJAXCall(debug, null, 'p=' + newPathAfterRemove, opt);
}
function removeAllBreadcrumb(path, opt) {
    g_AJAXHelper.makeAJAXCall(debug, null, 'p=' + path, 'reFacet=' + reFacet, opt);
}

var FacetedBCViewer = function(id, imgTxt, readXMLAsStringFlag) {
    this.parent(id, readXMLAsStringFlag);
    // Initialize the temporary Panel to display while waiting for external content to load
    this.imgContent = imgTxt;
    this.initVal = document.getElementById(this.id).innerHTML;
    this.wait = new YAHOO.widget.Panel("loading",
        { fixedcenter:true, close:false, draggable:false, modal:true, visible:false, width:"238px", height:"184px"});
    this.wait.setBody(this.imgContent);
    this.isWaitRendered = false;
    document.getElementsByTagName("body")[0].setAttribute("class", "yui-skin-sam ");
    document.getElementsByTagName("body")[0].className = "yui-skin-sam ";

};

FacetedBCViewer.prototype = new AjaxHelperListener();
FacetedBCViewer.prototype.constructor = FacetedBCViewer;
FacetedBCViewer.prototype.parent = AjaxHelperListener.prototype.constructor;
FacetedBCViewer.prototype.initValue = function(){
    return this.initVal;
}
FacetedBCViewer.prototype.notify = function(responseEle, callbackParam) {
    var myJSONObj = eval( "(" + responseEle + ")" );
    this.buildBreadCrumbs(myJSONObj, this.id);
};

FacetedBCViewer.prototype.toggleLoad = function(onLoading) {
    if (onLoading) {
        if (!this.isWaitRendered) {
            this.wait.render(document.body);
            this.isWaitRendered = true;
        }
        this.wait.show();
    } else {
        this.wait.hide();
    }
};

/**
 * Override reveal, because it is called by globalajax on every notify(), and default reveal behavior will turn div to visible
 * which, will display an empty div. This is not an problem when div has no special CSS, but when there is CSS property
 * we might see an empty bar on page
 */
FacetedBCViewer.prototype.reveal = function() {
    var ele = document.getElementById(this.id);
    if (ele.innerHTML.length > 0) {
        ele.style.display = 'block';
    } else {
        ele.style.display = 'none';
    }
};

/**
 * Override to use display:none instead of visiblity:hidden
 */
FacetedBCViewer.prototype.stealth = function() {
    if (document.getElementById(this.id)) {
        document.getElementById(this.id).style.display = 'none';
    }
};

/**
* Function that creates the HTML for the bread crumb component.  This works
* by doing the following:
* 1. Clears out the innerHTML of the components div (cellId name).
* 2. Checks to see if the static HTML elements that exist anytime at least one breadcrumb is present are in the DOM
*    and if not creates these HTML elements.
* 3. Loops through the individual bread crumbs (selected facets) and creates both the Facet and Facet Filter(s) HTML
*     elements.  The Facet is only displayed once as multiple facet filters may be selected for a given Facet.
* 4. Creates the "Remove All Selections" link. Which clears out all selected facets.
*
* The JSON object must follow this format:
* {
*    // Array of bread crumb objects.
*    breadcrumbs: [
*                    { facetName: "Price",
*                        selectedFacetFilter: "$25K - $35K",
*                        linkText: "clear",
*                        titleText: "clear",
*                        linkOnClick: "onclick-data"
*                    }, ...
*                 {}],
*    removeAllLink: {
*        linkText: "Some text",
*        linkOnClick: "onclick-data"
*    }
*  }
*/
FacetedBCViewer.prototype.buildBreadCrumbs = function (responseData) {
    if (!YAHOO.lang.isUndefined(responseData)) {
        var componentDiv = document.getElementById(this.id);
        componentDiv.innerHTML = "";

        // Only re-draw HTMl if we have bread crumbs present in AJAX response.
        // The length will be 1 even if none exist which is why (> 1) test is used.
        if (responseData.breadcrumbs.length > 1) {
            var userSelectedDiv = document.getElementById("userSelected");
            var dlElement = document.getElementById("breadCrumbList");

            if (YAHOO.lang.isNull(userSelectedDiv)) {
                userSelectedDiv = this.createElement("div", "userSelected", {"class":"facetOn"});
                componentDiv.appendChild(userSelectedDiv);

                var pTag = this.createElement("p", false, {"class":"title"}, "You Selected");
                userSelectedDiv.appendChild(pTag);

                dlElement = this.createElement("dl", "breadCrumbList", {"class":"termBreakText"});
                userSelectedDiv.appendChild(dlElement);
            }

            for (var i = 0; i < responseData.breadcrumbs.length - 1; i++) {
                var breadCrumb = responseData.breadcrumbs[i];
                // Only want to display facet <dt> once as there can be multiple facet filters applied to facet.
                var dtElement = document.getElementById(breadCrumb.facetName + "_BC");
                if (YAHOO.lang.isNull(dtElement)) {
                    dtElement = this.createElement("dt", breadCrumb.facetName + "_BC", {}, breadCrumb.facetName);
                    dlElement.appendChild(dtElement);
                }

                var ddElement = this.createElement("dd", breadCrumb.selectedFacetFilter + "_facetFilter",
                    {}, breadCrumb.selectedFacetFilter+ " ");
                dlElement.appendChild(ddElement);

                var clearHref = this.createElement("a", false,
                    {"title":breadCrumb.titleText,"href":"#", "onclick":breadCrumb.linkOnClick}, breadCrumb.linkText);
                ddElement.appendChild(clearHref);
            }

            var removeAllPTag = document.getElementById("removeAllSelections");
            if(YAHOO.lang.isNull(removeAllPTag)){
                removeAllPTag = this.createElement("p", "removeAllSelections", {"class":"title"}, "You Selected");
                var removeAllHref = this.createElement("a", false,
                    {"href":"#", "onclick":responseData.removeAllLink.linkOnClick}, responseData.removeAllLink.linkText);
                userSelectedDiv.appendChild(removeAllHref);

            }
        } 
    }
}
