/*
	Site:			Play and Learn
	Date:			December 2005
	File:			Behaviors for Play and Learn
	By:				Eric Shepherd
	
	Requires:	dhtml_lib.js, 
						moo.fx.js, 
						prototype.lite.js, 
						moo.fx.pack.js
*/

// declare global variables
var _thePageType;
var _GlobalResized = false; //tracks whether the header is in the default or shrunk size state
var _isSafari = (document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false; //boolean - is it safari?

function pageIdentity() { //sets _thePageType to the class name which identifies the state/template of the page
	var theContainer = document.getElementById('container');
	_thePageType = theContainer.className;
}

function addFx() {
	if (!SupportTest.isIE7) {
		myResize = new fx.Resize('header', {duration: 400});
	}
}

function closeTheBox(e) 
// close the box, with or without an effect.
{
	if (_thePageType == 'landing-page') myResize.modify(135,0); 
	hideActivityDivs();
	hideSelectedLinkStates();
	_GlobalResized = false;
}

function hideActivityDivs() 
// hide the activity lists, to be sure that there are never two showing at once
{
	var theDivs = document.getElementsByTagName('div');
	for (j=0; j<theDivs.length; j++) {
		if (theDivs[j].className.match(/activityGroup/g)) {
			theDivs[j].style.display = 'none';
		}
	}
}

function hideSelectedLinkStates() 
// take off highlighting on selected link, so that two are never on at once
{
		var theParentDiv = document.getElementById('age-list');
		var theLinks = theParentDiv.getElementsByTagName('a');
		for (i=0; i<theLinks.length; i++) {
			if (theLinks[i].className.match(/selected/g)) {
				theLinks[i].className = theLinks[i].className.replace('selected', '');
			}
		}
}

function showActivityList(e) 
// hides other activity lists and shows the selected list.
// css determines where the list shows up
{ 
	// first, we hide all the age groups, in case one of them is showing
	hideActivityDivs();
	// then, we cancel all the "selected" link states
	hideSelectedLinkStates();
	// then, scroll up the window
	if (_GlobalResized == false && _thePageType == 'landing-page') { 
		myResize.modify(-155, 0);
		_GlobalResized = true; 
	}	
	// then, we find the one that was selected and show it
	// get the list item clicked on
	var theListItem = DomHelper.AscendDom(this, 'li');
	var theId = theListItem.id; // and its id
	this.className += ' selected'; // to style the currently active link
	theId = theId.replace('li', 'age');
	theTarget = document.getElementById(theId);
	theTarget.style.display = 'block';
	EventHelper.CancelDefault(e); 
}

function init() 
// initialize all event listeners for the page and
// run initial helper functions to set global variables
{ 
// first, run helper functions
// so we don't have to set yet another window.load event!
	pageIdentity();
	addFx();
// then, get down to business
	if (!document.getElementById || !document.getElementsByTagName) return;
	var ageList = document.getElementById('age-list');
	var theLinks = ageList.getElementsByTagName('a');
	var allLinks = document.getElementsByTagName('a');
	for (j=0; j<allLinks.length; j++) {
		if (allLinks[j].className.match(/closeActivities/)) {
			EventHelper.AddEvent(allLinks[j], 'click', closeTheBox, false);
		}
	}
		
	for (i=0; i<theLinks.length; i++) {
		EventHelper.AddEvent(theLinks[i], 'click', showActivityList, false);
		if (_isSafari) theLinks[i].onclick = EventHelper.CancelClickSafari; //safari still doesn't support cancelDefault()
	}
}

EventHelper.AddEvent(window, 'load', init, false);
