// CONTENTS ====================================================================
/*
- DEFAULT 							- common code that runs on every page.
- FEATURE ROTATOR					- scripts for the feature rotator on the homepage
- SECTION NAVS						- scripts for the section subnav
*/

// VAR DECLARATIONS -----------------------------------------------------
int_currentRotatorItem 		= 0;						// which item is on the left of the rotator
arr_proj					= [];						// array to hold references for all the items
str_ease					= "easeInOutExpo";			// easing style to use - try 'easeOutElastic' or 'easeOutQuad' for different effects
int_animTime				= 900; 						// time (in milliseconds) over which to animate rotator - higher is slower

// DEFAULT --------------------------------------------------------------
function InitDefault(){
	
	// call other Init functions
	InitStyleHelper();
	InitFeatureRotator();
	InitSubNavs();
	
}

function InitStyleHelper() {
	/* set up handy style helpers - banding on tables etc */
	$('.body-copy tr:even').addClass('alt');
	$('.body-copy tr').each(function(){
		$(this).find('td:last, th:last').css('border', 'none');
	});
	$('#documents-list li:first').css('margin-top', 0);
}

// FEATURE ROTATOR --------------------------------------------------------------
function InitFeatureRotator(){
	
	// set active class & take the margin off the last item
	$('#feature-rotator').addClass('active');
	$('#feature-rotator ul li:last').css('margin-right', 0);
	
	// size UL to contain all items in a line
	var noItems = $('#feature-rotator ul li').length;
	var width = (noItems * $('#feature-rotator ul li:first').width()) + ((noItems-1) * 10);
	$('#feature-rotator ul').width(width);
	
	// easier to work with things in an array
	$("#feature-rotator li").each( function(){ arr_proj.push($(this)) } );
	
	// add control handling
	$('#next').click(function(){
		Rotate(1);
		return false;
	});
	$('#prev').click(function(){
		Rotate(-1);
		return false;
	});
	
	// 'disable' prev control initially
	$('#prev').hide();
}

function Rotate(_dir){
	int_currentRotatorItem += _dir;
	if(int_currentRotatorItem < 0) int_currentRotatorItem = 0;
	if(int_currentRotatorItem == arr_proj.length - 2) int_currentRotatorItem = arr_proj.length - 3;
	
	(int_currentRotatorItem == 0) 					? $('#prev').fadeOut() : $('#prev').fadeIn();
	(int_currentRotatorItem == arr_proj.length - 3) 	? $('#next').fadeOut() : $('#next').fadeIn();
	
	var offset = arr_proj[int_currentRotatorItem].offset().left - $('#feature-rotator ul').offset().left;
	$('#feature-rotator ul').stop().animate({"left":0-offset},int_animTime, str_ease)
}

// SECTION NAVS --------------------------------------------------------------
function InitSubNavs(){
	$('#section-nav ul').hide();
	$('#main-nav a').each(function(){
		if( $(this).attr('rel') != '' ) {
			$(this).click(function(){
				$('#section-nav ul').hide();
				$( '#' + $(this).attr('rel') ).slideDown();
				return false;
			})
		}
	})
}

// ======================================================================
