function buildTips(termClass,definition) {
    terms = $$('.'+termClass);
    for (i=0; i<terms.length; i++) {
        new Tip(terms[i], definition, {className: 'tip', delay: 0.2});
    }   
}

function initExpandables(expanderClass,expandableClass,effectType,containerID) {
    if (containerID) {
        expanders = $(containerID).select('.'+expanderClass);
        expandables = $(containerID).select('.'+expanderClass); 
    }
    else {
        expanders = $$('.'+expanderClass);
        expandables = $$('.'+expandableClass);  
    }
    for (i = 0; i < expanders.length; i++) {
        headingText = expanders[i].innerHTML;
        if ((headingText.indexOf('<img') != -1) || (headingText.indexOf('<IMG') != -1)) {
            headingText = headingText.split('>')[1];
        }
        headingText = headingText.replace(/\s/g,'');
        headingText = headingText.replace(/[^a-zA-Z 0-9]+/g,'');
        headingText = headingText + '-' + i;
        expanders[i].innerHTML = '<a href="#'+headingText+'" id="'+headingText+'-link">'+expanders[i].innerHTML+'</a>';
        expanderLink = $(headingText+'-link');
        expandables[i].id = headingText;
        Element.hide(expandables[i]);
        if (effectType == 'blind') {
            expanderLink.observe('click', blindContent.bindAsEventListener(this,expandables[i],expanders[i],expanderClass)); 
            expanderLink.onclick = function(){ return false; }
        }
        else { 
            expanderLink.observe('click', toggleContent.bindAsEventListener(this,expandables[i],expanders[i],expanderClass)); 
            expanderLink.onclick = function(){ return false; }
        }
    }
}

function blindContent(e,expandable,expander,expanderClass) {
    Effect.toggle(expandable,'blind',{duration:0.3});
    if (expander.className.indexOf('expanded') != -1) {
        expander.className = expanderClass;
    }
    else expander.className = expander.className + ' expanded';
}

function toggleContent(e,expandable,expander,expanderClass) {
    Element.toggle(expandable);
    if (expander.className.indexOf('expanded') != -1) {
        expander.className = expanderClass;
    }
    else expander.className = expander.className + ' expanded';
}

function expandAll(expanderClass,expandableClass) {
    expanders = $$('.'+expanderClass);
    expandables = $$('.'+expandableClass);  
    for (i = 0; i < expanders.length; i++) {
        Element.show(expandables[i]);
        expanders[i].className = expanders[i].className + ' expanded';
    }
}

function collapseAll(expanderClass,expandableClass) {
    expanders = $$('.'+expanderClass);
    expandables = $$('.'+expandableClass);  
    for (i = 0; i < expanders.length; i++) {
        Element.hide(expandables[i]);
        expanders[i].className = expanderClass;
    }
}

// VIDEOS

function loadVideos(videoContainer,username,category,numVideos) {
    if (category == '') category = 'uploads';
    $(videoContainer).innerHTML = '<p class="dimmed">Loading videos...</p>';
    new Ajax.Updater(videoContainer,'/includes/youtube_feeds.php?user='+username+
                     '&category='+category+
                     '&num_items='+numVideos,{
        //insertion: 'bottom',
        onComplete: function(){ 
            getVideo('first',null,username,category);
            newLiveSearch(videoContainer,'.feed-item','Search videos','top',numVideos,false)
            }
        }); 
}

function getVideo(videoID,videoTitle,username,category) {
    if (videoID == 'first') {
        videoID = $(username+'-'+category+'-nav').select('.feed-item')[0];
        videoID = videoID.id;
        //alert(videoID);
        videoTitle = $(username+'-'+category+'-nav').select('.feed-item-title')[0];
        //videoTitle = videoTitle.getAttribute('alt');
        videoTitle = videoTitle.innerHTML;
    }
    videoViewerID = username+'-'+category;
    $(videoViewerID).innerHTML = '<h4>'+videoTitle+' <span class="fine-print"> &mdash; <a href="http://www.youtube.com/user/'+username+'"><b>'+username+'</b></a></span></h4>'+
        '<object width="400" height="300">'+
        '<param name="movie" value="http://www.youtube.com/v/'+videoID+'&rel=0&showinfo=0"></param>'+
        '<embed src="http://www.youtube.com/v/'+videoID+'&rel=0&showinfo=0" '+
        'type="application/x-shockwave-flash" width="400" height="300">'+
        '</embed>'+
        '</object>';
}

function loadPhotos(photoContainer,username,tags,maxPhotos) {
    params = 'user='+username;
    if (tags) params = params + '&tags='+tags;
    new Ajax.Updater(photoContainer,'/includes/flickr_feeds.php?'+params,{
        onComplete:function(){ 
            //getLatestPhoto();
            newLiveSearch(photoContainer,'.feed-item','Search photos','bottom',maxPhotos,false)
            }
        });
    //new Ajax.Updater('student-photos','/includes/gallery.php');   
}

function getLatestPhoto() {
    if ($('latest-photo')) {
        photo = $$('.flickr-feed')[0].select('img')[0];
        photoURL = photo.getAttribute('src');
        photoURL = photoURL.replace(/_s.jpg/,'');
        photoURL = photoURL + '_m.jpg';
        photo = document.createElement('img');
        photo.setAttribute('src',photoURL);
        $('latest-photo').appendChild(photo);
    }
}

/*------------------- Live Search ------------------*/

function newLiveSearch(LS_containerID,LS_element,LS_prompt,LS_navLoc,LS_max,LS_showForm) {
    if (!LS_prompt) LS_prompt = 'Search';
    if (!LS_max) LS_max = 8;
    if (!LS_navLoc) LS_navLoc = 'top';
    //if (LS_showForm = '') LS_showForm = true;
    LS_container = $(LS_containerID);
    formHTML = '<form onsubmit="return false;" action="" class="LS-form">'+
                '<input onkeyup="liveSearch(this,\''+LS_containerID+'\',\''+LS_element+'\',\''+LS_prompt+'\','+LS_max+');" class="LS-input">'+
                '</form>';
    navHTML = '<p class="LS-pages-nav"><a style="display: none;" href="#" class="LS-previous-page"><span>Previous</span></a> '+ 
                '<span class="LS-current-page"></span> '+
                '<a style="display: none;" href="#" class="LS-next-page"><span>More</span></a></p>';
    errorMsg = '<p style="display: none;" class="LS-no-results error"></p>';
    
    if (LS_container.select('.youtube-nav') == '') {
        cHTML = LS_container.innerHTML;
        if (cHTML.indexOf('</h2>') != -1) {
            cHTMLArray = cHTML.split('</h2>', 2);
            LS_container.innerHTML = cHTMLArray[0]+'</h2>'+formHTML+errorMsg +navHTML+'<div class="animation-wrapper">'+cHTMLArray[1]+'</div>'+navHTML;
        }
        else {
            LS_container.innerHTML = formHTML+errorMsg+navHTML+'<div class="animation-wrapper">'+cHTML+'</div>'+navHTML;
        }
    }
    else {
        LS_container.select('.youtube-nav')[0].addClassName('animation-wrapper');
        LS_container.innerHTML = formHTML + errorMsg + navHTML + LS_container.innerHTML + navHTML;
    }
    //if (LS_navLoc == 'bottom') LS_container.innerHTML = formHTML + errorMsg + navHTML + LS_container.innerHTML + navHTML;
    //LS_container.innerHTML = formHTML + errorMsg + LS_container.innerHTML;
    searchInput = LS_container.select('.LS-input')[0];
    if (LS_showForm == false) {
        searchInput.hide();
        //if ($('past-dates')) Element.hide('past-dates');
    }
    searchInput.value = LS_prompt;
    searchInput.onfocus = function () { 
        if (this.value == LS_prompt) this.value = ''; 
        this.className = '';
        };
    searchInput.onblur = function () { 
        if (this.value == '') this.value = LS_prompt; 
        this.className = 'LS-input';
        };
    liveSearch(searchInput,LS_containerID,LS_element,LS_prompt,LS_max);
}
function liveSearch(searchInput,LS_containerID,LS_element,LS_prompt,LS_max) {
    LS_container = $(LS_containerID);
    allItems = LS_container.select(LS_element);
    searchStr = searchInput.value;
    if (searchStr == LS_prompt) searchStr = '';
    if (allItems.length <= LS_max) {
        searchInput.hide();
        //if ($('past-dates')) Element.hide('past-dates');
    }
    currentPage = 1;
    pageNumber = 1; 
    if (searchStr.length > 1 || searchStr == '') {
    
        liveSearchResults(LS_containerID,allItems,searchStr,LS_max,pageNumber,currentPage); 
    }
    return false;
}


function liveSearchResults(LS_containerID,allItems,searchStr,LS_max,pageNumber,currentPage,doAnimation) {
    var i = 0;
    if (doAnimation == null) doAnimation = false;
    LS_container = $(LS_containerID);
    previousPage = LS_container.select('.LS-previous-page');
    nextPage = LS_container.select('.LS-next-page');
    pageLocation = LS_container.select('.LS-current-page');
    noResults = LS_container.select('.LS-no-results')[0];
    pagesNav = LS_container.select('.LS-pages-nav');
    animationWrapper = LS_container.select('.animation-wrapper')[0];
    pagesNav[0].className = pagesNav[0].className + ' top';
    pagesNav[1].className = pagesNav[1].className + ' bottom';
    var itemGroups = new Array();
    for (z = 0; z < allItems.length; z++) {
        allItems[z].hide();
        itemDetails = allItems[z].innerHTML;
        itemDetails = itemDetails.replace(/<\/?[^>]+(>|$)/g, "");
        var searchExp = new RegExp(searchStr, "gi");
        if (searchExp.test(itemDetails)) {
            itemGroups[i] = allItems[z];
            itemGroups[i].show();
            i++;
        }
    }
    
    if (itemGroups.length == 0) {
        noResults.innerHTML = 'No results found &nbsp;<strong>:(</strong>';
        noResults.show();
        pagesNav.each(function(e){ e.hide(); });
    }
    else {
        noResults.innerHTML = '';
        noResults.hide();
        pagesNav.each(function(e){ e.show(); });
        for (z = 0; z < itemGroups.length; z++) {
            numberOfPages = itemGroups.length/LS_max;
            numberOfPages = Math.ceil(numberOfPages);
            if (numberOfPages == 0) numberOfPages = 1;
            if (numberOfPages == 1) { 
                pagesNav.each(function(e){ e.hide(); });
            }
            pageNumber = z/LS_max+1;
            pageNumber = Math.floor(pageNumber);
            //pageLocation.innerHTML = 'Page ' + currentPage + ' of ' + numberOfPages;
            if (pageNumber == currentPage) {
                itemGroups[z].show();
                if (z < (itemGroups.length - 1)) {
                    nextPage.each(function(e){
                        e.className = 'LS-next-page';
                        e.show();
                        e.onclick = function () {
                            currentPage = currentPage + 1;
                            liveSearchResults(LS_containerID,allItems,searchStr,LS_max,pageNumber,currentPage,true);
                            return false;
                        }
                    });
                }
                else {
                    nextPage.each(function(e){
                        e.show();
                        e.className = 'LS-next-page disabled';
                        e.onclick = function () { return false; }
                    });
                }
                if (currentPage != 1) {
                    previousPage.each(function(e){
                        e.show();
                        e.className = 'LS-previous-page';
                        e.onclick = function () {
                            currentPage = currentPage - 1;
                            liveSearchResults(LS_containerID,allItems,searchStr,LS_max,pageNumber,currentPage,true);
                            return false;
                        }
                    });                 
                }
                else {
                    previousPage.each(function(e){
                        e.show();
                        e.className = 'LS-previous-page disabled';
                        e.onclick = function () { return false; }
                    });
                }
            }
            else itemGroups[z].hide();
            
        }
    }
    if (doAnimation) {
        LS_container.style.minHeight = LS_container.offsetHeight+'px';
        animationWrapper.hide();
        new Effect.Parallel([     
          new Effect.SlideDown(animationWrapper, { sync: true }),
          new Effect.Appear(animationWrapper, { sync: true })
        ], { 
          duration: 0.3,
          afterFinish: function () { LS_container.style.minHeight = ''; },
          queue: { position: 'end', scope: 'livesearchscope' }
        });     
    }
    doAnimation = false;
}

function showRelevantContent(containerID,contentElements,keywords) {
    container = $(containerID);
    contents = container.select(contentElements);
    keywords = keywords.toLowerCase();
    keywords = keywords.replace(' ', '');
    keywords = keywords.split(',');
    contents.each(function(content){
        content.removeAttribute('_counted'); // for IE
        content.hide();
        contentHTML = content.innerHTML;
        contentHTML = contentHTML.replace(/<\/?[^>]+(>|$)/g, '');
        contentHTML = contentHTML.toLowerCase();
        for (i = 0; i < keywords.length; i++) {
            if (contentHTML.indexOf(keywords[i]) != -1) {
                content.show();
            }
        }
    });
    contents.each(function(content){
        if (content.style.display == 'none') content.remove();
    });
    //if (container.innerHTML.indexOf('<') == -1) container.up('.module').hide();
    //alert(contents.length);
    updatedContents = container.select(contentElements);
    updatedContents.each(function(e){ e.removeAttribute('_counted'); });
    if (updatedContents.length == 0) container.up('.module').hide();
}

var feedTimer;
function loadFeeds(containerID,numItems,tags,ajaxURL) {
    params = '';
    if (ajaxURL == null) ajaxURL = '/includes/feeds.php';
    if (tags) {
        tags = tags.replace(/(\s)/g, '');
        tags = tags.replace(',', '-');
        params = '?tags='+tags;
    }
    //numItems = 20;
    //clearTimeout(feedTimer);
    $(containerID).innerHTML = '<p class="dimmed">Loading...</p>';
    new Ajax.Updater(containerID,ajaxURL+params,{
        onComplete:function(){ 
            newLiveSearch(containerID,'.feed-item','Search...','bottom',numItems,false);
            }
        });
    //feedTimer = setTimeout(function(){loadFeeds(containerID,numItems)},100000);
}
function basicAjaxLoader(containerID,ajaxUrl) {
    $(containerID).innerHTML = '<p class="dimmed">Loading...</p>';
    new Ajax.Updater(containerID,ajaxUrl);
}
function createProgramModals() {
    if ($('homepage-admission')) {
        programs = $$('.sub-programs');
        programs.each(function(e){ 
            e.removeAttribute('_counted'); 
            new Control.Modal(e,{
                position: 'relative',
                offsetTop: -60,
                offsetLeft: 100,
                width: 300,
                overlayDisplay: true,
                overlayCloseOnClick: true,
                onSuccess: function () {}
            });                                  
        }); 
    }
}
function loadNewsFeed(pg) {
    feedContainer = $('campus-news');
    feedContainer.style.minHeight = feedContainer.offsetHeight+'px';
    if (pg == null) pg = 1;
    feedContainer.innerHTML = '<p class="loading"><img src="http://www.usask.ca/_usask/img/lightbox/loading.gif" alt="Loading..." /></p>';
    new Ajax.Updater(feedContainer,'/includes/news.php?page='+pg,{           
        onComplete:function(){ 
                new Effect.Parallel([     
                      //new Effect.SlideDown('news-animation-wrapper', { sync: true }),
                      new Effect.Appear('news-animation-wrapper', { sync: true })
                    ], { 
                      duration: 0.2,
                      afterFinish: function () { feedContainer.style.minHeight = ''; },
                      queue: { position: 'end', scope: 'newsscope' }
                    });
            }
        });
}

var tweetTimer;
function loadTweets(containerID,numTweets,username) {
    //clearTimeout(tweetTimer);
    if (username == null) username = '';
    //numTweets = 20;
    $(containerID).innerHTML = '<p class="dimmed">Loading Tweets...</p>';
    new Ajax.Updater(containerID,'/includes/tweets.php?items=30&username='+username,{
        onComplete:function(){ 
            newLiveSearch(containerID,'.feed-item','Search tweets...','bottom',numTweets,false);
            }
        });
    //tweetTimer = setTimeout(function(){loadTweets(containerID,numTweets,username)},100000);
}

function loadFeature(f) {

    $('features-wrapper').innerHTML = '<p class="loading"><img src="http://www.usask.ca/_usask/img/lightbox/loading.gif" alt="Loading..." /></p>';

    new Ajax.Updater('features-wrapper','/includes/features.php?feature='+f,{

        onComplete:function(){ 

            fImg = $('features-wrapper').select('img');

            new Effect.Appear(fImg[0],{

                duration:0.3,

                queue: { position: 'end', scope: 'featurescope' }

            });

        }
    });

}

function loadFacebookButton() {
    if ($('facebook-recommend')) {
        currentPage = document.location.href;
        new Ajax.Updater('facebook-recommend','/includes/facebook_recommend.php?p='+currentPage);
    }
}


var fTimer;

function switchFeature(fNumber) {
    clearTimeout(fTimer);
    fItems = $$('.feature-item');
    fDetails = $$('.feature-details');
    fImg = $$('.feature-image');
    fVid = $$('.feature-video');
    numberOfFeatures = fItems.length;
    if (fNumber >= numberOfFeatures) fNumber = 0;
    if (fNumber < 0) fNumber = numberOfFeatures - 1;
    fItems.each(function(e){ e.hide(); e.removeAttribute('_counted'); });
    fDetails.each(function(e){ e.hide(); e.removeAttribute('_counted'); });
    fImg.each(function(e){ e.hide(); e.removeAttribute('_counted'); });
    fItems[fNumber].show();
    fDetails[fNumber].show();
    fNext = $('feature-next');
    fPrev = $('feature-previous');
    //if (fNumber == 0) fPrev.hide();
    //else fPrev.show();
    new Effect.Appear(fImg[fNumber],{
        duration:0.4,
        queue: { position: 'end', scope: 'featurescope' }
        });
    if (numberOfFeatures > 1) { 
        fNumber++;
        //fTimer = setTimeout(function(){switchFeature(fNumber)},15000);
    }
    fNext.onclick = function() { 
        //clearTimeout(fTimer);
        //alert(fNumber);
        switchFeature(fNumber);
        this.blur();
        return false;
        
    };
    fPrev.onclick = function() { 
        //clearTimeout(fTimer);
        fNumber = fNumber-2;
        //alert(fNumber);
        switchFeature(fNumber);
        this.blur();
        return false;       
    };
    //fVid.each(function(e){ e.removeAttribute('_counted'); e.onmouseover = function() { clearTimeout(fTimer); }});
}

function createFeaturesNav() {
    //Element.show('features');
    fItems = $$('.feature-item');
    fItems.each(function(e){ e.hide(); e.removeAttribute('_counted'); });
    numberOfFeatures = fItems.length;
    randomFeature = Math.floor(Math.random()*numberOfFeatures)
    switchFeature(randomFeature);
}

function lastModifiedDate() {
    if ($('last-modified')) $('last-modified').innerHTML = $('modified-date').innerHTML;
}

function addIcons() {
    allLinks = $('content-and-context').getElementsByTagName('a');
    numLinks = allLinks.length;
    for (i = 0; i < numLinks; i++) {
        hrefAtr = allLinks[i].getAttribute('href');
        if (hrefAtr) {
            if (hrefAtr.indexOf('paws.usask.ca') != -1) {
                allLinks[i].className = allLinks[i].className + ' paws-login';
            }
            if (hrefAtr.lastIndexOf('.pdf') != -1) {
                //allLinks[i].className = allLinks[i].className + ' pdf';
            }
        }
    }
}

function hideEmptyContext() {
    if ($('context')) {
        if ($('context').innerHTML.indexOf('<') == -1) {
            $('context').hide();
        }
    }
    if ($('context-nav')) {
        if ($('context-nav').getElementsByTagName('li').length == 0) { $('context-nav').hide(); }
    }
}

function adjustColumnWidths() {
    columnContainers = $$('.columns-2');
    columnContainers.each(function(c){ 
        c.removeAttribute('_counted');
        columns = c.select('.column');
        columns[1].className = columns[1].className + ' adjusted';
    });
    columnContainers = $$('.columns-3');
    columnContainers.each(function(c){ 
        c.removeAttribute('_counted');
        columns = c.select('.column');
        columns[2].className = columns[2].className + ' adjusted';
    });
    columnContainers = $$('.columns-4');
    columnContainers.each(function(c){ 
        c.removeAttribute('_counted');
        columns = c.select('.column');
        columns[3].className = columns[3].className + ' adjusted';
    });
    
}


function countdownToDate(container,date,targetMessage) {
    // enter date in '12 January, 2010' format
    today  = new Date();
    todayEpoch  = today.getTime();  
    target = new Date(date); 
    targetEpoch = target.getTime(); 
    daysLeft = Math.floor(((targetEpoch - todayEpoch) / (60*60*24)) / 1000);
    if (daysLeft > 1) plural = 's';
    else plural = '';
    if (daysLeft > 0) {
        $(container).innerHTML = '<div class="wrapper"><p id="countdown"><span id="days-left">'+daysLeft+' day'+plural+'</span> until '+targetMessage+'</p></div>';
    }
    else $(container).innerHTML = '<div class="wrapper"><p id="countdown"><span id="days-left">Today</span> is '+targetMessage+'</p></div>';
    Effect.SlideDown(container,{
        duration:0.3,
        queue: { position: 'end', scope: 'featurescope' }
        });
}

function buildTabs(headingTag,containerID,count) {
    container = $(containerID);
    navOrientation = '';    
    classAddition = '';
    j = 0;
    contents = new Array();
    headings = new Array(); 
    if (container.className.indexOf('vertical') != -1) { 
        navOrientation = 'vertical';
        classAddition = '-vertical';
    }
    container.innerHTML = '<ul class="tabbed-nav'+classAddition+'"></ul>'+container.innerHTML;
    tabsList = container.select('ul')[0];
    tabsList.removeAttribute('_counted'); // for IE
    allContents = container.select('.tabbed-content');
    allContents.each(function(e){ e.removeAttribute('_counted'); }); // for IE
    // where nested tabbed groups exist, make sure we grab only the relevant content containers
    for (i = 0; i < allContents.length; i++) {
        if (allContents[i].up('.tabbed-group').id == containerID) {
            contents[j] = allContents[i];
            headings[j] = contents[j].select(headingTag)[0];
            j++;
        }
    }
    // hide all the content containers (except the first one) and create the tabs
    onLoadTab = '';
    for (i = 0; i < contents.length; i++) {
        linkClass = '';
        contents[i].className = contents[i].className + classAddition;
        if (i != 0) contents[i].hide();
        else linkClass = 'active';
        /*if (navOrientation != 'vertical')*/ headings[i].hide();       
        headingHTML = headings[i].innerHTML;
        headingAnchor = headingHTML.replace(/(<\/?[^>]+(>|$))|(\s)|([^a-zA-Z 0-9]+)/g, ''); // strip out spaces, tags, weird characters, etc.
        //contents[i].innerHTML = '<a name="'+headingAnchor+'" id="'+headingAnchor+'"></a>' + contents[i].innerHTML;
        contentAnchor = new Element('a');
        contentAnchor.id = headingAnchor;
        Element.insert( contents[i], {'top':contentAnchor} );
        
        currentURL = window.location.href;
        if (currentURL.indexOf('t=') != -1) urlID = currentURL.split('t=')[1];
        else urlID = currentURL.split('#')[1];
        if (urlID == headingAnchor) {
            onLoadTab = i;
        }
    
        tab = '<li><a href="#'+headingAnchor+'" class="'+linkClass+'">'+headingHTML+'</a></li>';
        tabsList.innerHTML = tabsList.innerHTML + tab;      
    }
    if (onLoadTab != '') {
        contents[0].hide();
        contents[onLoadTab].show();
        tabLinks = tabsList.select('a');
        tabLinks.each(function(e){ e.removeAttribute('_counted'); e.className = ''; }); // for IE
        tabLinks[onLoadTab].className = 'active';
    }
    // grab all the tab links and add onclick functions
    setTabActions(tabsList,contents);
    if (count != null) { 
        count++;
        getTabbedGroups(count);
    }
}

function setTabActions(tabsList,contents) {
    tabs = tabsList.select('a');
    tabs.each(function(e){ e.removeAttribute('_counted'); }); // for IE
    for (i = 0; i < tabs.length; i++) {
        tab = tabs[i];
        tab.i = i;
        tab.contents = contents;
        tab.tabs = tabs;
        tab.onclick = function() { displayTab(this.i,this.contents,this.tabs); return false; };
    }
    if (tabs.length == 1) {
        tabsList.hide();
        contents[0].style.border = 'none';
    }
}
// show selected content container and highlight the active tab
function displayTab(i,contents,tabs) {
    contents.each(function(e){ e.hide(); });
    contents[i].show();
    tabs.each(function(e){ e.className = ''; });
    tabs[i].className = 'active';
    tabs[i].blur(); 
}

function getTabbedGroups(count) {
    if (count == null) count = 0;
    tabbedGroups = $$('.tabbed-group');
    tabbedGroups.each(function(e){ e.removeAttribute('_counted'); });
    if (count < tabbedGroups.length) {
        contentHTML = tabbedGroups[count].innerHTML;
        headingTag = contentHTML.split('>',2)[1];       
        headingTag = headingTag.split('<')[1];
        containerID = tabbedGroups[count].id;
        buildTabs(headingTag,containerID,count);
    }

}

function createDDMenus() {
    var count = 0;
    var ddSubMenus = new Array();
    ddMenus = $$('.drop-down-menu');
    for (i=0; i<ddMenus.length;i++) {
        ddSubMenus[count] = ddMenus[i].getElementsByTagName('ul')[0];
        count++;
    }
    
    //ddSubMenus = $$('.drop-down-menu ul');
    document.body.onclick = function() {
        ddMenus.each(function(e){ e.removeClassName('active'); });
        for (i=0; i<ddSubMenus.length;i++) {
            Element.hide(ddSubMenus[i]);
        }
    };
    ddMenus.each(function(dd){ 
        dd.removeAttribute('_counted'); 
        trigger = dd.getElementsByTagName('a')[0];
        trigger.subMenu = dd.getElementsByTagName('ul')[0];
        trigger.dd = dd;
        Event.observe(trigger, 'click', function(event) { 
            event.stop();
            for (i=0; i<ddSubMenus.length;i++) {
                if (this.dd.hasClassName('active') == false) {
                    Element.hide(ddSubMenus[i]);
                    ddMenus[i].removeClassName('active');
                }
            }           
            if (this.dd.hasClassName('active') == false) { 
                this.dd.addClassName('active');              
            }
            else { this.dd.removeClassName('active'); }
            Effect.toggle(this.subMenu,'blind',{duration:0.1}); 
        });
    });     
}


function buildBookmarks(strWhichTag, sBookMarkNode) {
    var i;  
    g_anchorCount = 1;  
    var container = document.getElementById('content');
    var aMarkElements = container.getElementsByTagName(strWhichTag);
    if (aMarkElements.length != '') {
        var oList = document.createElement("UL");
        oList.setAttribute("id","bookmarksList");       
        for (i=0; i<aMarkElements.length;i++) {
            if (aMarkElements[i].className != 'expander') {
                var sName = aMarkElements[i].innerHTML;             
                var oAnchor = buildNamedAnchor();
                aMarkElements[i].innerHTML = oAnchor + aMarkElements[i].innerHTML;  
                var oListItem = document.createElement("LI");
                var oLink = document.createElement("A");
                var sLinkDest = "#bookmark" + g_anchorCount;
                oLink.setAttribute("href", sLinkDest);
                oLink.onclick = function () { Element.hide('bookmarksList'); };         
                oListItem.appendChild(oLink);
                oLink.innerHTML = sName; 
                oList.appendChild(oListItem);
                g_anchorCount++;
            }
        }
        if (oList.innerHTML != '') insertBookmarkList(oList,sBookMarkNode);
    }
}

function buildNamedAnchor() {
    return "<a name='bookmark" + g_anchorCount + "' id='bookmark" + g_anchorCount + "'></a>";
}

// Inserts the bookmark list inside the given container tag 
// indicated by sBookmarkNode
function insertBookmarkList(oList, sBookMarkNode) {
    var oListHost = document.getElementById(sBookMarkNode);
    oList.style.display = 'none';
    oListHost.innerHTML = '<p><a href="#" onclick="Element.toggle(\'bookmarksList\'); return false;">Table of contents</a></p>';
    oListHost.appendChild(oList);
}

Event.observe(window, 'load', function() {
    //addIcons();       
    hideEmptyContext();
    getTabbedGroups();
    createDDMenus();
    initExpandables('expander','expandable');
    //adjustColumnWidths();
    loadFacebookButton();
    //createProgramModals();
    if (document.getElementById('bookmarks')) {
        buildBookmarks('H2','bookmarks');
    }
});

