﻿/* Functions common to closerlook projects */

function getDirectoryForHREF(href) {
    var endIndex = href.lastIndexOf("/");
    if (endIndex < 0)
        return href;
    
    return href.substring(0, endIndex + 1);
}

/* This function creates the accordion-style menu 
   with the correct section open and link highlighted
*/
window.addEvent('domready', function() {
    var intSectionID=0;
    
    // Build an array of all the lists in the menu
    var arrSections = new Array();
    arrSections = $('menu').getElements('ul');
    
    // Loop through each list
    arrSections.each(function(section, sectionIndex){
        
        // Build an array of all the links in this list
        var arrLinks = new Array();
        arrLinks = section.getElements('a');

        // Loop through the links in this list
        arrLinks.each(function(item, index){
                                    
            // See if it matches our current URL
            var strCurrentPath = getDirectoryForHREF(window.location.href);
            var strItemPath = getDirectoryForHREF(item.href);
            
            if (strCurrentPath == strItemPath) {
                // Highlight this link
                item.setStyle('font-weight', 'bold');
                // Update the section ID passed to the accordion object
                intSectionID=sectionIndex;
            };
        });
    });
    
    //Apply the accordion effect to the menu
    var accordion = new Accordion('h3.atStart', 'ul.atStart', {
        show: intSectionID,
        opacity: false,
        onActive: function(toggler, element) {
            //Swap in a minus for the plus in the toggler
            var togglerText=toggler.getText();
            togglerText = togglerText.replace('+','-');
            toggler.setText(togglerText);
            
            //Make the text darker
            toggler.setStyle('color', '#f9e3ed');
            toggler.setStyle('background-image', 'url(/images/MenuBackground_hover.png)');
       },
       onBackground: function(toggler, element) {
            //Swap in a plus for the minus in the toggler
            var togglerText = toggler.getText();
            togglerText = togglerText.replace('-','+');
            toggler.setText(togglerText);
            
            //Make the text lighter
            toggler.setStyle('color', '#ffffff');
            toggler.setStyle('background-image', 'url(/images/MenuBackground.png)');
       }
    }, $('menu'));
});

/*
This function is used to show or hide the glossary terms.
*/
function showOrHide(el) {
    var element = $(el);
    var arrow = $(el+'arrow');

    if (element.getStyle('display')=='none') {
		arrow.removeClass('arrowclosed');
		arrow.addClass('arrow');
        element.setStyle('display', 'block');
    } else {
		arrow.removeClass('arrow');
		arrow.addClass('arrowclosed');
        element.setStyle('display', 'none');
    }
}


/*
This function is used for the image rollovers.
*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
