/* jQuery and other javascript functions */

var gPublisherVars = {
	pubName: 'BMJ'
};
 
$(document).ready(function() {
	
	var allCits = $("li.cit");
	var allpapCits = $("div.cit");
	if (!(allCits.length)) {
	    allCits = allpapCits;
	}
				
	if (allCits.length) {
		allCits.each(
			function(i) {
				var $thisCit = allCits.eq(i);
				var openAccessArticle = $thisCit.hasClass("openaccess"); 
				var creativeCommonsArticle = $thisCit.hasClass("creative-commons"); 
				var contentID = $thisCit.find("div.cit-metadata span.cit-doi").eq(0).text();
				if (!(contentID.length)) {
				    contentID = $thisCit.find("div.cit-metadata span.cit-pages").eq(0).text();
				}
				if (contentID.length && (contentID.length > 0)) {
					$thisCit.find("ul.cit-views").append('<li class="rlink"><span class="cit-sep">&#91;</span><a href="#" class="req-permissions-js" rel="permissions">Request permissions</a><span class="cit-sep">&#93;</span></li>');
					var newLink = $thisCit.find("a.req-permissions-js");
					if (newLink.length) {
						var title = $thisCit.find("div.cit-metadata h3");
						if (!(title.length)) {
							title = $thisCit.find("div.cit-metadata h4");
						}
						if (!(title.length)) {
							title = $thisCit.find("div.cit-metadata span.cit-title");
						}
						if (title.length) {
							title = title.eq(0).text().replace(/\s\s+/g," ");
						}
						else {
							title = '';
						}
						var date = $thisCit.find("div.cit-metadata span.cit-print-date").eq(0).text();
						if(!(date.length)){
							date = $thisCit.find("div.cit-metadata span.cit-ahead-of-print-date").eq(0).text();
							date = date.replace(/^Published Online First: /,"");
						}
						var authList = $thisCit.find("div.cit-metadata .cit-auth-list .cit-auth");
						var authStr = '';
						if (authList.length) {
							var authListArray = new Array(authList.length);
							authList.each(
								function(i) {
									authListArray[i] = (authList.eq(i).text());
								}
							);
							authStr = authListArray.join(', ');
						}
						var startpage = $thisCit.find("div.cit-metadata span.cit-pages-fpage").eq(0).text();
															
						contentID = contentID.replace(/^\s*[Dd][Oo][Ii]:\s*/,'');
						
						if(!(openAccessArticle || openAccessArticle)){			
							newLink.click(
								function(e) {
									RightslinkPopUp(title, date, authStr, startpage, contentID);
									e.preventDefault();
								}
							);
						}
						else{
							newLink.click(
								function(e) {
									RightslinkPopUp_oa(title, date, authStr, startpage, contentID);
									e.preventDefault();
								}
							);
						}
					}
				}

                                // this adds a FREE label to all Editor's Choice articles for all bmj-js but not bmj
                                if (gJournalVars.jnlID != "bmj") {
                                    $thisCit.find("span.editors-choice").append('<span class="free-editors-choice">FREE</span>');
                                }
			}
		);
	}

});



function RightslinkPopUp( title, date, author, startpage, contentID) {

	var url = "https://s100.copyright.com/AppDispatchServlet";
	var location = url
		+ "?publisherName=" + encodeURIComponent(gPublisherVars.pubName)
		+ "&publication=" + encodeURIComponent(gJournalVars.jnlID)
		+ "&title=" + encodeURIComponent(title)
		+ "&publicationDate=" + encodeURIComponent(date)
		+ "&author=" + encodeURIComponent(author)
		+ "&contentID=" + encodeURIComponent(contentID);
		
		location = location
	        + "&volumeNum=" + encodeURIComponent('')
		+ "&issueNum=" + encodeURIComponent('')
	        + "&startPage=" + encodeURIComponent(startpage)
	        + "&endPage=" + encodeURIComponent('')
	        + "&copyright=" + encodeURIComponent(gJournalVars.copy);
	        + "&orderBeanReset=true"; 
		
	PopUp = window.open(
		location, 'Rightslink', 'location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=650,height=550'
	);
}
function RightslinkPopUp_oa( title, date, author, startpage, contentID) {

	var url = "https://s100.copyright.com/AppDispatchServlet";
	var location = url
		+ "?publisherName=" + encodeURIComponent(gPublisherVars.pubName)
		+ "&publication=" + encodeURIComponent(gJournalVars.jnlID)
		+ "&title=" + encodeURIComponent(title)
		+ "&publicationDate=" + encodeURIComponent(date)
		+ "&author=" + encodeURIComponent(author)
		+ "&contentID=" + encodeURIComponent(contentID);

		location = location
		+ "&volumeNum=" + encodeURIComponent('')
		+ "&issueNum=" + encodeURIComponent('')
		+ "&startPage=" + encodeURIComponent(startpage)
		+ "&endPage=" + encodeURIComponent('')
		+ "&copyright=" + encodeURIComponent(gJournalVars.copy);

		location = location
		+ "&oa=y"
		+ "&orderBeanReset=true";

		PopUp = window.open(
		location, 'Rightslink', 'location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=650,height=550'
		);
}

