// Font Replacement
Cufon.replace('.featureBlock h3', {fontFamily: 'Futura'});
Cufon.replace('.featureBlock p', {fontFamily: 'Helvetica Neue'});
Cufon.replace('p.nugget', {fontFamily: 'Helvetica Neue'});
Cufon.replace('li.newsbit p', {fontFamily: 'Helvetica Neue'});


//MT Domready Handler
window.addEvent('domready', function(){


if (current_section() == 'home') {	
	// Set homepage items to pre-animated states	
	$('mission').setStyle('opacity',0);	
	$('mission').getElement('p.nugget').setStyle('opacity',0);
	//$('feature').setStyle('opacity',0);
	//$('feature').getElement('img').setStyle('opacity',0);
	document.getElement('div.story h3').setStyles({'opacity':0});
	document.getElement('div.story p').setStyles({'opacity':0});

	//Hide all Homepage newsbits
	newsitems = $$('.newsbit');
	newsitems.each ( function (element){
		element.setStyles({ display:'none', opacity:0});
	$('newslist').setStyle('opacity',0);
	});


}	
	//Initialize the Main Nav
	var subnavs = $$('.subnav');	
	var section = current_section();
	navInit(subnavs, section);
	
//	if(Browser.Engine.version < 5 && Browser.Engine.trident) alert('Currently the site has not be tested for Internet Explorer versions below 7.0. Before you continue, please download either Internet Explorer 7.0 from http://www.microsoft.com/windows/downloads/ie/getitnow.mspx, or Mozilla Firefox from http://www.mozilla.com/en-US/firefox/?from=getfirefox.');

});





// **************** ONLOAD HANDLER ******************


window.addEvent('load', function(){

//Cheat XHTML Strict
//Find all rel=external and add target=_blank
var links = $$('a');
links.each (function(element){
	if (element.getProperty('rel') == 'external') {
		element.setProperty('target','_blank');
	}
});

//Member Login Popdown

if ($('login-form')) {

	formFx = new Fx.Morph($('login-form'), {duration:300, transition:Fx.Transitions.Back.easeOut });
	$('login-form').store('state','retracted');
	
	$('login-form-trigger').addEvent('click', function() {
		if ($('login-form').retrieve('state') == 'deployed'){
			formFx.start('.retracted');
			$('login-form').store('state', 'retracted');
		} else {
			formFx.start('.deployed');
			$('login-form').store('state', 'deployed');
		}
		return false;
	});

}

//Check if we're on the home page a execute specific code
if (current_section() == 'home') {


	// ******** MISSION AND NEWS CODE ***********
	
		var newsitems = $$('.newsbit');
		//alert(newsitems[1].id);
		//startnews(newsitems);
	
		var newsText = $('mission').getElement('p.nugget')
		var startPos = $('mission').getStyle('top').toInt();
		var startHeight = $('mission').getCoordinates().height.toInt();
		var textHeight = newsText.getCoordinates().height.toInt()-20;
		var totalHeight = startHeight + textHeight;
		var restPos = startPos - (textHeight/2);
		var missionFx = new Fx.Morph($('mission'), {duration:600, transition:Fx.Transitions.Cubic.easeInOut, link:'chain'});
		var newsFx = new Fx.Morph(newsText, {duration:600, link:'chain' });
		var welcomeFx = new Fx.Morph($('mission').getElement('h3'), {duration:1000, transition:Fx.Transitions.Cubic.easeInOut, link:'chain'}); 
		var listFx = new Fx.Morph($('newslist'), {duration:1200, link:'chain'});
			
		$('mission').setStyle('height',totalHeight);
		
		missionFx.start({'opacity':1});
		missionFx.start.delay(3000, missionFx, { 'top':restPos});
		
		newsFx.start.delay(3000, newsFx, {'opacity':1, 'margin-top':'-20px'});
		welcomeFx.start.delay(9000, welcomeFx, {'margin-top':-(totalHeight-15), 
												'onComplete': function() 
													{
														listFx.start({'opacity':1});
														newsitems[0].setStyles({'display':'block', 'opacity':1});
														startnews(newsitems);
																	
													} 
												});
		newsFx.start.delay(9400, newsFx, {'opacity':0});
		
	
	// ******** MAIN FEATURE CODE **********
	
		var feature = $('feature');
		var featureFx = new Fx.Morph(feature, {duration:1000, link:'chain'});
		var featImg = $('feature').getElement('img');
		var imgFx = new Fx.Morph(featImg, {duration:600, link:'chain'});
		var storyhead = document.getElement('div.story h3');
		var storytext = document.getElement('div.story p');
		
		var storytextFx = new Fx.Morph(storytext, {duration:600,link:'chain'});
		var storyheadFx = new Fx.Morph(storyhead, {duration:600,link:'chain'});
		
		//featureFx.start.delay(0, featureFx, {'opacity':1});
		//imgFx.start.delay(1000, imgFx, {'opacity':1});
		
		storyheadFx.start.delay(1000, storyheadFx, {'opacity':1});
		storytextFx.start.delay(1000, storytextFx, {'opacity':1});
	



} //end home page specific code


// ********  MAIN NAVIGATION CODE ***********
	
	//determine what section we are on
	

	var triggers = $$('li.parent');	
	var sections = $$('ul.subnav');
	var sectionNav = "nav-" + current_section();	

	var currentsub = document.getElement('#'+sectionNav+' ul.subnav');
	myAccordion = new Fx.Accordion(triggers, sections, {display:currentsub, trigger:'mouseenter', fixedHeight:'230px', initialDisplayFx:false});	


	//Initialize the Main Nav
	navInit();

	triggers.each (function(element){
		
		//Setup element animations
		var link = element.getElement('a')
		link.set('morph', {duration:600, transition:Fx.Transitions.easeOut, link:'cancel'});
		var subNav = element.getFirst('ul.subnav');
		subNav.set('tween',{duration:600, transition:Fx.Transitions.easeOut, link:'cancel'});
/* 		element.set('tween', { duration:600, transition:Fx.Transitions.easeOut, link:'cancel'}); */
				
		element.addEvent('mouseenter', function(){
		
			$clear(idleTimer);
			var color = getColor(element.id);
			//element.getFirst('ul.subnav').tween('margin-top','0');
			element.tween('background-color','#fff8e5');
			element.getElement('a').morph({
				'background-color':color,
				'margin-right':'0', 
				'color':'#ffffff',
				'padding-right':'25px'});
			retractOthers(element.id);				
		
		});

							
	});

	//Secondary Nav animation code
	var subitems = $$('ul.subnav li a');
	subitems.each (function(el){
	
		
		var color = el.getStyle('color');
		var hoverColor = getColor(el.getParent('li.parent').id);

		var subItemsFx = new Fx.Morph(el, {duration:200, link:'cancel'});
	
		el.addEvent('mouseenter', function(){ subItemsFx.start({'color':hoverColor }); });
		el.addEvent('mouseleave', function(){ subItemsFx.start({'color':color}); });
	
	});


	//Setup mouseout event to close any deployed nav items on mouse exit after a
	//specific amount of time
	
	var idleTimer;
	
	$('navigation').addEvent('mouseleave', function(){
	
		var expandCurrentOnIdle = function(){ 
				var section = current_section();
				var sectionNav = "nav-"+section;
				var currentsub = document.getElement('#'+sectionNav+' ul.subnav');
				myAccordion.display(currentsub);
				retractOthers(sectionNav);
				//$(sectionNav).getFirst('ul.subnav').tween('margin-top','0');
				$(sectionNav).tween('background-color','#fff8e5');
				$(sectionNav).getElement('a').morph({
					'background-color':getColor(sectionNav),
					'margin-right':'0', 
					'color':'#ffffff',
					'padding-right':'25px'});
			};			

		idleTimer = expandCurrentOnIdle.delay(0);	
		
	});	
	


});





//  ************** SHARED FUNCTIONS *************



//Function to reset all deployed nav items except for current one

function retractOthers(current){
	var triggers = $$('li.parent');	
	triggers.each (function(element){
		if (element.id != current) {
			var sectionColor = getColor('nav-'+current_section());
			//element.getFirst('ul.subnav').tween('margin-top',-maxUlHeight); 
			element.tween('background-color','#ffedbe');
			element.getElement('a').morph({
				'background-color':'#ffedbe',
				'margin-right':'50px',
				'color': sectionColor,
				'padding-right':'0px'});
		}
	});
}
	
	






//Function to get the proper color of each nav item

function getColor(who) {
	var colors = new Array();
	colors['nav-about'] = '#9e1b32'; 
	colors['nav-grantees'] = '#004f5c'; 
	colors['nav-grants'] = '#65b83e'; 
	colors['nav-involved'] = '#733582'; 
	colors['nav-informed'] = '#ff8000';
	colors['nav-members'] = '#ffba43';
	return colors[who];
}


//The news looping function
var startnews = function(list) {
	
	total = list.length;
	i = 0;

	var shownews = function () {
		if (i==0){
			list[i].setStyle('display','block');
			list[i].fade('in');
			list.getLast().setStyles({'display':'none','opacity':0});
		} else {
			list[i-1].setStyles({'display':'none','opacity':0});
		}
		list[i].setStyle('display','block');
		list[i].fade('in');
		i++;
		if (i>=list.length) { i = 0 };
	}

	newsloop = shownews.periodical(8000);

}

// Returns the current section
var current_section = function () {
	//presume section is always the first class if
	//tag has multiple classes	
	return document.getElement("body").get('class').split(' ')[0];
}




function navInit() {

	//determine what section we are on
	var section = current_section();
	var sectionNav = "nav-"+section;
	
	//extend the Parent/subnav separator of the current section nav fully	
	if (section != "home"){ 
		$(sectionNav).getElement('a').setStyles({
			'margin-right':'0', 
			'padding-right':'25px', 
			'background-color':getColor(sectionNav), 
			'color':'white'
			});			 
		$(sectionNav).setStyle('background-color','#fff8e5');
	} 

}


