var userlocationsFO,neighborhoodsFO,allcitiesFO,allcatsFO,searchWhat,searchWhere;

function initGlobalFlyouts() {
    if ($chk($("allcities-links")) && self.allCitiesLinks) {
        writeFlyoutChunkLinks($("allcities-links"), allCitiesLinks,
            function(el) { return li(span(el)); }, ul, 3);
    }
    if ($chk($("allcats-links")) && self.allCatsLinks) {
        writeFlyoutChunkLinks($("allcats-links"), allCatsLinks,
            function(el) { return li(span(el)); }, ul, 3);
    }

    if ($chk($("allcats-sections")) && self.allCatsSectionLinks) {
        writeFlyoutChunkLinks($("allcats-sections"), allCatsSectionLinks,
            function(el) { return li(span(el)); }, ul, 4);
    }
    if ($chk($("allcities-flyout"))) {
        allcitiesFO = new FlyOutPane($('allcities-flyout'),$('allcities-flyout-trigger'),{},
            {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
        });
    }

    if ($chk($("allcats-flyout"))) {
        allcatsFO = new FlyOutPane($('allcats-flyout'),$('allcats-flyout-trigger'),{},
            {'pos' : {'x' : ['left','right',-240],'y' : ['top','bottom',0]}
        });
    }
    if ($chk($("userlocation-dd"))) {
        userlocationsFO = new ClickHoverPosPane($("userlocation-dd"),$("userlocation-dd-trigger"),{},
            {'pos' : {'x' : ['left','right',-293],'y' : ['top','bottom',-9]}
        });
    }

    if ($chk($("hoods-fly-out"))) {
        if ($chk($("hoods-fly-out-trigger"))) {
            neighborhoodsFO = new FlyOutPane($("hoods-fly-out"),$("hoods-fly-out-trigger"),{},
                {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
            });
        }
        if ($chk($("hoods-fly-out-trigger2"))) {
            neighborhoodsFO = new FlyOutPane($("hoods-fly-out"),$("hoods-fly-out-trigger2"),{},
                {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
            });
        }
    }
    if ($chk($("search-what")) && $chk($("search-what-label"))) {
        searchWhat = new LabelMagic("search-what","search-what-label");
    }
    if ($chk($("search-where")) && $chk($("search-where-label"))) {
        searchWhere = new LabelMagic("search-where","search-where-label");
    }
}

pedro.event.addToOnLoad(initGlobalFlyouts);

var tooltips;

function initTips() {
    tooltips = new Tips($$('.tool-tip'), {
        maxTitleChars: 50,
        className : 'tt-bubble',
        offsets : { x : 20, y : 0 }
    });
}

/* Don't want to hold up the load with this, so after the dom loads */
window.addEvent("load", function() { initTips(); });

/* User Location */

var userLocation = {};

userLocation.changeLocation = function(location,formId) {
    var formEl = byId(formId);
    if (!isNull(formEl)) {
        formEl.where.value = (!isUndef(location.where)) ? location.where : "";
        formEl.geoId.value = (!isUndef(location.geoId)) ? location.geoId : "";
        formEl.submit();
    }
}

userLocation.clearRecentLocations = function(childId, parentId, baseProdHost){
    var childEl = byId(childId);
    var parentEl = byId(parentId);
    if(parentEl != null && childEl != null){
        parentEl.removeChild(childEl);
    }
    //deleting recent_locs cookie
    if(navigator.cookieEnabled){
        document.cookie = 'recent_locs=; domain=.'+baseProdHost;
    }
}
if(byId("leaderboard")){
    function initLeaderBoard() {
        if(byId("leaderboard").childNodes.length == 0) {
            byId("leaderboard").setStyle("display","none");
        }
    }
    pedro.event.addToOnLoad(initLeaderBoard);
}

function writeFlyoutChunkLinks(elem, links, itemFunc, chunkFunc, chunkSize)
{
    var chunks = chunkArray(links, chunkSize);
    for (var i=0; i<chunks.length; i++)
    {
        var chunkChild = elem.appendChild(chunkFunc());
        for (var j=0; j<chunks[i].length; j++)
        {
            chunkChild.appendChild(itemFunc(chunks[i][j]));
        }
    }
}

function writeFlyoutLinks(elem, links, itemFunc)
{
    for (var i=0; i<links.length; i++)
    {
        elem.appendChild(itemFunc(links[i]));
    }
}

