var highlights = [],
    index = 0,
    height = 130,
    delay = 6000,
    suunta = 1,
    interval,
    canClick = true;

$(document).ready(function(){     

	/* Get highlight images and links */
	highlights = $('#scroller .scroller_container').children('div');
	
	/* Animate, if there is more than three highlight */
	if (highlights.length > 3)
	{
		$('#scroller .previous_item').css("display", "block");
		$('#scroller .next_item').css("display", "block");
		
		$('#scroller .previous_item').click(function()
		{
			if ( canClick )
			{
				clearTimeout(interval);
				suunta = -1;
				loopHighlights();
			}
		});
		
		$('#scroller .next_item').click(function()
		{
			if ( canClick )
			{
				clearTimeout(interval);
				suunta = 1;
				loopHighlights();
			}
		});
		
		/* Clone first three highlights as the last highlights */
		$('#scroller .scroller_container').append( highlights.slice(0,3).clone() );
		
		/* Get updated highlights and remove all other elements */
		highlights = $('#scroller .scroller_container').children('div');
		$('#scroller .scroller_container').html(highlights);
				
		/* Start animation */
		interval = setTimeout( loopHighlights, delay );
	}

        /* Initialize FAQ */
        if ($('.page-id-32').length) {
            $('h5')
                .css('cursor', 'pointer')
                .each(function () {
                    var content = $(this).nextUntil('h1,h2,h3,h4,h5,h6');
                    content.wrapAll('<div class="faq-content" />');
                    $(this).next()
                        .height(content.height() + 10)
                        .hide();
                })
                .click(function () {
                    $(this).next().slideToggle();
                });
        }

        /* Center subnavigation */
	if ( $( '.page-item-5.current_page_item' ).length > 0 || $( '.page-item-5.current_page_ancestor' ).length > 0 )
	{
		var submenuElementsWidth = 0;
		$( '#submenu .page_item' ).each( function(){ submenuElementsWidth += $( this ).outerWidth( true ); } );
		var newPosition = ( $( '.page-item-5' ).position().left + ( $( '.page-item-5' ).outerWidth() - 6 ) / 2 ) - submenuElementsWidth / 2;
		
		$( '#submenu' ).css( 'left', newPosition + 'px' );
	}
	
	if ( $( '.page-item-7.current_page_item' ).length > 0 || $( '.page-item-7.current_page_ancestor' ).length > 0 )
	{
		var submenuElementsWidth = 0;
		$( '#submenu .page_item' ).each( function(){ submenuElementsWidth += $( this ).outerWidth( true ); } );
		
		if ( submenuElementsWidth < 720 )
		{
			var newPosition = ( $( '.page-item-7' ).position().left + ( $( '.page-item-7' ).outerWidth() - 6 ) / 2 ) - submenuElementsWidth / 2;			
			$( '#submenu' ).css( 'left', newPosition + 'px' );
		}
	}
});

function loopHighlights ()
{
	/*Disable link operations*/
	canClick = false;
	
	//suunta = suunta || 1;
	index = index + suunta;
	
	/* Loops index between 0 and the amount of highlights */
	if( index + 2 >= ( highlights.length ) )
	{
		index = 1;
		$('#scroller .scroller_container').css({ top: '0px' });
	}
	else if ( index < 0 )
	{
		index = highlights.length -4;
		$('#scroller .scroller_container').css({ top: '-' + ((highlights.length -3) * height) + 'px' }); 
	}
	
	
	/* Do the slide animation */
	$('#scroller .scroller_container').animate({ top: '-' + (index * height) + 'px' } , 500, function(){ canClick = true; });
	suunta = 1;
	interval = setTimeout(loopHighlights, delay);
}


