// SOME NICE FUNCTIONS WE NEED LATER

function fDiv(n1, n2) {
	return Math.floor( n1/n2 );
}


// LET'S GET READY TO RUMBLE

// check if the document is ready

$(document).ready(function() {


// THE MENU

// undo CSS styling
$('#top-navigation li').css('paddingRight', '0px');

// get the size of the menu elements

var TheNavWidth = $('#top-navigation').width();
var NavElements = $('#top-navigation li').size();
var NavElementsSize = DiffNavElements = ElementSpacer = 0;

// accumulate to total width

$('#top-navigation li').each(function() {
	NavElementsSize += $(this).outerWidth();
});

// perform some magic calculations

var DiffNavElements = TheNavWidth - NavElementsSize;
var ElementSpacer = fDiv(DiffNavElements, NavElements - 1) - 3;
ElementSpacer = ElementSpacer + "px";

// and distribute them properly over the total width

$('#top-navigation li').each(function(i) {
	if (i < (NavElements - 2)) {
		$(this).css('marginRight', ElementSpacer);
	} else if (i == (NavElements - 2)) {
		$(this).css('marginRight', '0px');
	} else if (i == (NavElements - 1)) {
		$(this).css('float', "right");
	}
});


// THE PORTFOLIO

// only proceed if on portfolio page

if ($('body').hasClass('page-id-9')) {

	// wooza, let's make the sub nav pretty

	/*

	// style up
	$('ul#portfolio_nav li').not(':first').css('borderLeft', '1px solid #999');
	$('ul#portfolio_nav li').not(':last').css('paddingRight', '10px');
	$('ul#portfolio_nav li:first').css('paddingLeft', '0px');
	$('#portfolio_nav').css('float', 'left')

	var MenuLeft = MenuItemLeft = MenuItemWidth = MenuWidth = CenterPoint = 0;

	// finding the position of the active main nav element
	$('#top-navigation li.current_page_item').each(function(){
		MenuLeft = $(this).parent().offset().left;
		MenuItemLeft = $(this).offset().left;
		MenuItemWidth = $(this).width();
		CenterPoint = MenuItemLeft - MenuLeft + (MenuItemWidth / 2);
	});

	// getting the width of the sub nav and magically center it
	$('#portfolio_nav li').each(function(){
		MenuWidth += $(this).outerWidth()
	});
	var SubNavDistance = CenterPoint - (MenuWidth / 2);
	$('#portfolio_nav').css({
		'paddingLeft': SubNavDistance
	});

	*/

}


// THE TEAM LIST

// only proceed if on team page

if ($('body').hasClass('page-id-11') || $('body').hasClass('page-id-642')) {

	// do some pre-styling

	$('#team_teaser').css({
		'width': '380px',
		'position': 'absolute',
		'right': 0,
		'top': '5px'
	});
	$('#content ul li span strong').css('display', 'inline')
	// $('#content ul').css({
	$('#content .page').css({
		'position': 'relative',
		'overflow': 'visible'
	});
	$('#content ul li strong').css({
		'cursor': 'pointer',
		'fontWeight': 'normal'
	});
	$('#content ul li span').each(function() {
		$(this).css({
			'height': $(this).height(),
			'position': 'absolute',
			'top': '0',
			'right': '0'
		}).hide();
	});

	// now for some magic: the click events

	$('#content ul li').click(function() {

		// "de-bold" the state of other elements and hide teaser and person description

		$('#team_teaser').slideUp('fast');

		// $(this).find…
		$('#content ul li').not(this).find('strong').css('fontWeight', 'normal');
		$('#content ul li').not(this).find('span').slideUp('fast');

		// check if the clicked element is active or not

		if (
			($(this).find('strong').css('fontWeight') == 'bold') ||
			($(this).find('strong').css('fontWeight') == 700)) { // Opera

			// it is active, collaps list, hide person description, "de-bold" and show teaser

			// $(this).parent().animate({
			// 	height: ($(this).height())*($(this).siblings().length+1) + 'px'
			// }, 'fast' );

			$(this).find('strong').css('fontWeight', 'normal');
			$(this).find('span').slideUp('fast');
			$('#team_teaser').slideDown();

		} else {

			// it is not active, "bold" it and show person descritopn

			// $(this).parent().animate({
			// 	height: $(this).find('span').css('height')
			// }, 'slow' );

			$(this).find('strong').css('fontWeight', 'bold');
			$(this).find('span').slideDown();

		}

	});

}


// THE END

});
