// JavaScript Document
var highlights = [],
    index = 0,
    height = 63,
    delay = 2300,
    suunta = 1,
    interval,
    canClick = true;

$(document).ready(function(){     

	/* Get highlight images and links */
	highlights = $('#scroller .scroller_container').children('.scroller_image');
	
	/* Animate, if there is more than three highlight */
	if (highlights.length > 4)
	{
		$('#scroller .previous_item').show();
		$('#scroller .next_item').show();
		
		$('#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,4).clone() );
		
		/* Get updated highlights and remove all other elements */
		highlights = $('#scroller .scroller_container').children('.scroller_image');
		$('#scroller .scroller_container').html(highlights);
				
		/* Start animation */
		interval = setTimeout( loopHighlights, delay );
	}
});

function loopHighlights ()
{
	/*Disable link operations*/
	canClick = false;
	
	//suunta = suunta || 1;
	index = index + suunta;
	
	/* Loops index between 0 and the amount of highlights */
	if( index + 3 >= ( highlights.length ) )
	{
		index = 1;
		$('#scroller .scroller_container').css({ top: '0px' });
	}
	else if ( index < 0 )
	{
		index = highlights.length -5;
		$('#scroller .scroller_container').css({ top: '-' + ((highlights.length -4) * height) + 'px' }); 
	}
	
	
	/* Do the slide animation */
	$('#scroller .scroller_container').animate({ top: '-' + (index * height) + 'px' } , 500, function(){ canClick = true; });
	suunta = 1;
	interval = setTimeout(loopHighlights, delay);
}

function showRestOfTheMainArticle ()
{
	$( '#mainArticleShowButton' ).hide();
	$( '#mainArticleHideButton' ).fadeIn("slow");
	
	$( 'body' ).append( '<p style="visibility: hidden; position: absolute; left: 0; top: 2000px; width: 425px;" id="hiddenContent">' + $('#mainArticleContent').html() + $('#restOfTheText').html() + '</p>' );
	var newHeight = $( '#hiddenContent' ).height();
	$( '#hiddenContent' ).remove();
	
	$( '#mainArticleContent' ).height( newHeight );
	init_floatingBox();
	$( '#restOfTheText' ).fadeIn();
	
	/*$( '#mainArticleContent' ).animate( { height: newHeight + 'px' }, 500, 'linear', function() {
		$( '#restOfTheText' ).fadeIn();
		init_floatingBox();
	});*/
	
	return false;
}

function hideRestOfTheMainArticle ()
{
	
	$( '#mainArticleHideButton' ).fadeOut(200);
	
	//$( '#hiddenContent' ).fadeOut();
	//append( '<p style="visibility: hidden; position: absolute; left: 0; top: 2000px; width: 425px;" id="hiddenContent">' + $('#mainArticleContent').html() + $('#restOfTheText').html() + '</p>' );
	//var newHeight = $( '#hiddenContent' ).height();
	//$( '#hiddenContent' ).remove();
	
	$( '#mainArticleContent' ).css('height','auto' );
	$( '#restOfTheText' ).fadeOut(200,function() {
		$( '#mainArticleShowButton' ).show();
		init_floatingBox();
	});
	
	/*$( '#mainArticleContent' ).animate( { height: newHeight + 'px' }, 500, 'linear', function() {
		$( '#restOfTheText' ).fadeIn();
		init_floatingBox();
	});*/
	
	return false;
}