/**
 * Remove all children of an element in the DOM
 */
function removeChildren(elem) {
    while (elem.hasChildNodes()) {
        elem.removeChild(elem.firstChild);
    }
}

/**
 * Initialize a section
 *
 * This function will initialize the hhspalte, meaning it will highlight the correct section
 * without loading any data. This is used when the site is loaded so the default content will be
 * visible for searchengines.
 *
 * @uses display_hidden_ads()
 */
function initializeHspalteContent(sectionId) {
    /* Highlight the new section */
    YAHOO.util.Dom.addClass($(sectionId), 'active');

    /* Display the hidden ads */
    display_hidden_ads();
}

/**
 * Load some content from an url into the hhspalte column
 *
 * This function will remove the highlight from the old active section, and highlight the new. It
 * will also fetch the data associated with the new section and fill the content area. While it
 * fetches the data it will display a "loading" box to show that new data is loading.
 *
 * @uses removeChildren()
 * @uses display_hidden_ads()
 */
function loadHspalteContent(sectionId, imagePath) {
    /* Highlight the new section */
    YAHOO.util.Dom.removeClass(document.getElementById(activeSectionId), 'active');
    YAHOO.util.Dom.addClass(document.getElementById(sectionId), 'active');
    activeSectionId = sectionId;

    /* Now, lets display the "loading" box that will be displayed until the data is fetched */
    var section_container = document.getElementById('hhspalte_innhold');

    /* Lets remove all elements inside the container first */
    removeChildren(section_container);

    /* Create a container for the image and add a css class to it */
    var loading_container = document.createElement('div');
    YAHOO.util.Dom.addClass(loading_container, 'loading');

    /* Now, create the image tag and append this to the container */
    var loading_image = document.createElement('img');
	loading_image.setAttribute('src', imagePath  + '/images/loading.gif');
    YAHOO.util.Dom.addClass(loading_image, 'loading');

    loading_container.appendChild(loading_image);

    /* Append the container to the content container */
    section_container.appendChild(loading_container);

    /*
        section is a global array that describes the different sections that can be loaded. The sectionId is
        the key to elements in the array.
    */
    var callback = {
        success : function(o) {
            var section_container = document.getElementById('hhspalte_innhold');

            /*
                If the sectionId of the container that is finished loading is not the same as the
                active one, we do not want to display the content. This is to prevent slower
                sections to appear over other sections if a user clicks faster than the sections
                can load.
            */
            if (activeSectionId != o.argument['loadedSection']) {
                return;
            }

            /* Lets remove all elements inside the container first */
            removeChildren(section_container);

            /* Set the content while the element is invisible */
            section_container.innerHTML = o.responseText;

            /* Display the hidden ads */
            display_hidden_ads();
        },
        failure: function(o) {
            /* Do something clever */
        },
        argument: {"loadedSection": sectionId}
    }

    if (sections[sectionId]['url'] == '') {
        alert('Mangler url');
    } else {
        var transaction = YAHOO.util.Connect.asyncRequest('GET', sections[sectionId]['url'], callback);
    }
}

/**
 * Move hidden ads into the placeholder in the hhspalte column
 */
function display_hidden_ads() {
    var ph, rh;
    for (var i = 0; i < hiddenAds.length; i++) {
        ph = document.getElementById('webboard_placeholder_' + hiddenAds[i]);
        rh = document.getElementById('webboard_' + hiddenAds[i]);
        if (ph && rh) {
             ph.innerHTML = rh.innerHTML;
             YAHOO.util.Dom.removeClass(ph, 'hidden');
             YAHOO.util.Dom.addClass(rh, 'hidden');
        }
    }
}
