(function(){function m(a,b,c,d){function e(){if(f)return null;var g=b;if(b.childNodes&&b.childNodes.length&&!j)b=b[d?"lastChild":"firstChild"];else if(b[d?"previousSibling":"nextSibling"])b=b[d?"previousSibling":"nextSibling"],j=false;else if(b.parentNode)b=b.parentNode,b===a&&(f=true),j=true,e();g===c&&(f=true);return g}var d=!!d,b=b||a[d?"lastChild":"firstChild"],f=!b,j=false;return e}function s(a){for(var b=1;b<arguments.length;b++)for(key in arguments[b])a[key]=arguments[b][key];return a}function n(a){return(a||
"").replace(/^\s+|\s+$/g,"")}function A(a,b){var c="";document.defaultView&&document.defaultView.getComputedStyle?c=document.defaultView.getComputedStyle(a,"").getPropertyValue(b):a.currentStyle&&(b=b.replace(/\-(\w)/g,function(a,b){return b.toUpperCase()}),c=a.currentStyle[b]);return c}function q(a,b){for(;a&&!k(b).test(a.className);)a=a.parentNode;return a||null}function t(a,b){for(var c=m(a),d=null;d=c();)if(d.nodeType===1&&k(b).test(d.className))return d;return null}function u(a){for(var a=m(a),
b=null;b=a();)if(b.nodeType===3)break;return b}function v(a,b){if(a.getElementsByClassName)return a.getElementsByClassName(b);else{for(var c=[],d,e=m(a);d=e();)d.nodeType==1&&k(b).test(d.className)&&c.push(d);return c}}function o(a){for(var b=[],c=m(a);a=c();)a.nodeType===3&&b.push(a);return b}function k(a){return RegExp("(^|\\s+)"+a+"(?:$|\\s+)","g")}function w(a,b){if(!k(b).test(a.className))a.className=a.className+" "+b}function r(a,b){var c=k(b);if(c.test(a.className))a.className=n(a.className.replace(c,
"$1"))}function y(a,b){for(var c=0,d=b.length;c<d;c++)if(b[c]===a)return c;return-1}function h(a,b,c){a.addEventListener?a.addEventListener(b,c,false):a.attachEvent&&a.attachEvent("on"+b,c)}function x(a){a.preventDefault?a.preventDefault():a.returnValue=false}var z=function(){this.set_hash=function(a){window.location.hash=a};this.get_hash=function(){return window.location.hash};this.add_hashchange=function(a){h(window,"hashchange",a)}},l=function(a){this.options=s({},l.default_options,a);s(this,{counter:0,
savedSel:[],ranges:{},childs:[],blocks:{}});this.init()};l.version="29.09.2011-12:18:53";l.default_options={regexp:"[^\\s,;:\u2013.!?<>\u2026\\n\u00a0\\*]+",selectable:"selectable-content",marker:"txtselect_marker",ignored:null,select_message:null,location:new z,validate:false,enable_haschange:true,onMark:null,onUnmark:null,onHashRead:function(){var a=t(this.selectable,"user_selection_true");if(a&&!this.hash_was_read)this.hash_was_read=true,window.setTimeout(function(){for(var b=0,c=0;a;)b+=a.offsetLeft,
c+=a.offsetTop,a=a.offsetParent;window.scrollTo(b,c-150)},1)},is_block:function(a){return a.nodeName=="BR"||y(A(a,"display"),["inline","none"])==-1}};l.prototype={init:function(){function a(){var a=RegExp(d.options.regexp,"g"),b=window.getSelection().toString();if(b!=""&&a.test(b)&&d.range_is_selectable())d.marker.style.top=e.y-33+"px",d.marker.style.left=e.x+5+"px",w(d.marker,"show")}function b(a){a=a.touches.item(0);e={x:a.pageX,y:a.pageY}}function c(a){x(a);a.stopPropagation?a.stopPropagation():
a.cancelBubble=true;r(d.marker,"show");d.range_is_selectable()&&(d.addSelection(),d.updateHash(),d.options.onMark&&d.options.onMark.call(d),d.options.select_message&&d._show_message())}this.selectable=typeof this.options.selectable=="string"?document.getElementById(this.options.selectable):this.options.selectable;if(typeof this.options.marker=="string"){if(this.marker=document.getElementById(this.options.marker),this.marker===null)this.marker=document.createElement("a"),this.marker.setAttribute("id",
this.options.marker),this.marker.setAttribute("href","#"),document.body.appendChild(this.marker)}else this.marker=this.options.marker;if(typeof this.options.regexp!="string")throw"regexp is set as string";this.regexp=RegExp(this.options.regexp,"ig");var d=this;if(this.selectable){this.is_ignored=this.construct_ignored(this.options.ignored);this.options.select_message&&this.init_message();this.enumerateElements();var e;h(this.selectable,"mouseup",function(b){if(b.pageX==null){var c=document.documentElement,
d=document.body;e={x:b.clientX+(c&&c.scrollLeft||d&&d.scrollLeft||0)-(c.clientLeft||0),y:b.clientY+(c&&c.scrollTop||d&&d.scrollTop||0)-(c.clientTop||0)}}else e={x:b.pageX,y:b.pageY};window.setTimeout(a,1)});h(this.selectable,"touchmove",b);h(this.selectable,"touchstart",b);h(this.selectable,"touchend",function(){window.setTimeout(function(){var b=window.getSelection();b.rangeCount&&(b=b.getRangeAt(0).getClientRects(),(b=b[b.length-1])&&(e={x:b.left+b.width+document.body.scrollLeft,y:b.top+b.height/
2+document.body.scrollTop}));a()},1)});h(this.marker,"click",c);h(this.marker,"touchend",c);h(document,"click",function(a){(a.target||a.srcElement)!=d.marker&&r(d.marker,"show")});this.options.enable_haschange&&this.options.location.add_hashchange(function(){if(d.last_hash!=d.options.location.get_hash()){var a=[],b;for(b in d.ranges)a.push(b);d.delete_selections(a);d.readHash()}});this.readHash()}},delete_selections:function(a){for(var b=[],c=a.length;c--;){var d=a[c],e=v(this.selectable,d),f=t(e[e.length-
1],"closewrap");f.parentNode.removeChild(f);this.removeTextSelection(e);b.push(this.ranges[d]);delete this.ranges[d]}},removeTextSelection:function(a){for(var b=a.length;b--;){for(var c=a[b],d=0;d<c.childNodes.length;d++)c.parentNode.insertBefore(c.childNodes[d],c);c.parentNode.removeChild(c)}},is_internal:function(a){for(;a.parentNode;){if(a==this.selectable)return true;a=a.parentNode}return false},_siblingNode:function(a,b,c,d,e){for(e=e||this.regexp;a.parentNode&&this.is_internal(a);){for(;a[b+
"Sibling"];){for(a=a[b+"Sibling"];a.nodeType==1&&a.childNodes.length;)a=a[c+"Child"];if(a.nodeType==3&&a.data.match(e)!=null)return{_container:a,_offset:d*a.data.length}}a=a.parentNode}return null},prevNode:function(a,b){return this._siblingNode(a,"previous","last",1,b)},nextNode:function(a,b){return this._siblingNode(a,"next","first",0,b)},wordCount:function(a){var b=0;if(a.nodeType==3)(a=a.nodeValue.match(this.regexp))&&(b+=a.length);else if(a.childNodes&&a.childNodes.length){a=o(a);for(i=a.length;i--;)b+=
a[i].nodeValue.match(this.regexp).length}return b},words:function(a,b,c){a.nodeType==1&&(a=u(a));b=a.data.substring(0,b).match(this.regexp);if(b!=null){if(c=="start"&&(b=b.length+1),c=="end")b=b.length}else b=1;for(var c=a,a=this.getNum(a),d=this.getFirstTextNode(a);c&&c!=d;)c=this.prevNode(c,/.*/)._container,b+=this.wordCount(c);return a+":"+b},symbols:function(a){var b=0;if(a.nodeType==3)b=a.nodeValue.length;else if(a.childNodes&&a.childNodes.length)for(var a=o(a),c=a.length;c--;)b+=a[c].nodeValue.length;
return b},updateHash:function(){var a=[];for(key in this.ranges)a.push(this.ranges[key]);this.last_hash=a="#sel="+a.join(";");this.options.location.set_hash(a)},readHash:function(){var a=this.splittedHash();if(a){for(var b=0;b<a.length;b++)this.deserializeSelection(a[b]);this.updateHash();this.options.onHashRead&&this.options.onHashRead.call(this)}},splittedHash:function(){var a=this.options.location.get_hash();if(!a)return null;a=a.replace(/^#/,"").replace(/;+$/,"");if(!/^sel\=(?:\d+\:\d+(?:\:[^:;]*)?\,\d+\:\d+(?:\:[^:;]*)?;)*\d+\:\d+(?:\:[^:;]*)?\,\d+\:\d+(?:\:[^:;]*)?$/.test(a))return null;
a=a.substring(4,a.length);return a.split(";")},deserializeSelection:function(a){var b=window.getSelection();b.rangeCount>0&&b.removeAllRanges();(a=this.deserializeRange(a))&&this.addSelection(a)},deserializeRange:function(a){var b=/^([^,]+),([^,]+)$/.exec(a),c=b[1].split(":"),b=b[2].split(":");if(parseInt(c[0],10)<parseInt(b[0],10)||c[0]==b[0]&&parseInt(c[1],10)<=parseInt(b[1],10)){var d=this.deserializePosition(c,"start"),e=this.deserializePosition(b,"end");if(d.node&&e.node){var f=document.createRange();
f.setStart(d.node,d.offset);f.setEnd(e.node,e.offset);if(!this.options.validate||this.validateRange(f,c[2],b[2]))return f}}window.console&&typeof window.console.warn=="function"&&window.console.warn("Cannot deserialize range: "+a);return null},validateRange:function(a,b,c){var d=true,e;b&&(e=this.getPositionChecksum(a.getWordIterator(this.regexp)),d=d&&b==e);c&&(e=this.getPositionChecksum(a.getWordIterator(this.regexp,true)),d=d&&c==e);return d},getRangeChecksum:function(a){sum1=this.getPositionChecksum(a.getWordIterator(this.regexp));
sum2=this.getPositionChecksum(a.getWordIterator(this.regexp,true));return[sum1,sum2]},getPositionChecksum:function(){return""},deserializePosition:function(a,b){for(var c=this.blocks[parseInt(a[0],10)],d,e=0;c;){for(var f=RegExp(this.options.regexp,"ig");(myArray=f.exec(c.data))!=null;)if(e++,e==a[1]){if(b=="start")d=myArray.index;if(b=="end")d=f.lastIndex;return{node:c,offset:parseInt(d,10)}}(c=(c=this.nextNode(c,/.*/))?c._container:null)&&this.isFirstTextNode(c)&&(c=null)}return{node:null,offset:0}},
serializeRange:function(a){var b=this.words(a.startContainer,a.startOffset,"start"),c=this.words(a.endContainer,a.endOffset,"end");this.options.validate&&(a=this.getRangeChecksum(a),b+=":"+a[0],c+=":"+a[1]);return b+","+c},checkSelection:function(a){this.checkPosition(a,a.startOffset,a.startContainer,"start");this.checkPosition(a,a.endOffset,a.endContainer,"end");this.checkBrackets(a);this.checkSentence(a);return a},checkPosition:function(a,b,c,d){function e(a){return a.match(h.regexp)!=null}function f(a){return a.match(h.regexp)==
null}function j(a,b,c){for(;b>0&&c(a.data.charAt(b-1));)b--;return b}function g(a,b,c){for(;b<a.data.length&&c(a.data.charAt(b));)b++;return b}var h=this;if(c.nodeType==1&&b>0)b<c.childNodes.length?(c=c.childNodes[b],b=0):(container_txtnodes=o(c),c=container_txtnodes[container_txtnodes.length-1],b=c.data.length);if(d=="start"){if(c.nodeType==1&&n(c.textContent||c.innerText)!="")c=u(c),b=0;if(c.nodeType!=3||c.data.substring(b).match(this.regexp)==null)b=this.nextNode(c),c=b._container,b=b._offset;
b=g(c,b,f);b=j(c,b,e);a.setStart(c,b)}if(d=="end"){if(c.nodeType==1&&n(c.textContent||c.innerText)!=""&&b!=0)c=c.childNodes[a.endOffset-1],container_txtnodes=o(c),c=container_txtnodes[container_txtnodes.length-1],b=c.data.length;if(c.nodeType!=3||c.data.substring(0,b).match(this.regexp)==null)b=this.prevNode(c),c=b._container,b=b._offset;b=j(c,b,f);b=g(c,b,e);a.setEnd(c,b)}},checkBrackets:function(a){this._checkBrackets(a,"(",")",/\(|\)/g,/\(x*\)/g);this._checkBrackets(a,"\u00ab","\u00bb",/\\u00ab|\\u00bb/g,
/\u00abx*\u00bb/g)},_checkBrackets:function(a,b,c,d,e){var f=a.toString();if(d=f.match(d)){for(var d=d.join(""),j=d.length+1;d.length<j;)j=d.length,d=d.replace(e,"x");d.charAt(d.length-1)==c&&f.charAt(f.length-1)==c&&(a.endOffset==1?(c=this.prevNode(a.endContainer),a.setEnd(c.container,c.offset)):a.setEnd(a.endContainer,a.endOffset-1));d.charAt(0)==b&&f.charAt(0)==b&&(a.startOffset==a.startContainer.data.length?(c=this.nextNode(a.endContainer),a.setStart(c.container,c.offset)):a.setStart(a.startContainer,
a.startOffset+1))}},checkSentence:function(a){function b(){a.setEnd(c._container,c._offset+1)}var c,d;if(a.endOffset==a.endContainer.data.length){c=this.nextNode(a.endContainer,/.*/);if(!c)return null;d=c._container.data.charAt(0)}else c={_container:a.endContainer,_offset:a.endOffset},d=a.endContainer.data.charAt(a.endOffset);if(d.match(/\.|\?|\!/)){d=a.toString();if(d.match(/(\.|\?|\!)\s+[A-Z\u0410-\u042f\u0401]/))return b();if(a.startOffset==0&&a.startContainer.previousSibling&&a.startContainer.previousSibling.nodeType==
1&&a.startContainer.previousSibling.className=="selection_index")return b();for(var e,f=a.getElementIterator();e=f();)if(e.nodeType==1&&e.className=="selection_index")return b();if(d.charAt(0).match(/[A-Z\u0410-\u042f\u0401]/)){d=a.startContainer.data.substring(0,a.startOffset);if(!d.match(/\S/))d=this.prevNode(a.startContainer,/\W*/)._container.data;d=n(d);if(d.charAt(d.length-1).match(/(\.|\?|\!)/))return b()}return null}},mergeSelections:function(a){var b=[],c=a.getElementIterator(),d=c(),e=d,
f=q(d,"user_selection_true");f&&(f=/(num\d+)(?:$| )/.exec(f.className)[1],a.setStart(u(t(this.selectable,f)),0),b.push(f));for(;d;)d.nodeType==1&&k("user_selection_true").test(d.className)&&(e=/(num\d+)(?:$|)/.exec(d.className)[0],y(e,b)==-1&&b.push(e)),e=d,d=c();if(e=q(e,"user_selection_true"))e=/(num\d+)(?:$| )/.exec(e.className)[1],c=v(this.selectable,e),c=o(c?c[c.length-1]:null),c=c[c.length-1],a.setEnd(c,c.length);if(b.length)c=a.startContainer,d=a.startOffset,e=a.endContainer,f=a.endOffset,
this.delete_selections(b),a.setStart(c,d),a.setEnd(e,f);return a},addSelection:function(a){var a=a||this.getFirstRange(),a=this.checkSelection(a),a=this.mergeSelections(a),b="num"+this.counter;this.ranges[b]=this.serializeRange(a);a.wrapSelection(b+" user_selection_true");this.addSelectionEvents(b)},addSelectionEvents:function(a){for(var b=false,c=this,d=v(this.selectable,a),e=d.length;e--;)h(d[e],"mouseover",function(){for(var a=d.length;a--;)w(d[a],"hover");window.clearTimeout(b)}),h(d[e],"mouseout",
function(a){for(a=a.relatedTarget;a&&a.parentNode&&a.className!=this.className;)a=a.parentNode;if(!a||a.className!=this.className)b=window.setTimeout(function(){for(var a=d.length;a--;)r(d[a],"hover")},2E3)});e=document.createElement("a");e.className="txtsel_close";e.href="#";var f=document.createElement("span");f.className="closewrap";f.appendChild(e);h(e,"click",function(b){x(b);c.delete_selections([a]);c.updateHash();c.options.onUnmark&&c.options.onUnmark.call(c)});d[d.length-1].appendChild(f);
this.counter++;window.getSelection().removeAllRanges()},getFirstRange:function(){var a=window.getSelection();return a.rangeCount?a.getRangeAt(0):null},enumerateElements:function(){function a(d){for(var d=d.childNodes,e=false,f=false,j=0;j<d.length;++j){var g=d.item(j),h=g.nodeType;if(h!=3||g.nodeValue.match(c.regexp))if(h==3){if(!f)b++,e=document.createElement("span"),e.id="selection_index"+b,e.className="selection_index",g.parentNode.insertBefore(e,g),j++,c.blocks[b]=g,e=f=true}else h==1&&!c.is_ignored(g)&&
(c.options.is_block(g)?(g=a(g),e=e||g,f=false):f||(f=a(g),e=e||f))}return e}var b=0,c=this;a(this.selectable)},isFirstTextNode:function(a){for(var a=[a.previousSibling,a.parentNode.previousSibling],b=a.length;b--;)if(a[b]&&a[b].nodeType==1&&a[b].className=="selection_index")return true;return false},getFirstTextNode:function(a){return!a?null:(a=document.getElementById("selection_index"+a))?a.nextSibling.nodeType==1?a.nextSibling.childNodes[0]:a.nextSibling:null},getNum:function(a){for(;a.parentNode;){for(;a.previousSibling;){for(a=
a.previousSibling;a.nodeType==1&&a.childNodes.length;)a=a.lastChild;if(a.nodeType==1&&a.className=="selection_index")return a.id.replace("selection_index","")}a=a.parentNode}return null},construct_ignored:function(a){if(typeof a=="function")return a;else if(typeof a=="string"){for(var b=[],c=[],d=[],a=a.split(","),e=0;e<a.length;e++){var f=n(a[e]);f.charAt(0)=="#"?b.push(f.substr(1)):f.charAt(0)=="."?c.push(f.substr(1)):d.push(f)}return function(a){var e;for(e=b.length;e--;)if(a.id==b[e])return true;
for(e=c.length;e--;)if(k(c[e]).test(a.className))return true;for(e=d.length;e--;)if(a.tagName==d[e].toUpperCase())return true;return false}}else return function(){return false}},range_is_selectable:function(){var a,b,c,d=true,e=this.getFirstRange();if(!e)return false;for(e=e.getElementIterator();a=e();)if(a.nodeType==3&&a.data.match(this.regexp)!=null&&(b=b||a,c=a),a=d&&a.nodeType==3?a.parentNode:a,d=false,a.nodeType==1){for(;a!=this.selectable&&a.parentNode;){if(this.is_ignored(a))return false;a=
a.parentNode}if(a!=this.selectable)return false}b=q(b,"user_selection_true");c=q(c,"user_selection_true");return b&&c?(d=/(?:^| )(num\d+)(?:$| )/,d.exec(b.className)[1]!=d.exec(c.className)[1]):true},init_message:function(){var a=this;this.msg=typeof this.options.select_message=="string"?document.getElementById(this.options.select_message):this.options.select_message;this.close_button=this.get_close_button();this.msg_autoclose=null;h(this.close_button,"click",function(b){x(b);a.hide_message();a.save_message_closed();
clearTimeout(a.msg_autoclose)})},get_close_button:function(){return this.msg.getElementsByTagName("a")[0]},get_message_closed:function(){return window.localStorage?!!localStorage.masha_warning:!!document.cookie.match(/(?:^|;)\s*masha-warning=/)},save_message_closed:function(){window.localStorage?localStorage.masha_warning="true":this.get_closed()||(document.cookie+="; masha-warning=true")},_show_message:function(){var a=this;if(!this.get_message_closed())this.show_message(),clearTimeout(this.msg_autoclose),
this.msg_autoclose=setTimeout(function(){a.hide_message()},1E4)},show_message:function(){w(this.msg,"show")},hide_message:function(){r(this.msg,"show")}};var p=window.Range||document.createRange().constructor;p.prototype.splitBoundaries=function(){var a=this.startContainer,b=this.startOffset,c=this.endContainer,d=this.endOffset,e=a===c;c.nodeType==3&&d<c.length&&c.splitText(d);a.nodeType==3&&b>0&&(a=a.splitText(b),e&&(d-=b,c=a),b=0);this.setStart(a,b);this.setEnd(c,d)};p.prototype.getTextNodes=function(){for(var a=
this.getElementIterator(),b=[],c;c=a();)c.nodeType==3&&b.push(c);return b};p.prototype.getElementIterator=function(a){return a?m(null,this.endContainer,this.startContainer,true):m(null,this.startContainer,this.endContainer)};p.prototype.getWordIterator=function(a,b){var c=this.getElementIterator(b),d,e=0,f=0,h=false,g,k=this;return function(){if(e==f&&!h){do{do d=c();while(d&&d.nodeType!=3);h=!d;if(!h)value=d.nodeValue,d==k.endContainer&&(value=value.substr(0,k.endOffset)),d==k.startContainer&&(value=
value.substr(k.startOffset)),g=value.match(a)}while(d&&!g);g&&(e=b?0:g.length-1,f=b?g.length-1:0)}else b?f--:f++;return h?null:g[f]}};p.prototype.wrapSelection=function(a){this.splitBoundaries();for(var b=this.getTextNodes(),c=b.length;c--;){var d=document.createElement("span");d.className=a;b[c].parentNode.insertBefore(d,b[c]);d.appendChild(b[c])}};l.LocationHandler=z;window.MaSha=l;if(window.jQuery)window.jQuery.fn.masha=function(a){a=a||{};a=s({selectable:this[0]},a);return new l(a)}})();

