if(!a10) {var a10 = {};} //if a10 not defined yet

a10.nb = {}; //nav bubble
// ----------------------------------------------------------------------- Global Nav Bubble

a10.nb.navBubble = null; //html element for the nav bubble
a10.nb.clicked = false;
a10.nb.srcElement = null;
a10.nb.offsetTop = 20;
a10.nb.offsetLeft = -15;
a10.nb.position = "fixed";
a10.nb.bodyWidth = "";
/**
 * Repositions bubble after window resize
 */
a10.nb.repositionBubble = function() {
    if(a10.nb.srcElement && a10.nb.navBubble && a10.nb.navBubble.style.display != 'none') {
        var position = a10.util.findPos(a10.nb.srcElement);
        a10.nb.navBubble.style.top = (position[1]+a10.nb.offsetTop) + "px";
        a10.nb.navBubble.style.left = (position[0]+a10.nb.offsetLeft) + "px";
    }
}

/**
 * Nav bubble link mousedown handler. Updates the href of the anchor with the query an a query exists in the form.
 * If prefix is specified, update the href with the prefix to the query. If q param is already within the
 * href, replace the value with the new query value from the form. If q param doesn't exists in the href,
 * append the param
 *
 * @param li anchor
 * @param prefix if specified, updates the href with the specified url prefix
 * @addon
 */ 
a10.nb.md = function(li,prefix) {

    // In Ask City, we want to get the query from field 'bizWhatFld'.
    // In Ask Maps & Directions, get it from input field 'a'.
    // In all other cases, just get it from input field 'q'.
    var start = -1;
    var q = ($F('bizWhatFld') == "") ? $F('a') : $F('bizWhatFld');

    var siteid= null;
    var siteidindex = li.href.indexOf("siteid=");


    if(siteidindex != -1) {
        siteid = li.href.substr(siteidindex + 7, li.href.length); 
    }

    var dm = null;
    var dmindex = li.href.indexOf("dm=");

    if(dmindex != -1) {
        var dmend = li.href.indexOf("&", dmindex+1);
        if(dmend != -1) {
            dm = li.href.substr(dmindex+3, dmend-(dmindex+3));
        } else {
            dm = li.href.substr(dmindex+3);
        }
    }

    if( q=="" ) q = $F('q');

    if(li.md) {return true;}

    if(prefix) {
        li.href = prefix.replace("{0}", enc(q));
    } else if(li.href.indexOf("&q") > 0) {
        start = li.href.indexOf("&q");
        li.href = li.href.substr(0, start + 3) + enc(q);
    } else {
        li.href = li.href + "&q=" + enc(q);
    }

    if(dm) {
            li.href = li.href + "&dm=" + dm;
    }

    if( siteid != null ){

         li.href =  li.href + "&siteid=" + siteid;
    }
    // Append params : o, l, and siteID if present and not part of URL yet.
    if(li.href.indexOf("&o=") == -1 && $F('o') != ""){
        li.href = li.href + "&o=" + enc($F('o'));
    }

    if(li.href.indexOf("&l=") == -1 && $F('l') != ""){
        li.href = li.href + "&l=" + enc($F('l'));
    }

    // If locale is UK, then also append dm and siteID
    if(li.href.indexOf("&dm=") == -1 && $F('dm') != "") {
        li.href = li.href + "&dm=" + enc($F('dm'));
    }

    if(li.href.indexOf("&siteid=") == -1 && $F('siteid') != ""){
        li.href = li.href + "&siteid=" + enc($F('siteid'));
    }

    li.md = 1;
};

/**
 * Handle click event by check if the query is empty. If so, changes the url to the "empty" destination, else
 * fall back to default click implementation specified by a10.bn.md()
 *
 * @param anchor
 * @param prefix url prefix
 * @param empty url destination when query is empty
 */
a10.nb.mde = function(li,prefix,empty) {
    var q = $F('q');

    if(q) {
        q = q.replace(/^\s+/g, '').replace(/\s+$/g, '');
        if (q == '') {
            li.href = empty;
        } else {
            a10.nb.md(li, prefix);
        }
    }
};

/**
 * Display/hide navigation bubble
 *
 * @param event click event
 */
a10.nb.toggle = function(event) {
    if(!event) {event = window.event};

    var textHeight; // Height of the body text.

    // Layout Constants
    var MAX_TEXT_HEIGHT = 181; // Maximum height of the body div
    var MIN_TEXT_HEIGHT = 40; // Minimum height of the body div
    var LEFT_MARGIN = 20; // Left margin of body and header divs
    var TOP_MARGIN = 23; // Top margin of the body div.
    var BODY_WIDTH = a10.nb.bodyWidth!='' ? a10.nb.bodyWidth : 190; // Width of the body and header divs corresponds
    //to width of the navBubbleBody div.
    // If bubble is shown, simply hides the bubble and return.
    if (a10.nb.navBubble) {
        if(a10.nb.navBubble.style.display == '') {
            a10.nb.navBubble.style.display = 'none';
        } else {
            var position = a10.util.findPos(a10.nb.srcElement);
            a10.nb.navBubble.style.top = (position[1]+a10.nb.offsetTop) + "px";
            a10.nb.navBubble.style.left = (position[0]+a10.nb.offsetLeft) + "px";
            a10.nb.navBubble.style.display = '';
            a10.nb.clicked = true;
        }
        return;
    }

    // Otherwise, show the bubble ....

    // Close any open zoom bubble first.
    //closeZoomBubble();

    // Find the position on screen for the More link that was clicked.
    var srcElement = event.target || event.srcElement;
    //source can be an anchor or the image within the anchor
    if(srcElement.tagName.toLowerCase() != "a") {
        srcElement = srcElement.parentNode;
    }

    a10.nb.srcElement = srcElement;

    var position = a10.util.findPos(srcElement);

    // --------------------- Create the bubble DIV.
    a10.nb.navBubble = document.createElement("div");
    a10.nb.navBubble.style.width = "20px";
    a10.nb.navBubble.style.height = "20px";
    a10.nb.navBubble.style.zIndex = 14;//needs to be above video
    var bubblePositionTop = (position[1]+a10.nb.offsetTop);
    if (a10.browser.isIE6()) { // Fixed position hack for IE6
        a10.nb.navBubble.style.position = "absolute";

        // Given that navBubble uses aboslute positioning, document.body.scrollTop has to be subtracted from bubblePositionTop
        // because we are interested in the position of navBubble relative to the top of the client area, not the top of the
        // document.  document.body.scrollTop is then added to this value in the dynamic expression, which would keep the
        // object fixed on screen when the document is scrolled.
        a10.nb.navBubble.style.setExpression("top", "eval(document.body.scrollTop + " +
                                              (bubblePositionTop-document.body.scrollTop) + ")");


    } else {
        a10.nb.navBubble.style.position = a10.nb.position;
        a10.nb.navBubble.style.top = bubblePositionTop + "px";
    }
    a10.nb.navBubble.style.left = (position[0]+a10.nb.offsetLeft) + "px";
    document.body.appendChild(a10.nb.navBubble);

    var body = document.getElementById("navBubbleBody");
    body.style.display = '';
    // Append body to navBubble here to order to calculate the height of the text.
    a10.nb.navBubble.appendChild(body);

    // body.offsetHeight equals the full height of the text, since body's style.height hasn't been set yet.
    textHeight = body.offsetHeight;

    // Set body's height and overflow properties according to min and max text height.
    if (textHeight >= MIN_TEXT_HEIGHT && textHeight <= MAX_TEXT_HEIGHT) {
        body.style.height = textHeight + "px";
    } else {
        body.style.height = ((textHeight > MAX_TEXT_HEIGHT)? MAX_TEXT_HEIGHT : MIN_TEXT_HEIGHT) + "px";
    }

    new a10.Bubble(a10.nb.navBubble, body, BODY_WIDTH+15, 0, "up", true, a10.nb.toggle);

    // Set focus on the body using the dummy anchor
    $('navFocusAnchor').focus();

    // Capture click events on all elements of the bubble.
    /*body.onclick = bubbleBottom.onclick =
        bubbleMiddle.onclick = bubbleTop.onclick = bubbleClicked;*/

    function bubbleClicked() {
        a10.nb.clicked = true;
    }

    // Prevent mouseClicked() from closing the bubble we just opened.
    a10.nb.clicked = true;
}

/**
 * Close any open nav bubble.
 */
a10.nb.close = function() {
    // Hide any bubble currently shown.
    if (!a10.nb.clicked && a10.nb.navBubble) {
        // Hide the bubble.
        a10.nb.navBubble.style.display = 'none';
    }
    a10.nb.clicked = false;
}

//call back function to hook up additional functionality once the search suggestion module has been loaded
if(typeof nbloaded != 'undefined') {
    nbloaded();
}



// ---------------- End nav bubble
