/** onLoad */
jQuery(document).ready(function() {
	// h: prevent overflow:hidden from obscuring long mathml images 
	jQuery('.full-text').parents('#content').css('overflow','visible');	
	// h: move right hand column into position on fulltext 
	jQuery('#navbox').insertBefore('.article');
	// h: minor xsl changes to accomodate css styles
	jQuery('span.articletype').wrap(document.createElement('h2'));
	jQuery('.message').addClass('fig'); 	
	
	// display pop-up of abbreviated titles on left hand column
	outlineAbbreviations();
		
	// author affiliations toggle
	initAffiliations();
	
	// Transforms the "post this" list on the left nav to a "share this" button (icon list on the right)
	initShareThis();

});

/** functions */
function initShareThis () {
	jQuery('.iconlist').attr("id", "sharethis").hide();//add the id to apply the css style attached and hides the box
	jQuery('.iconlist li:even').addClass('left');
	jQuery('.postto_title').replaceWith('<a href="#" class="sharethis_title">Share this article</a>');
	jQuery('.sharethis_title')
		.mouseover(function() {$('#sharethis').stop(true, true).fadeIn();})//shows the box on link hover
		.click(function(){return false;})//disable the link
	jQuery('#sharethis').hover(function() {$(this).stop(true, true).show();},function() {$('#sharethis').stop(true, true).animate({opacity: 1.0}, 1000).fadeOut("slow");});//fades out after 1s out of the box
	//push down the bottom ad when the central bloc (content) is too short
	contentOffset = jQuery('.article').height() - (jQuery('#navbox').height() + jQuery('#sharethis').height());
	if(contentOffset < 0) {
		jQuery('#content').css({'padding-bottom':jQuery('#sharethis').height() + 'px'});
	}
}

function initAffiliations() {
	// Auto-collapse Author affiliations on full-text page
	jQuery('.article #ins_container').hide();
	jQuery('.full-text #ins_container').show();
	jQuery('.full-text .affiliations-toggle i').addClass('active');
	
	jQuery('.affiliations-toggle').click(toggleAffiliations);
	
	jQuery('p.authors sup a').click( function(e) {
		e.preventDefault();
		if($('#ins_container').is(":hidden")) toggleAffiliations();
		var ind = $(this).text();
		jQuery("#ins_container sup").parent().css("background-color","").eq(ind-1).css("background-color","lightGrey");
		//jQuery('html, body').animate({scrollTop: $("#ins"+ind ).offset().top}, 500);
	});

}

function toggleAffiliations() {
		jQuery('#ins_container').slideToggle('fast');
		jQuery('.affiliations-toggle i').toggleClass('active');//arrow
}

function outlineAbbreviations() {
	window.onscroll = document.documentElement.onscroll = setMenuOffset;
	
	// full text long titles popup
	jQuery('.long a').each(function() {
		// capture abbreviated title in memory
		var abbreviatedText = jQuery(this).text();
		// replace abbreviated title with full title
		jQuery(this).mouseover(function() {
			jQuery(this).removeClass("clipped").addClass("full");
			jQuery(this).text($(this).attr('title'));
		});
		// replace full title with abbreviated title
		jQuery(this).mouseout(function() {
			jQuery(this).removeClass("full").addClass("clipped");
			jQuery(this).text(abbreviatedText);
		});
	}); 
}

// make the outline box to be fixed while browsing the content
function setMenuOffset() {
	var scroll = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop);//scroll position (cross-browser)
	var leftNavBox = jQuery(".outline-wrapper");//box to move
	var leftNavTop = scroll + 20;//20 = leftnav's css "top" property
	var leftNavBottom = leftNavTop + leftNavBox.height();
	var contentTop = jQuery(".full-text").offset().top;//vertical position FROM which the position will be fixed
	var contentBottom = contentTop + jQuery(".full-text").height();//vertical position TO which the position will be fixed
	
	if (leftNavTop > contentTop)
		if (leftNavBottom > contentBottom) {
			var hackTop = contentBottom - leftNavBox.height();
			leftNavBox.css({'position':'absolute','top':hackTop + 'px'});
		}
		else
			leftNavBox.css({'position':'fixed','top':'20px'});
	else
		leftNavBox.css({'position':'absolute','top':''});
}

function returnToText(){
		if (window.location.hash) {
			window.history.back();
		}
		else {
			scrollTo(0, 0);
		}
	}

function totext() {
	document.write('<a href="javascript:returnToText();">Return to text</a>');
}

function loadInParent(link) {
	window.open(link);
}

/* dummy functions for xsl */ 
function initializeOutline() {}
function outlineok() {}
function outlineover() {}
function outlineout() {}


