// The Team page -- modeled off the yearbook page
// September 10 2008 Adriano
/*
 * Member selected
 */
function setCurrentIndex(inIndex, event) {
	/* Animate the card front/back */
	merge = new Animator({
		transition: Animator.makeEaseIn(3),
		duration: 1000
	});
	
	merge
	.addSubject(new NumericalStyleSubject(
		$("cardFront"), 'left', 0, 131
	))
	.addSubject(new NumericalStyleSubject(
		$("cardBack"), 'left', 263, 131
	))
	.addSubject(new CSSStyleSubject(
		$("biophoto"), 'filter:alpha(opacity=100); opacity:1.0;', 'filter:alpha(opacity=0); opacity:0.0;'
	))
	;
	merge.seekTo(1);
	
	/* Allow the animation to complete before changing the components */
	setTimeout('showBio(' + inIndex + ')', 1000);
	
	/* Allow the original animation and the new components to load before 
	 * continuing the animation */
	setTimeout('merge.seekTo(0)', 1500);
	
	/* reset href if successful; if not successful, fallback to loading the 
	 * individual */
	Event.stop(event);
}

/*
 * Calculate nth year based on startdate
 */
function calcYear(startDate) {
	var now = new Date();
	var dp = startDate.split("-");
	var start = new Date();
	start.setFullYear(dp[0]);
	start.setMonth(dp[1] - 1);
	start.setDate(dp[2]);
	return Math.ceil((now - start)/(31556926000));
}

/*
 * Return the year of the startdate
 */
 function getYear(startDate) {
	 var dp = startDate.split("-");
	 return dp[0];
 }
 
 /*
 * Render the individual bio
 */
function showBio (i) {
	/* change photo and attributes */
	if (members[i]['jpg']) {
		document.images.biophoto.src = members[i]['jpg'];
		document.images.biophoto.style.display = 'block';
	} else {
		document.images.biophoto.style.display = 'none';
	}

//	try {
//		// image.alt is read-only in some DOM (i.e. Windows Mobile 5)
//		document.images.biophoto.alt = members[i]['name']; 
//	} catch(err) {
//	}
	document.images.biophoto.title = members[i]['name'];

	/* change the mini photos */
	for (j=1; j<=2; j++) {
		imgId = 'miniphoto' + j;
		img = $(imgId);
		img.src = members[i][imgId];
		img.style.display = 'block';
	}
	
	/* adjust the info panel */
	if (!members[i]['year']) {
		members[i]['year'] = getYear(members[i]['startdate']);
	}
	
	var columns = new Array('name', 'title', 'position', 'hometown', 'year', 'quotation', 'quotationauthor');
	for (j=0; j<columns.length; j++) {
		if (members[i][columns[j]]) {
			$(columns[j]).style.display = 'block';
			$(columns[j] + 'Value').innerHTML = members[i][columns[j]];
		} else {
			$(columns[j]).style.display = 'none';
			$(columns[j] + 'Value').innerHTML = '';
		}
	}

	/* Enable/disable the appropriate blocks */
	if (!memberSelected) {
		setClassStyle(".noMemberSelected", "display", "none");
		setClassStyle(".memberSelected", "display", "block");
		memberSelected = true;
	}

	/* Reset the text properties of the card to default */
	$('info').style.paddingTop = 0;
	$('info').style.fontSize = '13px';
	setClassStyle('.cardDetail', 'padding-bottom', '11px');

	/* Adjust the text properties of the card based on content size */
	var h=Math.max($('info').scrollHeight, $('info').offsetHeight);
	var mh=parseInt(getElementStyle('info', 'maxHeight', 'max-height'));
	if (h > mh) {
		// Content too large, reduce
		var resize = true;
		if (resize) {
			// First try adjusting the line spacing
			for (var i=10; i > 2; i--) {
				setClassStyle('.cardDetail', 'paddingBottom', i + 'px');
				h=Math.max($('info').scrollHeight, $('info').offsetHeight);
				if (h <= mh) {
					resize = false;
					break;
				}
			}
		}
		
		if (resize) {
			// Adjust fontsize
			var fs = parseInt(getElementStyle('info', 'fontSize', 'font-size'));
			for (var i=(fs-1); i > 8; i--) {
				$('info').style.fontSize = i + 'px';
				h=Math.max($('info').scrollHeight, $('info').offsetHeight);
				if (h <= mh) {
					resize = false;
					break;
				}
			}
		}
		// Content may now be too small so don't use ELSE
	}
	if (h < mh) {
		// Center div#info inside the td
		$('info').style.paddingTop = (mh-h)/2 + 'px';
	}
}

var memberSelected = false;

