
/* A. Function That Listens For Page Load Event, Loads Other Functions */

window.onload = function() 
{		
	writeSectionStyle();
						MM_preloadImages("nav_project_on.gif","nav_donate_on.gif","nav_challenges_on.gif","nav_stories_on.gif","nav_views_on.gif","nav_photos_on.gif","nav_contact_on.gif","leftnav_on.gif");

	eventListeners();
}


/* B. Function That Listen For Post-Load Events */

function eventListeners()
{	
	/* 1. listen for click on photogallery thumbnail */ 
	if (document.getElementById("pg_summary"))
	{
		x =	document.getElementById("pg_summary").getElementsByTagName("A");
	
		for (var i = 0; i < x.length; i++)
		{
			x[i].onclick = chng_destination;
			x[i].onselect = chng_destination;
		}
	}
	
	/* 2. listen for submit of signup form */
	if (document.forms.signup) 
	{
		document.getElementById("signup").onsubmit=changeAction; 
	}	
	
	/* 3. listen for click in search box */
	if (document.getElementById("topsearch_input"))
	{
		document.getElementById("topsearch_input").blur();
		document.getElementById("topsearch_input").onfocus = reset;	
	}	
	
	/* 4. listen for user rolling over topnav images */	
	if (document.getElementById("topnav"))
	{
		var imgs,i;
		
		imgs = document.getElementById("topnav").getElementsByTagName("A");
		
		for (i=0;i<imgs.length;i++) 
		{
			imgs[i].onmouseover=function(){roll(this);};
			imgs[i].onmouseout=function(){roll(this);};
			imgs[i].onfocus=function(){roll(this);};
			imgs[i].onblur=function(){roll(this);};			
		}
	}	
}		


/* C. Functions That Change Things */

/* 1. make a click on main thumbnail PHOTO GALLERY load a popup gallery */
function chng_destination()
{		
	var new_href = this.href.replace(new RegExp("2219651"), "2258181");				  				 							
	new_href = new_href.replace(new RegExp("2219651"), "2258181");				  				
	new_href = new_href.replace(new RegExp("C00D"), "C025");
				  							 																																													
	var features = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=540,height=590,top=250,left=270";				
				
	window.open(new_href,'_blank',features);return false;
}

/* 2. append querystring to signup form action value, for prepopulating contact form with email address */
function changeAction() 
{
	document.getElementById("signup").action="/site/c.krKPIYPDImE/b.2230159/k.B3A0/Sign_up_for_our_newsletters/apps/ka/ct/contactus.asp?c=krKPIYPDImE&b=2230159&email=" + document.getElementById("signup_input").value;

	document.forms.signup.submit();
}

/* 3. have search text box react when clicked -- clear text, change text color */
function reset() 
{
	document.getElementById("topsearch_input").value='';
	document.getElementById("topsearch_input").style.color='#333';
}
	
/* 4. swap images by editing src value (checking for proper node type) */
function roll(o)
{
	var i,isnode,src,ftype,newsrc,nownode;
	for (i=0;i<o.childNodes.length;i++)
	{
		nownode=o.childNodes[i];
		if (nownode.nodeType==1 && /img/i.test(nownode.nodeName))
		{
			isnode=i;
			break;
		}
	}
		
	src = o.childNodes[isnode].src;
	ftype = src.substring(src.lastIndexOf('.'), src.length);
	
	if(/_on/.test(src))
	{
		newsrc = src.replace('_on','');
	}
	else
	{
		newsrc = src.replace(ftype, '_on'+ftype);
	}
	
	o.childNodes[isnode].src=newsrc;
}

/* 5. changed color of all links and header tags, by parsing breadcrumb for section, and changing body class tag */
function writeSectionStyle()
{	
	if (document.getElementById("breadcrumb"))
	{			
		var  b = document.getElementById("breadcrumb").innerHTML;

		var body = document.getElementsByTagName("BODY")[0];
		
		/* Attention: Edit these values below to match any top level nav items whose names are changed. Edit CSS to match.  - rprice@kintera.com */
		var sA = new Array('Project','Donate','Challenges','Stories','Views','Photos','Contact');
		
		for (i=0;i<sA.length;i++)
		{			
			if (b.indexOf(sA[i])>0) 
			{
				x = sA[i].toLowerCase();
				body.className = x;
				document.getElementById('footernav').getElementsByTagName("A")[i].className = "selected";		
			}
			
			
		}	
	}	
}	

