jQuery(function(){
	
	$('a.scrollData').click(function(){
		$($(this).attr('data-id')).click();
	});
	
	var sub = false;
	$('section.subStation > section.mainSubStation').each(function(){
		sub = true;
		$(this).hide();
    	var element = $($(this).prev()[0]);
    	element.addClass('subStation');
    	element.append(' <span class="a toggle"></span>');
       	element.click(function() {
			if ( $(this).hasClass('expanded') )
			{
				ClearAll();
			} else {
				
				// init current expanded item to correct scroll top
				var correctScroll = false;
				var $expanded =  $('h2.expanded').next();
				var expandedHeight = $expanded.height();
				
				if($expanded.length > 0 && $expanded.offset().top < $(this).offset().top) {
					correctScroll = true;
				}
				// collapse all
				ClearAll();

				// correct scroll top
				if(correctScroll) {
					$(window).scrollTop($(window).scrollTop() - expandedHeight);
				}
				
				// expand
				$(this).addClass('expanded');
				$(this).next().slideToggle(function() {
					$(this).parent().addClass('expanded');
				});
			}
		});
	});
	if ( sub )
	{
		$("#article p:last").addClass('last');
	}
});

function ClearAll()
{
	$('section.subStation > section.mainSubStation').hide();
	$('section.subStation').removeClass('expanded');
	$('h2').removeClass('expanded');			
}
