if (!ask) var ask = {};
if (!ask.nav) ask.nav = {};

/****************************************************************************************
 * [Class: Menu]
 * The JavaScript for a simple menu that just contains a list of links. The "Options" menu
 * uses this class directly. It also serves as a base class for ask.nav.MyStuffMenu. This class
 * is potentially reusable
 ****************************************************************************************/

/**
 *  Constructor for ask.nav.Menu.
 *
 *  @link    The link element that triggers the menu.
 *  @menu    The div element representing the menu.
 *  @offset  (Optional) Vertical offset of the menu from the top of the link.
 */
ask.nav.Menu = function(link, menu, offset, homepage) {
    this.link = $(link);
    this.menu = $(menu);
    this.offset = offset || 0;
    this.hoverTimeoutID = null;
    this.activityID = 30735;
    this.homepage = homepage;

    var _this = this;
	if (this.link) {
        this.link.onclick = function () { _this.toggle(); return false; };
	}
    a10.event.addListener(document, "click", function(e) { _this._documentClicked(e) });
    //a10.event.addListener(this.link,  "mouseover", function(e) { _this._startHoverTimeout(e) } );
    //a10.event.addListener(this.link,  "mouseout", function(e) { _this._clearHoverTimeout(e) } );

    if(a10.browser.isIE6()) {
	    if (!this.cover && this.menu) {
            this.cover = new a10.IFrameCover(this.menu.id, { left:3, width:-6, height:-4 });
		}
    }
};

/**
 * Instance Methods
 */
ask.nav.Menu.prototype = {

    toggle: function() {
        if (!a10.element.visible(this.menu)) {
            this.show();
        }
        else {
            this.hide();
        }
    },

    show: function() {
        if (this.activityID) sct(this.activityID);

        if (a10.element.visible(this.menu)) return;
        if (ask.closeAllPopUps) ask.closeAllPopUps();

        // Position the menu based on position of link.
        var pos = a10.util.findPos(this.link);

        if (this.homepage) {
            if (!a10.browser.isIE6()){
                this.menu.style.left = - this.link.offsetWidth + 21 +  'px'
            } else {
                this.menu.style.left = - this.link.offsetWidth + 20 +  'px'
            }
        } else {
            if (a10.browser.isFirefox()) {
                this.menu.style.right = a10.browser.innerDimension()[0] - pos[0] - this.link.offsetWidth - 14 +  'px';
            }
            else{
                if (!a10.browser.isIE6()){
                    this.menu.style.right = a10.browser.innerDimension()[0] - pos[0] - this.link.offsetWidth - 18 +  'px';
                } else {
                    this.menu.style.right = a10.browser.innerDimension()[0] - pos[0] - this.link.offsetWidth  - 17 + 'px';
                }
            }
        }

        //this.menu.style.top = pos[1] + this.offset + 'px';
        if(document.getElementById("ask_header")){
            var promoHeight = $('header_promo')?$("header_promo").offsetHeight:0;
            if (!a10.browser.isIE6()){
                this.menu.style.top = this.link.offsetTop + $('ask_header').offsetHeight + (a10.browser.isIE() ? -1 : -4) - promoHeight + 1 + 'px';
            } else {
                this.menu.style.top = this.link.offsetTop + $('ask_header').offsetHeight  - promoHeight + 'px';
            }
        }
        else if (this.homepage) {
            this.menu.style.top = this.link.offsetTop + 23 + 'px';
        }
        else{
            this.menu.style.top = this.link.offsetTop + 20 + 'px';            
        }
        a10.element.show(this.menu);

        if (a10.browser.isIE6()) {
            this.cover.show();
        }
    },

    hide: function() {
        if (!a10.element.visible(this.menu)) return;

        if (a10.browser.isIE6()) {
            this.cover.hide();
        }

        a10.element.hide(this.menu);
    },

    /**
     * Event handler for when the mouse clicked anywhere in the page.
     * Used to close this menu if the click falls outside of the menu.
     */
    _documentClicked: function(event) {
        var x = event.clientX;
        var y = event.clientY;
        if (!a10.element.containsPoint(this.menu, x, y, false) &&
            !a10.element.containsPoint(this.link, x, y, false)) {
            this.hide();
            /*if(this.link && (this.link.id == 'optionsLink') && this.link.className && (GlobalNav.Messages.msLocale != 'fr_FR'))
            {
                this.link.className ="ask_header_item";
            }*/
        }
    },

    /**
     * Event handler for when mouse overs over the link.  After the
     * timeout expires, the menu is automatically displayed.
     */
    _startHoverTimeout: function(event) {
        var _this = this;
        this.hoverTimeoutID = setTimeout(function() {
            _this.hoverTimeoutID = null;
            _this.show();
        }, 250);
    },

    /**
     * Clear the hover timeout if mouse moved out of the link.
     */
    _clearHoverTimeout: function(event) {
        clearTimeout(this.hoverTimeoutID);
        this.hoverTimeoutID = null;
    }
};


/****************************************************************************************
 * [channelMoreMenu]
 *
 * Controller for the more menu.
 ****************************************************************************************/
ask.nav.MoreMenu = function(link, menu, offset) {
    this.link = $(link);
    this.menu = $(menu);
    this.offset = offset || 0;
    this.hoverTimeoutID = null;
    this.activityID = 30735;

    var _this = this;
	if (this.link) {
        this.link.onclick = function () { _this.toggle(); return false; };
	}
    a10.event.addListener(document, "click", function(e) { _this._documentClicked(e) });


    if(a10.browser.isIE6()) {
        if (!this.cover && this.menu) {
            this.cover = new a10.IFrameCover(this.menu.id, { left:0, width:2, height:2 });

    }
        }
};

a10.util.subclass(ask.nav.MoreMenu, ask.nav.Menu);

a10.util.extend(ask.nav.MoreMenu.prototype, {

    show: function() {
        if (this.activityID) sct(this.activityID);
        if (a10.element.visible(this.menu)) return;
        if (ask.closeAllPopUps) ask.closeAllPopUps();

        // Position the menu based on position of link.
        var pos = a10.util.findPos(this.link);


        var extraHeight = $('refHeader')?$("refHeader").offsetHeight:0;

        if (!a10.browser.isIE6()) {
            this.menu.style.left    = ask.nav.MoreMenu.prototype.pageX(this.link) - 4 + 'px';
        } else {
            if (GlobalNav.Variables._locale == 'uk') {
                this.menu.style.left = pos[0] - 88 + 'px'; 
            }
            else {
                this.menu.style.left = pos[0] - 150 + 'px';
            }
        }
        this.menu.style.top = "0px";//pos[1] + this.offset - 60 - extraHeight + 'px';
//        alert(pos[1] + " " + this.offset + " " +extraHeight);

        this.menu.style.display = 'block';
        //this.link.style.fontWeight = 'bold';
        if (a10.browser.isIE6()) {
            this.cover.show();
        }
    },
    hide: function() {
        if (this.link) {
            this.link.style.fontWeight = '';
        }
        ask.nav.MoreMenu.superClass.hide.call(this);

    },

    _documentClicked: function(event) {
        var x = event.clientX;
        var y = event.clientY;
        var dependPageScroll = false;
        if (GlobalNav.Variables._locale == 'uk') {
            dependPageScroll = true;
        }
        if (!a10.element.containsPoint(this.menu, x, y, dependPageScroll) &&
            !a10.element.containsPoint(this.link, x, y, dependPageScroll)) {
            this.hide();
            if(this.link && (this.link.id == 'ask_header_moreLink') && this.link.className && (GlobalNav.Messages.msLocale != 'fr_FR')){
                this.link.className ="ask_header_item";
            }
        }
    },
    pageX: function(e){ var iPageX = e.offsetLeft; while ((e = e.offsetParent)) iPageX += e.offsetLeft; return iPageX; }
    }
);
/* Subscribe menu */

 ask.nav.SubscribeMenu = function(link, menu, offset) {
    ask.nav.SubscribeMenu.baseConstructor.call(this, link, menu, offset);
};

a10.util.subclass(ask.nav.SubscribeMenu, ask.nav.MoreMenu);

a10.util.extend(ask.nav.SubscribeMenu.prototype, {

	    show: function() {
        //Removed activity logging for TRES-15022
        //if (this.activityID) sct(this.activityID);
		if (a10.element.visible(this.menu)) return;
		if (ask.closeAllPopUps) ask.closeAllPopUps();

		// Position the menu based on position of link.
		var pos = a10.util.findPos(this.link);


		var extraHeight = $('refHeader')?$("refHeader").offsetHeight:0;

			 this.menu.style.top = 10 + 'px';
			this.menu.style.left = 165 + 'px';
            if(a10.browser.isSafari() || a10.browser.isOpera()|| a10.browser.isFirefox()){
               this.menu.style.left = 0 + 'px'; 
            }
        this.menu.style.display = 'block';
	    }
    }
);
/****************************************************************************************
 * [Class: MyStuffMenu (extends Menu)]
 * This class manages a sliding menu that shows a hierarchical tree of links. The "MyStuff"
 * menu uses this class. For brevity, this class is heavily tied to the MyStuff
 * functionality, but the sliding menu portion could potentially be extracted and reused in
 * another application.
 *
 * What's a sliding menu?
 *
 * The MyStuff menu contains folders.  When a folder is clicked, its content forms
 * a new menu that replaces the existing menu.  User can then click another folder to
 * drill down to another level, or backup using the "Back" button.  So visually, the
 * menus behave like slides, and hence the name.  Each level in the menu is called
 * a "slide".
 *
 * In HTML, a slide is a div with the class "slide".  Within the slide div is a
 * title div, and an ul (unordered list) containing the menu items.  You can see this
 * structure in ask/navDropdowns.tag, where you can find some hard-coded root-level slides.
 * In JavaScript, the MyStuffMenuSlide class can generate the slide markup dynamically.
 *
 * The way the sliding menu works is as follows.  To display a new slide, build the new slide
 * using MyStuffMenuSlide and pass it to MyStuffMenu.newSlide().  newSlide() then
 * hides the old slide, placed it on a stack, and displays the new slide in its place.
 * When the user presses the back button, MyStuffMenu.back() is called, which retrieves
 * the old slide, displays it, and discards the new slide.  All root-level slides, slides on
 * the stack, and the currently displaying slides are children of the "myStuffMenu"
 * div (defined in ask/navDropdowns.tag), but only one slide is visible at any time.
 ****************************************************************************************/

/**
 * Constructor for ask.nav.MyStuffMenu.
 *
 * @link see Menu
 * @menu see Menu
 * @offset see Menu
 */
ask.nav.MyStuffMenu = function(link, menu, offset) {
    ask.nav.MyStuffMenu.baseConstructor.call(this, link, menu, offset);

    this.wasClicked = false;
    this.slides = new Array(); // Stack of slides

    var _this = this;
    a10.event.addListener(this.menu, 'click', function() { _this.wasClicked = true; });

    // Not using addListener since it's easier to return false and cancel default action
    // this way (required for Opera).  Returning false doesn't seem to work with addListener.
    if ($('signOutLink')) {
        $('signOutLink').onclick = function() { _this.signOut(); return false; };
    }
    if ($('backButton')) {
        $('backButton').onclick = function() { _this.back(); return false; };
    }
};

a10.util.subclass(ask.nav.MyStuffMenu, ask.nav.Menu);

/**
 * Instance Methods
 */
a10.util.extend(ask.nav.MyStuffMenu.prototype, {
    show: function() {
        if (a10.element.visible(this.menu)) return;

        this.updateStatus();
        ask.nav.MyStuffMenu.superClass.show.call(this);
        this.updateSlideHeight(this.currentSlide());
    },

    hide: function() {
        this.reset();
        ask.nav.MyStuffMenu.superClass.hide.call(this);
    },

    /**
     * Overrides _documentClicked() from the superclass.  This method
     * works a little bit differently in that it relies on the wasClicked variable.
     * wasClicked indicates whether the user clicked this menu in the last
     * click event.  If the document was clicked but the menu was not, as
     * indicated by this function being called and wasClick == false, then
     * the menu is closed.  The reason wasClick is needed is because the
     * MyStuff menu may change size when user clicks an item in the menu.
     * Since the _documentClicked() is invoked after the menu size change,
     * sometimes a10.element.containsPoint() may return false even when
     * the user clicked somewhere within the menu.
     */
    _documentClicked: function(event) {
        var x = event.clientX;
        var y = event.clientY;
        if (!this.wasClicked && !a10.element.containsPoint(this.link, x, y, false)) {
            this.hide();

            if(this.link && (this.link.id == 'myStuffLink') && this.link.className && (GlobalNav.Messages.msLocale != 'fr_FR')){
                this.link.className ="ask_header_item";
            }
        }
        this.wasClicked = false;
    },

    /**
     * Update MyStuff menu title, drop down title, and drop down menu based on
     * the user type (nobody, guest or user).
     */
    updateStatus: function() {
        ask.nav.updateStatusMystuff(); /* re-using existing plumbing in a10f/dialog/signin.js, but this can go directly in a11 signin.js */

        a10.element.hide('userMenu');
        a10.element.hide('guestMenu');
        a10.element.hide('nobodyMenu');

        var title;
        if (ask.mystuff.API.isLoggedInUser()) {
            var displayName = ask.mystuff.API.nickname();
            //title = displayName + "'s " + GlobalNav.Messages.msStuff;
            title = a10.util.getMyStuffTitle(GlobalNav.Messages.msTitleDefault,GlobalNav.Messages.msTitleSpecial,displayName,GlobalNav.Messages.msLocale);
            a10.element.show('userMenu');
            this.activityID = 51237;
        } else if (ask.mystuff.API.isGuestUser()){
            title = GlobalNav.Messages.msGuestTitle;
            a10.element.show('guestMenu');
            this.activityID = 43759;
        } else { // Nobody
            title = GlobalNav.Messages.msDefaultTitle;
            a10.element.show('nobodyMenu');
            this.activityID = 43759;
        }

        if ($("mslabel")) {
            $("mslabel").innerHTML = title;
        }
        if ($('myStuffTitle')) {
            $('myStuffTitle').innerHTML = title;
        }
    },

    /**
     * Handler for the "Sign Out" link within the MyStuff Menu.
     */
    signOut: function() {
        ask.mystuff.API.logout();
        this.updateStatus();
        if (ask.closeAllPopUps) ask.closeAllPopUps();
    },

    /**
     * Return the currently displaying slide.
     */
    currentSlide : function() {
        if (this.menu) {
            var slides = this.menu.childNodes;
            for(var i = 0; i < slides.length; i++) {
                if (slides[i].className == 'slide' && a10.element.visible(slides[i])) return slides[i];
            }
        }
    },

    /**
     * Insert a new slide, display it, and place the old slide on the stack.
     */
    newSlide : function(newSlide) {
        var oldSlide = this.currentSlide();
        var backButton = $('backButton');

        a10.element.hide(oldSlide);
        this.menu.insertBefore(newSlide, oldSlide);
        a10.element.show(newSlide);
        this.slides.push(oldSlide);
        if (!a10.element.visible(backButton)) a10.element.show(backButton);

        if (a10.browser.isIE6()) {
            this.updateSlideHeight(newSlide);
            this.cover.reposition();
        }
    },


    /**
     * Retrieve the previous slide from the stack, display it, and discard the
     * current slide.  Returns true if there are still menus on the stack after
     * moving back a level.
     */
    back : function() {
        var currentSlide = this.currentSlide();
        var previousSlide = this.slides.pop();

        if (!previousSlide) return;  // Unexpected case.

        this.menu.insertBefore(previousSlide, currentSlide);
        this.menu.removeChild(currentSlide);
        a10.element.show(previousSlide);

        var isRoot = this.slides.length == 0;
        if (isRoot) a10.element.hide('backButton');

        if (a10.browser.isIE6()) {
            this.updateSlideHeight(previousSlide);
            this.cover.reposition();
        }

        return !isRoot;
    },

    /**
     * Reset menu to the root level.
     */
    reset : function() {
        while (this.back()) {}
    },

    /**
     * IE6 workaround for lack of support for max-height.  This function
     * manually show the vertical scrollbar when the menu exceeds a certain
     * height. For other browser, the max-height CSS property is used.
     */
    updateSlideHeight: function(slide) {
        if (!a10.browser.isIE6()) return;

        var content = slide.getElementsByTagName('ul')[0];
        content.style.height = 'auto';
        var height = a10.element.getHeight(content);

        if (height > 200) {
            content.style.height = '200px';
        }
    }
});



/****************************************************************************************
 * [Class: MyStuffMenuSlide]
 * Think of this as the data model for a "slide" in the MyStuffMenu, that also knows how
 * to generate the HTML markup for itself.
 ****************************************************************************************/

/**
 * Constructor for MyStuffMenuSlide
 *
 * @title the title to display in the slide's title bar.
 */
ask.nav.MyStuffMenuSlide = function(title) {
    this.slideDiv = document.createElement('div');
    this.slideDiv.style.display = 'none';
    this.slideDiv.className = "slide";

    var titleDiv = document.createElement('div');
    titleDiv.className = 'title';
    titleDiv.innerHTML = a10.util.truncate(title, ask.nav.MyStuffMenuSlide.TITLE_LENGTH);
    this.slideDiv.appendChild(titleDiv);

    this.contentDiv = document.createElement('ul');
    this.contentDiv.className = 'content';
    this.slideDiv.appendChild(this.contentDiv);

    this.hasContent = false;
};

/**
 * Class Variables
 */
ask.nav.MyStuffMenuSlide.TITLE_LENGTH = 29;
ask.nav.MyStuffMenuSlide.LABEL_LENGTH = 35;

/**
 * Instance Methods
 */
ask.nav.MyStuffMenuSlide.prototype = {
    /**
     * Add an entry (item) to the slide.
     *
     * @className CSS class for this item.  Can be one of the following: folder, web, image, video, blogs, or
     *            smartAnswer.  The className determines the icon for this item.  In the case of folder,
     *            the structure is also slightly different to support the folder item's right arrow.
     * @url URL that'll be loaded when this item is clicked.
     * @name Name to display for this item.  If the name is too long, it is truncated.
     * @onclick onclick handler for this item.
     */
    addEntry: function(className, url, name, onclick) {
        var li = document.createElement('li');
        li.className = className;

        var a = document.createElement('a');
        a.href = url;
        a.className = 'L19';
        li.appendChild(a);
        if (onclick) {
            a.onclick = onclick;
        }
        a.onmousedown = function () { sct(51137) };

        // Set name
        name = a10.util.truncate(name, ask.nav.MyStuffMenuSlide.LABEL_LENGTH);
        if (className == 'folder') {
            var div = document.createElement('div');
            div.innerHTML = a10.util.htmlEncode(name);
            a.appendChild(div);
        } else {
            a.innerHTML = a10.util.htmlEncode(name);
        }

        this.contentDiv.appendChild(li);
        this.hasContent = true;
    },

    /**
     * Returns the HTML representation of this slide for insertion into the sliding
     * menu.
     */
    toDiv: function() {
        if (!this.hasContent) {
            var li = document.createElement('li');
            li.className = 'noResult';
            li.innerHTML = GlobalNav.Messages.msNoResult;
            this.contentDiv.appendChild(li);
        }

        return this.slideDiv
    }
};


/****************************************************************************************
 * [MyStuffMenuController]
 * Whereas Menu, MyStuffMenu and MyStuffMenuSlide are view components,
 * MyStuffMenuController, as the name suggests, is a controller object that reacts to
 * user's clicks on the items in the MyStuff menu. Like a typical controller, it acts as
 * the glue between the View and the Modal (modal is, in this case, the MyStuff backend
 * service). More precisely, it handles the user's onclick event, sends a request to the
 * MyStuff backend service, turns service's response into a MyStuffMenuSlide object, and
 * add the slide to the MyStuffMenu, which updates the UI.
 ****************************************************************************************/

ask.nav.MyStuffMenuController = {
    /**
     * Onclick handlers for items in the menu.  Each of these function makes a request
     * to the MyStuff backend.
     */
    show: {
        recentSearches: function() {
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('getsavehistory', 'ask.nav.MyStuffMenuController.process.recentSearches'));
            req.makeRequest();
        },
        savedResults: function() {
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('getfolder', 'ask.nav.MyStuffMenuController.process.savedResults'));
            req.makeRequest();
        },
        folders: function() {   // Get top-level folders
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('getfolder', 'ask.nav.MyStuffMenuController.process.folders'));
            req.makeRequest();
        },
        folder: function(guid) {  // Get subfolders
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('getfolder', 'ask.nav.MyStuffMenuController.process.folders', 'folderID=' + guid));
            req.makeRequest();

            return false;
        },
        tags: function() {
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('gettag', 'ask.nav.MyStuffMenuController.process.tags'));
            req.makeRequest();
        },
        tag: function(tagName) {
            var req = new JSONscriptRequest(ask.mystuff.API.buildRequest('gettag', 'ask.nav.MyStuffMenuController.process.tag', 'tagName=' + tagName));
            req.makeRequest();

            return false;
        }
    },

    /**
     * Callback functions for the MyStuff backend request.  Each of these functions builds a slide
     * using the returned data, and displays the slide in the MyStuff menu.
     */
    process: {
        recentSearches: function(data) {
            if (data.status == 'success') {
                var newSlide = ask.nav.MyStuffMenuController.buildSlide(data.element, GlobalNav.Messages.msRecent, 'history');
                myStuffMenu.newSlide(newSlide);
            } else  {
                alert('display failure');
            }
        },
        savedResults: function(data) {
            if (data.status == 'success') {
                var newSlide = ask.nav.MyStuffMenuController.buildSlide(data.element, GlobalNav.Messages.msSavedResults, 'savedResults');
                myStuffMenu.newSlide(newSlide);
            } else  {
                alert('display failure');
            }
        },
        folders: function(data) {
            if (data.status == 'success') {
                var title, type;
                if (!data.element.folderPath) { // Root
                    title = GlobalNav.Messages.msFolders;
                    type = 'topFolders'
                } else { // Subfolders
                    title = data.element.folderName;
                    type = 'folder';
                }

                var newSlide = ask.nav.MyStuffMenuController.buildSlide(data.element, title, type);
                myStuffMenu.newSlide(newSlide);
            } else  {
                alert('display failure');
            }
        },
        tags: function(data) {
            if (data.status == 'success') {
                var newSlide = ask.nav.MyStuffMenuController.buildSlide(data.element, GlobalNav.Messages.msTags, 'tag');
                myStuffMenu.newSlide(newSlide);
            } else  {
                alert('display failure');
            }
        },
        tag: function(data) {
            if (data.status == 'success') {
                var newSlide = ask.nav.MyStuffMenuController.buildSlide(data.element, data.element.tag, 'savedResults');
                myStuffMenu.newSlide(newSlide);
            } else  {
                alert('display failure');
            }
        }
    },

    /**
     * Build a slide using data returned from the MyStuff server.
     *
     * @element the element hash of MyStuff's returned data
     * @type one of these values: 'savedResults', 'topFolders', 'folder', 'tag', 'history', indicating
     *       different types of slides to build.
     */
    buildSlide: function(element, title, type) {
        var slide = new ask.nav.MyStuffMenuSlide(title);

        // Map MyStuff API type to GlobalNav CSS class, which determines the Icon image
        var typeToClass = {
                // The duplicate entries are due to the Search history API using different IDs from the rest.
                  'Pictures' : 'image',
                  'Web' : 'web',
                  'web' : 'web',
                  'image' : 'image',
                  'video' : 'video',
                  'Blog' : 'blogs',
                  'Video' : 'video'
        };

        // Maps MyStuff API types to the corresponding channel's search URL.  Used for constructing
        // URL of Recent Searches.
        var typeToURL = {
            'Pictures' : GlobalNav.Constants.imageURL,
            'image'    : GlobalNav.Constants.imageURL,
            'Web'      : GlobalNav.Constants.webURL,
            'web'      : GlobalNav.Constants.webURL,
            'Blog'      : GlobalNav.Constants.blogURL,
            'Video'      : GlobalNav.Constants.videoURL
        }

        // No result
        if ((!element.folders || element.folders.length == 0) &&
            (!element.results || element.results.length == 0)) {
            return slide.toDiv();
        }

        if (type == 'folder') {
            insertFolders(element.folders, slide);
        }

        if (type == 'topFolders') {
            insertFolders(element.folders, slide);
            return slide.toDiv();
        }

        if (type == 'tag') {
            insertTags(element.results, slide);
            return slide.toDiv();
        }

        for (var i=0; i < element.results.length; i++) {
            var result = element.results[i];

            var url;
            var name;

            if (type == 'history') {
                name = result.query;

                if (typeToURL[result.type]) {
                    url = typeToURL[result.type].replace(/qsrc=\d*/, 'qsrc=177') +
                          encodeURIComponent(result.query);
                } else {
                    a10.debug('WARNING: Unexpected Recent Search type: ' + result.type + '.  Skipping this item.');
                    continue;
                }
            } else {
                url = result.type == 'video'? result.surl : result.url;
                name = result.name || null;
            }

            slide.addEntry(typeToClass[result.type], url, name);
        }

        return slide.toDiv();

        // Helpers

        function insertFolders(array, slide) {
            for (var i=0; i < array.length; i++) {
                var result = array[i];
                var onclick = function(guid) { // create closure for guid
                    return function () { return ask.nav.MyStuffMenuController.show.folder(guid) }
                }(result.guid);
                slide.addEntry('folder', '#', result.name, onclick);
            }
        }

        function insertTags(array, slide) {
            for (var i=0; i < array.length; i++) {
                var result = array[i];
                var onclick = function(name) { // create closure for name
                    return function () { return ask.nav.MyStuffMenuController.show.tag(name) };
                }(result.name);
                slide.addEntry('folder', '#', result.name, onclick);
            }
        }
    }
};



/****************************************************************************************
 * [OptionsMenuController]
 *
 * Controller for the Options menu.
 ****************************************************************************************/

ask.nav.OptionsMenuController = {
    showDialogWithTab: function(tabID) {
        optionsDialog.tabToShow = tabID || 'locations';
        optionsDialog.show();
    }
};


ask.nav.showAllSkins = function(url) {
    this.href = url;
    ct(this, 13035);
    setTimeout(function(){
       window.location = url;
    }, 0);
}

/**
 * Ask11 Global Nav - There are 2 links in the top-right corner that relate to MyStuff.
 * 1) Settings link, text only -- will change to "{users name}'s Settings" if user logged in.
 * 2) Sign In/Sign Out link -- Sign In brings up sign in dialog. After mystuff js runs, page elements will change without page refresh.
 */
ask.nav.signInOut = function() {
    if (ask.mystuff.API.isLoggedInUser()) {
        ask.mystuff.API.logout();
        ask.nav.updateStatusMystuff();
    } else { // Nobody
        signinDialog.show();
    }
};
ask.nav.updateStatusMystuff = function() {
    var settingsTitle,signTitle,signActId;
    if (ask.mystuff.API.isLoggedInUser()) {
        var displayName = ask.mystuff.API.nickname();
        settingsTitle = displayName + "'s Settings";
        signTitle = "Sign Out";
        signActId = 51138;
    } else { // Nobody
        settingsTitle = "Settings";
        signTitle = "Sign In";
        signActId = 51143;
    }

    if ($("settingsLabel")) {
        $("settingsLabel").innerHTML = settingsTitle;
    }
    if ($("signInOutLink")) {
        $("signInOutLink").innerHTML = signTitle;
        $("signInOutLink").onmousedown = function() { sct(signActId); };
    }
};


ask.nav.updateQuery = function(li) {
    var qele = $('q');
    var what = $('bizWhatFld');
    var where = $('bizWhereFld');
    var href = li.href;
    var newHref = '';
    var browseIdx, qIdx;
    if (qele || (what && where)) {
    	if (qele) {   		
    		var q = qele.value;
    	}
    	else {
    		var q = what.value+' '+where.value;
    	}
        if ((qIdx = href.indexOf('q=')) != -1) {
            var tailStart = href.indexOf('&', qIdx);
            var prefix = href.substr(0, qIdx+2);
            newHref = prefix + enc(q);
            if (tailStart != -1) {
                newHref += href.substr(tailStart);
            }
        }
        if (newHref != '') {
            li.href = newHref;
        }
    }
    return true;
}



/**
 * Invoke the callback function navloaded().
 */
if(typeof navloaded != 'undefined') {
    navloaded();
}

if(typeof navloaded1 != 'undefined') {
    navloaded1();
}
    