﻿//DESCRIPTION: Add two-way hyperlinks between footnotes and their cues.
// Peter Kahrel


(function () {
	
	function main () {
		var i, j, fn, doc = app.activeDocument,
			stories = doc.stories.everyItem().getElements(),
			id, cue_source, note_source, cue_anchor, note_anchor;
		
		for (j = 0; j < stories.length; j++) {
			fn = stories[j].footnotes;
			for (i = 0; i < fn.length; i++) {
				try {
					cue_source = doc.hyperlinkTextSources.add (stories[j].characters[fn[i].storyOffset.index]);
					note_source = doc.hyperlinkTextSources.add (fn[i].words[0]);
					cue_anchor = doc.hyperlinkTextDestinations.add (fn[i].storyOffset);
					note_anchor = doc.hyperlinkTextDestinations.add (fn[i].insertionPoints[0]);
					doc.hyperlinks.add (cue_source, note_anchor);
					doc.hyperlinks.add (note_source, cue_anchor);
				} catch (_) {
					// The note was linked earlier
				}
			}
		}
	}

	main();
}());