function icbFirstLast() {
	$$('.module').each(function(module) {
		var blocks=$(module).select('.image_content_block');
		if (blocks.size()>0) {
			$(blocks.first()).addClassName('first');
			$(blocks.last()).addClassName('last');
		}
	});
}

/**
 * Initialize the .module_tabbed tabbing system.
 * <p>
 * Go through each module, one at a time.
 * For each, hide any inactive tabs.
 * If an active tab was already specified, turn it on.
 * If no active tab was specified, make the first one active and turn it on.
 * Then bind all of the tab links to deactivate any other tabs and activate themselves.
 * </p>
 * @returns {void}
 * @author Nick Davison
 */
function initTabs() {
	$$('.module_tabbed').each(function(module) {
		var $_tabParent=$(module);
		
		var _tabs=$_tabParent.select('.module_tabs li a');
		var _tabContents=$_tabParent.select('.module_tab');
		
		// Hide all of the tabs
		_tabContents.each(function(tab) {
			tab.style.display="none";
		});
		
		// Search for any tabs that are already set to active (if so, display their content)
		var bAlreadyActive=false;
		_tabs.each(function(tab) {
			if ($(tab).hasClassName('active')) {
				bAlreadyActive=true;
				var rel=tab.getAttribute('rel');
				$(rel).style.display="block";
			}
		});
				
		// If there weren't any active, activate the first one
		if (!bAlreadyActive) {
			$(_tabs[0]).addClassName('active');
			var rel=_tabs[0].getAttribute('rel');
			$(rel).style.display="block";
		}
				
		// Now bind the clicks
		_tabs.each(function(tab) {
			$(tab).observe('click', function(evt) {
				evt.stop();
											 
				_tabs.each(function(t) {
					$(t).removeClassName('active');
				});
				
				_tabContents.each(function(t) {
					t.style.display="none";
				});
				
				$(tab).addClassName('active');
				var rel=tab.getAttribute('rel');
				$(rel).style.display="block";
			});
		});
	});
}

/**
 * Set up a defaults system for form fields.
 *
 * <p>Use rev attributes to store what the default
 * text should be. Clear it when the fields gain
 * focus, reset it when they lose focus and are empty.</p>
 *
 * <p>Additionally, set and remove the active/inactive classes,
 * inactive when the rev state is used, active when not.</p>
 *
 * @returns {void}
 * @author Nick Davison
 */
function initRevFieldDefaults() {
	// Find all of the inputs and textareas with a rev attribute
	$$('input[rev], textarea[rev]').each(function(el) {											  
		$(el).observe('blur', function(evt) {
			if (this.value=="") {
				this.value=this.getAttribute('rev');
				$(this).addClassName('inactive');
				$(this).removeClassName('active');
			}
		});
		
		$(el).observe('focus', function(evt) {
			if (this.value==this.getAttribute('rev')) {
				this.value="";
				$(this).addClassName('active');
				$(this).removeClassName('inactive');
			}
		});
		
		if ( (el.value=="") || (el.value==el.getAttribute('rev')) ) {
			el.value=el.getAttribute('rev');
			$(el).addClassName('inactive');
			$(el).removeClassName('active');
		}
	});
}

/**
 * Set up the flyout nav system.
 *
 * <p>As IE6 cannot be trusted to apply :hover to non link elements, use JS for the flyouts</p>
 *
 * @returns {void}
 * @author Nick Davison
 */
function initFlyoutNav() {
	$$('li.hn_primary').each(function(nav) {
		$(nav).observe('mouseover', function(evt) {
			$(this).addClassName('hover');
		});
		
		$(nav).observe('mouseout', function(evt) {
			$(this).removeClassName('hover');
		});
	});
}

function initForms() {
	$$('form.validate_form').each(function(f) {
		f.onsubmit=function() {
			var isFormValid=true;
			$(this).select('input.validate, textarea.validate, select.validate').each(function(el) {
				var isValid=true;
				if ($(el).match("select")) {
					if ( (el.selectedIndex==null) || (el.selectedIndex==-1)) {
						isValid=false;
					}
				} else {
					if (el.value=="") {
						isValid=false;
					}
				}
				
				if (!isValid) {
					isFormValid=false;
					$(el.parentNode).addClassName('error');
				} else {
					$(el.parentNode).removeClassName('error');
				}
			});
			return isFormValid;
		}
	});
}

function loseForumCellspacing() {
	$$('.forum table').each(function(t) {
		t.cellSpacing=0;
	});
	
	$$('.forum table tr').each(function(tr) {
		tr.select('td').each(function(td, i) {
			$(td).addClassName('column_'+i);
		});
	});
}

function loseGenericTableCellSpacing() {
	$$('table.generic').each(function(t) {
		t.cellSpacing=0;
	});
	
	$$('table.generic tr').each(function(tr) {
		tr.select('td').each(function(td, i) {
			$(td).addClassName('column_'+i);
		});
	});
}

function initializeHomeFlash() {
	// If we're on the home page
	if (document.getElementById('home')) {
		// If there's a div created to receive the flash.
		if (document.getElementById('page_feature_flash_content')) {
			// Use swfobject to pull it in
			swfobject.embedSWF("/swf/rotator2/deploy/rotator.swf", "page_feature_flash_content", "650", "344", "9.0.124", "expressinstall.swf", {xmlPath:'/pages/home_features', timerLength:'8'}, {menu:'false', quality:'best', scale:'noscale', bgcolor:'#000000', wmode: 'transparent'}, {id:'rotator'});
		}
	}
}

function initMeetTheTeamFlash() {
	if (document.getElementById('header_meet_the_team_content')) {
		var homeVal="false";
		if (document.getElementById('home')) {
			homeVal="true";
		}
		swfobject.embedSWF("/swf/meetTheTeam.swf", "header_meet_the_team_content", "375", "114", "9.0.124", "", {clickURL:'/blogs', isHome:homeVal}, {menu:'false', quality:'best', bgcolor:'#000000', wmode: 'transparent'});
	}
}

var flashReplaceCount=0;
function initHeadlineFlash() {
	$$('h1, .module_drivers_large h2').each(function(el) {
		bBanner=false;
						   
		// Find out if it's a banner we're replacing
		if (el.parentNode.id) {
			if (el.parentNode.id=="page_banner") {
				bBanner=true;
			}
		}
		
		// If the element doesn't already have an id, give it one
		if ( (typeof el.id=="undefined") || (el.id==null) || (el.id=="")) {
			el.id="flashReplaceId"+flashReplaceCount;
			flashReplaceCount++;
		}
		
		var id=el.id; // Get the id
		var titletext=el.innerHTML; // Get the current inner text
		var newInnerHTML='<div id="'+id+'_inner">'+titletext+'</div>'; // Wrap the inner text in a div.
		el.innerHTML=newInnerHTML; // And write it back in
		// We do that wrapping so we can still style the <h1> with an <object> inside it, rather than having the <object> replace the <h1> and give us no CSS control over positioning.
		
		
		if (!bBanner) {
			// Convert the text to Flash
			swfobject.embedSWF("/swf/headline.swf", id+"_inner", "100%", "37", "9.0.124", "", {title:titletext}, {menu:'false', quality:'best', scale:'noscale', bgcolor:'#000000', wmode: 'transparent'});
		} else {
			// Get the number from the inner text
			var num=titletext.toString().toLowerCase().replace(/[^1-9]/gi, '');
			var height=157; // Most are 157 high, we'll reset if we need to
			var filename="02_blogs";
			switch(num) {
				case "2" :
					filename="02_blogs";
					break;
				case "3" :
					filename="03_videos";
					height=167;
					break;
				case "4" :
					filename="04_news";
					height=167;
					break;
				case "5" :
					filename="05_drivers";
					break;
				case "6" :
					filename="06_tracks";
					break;
				case "7" :
					filename="07_games";
					break;
			}
			
			swfobject.embedSWF("/swf/"+filename+".swf", id, "925", height, "9.0.124", "", {animate:'true'}, {menu:'false', quality:'best', bgcolor:'#000000'});
		}
	});
}

function initRaceResultsFlashHeaders() {
	$$('.module_drivers_small h2').each(function(el) {
		// If the element doesn't already have an id, give it one
		if ( (typeof el.id=="undefined") || (el.id==null) || (el.id=="")) {
			el.id="flashReplaceId"+flashReplaceCount;
			flashReplaceCount++;
		}
		
		var id=el.id; // Get the id
		var titletext=el.innerHTML; // Get the current inner text
		var newInnerHTML='<div id="'+id+'_inner">'+titletext+'</div>'; // Wrap the inner text in a div.
		el.innerHTML=newInnerHTML; // And write it back in
		// We do that wrapping so we can still style the <h1> with an <object> inside it, rather than having the <object> replace the <h1> and give us no CSS control over positioning.												   
							
		swfobject.embedSWF("/swf/raceResultsHeadline.swf", id+"_inner", "100%", "61", "9.0.124", "", {title:titletext, date:''}, {menu:'false', quality:'best', scale:'noscale', bgcolor:'#eff6e8', wmode: 'transparent'});
	});
}

// JavaScript Document
document.observe('dom:loaded', function() {
	initTabs(); // Initialize the .module_tabbed tabbing system.
	initRevFieldDefaults(); //Set up a defaults system for form fields.
	icbFirstLast(); // Add first and last classes to lists of image content blocks within modules
	initFlyoutNav(); // Set up the flyout nav system
	initForms(); // Initialize generic form validation
	loseForumCellspacing();
	loseGenericTableCellSpacing();
	initializeHomeFlash();
	initMeetTheTeamFlash();
	initHeadlineFlash();
	initRaceResultsFlashHeaders();
});