// teamImageRefresh
// this function controls image shrink/enlarge according to the current subLabel
function teamImageRefresh() {
	
	// are we NOT already refreshing the images?
	if (!teamImageRefreshing) {
		
		// tell other scripts we're refreshing the images
		teamImageRefreshing = true;
	
		// shrink all pics
		$("#team-images a").animate({width:56,marginTop:20},300,function() {
			// remove "selected" class
			$("#team-images a").removeClass("selected");				
		});
	
		// enlarge selected pic
		$("#team-images a").each(function(i,e) {
			
			// set a pic ID based on the subLabel
			var myID = "pic-" + subLabel;
			
			// if the id attribute matches our test value...
			if ($(e).attr("id") == myID) {
				
				// animate the picture to a larger size and shorter top margin
				$(e).animate({width:80,marginTop:4},350,function(){
					
					// add selected class
					$(this).addClass("selected");
					
					// tell other scripts we're done
					teamImageRefreshing = false;
					
				});
				
				// set the new x value of the image bar
				var myX = i * 57 - 56; // position * (width + margin of 1px) + (padding of image)
				
				// animate the image bar into the right position
				$(this).parent("div").animate({marginLeft:-(myX)});				
				
			}
			
		});
		
	}
	
}


// onBioChange
// this function is run each time content is loaded from the team navigation links
function onBioChange() {
	
	// team ajax navigation
	$("a.team-nav").teamNav();

	// normal ajax navigation
	$("a.ajax-nav").ajaxNav();
	
	// reset pagination
	$("div.page").pagination();
	
	// reset navigating flag
	navigating = false;

}

// setBio
// this function fades out the current bio, retrieves the new one based on the label passed, and fades in the new content
function setBio(newSubLabel) {
	
	// team image refresh
	teamImageRefresh();

	// fade out pagination
	$("#pagination").fadeOut();
	
	// check for no-image label
	if (newSubLabel == "the-working-principal") $("#team-images-holder").fadeOut(300);

	// set content
	$("#team-content").fadeOut(300,function() {
	
		// retrieve new content
		$.get("b/" + newSubLabel + "/",function(data){ 

			// set the new content HTML
			$("#team-content").html(data);
			
			// fade in
			$("#team-content").fadeIn(300);
			
			// fade in pics
			if (newSubLabel != "the-working-principal") $("#team-images-holder").fadeIn(300);
			
			// loop through each team member link 
			$("#team-nav a").each(function(i,e) {
				
				// check label for current element
				var testLabel = $(e).attr("href").split("|")[1];
				
				// ie fix
				// if (testLabel.indexOf("/") > -1) testLabel = testLabel.substring(testLabel.lastIndexOf("/")+1,testLabel.length);
				
				// if this is the correct label, set selected
				if (testLabel == newSubLabel) $(e).addClass("selected");
				
			});

			// run onBioChange
			onBioChange();

		});

	});
	
	
}

// teamNavPrevious
// this function decrements the navigation of team members by click of left arrow
$.fn.teamNavPrevious = function() {
	
	// bind click action to this element
	$(this).unbind();
	$(this).bind("click",function(e) {

		// prevent default click action
		e.preventDefault();

		// only actuate if we're NOT already navigating
		if (!navigating) {
		
			// tell the scripts that we're navigating
			navigating = true;
			
			// tell the scripts that we're refreshing the team image
			teamImageRefreshing = false;

			// de-select all team member links
			$("#team-nav a").removeClass("selected");
	
			// defaults
			var newSubLabel = "";
			var done = false;
	
			// find the previous url
			$("#team-nav a").each(function(i,e){
					
				// check the label of current element		
				var testLabel = $(e).attr("href").split("|")[1];
				
				// ie fix
				// if (testLabel.indexOf("/") > -1) testLabel = testLabel.substring(testLabel.lastIndexOf("/")+1,testLabel.length);
			
				// is this the first team member and the current label?
				if (i == 1 && subLabel == testLabel) {

					// get last entry for "previous" navigation
					newSubLabel = $("#team-nav a:last").attr("href").split("|")[1];
					
					// flag for being done this search
					done = true;

				}
	
				// if we're not done and we're not on the non-member link
				if (!done && i != 0) {
					
					// if we find the label, set the flag
					if (subLabel == testLabel) {
						done = true;
					// otherwise, let's keep resetting our new label
					} else {
						newSubLabel = $(e).attr("href").split("|")[1];
					}
				}
			
			});

			// ie fix
			if (newSubLabel.indexOf("/") > -1) newSubLabel = newSubLabel.substring(newSubLabel.lastIndexOf("/")+1,newSubLabel.length);

			// fix the url for future bookmarking
			location.href = siteURL + "/p/#about-us-team|" + newSubLabel;
			
			// set parts and pull new label/subLabel
			parts = "about-us-team|" + newSubLabel;
			pullParts();
			
			// set new bio content
			setBio(newSubLabel);

		
		}
	
	
	});
	
}


// teamNavNext
// this function increments the navigation of team members by click of right arrow
$.fn.teamNavNext = function() {
	
	// bind the click action to this element
	$(this).unbind();
	$(this).bind("click",function(e) {

		// prevent default click action
		e.preventDefault();

		// only do the following if we're NOT already navigation
		if (!navigating) {
		
			// tell other scripts we're now navigating
			navigating = true;
			
			// tell other scripts we're now refreshing the team images
			teamImageRefreshing = false;

			// de-select all team member links
			$("#team-nav a").removeClass("selected");
	
			// defaults
			var newSubLabel = "lou-switzer";
			var done = false;
			var next = false;

			// find the next url
			$("#team-nav a").each(function(i,e){

				// get the label
				var testLabel = $(e).attr("href").split("|")[1];
				
				// ie fix
				// if (testLabel.indexOf("/") > -1) testLabel = testLabel.substring(testLabel.lastIndexOf("/")+1,testLabel.length);
		
				// if we're not done and the "next element" is the element we need, get the label
				if (!done && next) {
					newSubLabel = testLabel;
					done = true;
				}
		
				// if THIS element is the current label, then the "next element" will be the element we need
				if (testLabel == subLabel) {
					next = true;
				}
			
			});
		
			// fix the url for future bookmarking
			location.href = siteURL + "/p/#about-us-team|" + newSubLabel;
			
			// set parts and retrieve label/sublabel
			parts = "about-us-team|" + newSubLabel;
			pullParts();

			// set the new bio content
			setBio(newSubLabel);
		
		}

	});	
	
}


// teamNav
// this function sets up all of the persistent team navigation items and any incidental inline team navigation
$.fn.teamNav = function() {

	// bind a click event to all team navs
	$(this).unbind();
	$(this).bind("click",function(e) { 
		
		// prevent default click action	
		e.preventDefault();

		// don't do anything unless we're NOT already navigating
		if (!navigating) {
		
			// flag to tell the scripts we're now navigation
			navigating = true;
			
			// flag to tell the scripts we're setting the team images
			teamImageRefreshing = false;
		
			// de-select all team member links
			$("#team-nav a").removeClass("selected");
			
			// get the label (after the #)
			url = $(this).attr("href").split("|")[1];
			
			// fix for ie
			// if (url.indexOf("/") > -1) url = url.substring(url.lastIndexOf("/")+1,url.length);
		
			// copy "this" to local scope
			var myNav = this;
		
			// is this the first item? no pic on the first item
			if ($("#team-nav a")[0] == myNav) $("#team-images-holder").fadeOut(300);

			// fix the url for future bookmarking
			location.href = siteURL + "/p/#about-us-team|" + url;

			// set parts and then set label/subLabel
			parts = "about-us-team|" + url;
			pullParts();

			// set the new bio content
			setBio(url);

		}
		
	});
	
}

