$(document).ready(function() {
  // Add Slideshow behavior (jQuery Cycle Lite) to homepage
  $('#slider').cycle({
    fx: 'fade',
    next:   '#next-project', 
    prev:   '#prev-project',
    pause: false,
    pager: '#slider-nav'
	});	

	// Scroll back to top
  $.localScroll();

  // Locations footer, add "last" class to every 4th project
  $('.offices li:nth-child(4n)').addClass("last");

  // Locations overview, add "last" class to every 5th project
  $('.locations-nav li:nth-child(5n)').addClass("last");

  // Locations detail, add "last" class to every 4th person
  // And add clear class to every 5th person  
  $('.functions li:nth-child(4n)').addClass("last").next('li').addClass('first');

  if ($('.ie8, .ie7, .ie6').length){
	$('<li class="clear">&nbps;</li>').insertAfter($('.functions li:nth-child(4n)'));
  }

  // Clear every 3d li in contact form
  //$('fieldset li:nth-child(3n)').css("clear", "both");

	// if we're mobile, replace the nav with a dropdown
	if (screen.width < 480){
		// Create the dropdown base
		$("<select />").appendTo("header nav");

		// Create default option "Go to..."
		var gs = '';
		if (!$('header nav li.current').length){
			gs = 'selected';
		}
		$("<option />", {
		   "selected": gs,
		   "value"   : "",
		   "text"    : "Go to..."
		}).appendTo("header nav select");

		// Populate dropdown with menu items
		$("header nav a").each(function() {
			var el = $(this),
			sl = el.parent('li').hasClass('current') ? 'selected' : '';
			$("<option />", {
				"selected": sl,
				"value"   : el.attr("href"),
				"text"    : el.text()
			}).appendTo("header nav select");
		});
		
		// make it work :-)
		$("header nav select").change(function() {
		  window.location = $(this).find("option:selected").val();
		});
	}
	
	// same height for the service-nav
	function resizeServices(){
		var _highest = 0;
		$('.service-nav li p:not(.link)').css('height', 'auto');
		$('.service-nav li p:not(.link)').each(function(){
			if ($(this).height() > _highest){
				_highest = $(this).height()
			}
		});
		$('.service-nav li p:not(.link)').css('height', _highest + 'px');
	}
	
	if ($('.service-nav').length){
		resizeServices();
		$(window).resize(function(){
			console.log('resize services');
			resizeServices();
		});
	}

});


