// JavaScript Document

$(document).ready( function() {

	// ======================================================================
	// Initially position the contacts
	// ======================================================================

	// Loop over all the horizontal carriers
	$('section.section_aside_contacts_container section.section_aside_contacts_hcarrier').each( function(i) {

		// These carriers will need a dynamically assigned id that corresponds to the thumbnail carrier
		
		$(this).children('article.article_aside_contact').each( function(ii) {

			$(this).css('left', ii * 390 + 'px');
		
		});
		
	});
	
	// And show the main contact container if there is only one
	if ($('section.section_aside_contacts_container section.section_aside_contacts_hcarrier').children('#section_aside_contacts_list').length > 0) {
		// $('.section_aside_contacts_container').css({'display': 'block'});
		// $('section.section_aside_contacts_container').slideUp();
	}
	
	
	
	/* 
	
	// And hide all the thumbs we don't need
	$('aside#aside_main section#section_aside_contacts_list section.section_aside_contacts_list_hcarrier').not(':first-child').slideUp();
	
	
	// Equalise the sizes of each thumbnail
	var nBiggestThumb = 0;
	
	$('article.article_aside_contact_small').each( function() {
		if ($(this).height() > nBiggestThumb) {
			nBiggestThumb = $(this).height();
		}
	});

	$('article.article_aside_contact_small').css('min-height', nBiggestThumb + 'px');
	
	*/

	// Equalise the sizes of each thumbnail
	equaliseHeights($('article.article_aside_contact_small'));
	
	//var nBiggestContact = 0;

    $('article.article_aside_contact').each( function() {
        if ($(this).height() > 170 && $(this).is(':first-child')) {
        	$( this ).parent().height($(this).height()+20) ;
		}
	});

	//$('section.section_aside_contacts_hcarrier').css('min-height', (nBiggestContact + 20) + 'px'); 
	
	
	
	// ======================================================================
	// Add click functionality to the thumbnail contacts
	// ======================================================================
	
	$('.section_aside_contacts_list_hcarrier').on('click', 'a.a_people_thumb', function(e) {
		
		// $('.section_aside_contacts_container').animate({ 'height': '180px' }, 500);
		
		$('.section_aside_contacts_container').slideDown();
		
		var thisButton = $(this);
		var nThisIteration = $(this).data('thisiteration');
		var nParentParentIteration = $(this).parent().parent().data('thisiteration');
		var prevParent = $( this ).parents().eq(3).find('section.section_aside_contacts_hcarrier');
        var prevArticleNo = nThisIteration+1;

        prevParent.height(prevParent.find('article:nth-of-type('+prevArticleNo+')').height()+20) ;
		
		// The thumbnail carrier will need a dynamically assigned id that corresponds with the main contact carrier 
		
//		$('section.section_aside_contacts_container section.section_aside_contacts_hcarrier').each( function(i) {
//			if (nParentParentIteration == $(this).data('thisiteration')) {
        $(this).parent().parent().parent().parent().children().children(".section_aside_contacts_hcarrier").stop().animate({
            'margin-left': ((nThisIteration * -390)) + 'px'
        }, 500);
//			}
//		});
		
		if ($(window).width() > 999) {
			$('body').animate({ scrollTop: '400px' }, 500);
		}
		
		e.preventDefault();
		
	});
	
	// -------------------------------------------------------------------------------------
	// Initial state of global media contacts pod
	// -------------------------------------------------------------------------------------
	
	$('.gmc_preselection').trigger('click');
	
	// -------------------------------------------------------------------------------------
	// Add a "View more people..." button under the first contact
	// -------------------------------------------------------------------------------------
	
	$('section.section_aside_contacts_container').after('<p class="p_show_more_people"><a class="arrow_right margin_left_10 margin_right_10" href="#">' + $('section.section_aside_contacts_container').attr('data-morebutton') + '</a></p>');
	$('p.p_show_more_people').hide();
	
	if ($('section#section_aside_contacts_list').length > 0 && $('section#section_aside_contacts_list').children('section.section_aside_contacts_list_hcarrier').length >= 1 && !$('.section_aside_contacts_container').hasClass('section_aside_contacts_globalmedia')) {
        $('p.p_show_more_people').each( function() {
            if ($(this).next('section#section_aside_contacts_list').length > 0) {
				$(this).show();
            }
        });
        // $('p.p_show_more_people').show();
		$('section#section_aside_contacts_list').slideUp();
		$('p.p_show_more_people a').click( function(e) {
			$(this).parent().next().slideToggle();
			$(this).parent().prev().children(".section_aside_contacts_hcarrier").stop().animate({
                'margin-left': '0px'
            }, 500);
			e.preventDefault();
		});
	}

	
	
	
	
});