var select={};
(function(){function p(a,b){for(;a&&a.parentNode!=b;)a=a.parentNode;return a}function u(a,b){for(;!a.previousSibling&&a.parentNode!=b;)a=a.parentNode;return p(a.previousSibling,b)}function s(a){var b=a.nextSibling;if(b){for(;b.firstChild;)b=b.firstChild;return b.nodeType==3||isBR(b)?b:s(b)}else{for(a=a.parentNode;a&&!a.nextSibling;)a=a.parentNode;return a&&s(a)}}select.ie_selection=document.selection&&document.selection.createRangeCollection;select.scrollToNode=function(a,b){if(a){for(var c=a,e=document.body,
d=document.documentElement,f=!c.nextSibling||!c.nextSibling.nextSibling||!c.nextSibling.nextSibling.nextSibling,g=0;c&&!c.offsetTop;){g++;c=c.previousSibling}if(g==0)f=false;if(!(webkit&&c&&c.offsetTop==5&&c.offsetLeft==5)){g=g*(c?c.offsetHeight:0);var j=0,h=a?a.offsetWidth:0;for(c=c;c&&c.offsetParent;){g+=c.offsetTop;isBR(c)||(j+=c.offsetLeft);c=c.offsetParent}c=e.scrollLeft||d.scrollLeft||0;e=e.scrollTop||d.scrollTop||0;var k=false,l=window.innerWidth||d.clientWidth||0;if(b||h<l){if(b){var n=select.offsetInNode(a),
m=nodeText(a).length;if(m)j+=h*(n/m)}h=j-c;if(h<0||h>l){c=j;k=true}}j=g-e;if(j<0||f||j>(window.innerHeight||d.clientHeight||0)-50){e=f?1E6:g;k=true}k&&window.scrollTo(c,e)}}};select.scrollToCursor=function(a){select.scrollToNode(select.selectionTopNode(a,true)||a.firstChild,true)};var i=null;select.snapshotChanged=function(){if(i)i.changed=true};select.snapshotReplaceNode=function(a,b,c,e){function d(f){if(a==f.node){i.changed=true;if(c&&f.offset>c)f.offset-=c;else{f.node=b;f.offset+=e||0}}else if(select.ie_selection&&
f.offset==0&&f.node==s(a))i.changed=true}if(i){d(i.start);d(i.end)}};select.snapshotMove=function(a,b,c,e,d){function f(g){if(a==g.node&&(!d||g.offset==0)){i.changed=true;g.node=b;g.offset=e?Math.max(0,g.offset+c):c}}if(i){f(i.start);f(i.end)}};if(select.ie_selection){var v=function(a){function b(h){for(var k=null;!k&&h;){k=h.nextSibling;h=h.parentNode}return c(k)}function c(h){for(;h&&h.firstChild;)h=h.firstChild;return{node:h,offset:0}}var e=document.selection.createRange();e.collapse(a);a=e.parentElement();
if(!isAncestor(document.body,a))return null;if(!a.firstChild)return c(a);var d=e.duplicate();d.moveToElementText(a);d.collapse(true);for(var f=a.firstChild;f;f=f.nextSibling){if(f.nodeType==3){var g=f.nodeValue.length;d.move("character",g)}else{d.moveToElementText(f);d.collapse(false)}var j=e.compareEndPoints("StartToStart",d);if(j==0)return b(f);if(j!=1){if(f.nodeType!=3)return c(f);d.setEndPoint("StartToEnd",e);return{node:f,offset:g-d.text.length}}}return b(a)};select.markSelection=function(){i=
null;if(document.selection){var a=v(true),b=v(false);if(a&&b)i={start:a,end:b,changed:false}}};select.selectMarked=function(){function a(e){var d=document.body.createTextRange(),f=e.node;if(f)if(f.nodeType==3){d.moveToElementText(f.parentNode);for(e=e.offset;f.previousSibling;){f=f.previousSibling;e+=(f.innerText||"").length}d.move("character",e)}else{d.moveToElementText(f);d.collapse(true)}else{d.moveToElementText(document.body);d.collapse(false)}return d}if(i&&i.changed){var b=a(i.start),c=a(i.end);
b.setEndPoint("StartToEnd",c);b.select()}};select.offsetInNode=function(a){var b=document.selection;if(!b)return 0;b=b.createRange();var c=b.duplicate();try{c.moveToElementText(a)}catch(e){return 0}b.setEndPoint("StartToStart",c);return b.text.length};select.selectionTopNode=function(a,b){function c(l,n){if(n.nodeType==3){for(var m=0,o=n.previousSibling;o&&o.nodeType==3;){m+=o.nodeValue.length;o=o.previousSibling}if(o){try{l.moveToElementText(o)}catch(x){return false}l.collapse(false)}else l.moveToElementText(n.parentNode);
m&&l.move("character",m)}else try{l.moveToElementText(n)}catch(y){return false}return true}var e=document.selection;if(!e)return false;var d=e.createRange(),f=d.duplicate();d.collapse(b);var g=d.parentElement();if(g&&isAncestor(a,g)){f.moveToElementText(g);if(d.compareEndPoints("StartToStart",f)==1)return p(g,a)}b=0;for(g=a.childNodes.length-1;b<g;){var j=Math.ceil((g+b)/2),h=a.childNodes[j];if(!h)return false;if(!c(f,h))return false;if(d.compareEndPoints("StartToStart",f)==1)b=j;else g=j-1}if(b==
0){e=e.createRange();d=e.duplicate();try{d.moveToElementText(a)}catch(k){return null}if(e.compareEndPoints("StartToStart",d)==0)return null}return a.childNodes[b]||null};select.focusAfterNode=function(a,b){var c=document.body.createTextRange();c.moveToElementText(a||b);c.collapse(!a);c.select()};select.somethingSelected=function(){var a=document.selection;return a&&a.createRange().text!=""};var w=function(a){var b=document.selection;if(b){b=b.createRange();b.pasteHTML(a);b.collapse(false);b.select()}};
select.insertNewlineAtCursor=function(){w("<br>")};select.insertTabAtCursor=function(){w("\u00a0\u00a0\u00a0\u00a0")};select.cursorPos=function(a,b){var c=document.selection;if(!c)return null;for(var e=select.selectionTopNode(a,b);e&&!isBR(e);)e=e.previousSibling;c=c.createRange();var d=c.duplicate();c.collapse(b);if(e){d.moveToElementText(e);d.collapse(false)}else{try{d.moveToElementText(a)}catch(f){return null}d.collapse(true)}c.setEndPoint("StartToStart",d);return{node:e,offset:c.text.length}};
select.setCursorPos=function(a,b,c){function e(f){var g=document.body.createTextRange();if(f.node){g.moveToElementText(f.node);g.collapse(false)}else{g.moveToElementText(a);g.collapse(true)}g.move("character",f.offset);return g}var d=e(b);c&&c!=b&&d.setEndPoint("EndToEnd",e(c));d.select()};select.getBookmark=function(a){var b=select.cursorPos(a,true);a=select.cursorPos(a,false);if(b&&a)return{from:b,to:a}};select.setBookmark=function(a,b){b&&select.setCursorPos(a,b.from,b.to)}}else{var t=function(a,
b){for(;a.nodeType!=3&&!isBR(a);){var c=a.childNodes[b]||a.nextSibling;for(b=0;!c&&a.parentNode;){a=a.parentNode;c=a.nextSibling}a=c;if(!c)break}return{node:a,offset:b}};select.markSelection=function(){var a=window.getSelection();if(!a||a.rangeCount==0)return i=null;a=a.getRangeAt(0);i={start:t(a.startContainer,a.startOffset),end:t(a.endContainer,a.endOffset),changed:false}};select.selectMarked=function(){function a(){if(c.start.node==c.end.node&&c.start.offset==c.end.offset){var d=window.getSelection();
if(!d||d.rangeCount==0)return true;d=d.getRangeAt(0);d=t(d.startContainer,d.startOffset);return c.start.node!=d.node||c.start.offset!=d.offset}}function b(d,f){if(d.node)d.offset==0?e["set"+f+"Before"](d.node):e["set"+f](d.node,d.offset);else e.setStartAfter(document.body.lastChild||document.body)}var c=i;if(c&&(c.changed||webkit&&a())){var e=document.createRange();b(c.end,"End");b(c.start,"Start");r(e)}};var r=function(a){var b=window.getSelection();if(b){b.removeAllRanges();b.addRange(a)}},q=function(){var a=
window.getSelection();return!a||a.rangeCount==0?false:a.getRangeAt(0)};select.selectionTopNode=function(a,b){var c=q();if(!c)return false;var e=b?c.startContainer:c.endContainer,d=b?c.startOffset:c.endOffset;window.opera&&!b&&c.endContainer==a&&c.endOffset==c.startOffset+1&&a.childNodes[c.startOffset]&&isBR(a.childNodes[c.startOffset])&&d--;return e.nodeType==3?d>0?p(e,a):u(e,a):e.nodeName.toUpperCase()=="HTML"?d==1?null:a.lastChild:e==a?d==0?null:e.childNodes[d-1]:d==e.childNodes.length?p(e,a):d==
0?u(e,a):p(e.childNodes[d-1],a)};select.focusAfterNode=function(a,b){var c=document.createRange();c.setStartBefore(b.firstChild||b);if(a&&!a.firstChild)c.setEndAfter(a);else a?c.setEnd(a,a.childNodes.length):c.setEndBefore(b.firstChild||b);c.collapse(false);r(c)};select.somethingSelected=function(){var a=q();return a&&!a.collapsed};select.offsetInNode=function(a){var b=q();if(!b)return 0;b=b.cloneRange();b.setStartBefore(a);return b.toString().length};select.insertNodeAtCursor=function(a){var b=q();
if(b){b.deleteContents();b.insertNode(a);webkitLastLineHack(document.body);if(window.opera&&isBR(a)&&isSpan(a.parentNode)){b=a.nextSibling;var c=a.parentNode,e=c.parentNode;e.insertBefore(a,c.nextSibling);for(c="";b&&b.nodeType==3;b=b.nextSibling){c+=b.nodeValue;removeElement(b)}e.insertBefore(makePartSpan(c,document),a.nextSibling)}b=document.createRange();b.selectNode(a);b.collapse(false);r(b)}};select.insertNewlineAtCursor=function(){select.insertNodeAtCursor(document.createElement("BR"))};select.insertTabAtCursor=
function(){select.insertNodeAtCursor(document.createTextNode("\u00a0\u00a0\u00a0\u00a0"))};select.cursorPos=function(a,b){var c=q();if(c){for(var e=select.selectionTopNode(a,b);e&&!isBR(e);)e=e.previousSibling;c=c.cloneRange();c.collapse(b);e?c.setStartAfter(e):c.setStartBefore(a);c=c.toString();return{node:e,offset:c.length}}};select.setCursorPos=function(a,b,c){function e(f,g,j){function h(m){m.nodeType==3?k.push(m):forEach(m.childNodes,h)}if(g==0&&f&&!f.nextSibling){d["set"+j+"After"](f);return true}if(f=
f?f.nextSibling:a.firstChild){if(g==0){d["set"+j+"Before"](f);return true}for(var k=[];;){for(;f&&!k.length;){h(f);f=f.nextSibling}var l=k.shift();if(!l)return false;var n=l.nodeValue.length;if(n>=g){d["set"+j](l,g);return true}g-=n}}}var d=document.createRange();c=c||b;e(c.node,c.offset,"End")&&e(b.node,b.offset,"Start")&&r(d)}}})();