//function for side menu
function ShowSub(el) {
	//document.getElementById(el).style.display='block';
}
function HideSub(el) {
	//document.getElementById(el).style.display='none';
}

/**
 * Register an event listener on an element
 */
/**
 * document.getElementById(); replacement.
 */
function $() {
    var elements = new Array();
    
    if (arguments[0] == '') { return elements; }
    // Find all the elements supplied as arguments
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
       
        // If the argument is a string assume it's an id
        if (typeof element == 'string') {
            element = document.getElementById(element);
        }
        
        // If only one argument was supplied, return the element immediately
        if (arguments.length == 1) {
            return element;
        }
        
        // Otherwise add it to the array
        elements.push(element);
    }
    
    // Return the array of multiple requested elements
    return elements;
};

/**
 * Checks to see if the current browser is compatible with the entire library
 */
function isCompatible(other) {
    // Use capability detection to check requirements
    if( other===false 
        || !Array.prototype.push
        || !Object.hasOwnProperty
        || !document.createElement
        || !document.getElementsByTagName
        ) {
        alert('TR- if you see this message isCompatible is failing incorrectly.');
        return false;
    }
    return true;
}

function addEvent( node, type, listener ) {
    // Check compatibility using the earlier method
    // to ensure graceful degradation
    if(!isCompatible()) { return false }
    if(!(node = $(node))) return false;
    
    if (node.addEventListener) {
        // W3C method
        node.addEventListener( type, listener, false );
        return true;
    } else if(node.attachEvent) {
        // MSIE method
        node['e'+type+listener] = listener;
        node[type+listener] = function(){node['e'+type+listener]( window.event );}
        node.attachEvent( 'on'+type, node[type+listener] );
        return true;
    }
    
    // Didn't have either so return false
    return false;
};

/**
 * Retrieve an array of element base on a class name
 */
function getElementsByClassName(className, tag, parent){
    parent = parent || document;
    if(!(parent = $(parent))) return false;
    
    // Locate all the matching tags
    var allTags = (tag == "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag);
    var matchingElements = new Array();
    
    // Create a regular expression to determine if the className is correct
    className = className.replace(/\-/g, "\\-");
    var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
    
    var element;
    // Check each element
    for(var i=0; i<allTags.length; i++){
        element = allTags[i];
        if(regex.test(element.className)){
            matchingElements.push(element);
        }
    }
    
    // Return any matching elements
    return matchingElements;
};

function setBody(body_id) {
  addEvent(window,'load', function(W3CEvent) {
             var body_tag = document.getElementsByTagName('BODY');
             body_tag[0].setAttribute('id',body_id);
	   });
}

function setMenuItem() { 
  addEvent(window, 'load', function (W3CEvent) {
     var filename = window.location.toString();
     if (filename.indexOf('/about_us/') != -1) {
        selMenuItem = 'about_us';
     } else if (filename.indexOf('/what_we_do/') != -1) {
        selMenuItem = 'what_we_do';
     } else if (filename.indexOf('/home/') != -1) {
        selMenuItem = 'home';
     } else if (filename.indexOf('/get_involved/') != -1) {
        selMenuItem = 'get_involved';
     } else if (filename.indexOf('/news/') != -1) {
        selMenuItem = 'news';
     } else if (filename.indexOf('/careers/') != -1) {
        selMenuItem = 'careers';
     }  else {
        selMenuItem = '';      
        return;
     }
     var menuItem = '../images/'+selMenuItem+'_nav_hover.jpg';
     var menuImage = $('nav_'+selMenuItem);
     menuImage.setAttribute('src',menuItem);
  });
}

function registerMultiStateAnchorListeners(anchor,anchorImage,path,extension) {
    // Load the over state image
    // the loading will occur asynchronously to the rest of the script

    var imageMouseOver = new Image()
    imageMouseOver.src = path + '_hover' + extension;
    
    // Change the image source to the over image on mouseover
    addEvent(anchor, 'mouseover', function (W3CEvent) { 
        if (selMenuItem == '')
	    return;
        if (imageMouseOver.src.indexOf('home') != -1)
            return;
        if (imageMouseOver.src.indexOf(selMenuItem) != -1)
	    return;
        anchorImage.src = imageMouseOver.src; 
    });
    
    // Change the image source to the original on mouseout
    addEvent(anchor, 'mouseout', function (W3CEvent) { 
        anchorString = path+extension;
        if (selMenuItem == '')
	    return;
        if (imageMouseOver.src.indexOf('home') != -1)
            return;
        if (anchorString.indexOf(selMenuItem) != -1)
	    return;
        anchorImage.src = path + extension;
    });
}

function initMultiStateAnchors(W3CEvent) {
    // Locate all the anchors on the page
    var anchors = getElementsByClassName('ms','a');

    // Loop through the list
    for (var i=0; i<anchors.length ; i++) {
        
        // Find the first child image within the anchor
        var anchorImage = anchors[i].getElementsByTagName('img')[0];
        
        if(anchorImage) {
            
            // If there is an image, parse the source
            var extensionIndex = anchorImage.src.lastIndexOf('.');
            var path= anchorImage.src.substr(0, extensionIndex);
            var extension= anchorImage.src.substring(
                extensionIndex,
                anchorImage.src.length
            );
            
            // Add the various mouse handlers and pre-load the images.

            registerMultiStateAnchorListeners(
                anchors[i],
                anchorImage,
                path,
                extension
            );
        }   
    }
}

// Modified the tagged anchors when the document loads
addEvent(window,'load',initMultiStateAnchors);
setMenuItem();

