$( document ).ready( function() {

	if( !$( 'BODY' ).hasClass( 'index' ) )
	{
		var vph = getVportHeight();
		var cnt = $( 'DIV#content' ).outerHeight();
		var stl = $( 'DIV#sitelinks' ).outerHeight();
		var diff = vph - cnt - stl - 411;

		if( $( 'DIV#hypebar' ).length )
			diff -= $( 'DIV#hypebar' ).outerHeight();
		
		if( diff > 0 )
			$( 'DIV#content' ).css( { minHeight: cnt + diff } );
	}

	$( 'UL.navi-main LI.level2 UL' ).remove();

	$( 'UL.navi-main LI.level1:nth-child(2) UL' )
		.attr( 'id', 'tuotepalvelu' );

	$( 'UL#tuotepalvelu LI' ).addClass( 'tp-tlink' );
	$( 'UL#tuotepalvelu LI:nth-child(1) A' ).prepend( '<img src="/_img/ui/tpmenu-tuoteperheet.gif" width="106" height="84" class="thumb" alt="" />' );
	$( 'UL#tuotepalvelu LI:nth-child(2) A' ).prepend( '<img src="/_img/ui/tpmenu-palvelut.gif" width="106" height="84" class="thumb" alt="" />' );
	$( 'UL#tuotepalvelu' ).prepend( '<li id="tp-text">' + $( 'DIV#tp-textblock' ).html() + '</li>' );

	$( 'UL.navi-main LI.level1' ).hover( 
		function() { $( this ).children( 'UL' ).css( { display: 'block' } ); },
		function() { $( this ).children( 'UL' ).css( { display: 'none' } ); }
	);
	
	$( 'H1#sltitle' ).click( function() {
	
		$( this ).toggleClass( 'open' );
		
		if( $( 'UL#sllist' ).hasClass( 'open' ) )
		{
			$( 'UL#sllist' ).slideUp( 400, function() {
				$( this ).removeClass( 'open' );
				$( 'DIV#sitelinks' ).css( { minHeight: 43 } );
			});
		}
		else
		{
			$( 'DIV#sitelinks' ).css( { minHeight: 44 } );
			var scrollElm = getScrollElm();
			$( scrollElm ).animate( { scrollTop: '-=1' }, 50, function() {

				$( 'UL#sllist' ).addClass( 'open' );
				$( 'UL#sllist' ).slideDown( 200, function() {
					var diff = $( 'UL#sllist' ).outerHeight();
					$( scrollElm ).animate( { scrollTop: '+=' + diff }, 300 );
				});

			});
		}
	
	});
	
	$( 'A.biomore' ).click( function() {

		var e = $( this );
		e.blur();
		
		if( e.parent( '.bioinfo' ).hasClass( 'open' ) )
		{
			e.parent( '.bioinfo' ).animate( { height: 17 }, 200, function() {
				e.parent( '.bioinfo' ).removeClass( 'open' );
				e.html( 'Lisätiedot &darr;' );
			});
		}
		else
		{
			e.parent( '.bioinfo' ).animate( { height: 100 }, 200, function() {
				e.parent( '.bioinfo' ).addClass( 'open' ).css( { height: 'auto' } );
				e.html( 'Lisätiedot &uarr;' );
			});
		}
		
		return false;

	}).html( 'Lisätiedot &darr;' );

});

function getVportHeight()
{
	return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
}

function getScrollElm()
{
	var scrollElement = 'HTML, BODY';

	$( 'HTML, BODY' ).each( function() {
		var initScrollTop = $( this ).attr( 'scrollTop' );
		$( this ).attr( 'scrollTop', initScrollTop + 1 );
		if( $( this ).attr( 'scrollTop' ) == initScrollTop + 1 )
		{
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
		else
		{
			$( this ).attr( 'scrollTop', initScrollTop - 1 );
			if( $( this ).attr( 'scrollTop' ) == initScrollTop - 1 )
			{
				scrollElement = this.nodeName.toLowerCase();
				$(this).attr('scrollTop', initScrollTop);
				return false;
			}
		}
	});
	return scrollElement;
}
