/* jQuery and other javascript functions */

var gPublisherVars = {
	pubName: 'BMJ'
};

$(document).ready(function() {

	var allCits = $("li.cit");
	if (allCits.length) {
		allCits.each(
			function(i) {
				var $thisCit = allCits.eq(i);
				var contentID = $thisCit.find("div.cit-metadata span.cit-doi").eq(0).text();
				if (contentID.length && (contentID.length > 0)) {
					$thisCit.find("ul.cit-views").append('<li><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 = title.eq(0).text().replace(/\s\s+/g," ");
						}
						else {
							title = '';
						}
						var date = $thisCit.find("div.cit-metadata span.cit-print-date").eq(0).text();
						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*/,'');
						newLink.click(
							function(e) {
								RightslinkPopUp(title, date, authStr, startpage, contentID);
								e.preventDefault();
							}
						);
					}
				}
			}
		);
	}

});



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'
	);
}

