// We really need getElementById to work, so fake it for
// older IE versions.  Copied from 
// http://www.metalusions.com/backstage/articles/8/
if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

// http://www.faqts.com/knowledge_base/view.phtml/aid/2193/fid/128
function getElementsByClass (className) {
    var all = document.all ? document.all : document.getElementsByTagName('*');
    var elements = new Array();
    for (var e = 0; e < all.length; e++) {
	if (all[e].className == className) {
	    elements.push( all[e] );
	}
    }
    return elements;
}


function printRefList( pmidList, invertSelection ) {
    // pmidList is a list of pubmed ids to print (or doi's)
    // if invertSelection is specified then we'll print everything but those

    var pmidListHash = new Object;
    if( pmidList ) {
	for( var i=0; i<pmidList.length; i++ ) {
	    pmidListHash[pmidList[i]] = 1;
	}
    }

    if( highlightAuthor ) {
	var regExp = new RegExp( highlightAuthor );
    }

    document.write( '<dl>'+"\n" );

    // add any additional refs defined in the config file
    refListRefs = refListRefs.concat( addRefs );

    var refListUse = new Array;
    // filter by pmid if we've provided a list
    if( pmidList ) {
	for( var i=0; i<refListRefs.length; i++ ) {
	    var set = false;
	    if( pmidListHash[refListRefs[i].pmid] || 
		pmidListHash[refListRefs[i].doi] ||
		pmidListHash[refListRefs[i].id]
		) {
		set = true;
	    }
	    if( ( set && !invertSelection ) ||
		( !set && invertSelection )
		) {
		refListUse.push( refListRefs[i] );
	    }
	}
    }
    else {
	refListUse = refListRefs.slice();
    }

    // sort the refList by the chosen function
    refListUse = refListUse.sort( sortByCustom );

    // go through each ref and print
    for( var i=0; i<refListUse.length; i++ ) {
	var refnum = (i+1);
	if( reverseNumber ) { refnum = refListUse.length - i; }

	var linkId = refListUse[i].pmid || refListUse[i].doi || refListUse[i].id;

	document.write( '<div class="ref">'+"\n" );
	document.write( '<a name="'+linkId+'"><dd class="refnum">'+refnum+'</dd></a><dt class="refbody">'+"\n" );

	for( var o=0; o<elementOrder.length; o++ ) {
	    if( elementOrder[o] == "authors" ) {
		// need to treat authors differently, otherwise any change we
		// make to author list makes it's way back to refListRefs by
		// following pointers.
		var auList = refListUse[i].authors.slice();

		if( maxNumAuthors ) {
		    var auListUse = auList.splice( 0, maxNumAuthors );
		    if( auList.length > 0 ) {
			auListUse.push( '<span class="etal">et al</span>' );
		    }
		    auList = auListUse;
		}

		if( highlightAuthor ) {
		    for( var j=0; j<auList.length; j++ ) {
			if( regExp.test(auList[j]) ) {
			    auList[j] = '<span class="highlightAuthor">'+
				auList[j] + '</span>';
			}
		    }
		}

		document.write( '<span class="'+elementOrder[o]+'">'+
				auList.join(', ')+
				'</span>'+"\n" );
	    }

	    else if( elementOrder[o] == "location" ) {
		document.write( '<span class="location">' );
		var locOrder = locationElementOrder; // default for journals
		if( refListUse[i].editor ) {
		    var locOrder = bookLocationElementOrder;
		}

		for( var l=0; l<locOrder.length; l++ ) {
		    if( refListUse[i][locOrder[l]] ) {
			document.write( '<span class="'+locOrder[l]+'">'+
					refListUse[i][locOrder[l]]+'</span>' );
		    }
		}
		// if we have no pages or volume then show epub message
		if( !refListUse[i].editor && !refListUse[i].pages && !refListUse[i].volume ) {
		    if( refListUse[i].note ) {
			document.write( '<span class="epub">'+
					refListUse[i].note+'</span>' );
		    }
		    else {
			document.write( '<span class="epub">'+
					epubMessage+'</span>' );
		    }
		}

		document.write( '</span>'+"\n" );
	    }

	    else if( elementOrder[o] == "links" ) {
		document.write( '<span class="links">' );
		for( var l=0; l<linksElementOrder.length; l++ ) {
		    if( refListUse[i][linksElementOrder[l]] ) {
			document.write( '<span class="link">'+
					linkUrls[linksElementOrder[l]].tag+':'+
					'<a href="'+
					linkUrls[linksElementOrder[l]].url+refListUse[i][linksElementOrder[l]]+
					'">'+
					refListUse[i][linksElementOrder[l]]+
					'</a></span>'+
					"\n" );
		    }
		}
		document.write( '</span>'+"\n" );
	    }

	    else if( elementOrder[o] == "supplement" ) {
		if( refListUse[i].supplement ) {
		    document.write( '<span class="supplement">'+
				    '<a href="'+
				    refListUse[i].supplement+
				    '">'+
				    supplementMessage+
				    '</a></span>'+"\n" );
		}
	    }

	    else {
		if( refListUse[i][elementOrder[o]] ) {
		    document.write( '<span class="'+elementOrder[o]+'">'+
				    refListUse[i][elementOrder[o]]+'</span>'+"\n" );
		}
	    }
	}

	document.write( '</dt></div>'+"\n" );
    }
    document.write( '</dl>'+"\n" );

    // hide all the reference numbers if we don't want them
    if( !showRefNumber ) {
	var el = getElementsByClass( 'refnum' );
	for( var i=0; i<el.length; i++ ) {
	    el[i].style.display = 'none';
	}
	el = getElementsByClass( 'refbody' );
	for( var i=0; i<el.length; i++ ) {
	    el[i].style.marginLeft = '0';
	}
    }
}


function sortByCustom(a,b) {
    // if we're sorting by date, but don't have a date tag
    // then fake it by year
    if( orderRefsBy == "date" ) {
	if( !a.date && a.year ) {
	    a.date = a.year+"0000";
	}
	if( !b.date && b.year ) {
	    b.date = b.year+"0000";
	}
    }

    var x = a[orderRefsBy];
    var y = b[orderRefsBy];
    if( orderRefsBy == "authors" ) {
	x = a[orderRefsBy][0];
	y = b[orderRefsBy][0];
    }

    if( reverseOrder ) {
	var z = x;
	x = y;
	y = z;
    }
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}



