var body = null;
var section = "commitment";

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
document.observe("dom:loaded", function() {	

	body = $$('body')[0];
	
	if (window.location.hash != '') {
		section = window.location.hash.substr(1);
		$('view-as-list').setAttribute('href', section+'.jsp');
	}
	
	body.id = 'body-'+section;
	
	if ($('flashContent'))
		flashInit(siteURL, section);
		
	mainNav();
	filterInit();
	articleLinks();
	redirector();
	
	if ($('view-as-list') != null) {
		$('view-as-list').observe('click', function(event){
			window.location.href = section+'.jsp#'+section;
		});
	}
});

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function redirector() {
	var location = window.location.href;
	if (location.indexOf("redirect.html") == -1)
		return;
	
	var target = location.split('?target=')[1];
	$$('a.btn2')[0].setAttribute('href', target);
}

function flashInit(siteURL, section) {
		
	var flashvars = {
		baseURL: siteURL,
		section: section,
	  	filter: "all"
	};
	var params = {
		wmode: "transparent",
		scriptaccess: "sameDomain"
	};
	var attributes = {
		name: "flashContent"
	};

	swfobject.embedSWF("./environment.swf", "flashContent", "980", "390", "9.0.28", "", flashvars, params, attributes);
	//swfobject.embedSWF("./environment.swf", "flashContent", "980", "370", "9.0.0","expressInstall.swf",flashvars, params, attributes);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function mainNav() {
		
	var navLinks = $$('#nav li a');
	
	for(i=0; i<navLinks.length; i++){
		navLinks[i].observe('click', function(event){			
			section = this.up().id.substr(4);
			body.id = 'body-'+section;
			
			if (section == 'commitment') {
				window.location.href = '/commitment.jsp';
			}	
			else if ($('flashContent')) {
				if (section != 'initiatives')
					$('filter').style.display = "none";
				else 
					$('filter').style.display = "block";
					
				document.getElementById("flashContent").mainNavPress(section);
				
				$('view-as-list').setAttribute('href', section+'.jsp');				
				window.location.href = '#'+section;
			}
			else 
				window.location.href = '/environment.jsp#'+section;
				
			Event.stop(event);		
		});
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function ffMainNavPress(section) {
	
	body.id = 'body-'+section;
	
	window.location.hash = '#'+section;
	
	return true;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function ffArticlePress(url) {
	
	window.location.href = url;
	
	return true;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function filterInit() {
	
	var filters = $$('#subNav #filter dd');
	var allFilter = $('filter-all');
	var clickedFilters = new Array();
	
	filters.invoke('observe', 'click', function() {	
		
		var filterID = this.id.substr(7);
		
		if (clickedFilters.indexOf(filterID) == -1) {
			
			if(this.id != 'filter-all') {
				allFilter.removeClassName('filtered');
				this.addClassName('filtered');
				clickedFilters = unset(clickedFilters,'all');
				clickedFilters.push(filterID);
			} else {
				filters.invoke('removeClassName', 'filtered');
				allFilter.addClassName('filtered');
				clickedFilters = new Array('all');
			}
		
		} else {
			
			if(this.id != 'filter-all') {
				this.removeClassName('filtered');
				clickedFilters = unset(clickedFilters,filterID);
			}
		}
		
		document.getElementById("flashContent").filterPress(clickedFilters.join('|'));
		
	});
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function articleLinks() {

	articles = $$('#threeColumns .column .article');
	links = $$('#threeColumns .column .article h3 a');
	for(i=0; i<articles.length; i++){
		articles[i].observe('click', function(event){
			document.location.href = links[articles.indexOf(this)];
		});
		
		// gives any article with an image a class of withImg for background positioning
		if(articles[i].childElements()[0].match('img')){
			articles[i].addClassName('withImg');
		}		
	}
}

function unset(array,value) {
		
	var output = new Array(0);
	var j = 0;
	
	for (var i=0; i<array.length; i++) {
	
		if (array[i] != value) {
			output[j++] = array[i];
		}
	}
	
	return output;
}