/** Utility functions */

/** on Load */
jQuery(document).ready(function() {
		toggleList();
		hideAdverts();
});	


/** Functions */

// collapsing list utility 
// toggle visibility of element that is a sibling of an element with class .collapser
function toggleList() {
	jQuery('.collapser').siblings('ul').addClass('hide'); // show list if js is disabled
	jQuery('.collapser').click(function() {
		jQuery(this).siblings('ul').toggleClass('hide'); 
		jQuery(this).children('i').toggleClass('active');
	});
}

// Remove advert block when there is no ad inside 
function hideAdverts() {
	jQuery('.google-ad,.skyscraper,.advert').each(function() {
	   if (jQuery(this).children('dd').height() < 20) {
			if(jQuery.browser.msie) {
				jQuery(this).css('visibility','hidden'); 
			} else {
				jQuery(this).css('display','none');
			}
		}
	});
}   

// If there is an element with id "scroll-here" than it will be focused
jQuery(document).ready(function() {
	if(jQuery('#scroll-here').length == 1) {
		window.location = "#scroll-here";
	}
});

// Post Comment page manipulation
function postCommentPage() {
	// Move navbox out of main content
	jQuery('#navbox').insertBefore('.post-comment');
}

jQuery(document).ready(postCommentPage);

// Format the right navigation bar

jQuery(document).ready(
	function() {
		var boxSelectors = ['.citation', '.article', '.comments', '.email2friend', '.articlestats', '.prepublication'];
		for(var i = 0; i < boxSelectors.length; i++) {
			if(jQuery(boxSelectors[i]).length) {
				jQuery('#navbox').insertBefore(boxSelectors[i]);
			}
		}
	}
);

// Article navigation bar
function toggleauthors(auth,button)
{
	try{
	var aut= document.getElementById(auth);
	var but = document.getElementById(button);

	if(aut == null || but == null) { return; }

	if (aut.style.display != "none")
	{
	    aut.style.display = "none";
	    but.src = "/images/biomed/grey-arrow-right.png";
		jQuery('#' + button).parent().css('background-image', 'none');
	}
	else
	{
		aut.style.display = "block";
	 	but.src = "/images/biomed/grey-arrow-down.png";
	}
	}
	catch(e){}
}

function prepareArticleMetadata() {
	toggleauthors('authpm','authbuttonpm');
	toggleauthors('authg','authbuttong');
	toggleauthors('authos','authbuttonos');
	toggleauthors('autha','authbuttona');
}
var popup_window = 0;
function popup(url,n,w,h) {
	if (!popup_window.top) popup_window = window.open(url,n,'width='+w+',height='+h+',resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
	popup_window.focus();
}

// Collapse authors
jQuery(document).ready(prepareArticleMetadata);


/* JSON stuff */

jQuery(document).ready(function() {

	jQuery('.jsonloader').click(function(event) {
		linkId = jQuery(this).attr('id'); /* global variable */

		event.preventDefault();

		var webApiUrl = site.abbreviation + "/webapi/1.0/" + linkId + ".json";

		var options = {
			type: "GET",
			url: webApiUrl,
			dataType: "json",
			timeout: 6000, // 6 seconds
			beforeSend: function (XMLHttpRequest) {
				startAjaxSpinner();
			},
			success : function(data, textStatus) {
				displayRss();
				updateArticleList(linkId, data);
				makeTabCurrent(linkId);
				stopAjaxSpinner();
				
			},
			error : function (XMLHttpRequest, textStatus, errorThrown) {
				jQuery('.ajax-error').slideDown("fast").fadeOut(6000);
			},
			complete: function (XMLHttpRequest, textStatus) {
				stopAjaxSpinner();
			}
		}
		jQuery.ajax(options);
	});

	function displayRss() {
		jQuery('ul#article-pane-nav li').find('a.rss').hide();
		jQuery('#'+linkId).siblings().children('a.rss').show();
	}

	function startAjaxSpinner(){
		//jQuery('.ajax-spinner').html("<img src=\"/images/icons/ajax-loader.gif\" alt=\"\" />").show();
	}

	function stopAjaxSpinner(){
		//jQuery('.ajax-spinner').hide();
	}

	function makeTabCurrent(linkId){
		jQuery('.article-panel-tabs').removeClass('current');
		jQuery('#'+linkId).parent().addClass('current');
	}

	function updateArticleList(linkId, data){
		jQuery('#articles').html(tmpl(linkId + "_template", data));
	}

});

/* Advanced Search */
jQuery(function(){
var optionsToggler=jQuery('p.options a.options-toggler');

optionsToggler.click(
	function(){
	jQuery(this).add(jQuery("i", this)).toggleClass('active');
});			

});

//* IE 6 and 7 input:focus */
jQuery(document).ready(function() {
	jQuery('input').focus(function() {
		jQuery(this).css('color','#000000');
	});
});

	
/* until the xsl is updated we need this line to replace h3 with h1 inside topmatter on article type pages */
jQuery(document).ready(function() {
	jQuery('#topmatter h3').each(function(){$(this).replaceWith("<h1>" + $(this).html() + "</h1>");});
});