/** functions */


function mathmlLightBox() {
	var resize = false;
	var largestEquationWidth;
	var padding = 30;
	var innerColWidth = $('div.full-text').width() - padding;

	function getLargestEquation() {
		var arr=[];
		var largest;
		var images = jQuery('.mathimg'); 
		
		for(var i = 0; i < images.length; i++ ){
			arr[i]=images[i].width	
		}
		largest = Math.max.apply(Math, arr);
		//console.log("largestEquationWidth: "+largest);
		return largest;
	}
	
	largestEquationWidth = getLargestEquation();
			
	if (largestEquationWidth > innerColWidth){
		var stretchFactor = largestEquationWidth - innerColWidth;
		var newDivContentWidth = $('#content').width() + stretchFactor ;
		var newInnerColWidth = $('div.full-text').width()+ stretchFactor;
		stretchOrResize(stretchFactor,newDivContentWidth,newInnerColWidth)
	}

	function stretchOrResize(stretchFactor,newDivContentWidth,newInnerColWidth){
		var screenWidth,returnValue;
		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) {
			screenWidth = $(document).width();
		}else{
			screenWidth = screen.width;
		}

		//alert(" originalInnerColWidth: "+innerColWidth+"\r\n LargestEquWidth: "+largestEquationWidth+"\r\n\r\n We need to stretchBy: "+stretchFactor+"\r\n\r\n newInnerColWidth becomes: "+newInnerColWidth+"\r\n\r\n contentWidth will stretch too"+" \r\n\r\n originalContentWidth: "+$('#content').width()+"\r\n newContentWidth: "+newDivContentWidth+"\r\n\r\n newContentWidth + padding: "+(newDivContentWidth+padding) +"\r\n screenRes: "+screenWidth+" \r\n\r\n maximumInnerWidth this screen can handle : "+ (newInnerColWidth - ((newDivContentWidth + padding) - screenWidth)));
		if ((newDivContentWidth + padding) > screenWidth){
			stretchAndResize(newDivContentWidth,screenWidth,newInnerColWidth);
			//console.log("EquationTooLarge, stretchAndResize");
			resize = true;
		}else{
			//console.log("EquationFits, stretch");
			stretchContent(newDivContentWidth,newInnerColWidth);
		}
	}
	function stretchAndResize(newDivContentWidth,screenWidth,newInnerColWidth){
		var extraScreenWidth = (newDivContentWidth + padding) - screenWidth;
		
		$('#content').width(screenWidth - padding);
		$('div.full-text').width(newInnerColWidth - extraScreenWidth);
		
	}
	function stretchContent(newDivContentWidth,newInnerColWidth){
		$('#content').width(newDivContentWidth);
		$('div.full-text').width(newInnerColWidth);
	}
	
jQuery('.mathimg').each(function() {
  
    var originalEvent = jQuery(this).parent().attr('href'), originalWidth = jQuery(this).width();
	// manipulate <a>'s behaviour
   if(originalWidth > 100){ 
		jQuery(this).parent().attr('onclick', '');
		jQuery(this).parent().bind('click', function(e) { e.preventDefault(); });
		jQuery(this).parents('a').attr('href',$(this).attr('src')).attr('class','group').attr('title','');
		
		// adding tooltip on mouseover
		if(jQuery(this).parent().is("a[class='group']")){
			jQuery(this).mouseover(function() {
			jQuery(this).attr('title','Click to Enlarge');
			});
			// Onlick remove title
			jQuery(this).click(function() {
			jQuery(this).attr('title','');
			});
		}
	}
	// resize large image and add toggle
	if (resize) {
		var resizeFactor;
		padding = 30;
		
		if($('div.full-text').width()/largestEquationWidth > 0.8){
			resizeFactor = 0.8;
		}else{
			resizeFactor = $('div.full-text').width()/largestEquationWidth;
		}
		//console.log("resizeFactor: "+resizeFactor);
		jQuery(this).css('width',(originalWidth * resizeFactor) - padding);
	}

});	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))){
	//do not activate lightBox effect for Iphone or Ipad
	}else{
        // activate lightbox effect
        $("a.group").attr('title','');
        jQuery("a.group").fancybox({
                hideOnContentClick : true,
                centerOnScroll: true

        });
    }
}
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('#article-navigation-bar').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>');
}


jQuery(function(){
	jQuery().zoom(function(){
		if(jQuery.browser.msie && jQuery.browser.version == "7.0") {
			if(window.screen.deviceXDPI > 129) {
				jQuery(".outline-wrapper").css('display','none');
			} else {
				jQuery(".outline-wrapper").css('display','block');
			}
		}
	});
});

/* dummy functions for xsl */
function initializeOutline() {}
function outlineok() {}
function outlineover() {}
function outlineout() {}

/** onLoad */
jQuery(document).ready(function() {
	// h: minor xsl changes to accomodate css styles
	// jQuery('span.articletype').wrap(document.createElement('h2')); vm not xsl
		
	if(jQuery(".full-text").offset()) { outlineAbbreviations(); }

	if(jQuery('.article').offset()) { initAffiliations(); }
	
	if (jQuery('#sharethis').offset()) { initShareThis(); }
	
	if (!($.browser.webkit)){
		if (jQuery('.mathimg').offset()) { mathmlLightBox();}
	}
		
});

// webkit cannot handle document.ready with width() or height()
 jQuery(window).load(function() {
	if ($.browser.webkit) {
		if (jQuery('.mathimg').offset()) { mathmlLightBox();}
	}
 })


