// enclopsed anonymous function to maintain namespace
(function() {
    var content = document.getElementById('content'); // get div#content
    var sub = document.getElementById('sub'); // get div#sub
    var page = document.getElementById('page'); // get div#page

    if(content != null && sub != null && page != null) {
        page.insertBefore(sub, content); // put sub before content

        // only for docs pages
        if(window.location.pathname.search('\/docs') != -1) {
            var divSection = document.createElement('div'); // make div.section
            divSection.className = 'section'; // add class

            // add div.section to sub
            sub.appendChild(divSection);

            var childArray = new Array(); // array to hold the items to move

            // add all sub's children to div.section
            for(var i = 0; i < sub.childNodes.length; i++) {
                var child = sub.childNodes[i];
            
                if(child.className != undefined && child.className.search('section') == -1) {
                    childArray.push(child);
                }
            }

            for(var i = 0; i < childArray.length; i++) {
                var child = childArray[i];
            
                divSection.appendChild(child);
            }
        }
    }

    // add "Powered by" text before the logo
    var branding = document.getElementById('branding'); // get div#branding
    var brandingLogo = document.getElementById('branding-logo'); // get div#branding-logo
    if(branding != null && brandingLogo != null) {
        var poweredBy = document.createElement('span');
        poweredBy.id = 'powered-by'; // set id
        poweredBy.appendChild(document.createTextNode('_')); // add text

        branding.insertBefore(poweredBy, brandingLogo); // add powered by before the branding logo
    }

    // add Follow us at the end of div#local
    var local = document.getElementById('local'); // get div#local
    if(local != null) {
        // sub function for me to add icon links
        function createIconLink(name, className, linkSrc) {
            var a = document.createElement('a'); // anchor link
            a.href = linkSrc; // set link src
            a.target = '_blank'; // new window

            var name = document.createTextNode(name); // link text
            
            a.className = className;
            
            a.appendChild(name); // add text to anchor tag

            return a;
        }

        var span = document.createElement('span');
        span.id = 'follow-us'; // set id
        span.appendChild(document.createTextNode('Follow us ')); // set text

        // create img icons and link
        var twitter = createIconLink('twitter', 'twitter', 'http://www.twitter.com/YellowAPI');
        var facebook = createIconLink('facebook', 'facebook', 'http://www.facebook.com/pages/YellowAPIcom-Official/187475997941109');
        var linkedIn = createIconLink('linkedin', 'linkedin', 'http://www.linkedin.com');

        // add the icons to the span
        span.appendChild(twitter);
        span.appendChild(facebook);

        local.appendChild(span); // add span to div#local
    }

    // process locale-list manipulations
    var localeList = document.getElementsByClassName('locale-list')[0]; // get div.locale-list
    if(localeList != undefined && localeList != null) {
        var switchLI = null; // store the LI we want to move and do it after the fact

        for(var i = 0; i < localeList.childNodes.length; i++) {
            var childLI = localeList.childNodes[i];
        
            if(childLI.tagName != undefined) { // check for valid elements
                if(childLI.className.search('active') != -1) {
                    childLI.style.display = 'none'; // hide active LIs
                } else {
                    var userNav = document.getElementById('user-nav'); // get div#user-nav
                    if(userNav != null) {
                        var userUL = userNav.getElementsByTagName('ul')[0]; // get the UL in the user-nav
                        if(userUL != undefined && userUL != null) {
                            var childA = childLI.getElementsByTagName('a')[0]; // get the A in the LI
                            if(childA != undefined && childA != null) {
                                // first strip the (Canadian) out of them ;p
                                childA.innerHTML = childA.innerHTML.replace('(Canadian)', '');
                                childA.innerHTML = (childA.innerHTML.search('French') != -1 ? 'Fran&ccedil;ais' : childA.innerHTML);
                                childA.style.backgroundImage = ''; // clear the background-image
                                childA.style.textIndent = '0px'; // reset the text-indent

                                switchLI = childLI;
                            }
                        }
                    }
                }
            }
        }

        if(switchLI != null) {
            userUL.appendChild(switchLI);
        }
    }

var myCheckbox = document.getElementById('members-ypg_optin_mail');
if(myCheckbox != null) {
    myCheckbox.checked = true;}
})();


