// JavaScript Document
jQuery(function( $ ){		
	var urls = [ 'homeMastheadBkgd', 'aboutMastheadBkgd', 'whatWeDoMastheadBkgd', 'theLandsMastheadBkgd', 'conservationMastheadBkgd', 'educationMastheadBkgd', 'recreationMastheadBkgd', 'newsMastheadBkgd', 'calendarMastheadBkgd', 'howYouCanHelpMastheadBkgd', 'contactMastheadBkgd' ];
	var siteBase = location.href;
	var dotCom = siteBase.indexOf('.com');
	siteBase = siteBase.substr(0, (dotCom + 4));
	siteBase = siteBase+"/";
	$.preload( urls, {
		base: siteBase,
		ext:'.jpg',
		onComplete:function( data ){
			var img = new Image();
			img.src = data.image;
		},
		onFinish:function(){
			rotateImages(11);
		}
	});
});
var masthead = new Array();
masthead[0] = "/imgs/masthead/homeMastheadBkgd.jpg";
masthead[1] = "/imgs/masthead/aboutMastheadBkgd.jpg";
masthead[2] = "/imgs/masthead/whatWeDoMastheadBkgd.jpg";
masthead[3] = "/imgs/masthead/theLandsMastheadBkgd.jpg";
masthead[4] = "/imgs/masthead/conservationMastheadBkgd.jpg";
masthead[5] = "/imgs/masthead/educationMastheadBkgd.jpg";
masthead[6] = "/imgs/masthead/recreationMastheadBkgd.jpg";
masthead[7] = "/imgs/masthead/newsMastheadBkgd.jpg";
masthead[8] = "/imgs/masthead/calendarMastheadBkgd.jpg";
masthead[9] = "/imgs/masthead/howYouCanHelpMastheadBkgd.jpg";
masthead[10] = "/imgs/masthead/contactMastheadBkgd.jpg";
var loopCounter = 0;
function rotateImages(imagesLength) {
	if(loopCounter == (imagesLength - 1)) { loopCounter = 0; } else { loopCounter++; }
	jQuery("#masthead").fadeTo(2000, 1.0, function() {
		jQuery("#mastheadBack").css("background-image", "url("+masthead[loopCounter]+")");
		jQuery("#masthead").fadeTo(4000, 0.0, function() {
			if(loopCounter == (imagesLength - 1)) { loopCounter = 0; } else { loopCounter++; }
			jQuery("#masthead").css("background-image", "url("+masthead[loopCounter]+")");
			jQuery("#masthead").fadeTo(2000, 0.0, function() {
				jQuery("#masthead").fadeTo(4000, 1.0, function() { rotateImages(imagesLength); });
			});
		});
	});
}
