<!--
// ==============================
// SET VARIABLES
// ==============================


// ==============================
// RUN ALL ONLOAD JQUERY COMMANDS
// ==============================
$(document).ready(function() {
	if (jQuery.browser.msie && (jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0')) {
		$('div#primaryNav > ul > li').mouseover(function() {
			 $(this).children('ul').css({left:'auto'});
		});
		$('div#primaryNav > ul > li').mouseout(function() {
			 $(this).children('ul').css({left:'-9999em'});
		});
	}
	// MAIN NAV ON STATE
	// Process the URL, the parseURL() function is in urlFunctions.js
	var urlParts = parseURL();
	var navID = activeNavID(urlParts);

	$(navID.mainID).addClass('on');
	$(navID.subID).addClass('on');



	
	//	===================
	//	CONNECT WITH US BOX
	//	Forces the component box to the bottom of the right margin. If it's not there already.
	//	===================
	var currentURL = parseURL();
	var connectWithUsHeight =  $('div#rightSection .connect').outerHeight();
	var otherRightMarginContent = $('div#rightSection').outerHeight() - connectWithUsHeight;
	// 28 pixels is added to account for the bottom of the nav and some extra for different browser renderings.
	var mainBodyHeight = $('div#mainBody').outerHeight() + 28;
	// If we're on the home page, we have to subtract the height of the slideshow.
	if (currentURL.pathFile == '/network/' ||currentURL.pathFile == '.index.php') {
		mainBodyHeight -= 356;
		otherRightMarginContent -= 20;
	}
	// if the height of all the right margin content is less the the main body height, reposition the box.
	if ((connectWithUsHeight + otherRightMarginContent) < mainBodyHeight) {
		var connectWithUsTopMargin = mainBodyHeight - (connectWithUsHeight + otherRightMarginContent);
		$('div#rightSection .connect').css('margin-top',connectWithUsTopMargin);
	}
	



	// ==============================
	// FONT SIZE BUTTONS
	// ==============================
	$('.increaseSize').mousedown(function() {
		var currentFontSize = parseInt($('body').css('font-size'));
		var newFontSize = currentFontSize + 1;
		$('body').css('font-size',newFontSize);
	});
	$('.decreaseSize').mousedown(function() {
		var currentFontSize = parseInt($('body').css('font-size'));
		var newFontSize = currentFontSize - 1;
		$('body').css('font-size',newFontSize);
	});



	
	//	==================
	//	PARTNER PAGE
	//	==================
	// First row
	/*$('div.threeColGrid table tr:eq(0)').addClass('firstRow');
	
	// Column one and three
	$('div.threeColGrid table tr').each(function() {
		$(this).find('td:eq(0)').addClass('one');
		$(this).find('td:eq(2)').addClass('three');
	});
	
	
	// Empty cells
	$('div.threeColGrid td:emtpy').addClass('empty');*/
	
	//	==============================
	//	HOME PAGE SLIDESHOW
		// Wrapped in a conditional to skip all of this code on all but the home page.
	//	==============================
	if (typeof(homeSlideShowPresent) != 'undefined') {
		var currentHomeSlide = 0;
		var homeSlidefadeDelay = 10000;
	// Initialize the first slide.
	$('#homeSlideshow .slide .pic').hide();
	$('#homeSlideshow .slide .right').hide();
	$('#homeSlideshow .slide:eq(0) .pic').show();
	$('#homeSlideshow .slide:eq(0) .right').show();
	// Create the controls
	var totalSlides = $('#homeSlideshow .slide').size();
    $('div#slideshowControls ul').append('<li class="leftArrow"></li>');
    $('div#slideshowControls ul').append('<li class="pause"></li>');
	for (i=1; i<=totalSlides; i++) {
		$('div#slideshowControls ul').append('<li class="link"' + ' ref="' + (i - 1) + '">' + i + '</li>');
	}
    $('div#slideshowControls ul').append('<li class="rightArrow"></li>');
	// Initialize the on state
	$('#slideshowControls ul li:eq(2)').addClass('on');
	// Position the controls.
		var slideShowULWidth = 0;
		$('#slideshowControls ul li').each(function() {	
			slideShowULWidth += $(this).outerWidth();
			//alert($(this).outerWidth());
		});
		slideShowULWidth += 2;
		$('#slideshowControls').width(slideShowULWidth);
		var newLeftMargin = 703 + ((240 - slideShowULWidth)/2);
		//alert (newLeftMargin.toString() + 'px');
		$('#slideshowControls').css('margin-left',newLeftMargin);
	}
	
	/*$('#slideshowControls div').mouseover(function() {
		$(this).addClass('hover');					
	});
	$('#slideshowControls div').mouseout(function() {
		$(this).removeClass('hover');					
	});*/
	$('#slideshowControls li.pause').click(function() {
		$(document).stopTime('homeSlideshow');
		$(document).stopTime('homeSlideshowBackword');
		$(document).stopTime('homeSlideshowForward');
		$(this).css({
			'background-position':'-20px'
		});
	});
	$('#slideshowControls li.link').click(function() {
		$(document).stopTime('homeSlideshow');
		transitionHomeSlide($(this).attr('ref'), 'manual');					
	});
	function transitionHomeSlide(slide, dir) {
		var fadeDuration = 750;
		if (dir == 'forward') {
			if (currentHomeSlide == (totalSlides - 1)) {
				slide = 0;
			} else {
				slide = parseInt(currentHomeSlide) + 1;
			}
		} else if (dir == 'back') {
			if (currentHomeSlide == 0) {
				slide = totalSlides - 1;
			} else {
				slide = parseInt(currentHomeSlide) - 1;
			}
		}
		
		//alert('old: ' + currentHomeSlide + ', next: ' + slide);
		// Switch the on state on the controls
		$('#slideshowControls li.link').removeClass('on');
		$('#slideshowControls li.link:eq(' + slide + ')').addClass('on');
		
		// Hide the new image before fading it in.
		$('div#homeSlideshow .slide:eq(' + slide + ' ) .pic').hide();
		// Fade out the old slide
		$('div#homeSlideshow .slide:eq(' + currentHomeSlide + ') .pic').fadeOut(fadeDuration);
		// Fadein in the new slide, slide the new slate in, bring the controls back.
		$('div#homeSlideshow .slide:eq(' + slide + ') .pic').fadeIn(fadeDuration, function () {
			// Hide all slates
			$('div#homeSlideshow .slide .right').hide();
			// Reveal the new slate
			$('div#homeSlideshow .slide:eq(' + slide + ') .right').show();
		});
		// set the new slide as the current slide, for fading out on the next iteration.
		currentHomeSlide = slide;
	}
	
	
	$('#slideshowControls li.rightArrow').click(function() {
		$(document).stopTime('homeSlideshow');
		$(document).stopTime('homeSlideshowBackword');
		$(document).oneTime(100, 'homeSlideshowOneoff', function(i) {
 			transitionHomeSlide(-1, 'forward');
		}, 0);
		$(document).everyTime(homeSlidefadeDelay, 'homeSlideshowForward', function(i) {
 			transitionHomeSlide(-1, 'forward');
		}, 0);
		$('#slideshowControls li.pause').css({'background-position':'0px'});
	});
	$('#slideshowControls li.leftArrow').click(function() {
		$(document).stopTime('homeSlideshow');
		$(document).stopTime('homeSlideshowForward');
		$(document).oneTime(100, 'homeSlideshowOneoff', function(i) {
 			transitionHomeSlide(-1, 'back');
		}, 0);
		$(document).everyTime(homeSlidefadeDelay, 'homeSlideshowBackword', function(i) {
 			transitionHomeSlide(-1, 'back');
		}, 0);	
		$('#slideshowControls li.pause').css({'background-position':'0px'});			
	});
	// Set the autoloop. This is canceled once a button is clicked.
	// The jquery.timers plugin throws a non-supported method error, it doesn't appear to break the functionality.
	// But it will break any jQuery that follows, so this must be the last set of code.
	if ($('#homeSlideshow .slide').size() > 1) {
		$(document).everyTime(homeSlidefadeDelay, 'homeSlideshow', function(i) {
 			transitionHomeSlide(-1, 'forward');
		}, 0);
	} else {
		$('#slideshowControls').hide();
	}
	
});

// Returns an object that contains the active main and left nav IDs based on the url.
function activeNavID(urlParts) {
	var output = new Object();
	output.mainID = null;
	output.subID = null;
	// Check the first URL directory after "/network/"
	switch (urlParts.pathParts[2]) {
		case 'national-programs':
			output.mainID = null;
			output.subID = '.inThisSection li.n1';
			break;
		case 'training-certification':
			output.mainID = null;
			output.subID = '.inThisSection li.n2';
			break;
		case 'foreclosure-resources':
			output.mainID = null;
			output.subID = '.inThisSection li.n3';
			break;
		case 'news-media':
			output.mainID = null;
			output.subID = '.inThisSection li.n4';
			break;
		case 'about-us':
			output.mainID = null;
			output.subID = '.inThisSection li.n5';
			break;
		case 'nonprofits':
			output.mainID = '#primaryNav li.n6 a';
			output.subID = '.inThisSection li.n6';
			break;
		case 'donors':
			output.mainID = '#primaryNav li.n7 a';
			output.subID = '.inThisSection li.n7';
			break;
		case 'consumers':
			output.mainID = '#primaryNav li.n8 a';
			output.subID = '.inThisSection li.n8';
			break;
		default:
			output.mainID = null;
	}
	return output;
}

function randomRange(minNum,maxNum) {
	var randomnumber=Math.floor(Math.random() * (maxNum - 1 + minNum)) + minNum;
	return randomnumber;
}
//-->  

