/* Scripts for the Miller-Beck Institute of Music Website */
/* June 19, 2010 */

function hideProfileInfo(){
	// if JavaScript is disabled, divs will be visible
	// if JavaScript is enabled, this script will run onload to hide them and hideShow() can be used to toggle their visibility
	
	//get all divs inside div#profile
	var allProfileDivs = $('profile').getElements('div');
	
	//determine what is actually profile content (not an internal div) by looking for .showMe or .hideMe
	for(i=0; i<allProfileDivs.length; i++){
		if(allProfileDivs[i].hasClass('showMe') || allProfileDivs[i].hasClass('hideMe')){
			//if profile content, set class to hideMe
			allProfileDivs[i].set('class', 'hideMe');
		}
	}
	
	//also, clear class for profile links (make not active)
	var allProfileLinks = $('profileNav').getElements('a');
	for(i=0; i<allProfileLinks.length; i++){
		allProfileLinks[i].erase('class');
	}
}

function hideShow(trigger){
	//used to toggle the visibility of the profile content divs

	//hide and deactivate all
	hideProfileInfo();
	
	//give clicked link .active class
	var activeLink = $(trigger);
	activeLink.set('class', 'active');
	
	//find anchor that trigger links to (id and name of anchor must be same)
	var linksTo = trigger.get('href');
	var anchorName = linksTo.slice(1); //to remove the '#'
	var theAnchor = $(anchorName); //get as object by id
	
	//get all divs inside div#profile
	var allProfileDivs = $('profile').getElements('div');
	
	
	//determine what is actually profile content (not an internal div) by looking for .showMe or .hideMe
	for(i=0; i<allProfileDivs.length; i++){
		if(allProfileDivs[i].hasClass('showMe') || allProfileDivs[i].hasClass('hideMe')){
			
			//if it contains the anchor as a child, set to showMe
			if(allProfileDivs[i].hasChild(theAnchor)){
				allProfileDivs[i].set('class', 'showMe');
			}
		}
	}
}


window.addEvent('domready', function(){
	//run hideProfileInfo, if it exists
	if ($("profile")){
		hideProfileInfo();
	}
	
	// get all anchors
	var allAnchors = $$('a');
	
	// get url as a string
	var page = window.location.toString();
	
	// set the class of all anchors to none and
	// compare anchors to url to determine which is/are active
	
	for(i=0; i<allAnchors.length; i++){
		allAnchors[i].erase('class');
		if(allAnchors[i].match(page)){
			//alert('Found one! ' + allAnchors[i]);
			allAnchors[i].set('class', 'active');
		}
	}
});