/*\

title: $:/plugins/felixhayashi/hotzone/hotzone.js
type: application/javascript
module-type: startup

@preserve

\*/
(function(){"use strict";exports.name="hotzone";exports.platforms=["browser"];exports.after=["story"];exports.synchronous=true;exports.startup=function(){var t=require("$:/plugins/felixhayashi/hotzone/config.js").config;var e=null;var i=false;var r=document.getElementsByClassName(t.classNames.storyRiver)[0];var s=$tw.wiki.getTiddlerData(t.references.userConfig,{});var a=isNaN(parseInt(s.focusOffset))?150:parseInt(s.focusOffset);var n=function(e,i,r){if(!(e instanceof Element))return;if(!$tw.utils.hasClass(e,t.classNames.tiddlerFrame))return;var s=e.getElementsByClassName(t.classNames.tiddlerTitle)[0];if(s){var a=s.innerText||s.textContent;return a.trim()}};var o=function(t){if(!i){i=true;window.setTimeout(f,t||0)}};var l=function(e,i){$tw.wiki.addTiddler(new $tw.Tiddler({title:t.references.focussedTiddlerStore,text:e},$tw.wiki.getModificationFields()));if(i){var r=document.getElementsByClassName("hzone-focus")[0];if(r){$tw.utils.removeClass(r,"hzone-focus")}$tw.utils.addClass(i,"hzone-focus")}};var f=function(){i=false;var s=$tw.wiki.getTiddler("$:/StoryList");if(s&&s.fields.list.length){var o=null;var f=Number.MAX_VALUE;var d=r.children;var u=t.classNames.tiddlerFrame;for(var c=d.length;c--;){if($tw.utils.hasClass(d[c],u)){var v=d[c].getBoundingClientRect();var w=Math.min(Math.abs(a-v.top),Math.abs(a-v.bottom));if(w<f){o=d[c];f=w}}}var m=n(o);if(m!==e&&$tw.wiki.getTiddler(m)){e=m;l(e,o);return}}else if(e){e="";l(e)}};var d=function(t){if(t["$:/HistoryList"]){if(!$tw.wiki.tiddlerExists("$:/HistoryList"))return;var e=$tw.wiki.getTiddler("$:/HistoryList").fields["current-tiddler"];var i=$tw.wiki.getTiddlerList("$:/StoryList");var r=i.indexOf(e)>=0;if(!r)return;o($tw.utils.getAnimationDuration()+100)}else if(t["$:/StoryList"]){o($tw.utils.getAnimationDuration()+100)}};var u=function(t){o(250)};$tw.wiki.addEventListener("change",d);window.addEventListener("scroll",u,false);u()}})();