$(document).ready(function() {
	SetExternalLinks();
	if( $(".profile-list").width() != null){
		InitPeopleRotator();
	}
	
});

// External Link - New Window ============================================================
function SetExternalLinks(){
	$('a[rel*=external]').click(function() { window.open(this.href); return false; });
}

// People Rotator ========================================================================

function InitPeopleRotator() {

	if ($('.profile-list .list li').length > 6) {
		$(".profile-list a.previous").css({ display: "block" });
		$(".profile-list a.next").css({ display: "block" });
		$(".profile-list .cover-left").css({ display: "block" });
		$(".profile-list .cover-right").css({ display: "block" });
	}
	$(".profile-list ul li").css({ width: "98px" });

	var itemWidth = 98;
	var fullWidth = 0;
	var visibleWidth = 588;
	var highest = 0;
	var coverExtra = 40;
	$(".profile-list li").each(
		function() {
			fullWidth += $(this).width();
			if ($(this).height() > highest) {
				highest = $(this).height();
			}
		}
	);

	$(".profile-list ul").width(fullWidth);

	if ($('.profile-list .list li').length < 6) {
		$(".profile-list ul").css({ 'margin': 'auto' });
	} else {

		$(".profile-list .cover-left").height(highest + coverExtra);
		$(".profile-list .cover-right").height(highest + coverExtra);

		var initMargin = $(".profile-list ul").css("margin-left");
		initMargin = parseFloat(initMargin.split("px"));

		var offSet = $('.profile-list ul').offset().left;
		var offSetLeft = $('.profile-list ul a.selected').offset().left;

		var offSetMoveTo = (offSetLeft - offSet);

		if (fullWidth < (visibleWidth + offSetMoveTo)) {
			offSetMoveTo = fullWidth - visibleWidth;
		}

		$('.profile-list .list ul').animate({ marginLeft: '-' + offSetMoveTo + 'px' }, 100);

		$(".profile-list a.next").click(
		function() {
			var theMargin = $(".profile-list .list ul").css("margin-left").split("px");
			theMargin = parseFloat(theMargin);
			var moveTo = theMargin - itemWidth;
			var maxNegMargin = initMargin - $(".profile-list .list ul").width() + visibleWidth;
			if (moveTo < maxNegMargin) {
				// extent reached
			} else {
				$(".profile-list .list ul").animate({ marginLeft: moveTo }, 50);
			}
			return false;
		}
	);

		$(".profile-list a.previous").click(
		function() {
			var theMargin = $(".profile-list .list ul").css("margin-left").split("px");
			theMargin = parseFloat(theMargin);
			var moveTo = theMargin + 98;
			if (moveTo > initMargin) {
				// extent reached
			} else {
				$(".profile-list .list ul").animate({ marginLeft: moveTo }, 50);
			}
			return false;
		}
	);
	}
}
