/* 	Library Javascript - requires jQuery
		PE. 10 May 2009
*/	
	
$(document).ready(function() {
	// Add 'Page information' link
	$('#footer ul li:last').before('<li><a href="#" onclick="showMetaInfo();return false">Page information</a> |</li>');
	// GA event tracking for eLibrary links 
	// N.B. this will not include the library homepage which has separate code
	$('#mainNav a[href*=//elibrary.uwe.ac.uk]').click(function(){
		pageTracker._trackEvent('link','eLibrary',location.pathname + '_sidebar');
	});
	$('#content a[href*=//elibrary.uwe.ac.uk]').click(function(){
		pageTracker._trackEvent('link','eLibrary',location.pathname);
	});
	// Track UWE documents
	// N.B. this will not include the library homepage
	$('#content a[href$=.pdf], #content a[href$=.doc], #content a[href$=.docx], #content a[href$=.xls], #content a[href$=.xlsx], #content a[href$=.ppt], #content a[href$=.pptx], #content a[href$=.mp3]').click(function(){
		if (this.hostname.match(/uwe\.ac\.uk/)) {
			pageTracker._trackEvent('document',this.pathname,location.pathname);
		}
	});	
	// Track Informs tutorials
	$('#content a[href*=perl/jump]').click(function(){
		var tut_id = this.href.replace(/^http:\/\/www\.informs\.intute\.ac\.uk\/informs_perl\/jump\.pl\?(.+)/g, "$1");
		pageTracker._trackEvent('tutorial','Informs_' + tut_id,location.pathname);
	});
	// Track Science Direct tutorials
	$('#content a[href*=//help.sciencedirect.com]').click(function(){
		var tut_page = this.pathname.replace(/^.*\/(.*)\.html/g, "$1");
		pageTracker._trackEvent('tutorial','Science_Direct_' + tut_page,location.pathname);
	});
	// GA event tracking on Delicious tag links
	$("#content a[href^='http://www.delicious.com/']").click(function() {
		var link_text = $(this).text();
		pageTracker._trackEvent('link','Delicious',link_text);
	});		
	// Fix links to #top
	$('#content a[href=#top]').attr({href:'#uweTop'});
});

// 'Page information' pop-up
// 	Requires '#meta-div' styles in /library/includes/branding/better/custom/css/library.css
function showMetaInfo() {  
	if ($('#meta-div').length < 1) {
		var title_txt = $('title').html();
		var msg = '<p class="meta-info-heading">Page information</p><p><strong>title:&nbsp;</strong> ' + title_txt + '</p>';
		$('meta[name]').each(function() { 
			msg += '<p><strong>' + this.getAttribute('name') + ':&nbsp;</strong> ' + this.getAttribute('content') + '</p>';
		});
		msg += '<p><strong>last modified:</strong>&nbsp;' + doc_last_modified;
		$('body').append('<div id="meta-div">' + msg + '</p><p id="meta-div-close"><a href="#" onclick="hideMetaInfo();return false">Close</a></p></div>');
		// Avoid problem with MSIE < 7		
   	if (!($.browser.msie && $.browser.version.substr(0,1)<"7")) {
			$('#meta-div').attr('style','position:fixed');
 		}
	}
}
function hideMetaInfo() {
	$('#meta-div').remove();
}

// Form field focus
function formFocus(FieldID) {
	document.getElementById(FieldID).focus();
}

