(function($){var SP=" ",NBSP="\u00a0",ELLIPSIS="\u2026",SHY="\u00ad",ZWSP="\u200b",MORE_HYPH_TRIM=/(?:^|[ \\\/\=\&\+\$\.\?\!\,\:\;\-\u2026\u0085\u00ad\u200b\u2010\u2012\u2013\u0096\u0097\u00a2-\u00a5\u20ac\u0080\{\(\[])?(?:[^ \\\/\=\&\+\$\.\?\!\,\:\;\-\u2026\u0085\u00ad\u200b\u2010\u2012\u2013\u0096\u0097\u00a2\u00a3\u00a5\u20ac\u0080\{\(\[]+|$)$/,MORE_WORD_TRIM=/(?:^|[ \\\/\=\&\+\$\.\?\!\,\:\;\u2026\u00a2-\u00a5\u20ac\u0080\{\(\[]+)?(?:[^ \\\/\=\&\+\$\.\?\!\,\:\;\u2026\u00a2-\u00a5\u20ac\u0080\{\(\[]+|$)$/,HYPH_RX=/[\-\u0085\u00ad\u2010]/g,URL_RX=/(?:\w*:\/\/)(?:(?:\w*:)?(?:\w*)@)?(?:[\w\.]*)?(?::\d*)?(?:\/[\w#!:\.\?+=&%@!\-]*)*/,URL_HYPH_RX=/(?:\/+|[#!\.\?+=&%@!\-])/g,MAIL_RX=/[\w\-\.]+@[\w\.]+/,MAIL_HYPH_RX=/[\-\.\@]/g,NUM_RX=/^\-?[\$\u00a2-\u00a5]?\-?(?:[\d,]*[\.\u00b7]\d*|[\.\u00b7]\d+)$/,NUM_HYPH_RX=/[\,\.\u00b7]/g;
var measureOk=false,shyOk=false,zwspOk=false,brealAllOk=false,initialised=false,wrpSet=false,wrp;
$.fn.flowfit=function(cfg){function _initialise(){var jqTmpEl,tmpEl,heightZwChr,origWidth,nbChr="#";
jqTmpEl=$("<div>"+nbChr+nbChr+"</div>").appendTo("body").css({position:"absolute",overflow:"hidden",width:"auto",height:"auto",display:"block",padding:0,margin:0,top:0,right:0,"font-size":"10px","line-height":"10px"});
tmpEl=jqTmpEl.get(0);
measureOk=!!(tmpEl.scrollWidth&&tmpEl.clientWidth);
if(measureOk){jqTmpEl.text(nbChr+SHY+nbChr);
origWidth=tmpEl.clientWidth;
if(tmpEl.clientWidth==origWidth){heightZwChr=tmpEl.clientHeight;
jqTmpEl.css("width",(origWidth-1)+"px");
if(tmpEl.clientHeight!=heightZwChr){shyOk=true;
wrp=SHY;
wrpSet=true
}}jqTmpEl.text(nbChr+ZWSP+nbChr);
heightZwChr=tmpEl.clientHeight;
jqTmpEl.css("width","auto");
if(tmpEl.clientHeight!=heightZwChr&&tmpEl.clientWidth==origWidth){zwspOk=true;
if(!shyOk){wrp=ZWSP;
wrpSet=true
}}tmpEl=null;
jqTmpEl.remove();
jqTmpEl=null
}}function _breakString(str){var joinNext=false,strt=0,end,strLen,out=[""],first=true,myWrap,chunk,chunkHasHyphen=false,afterlastHyph,subChunkAry,hyphRxIterAry,isUrl,isEmail,isNum,myRx;
myWrap=(((isUrl=(str.match(URL_RX)!==null))||(isEmail=(str.match(MAIL_RX)!==null))||(isNum=(str.match(NUM_RX)!==null)))&&zwspOk)?ZWSP:wrp;
strLen=str.length;
while(strt<strLen){if(first){end=minHyphLen
}else{end=strt+minHyphChunkLen
}if((!shyOk)||((strLen-end-1)<minHyphChunkLen)){end=strLen
}chunk=str.substring(strt,end);
chunkHasHyphen=(chunk.match(HYPH_RX));
if(first||chunkHasHyphen||joinNext||isNum||isUrl||isEmail){if(zwspOk&&(chunkHasHyphen||isNum||isUrl||isEmail)){afterlastHyph=0;
subChunkAry=[];
rxSplit=false;
if(isNum){myRx=NUM_HYPH_RX
}else{if(isUrl){myRx=URL_HYPH_RX
}else{if(isEmail){myRx=MAIL_HYPH_RX
}else{myRx=HYPH_RX
}}}while((hyphRxIterAry=myRx.exec(chunk))!==null){subChunkAry.push(chunk.substring(afterlastHyph,hyphRxIterAry.index)+hyphRxIterAry[0]);
afterlastHyph=hyphRxIterAry.index+hyphRxIterAry[0].length;
rxSplit=true
}if(afterlastHyph<chunk.length){subChunkAry.push(chunk.substring(afterlastHyph))
}out[out.length-1]+=subChunkAry.join(ZWSP)
}else{out[out.length-1]+=chunk
}first=false
}else{out.push(chunk)
}if(chunkHasHyphen&&!isNum){joinNext=true
}else{joinNext=false
}strt=end
}return out.join(myWrap)
}function _truncString(str){var lastSp,lastWord;
lastSp=str.lastIndexOf(SP);
lastWord=(lastSp!=-1)?str.substring(lastSp+1):str;
if(lastWord.length>mustSplitLen){str=str.replace(MORE_HYPH_TRIM,"")
}else{str=str.replace(MORE_WORD_TRIM,"")
}if(moreTrim){str=str.replace(moreTrimRx,"")
}return str
}function _flowText(ofCtxt,el,displayState,forceMore){forceMore=(forceMore!==undefined)?forceMore:false;
var words,wordsLen,moreTxt,word,j,jqEl=$(el),ctxt=ofCtxt.get(0),txt="",concatTxt="",overflowState=0,first=true;
if(jqEl.children().length===0){words=$.trim(jqEl.text()).split(/[ \t\n\r\f]+/);
wordsLen=words.length;
jqEl.empty();
if(!!(sel)){ofCtxt.css({overflow:"hidden"});
jqEl.css({display:displayState,zoom:1})
}else{jqEl.css({overflow:"hidden"})
}for(j=0;
j<wordsLen;
j++){word=(!forceMore)?((wrpSet)?_breakString(words[j]):words[j]):words[j];
concatTxt=(j===0)?word:txt+SP+word;
jqEl.text(concatTxt);
if((ctxt.scrollHeight>ctxt.clientHeight)||(first&&forceMore&&j==(wordsLen-1))){moreTxt=concatTxt;
do{if(moreTxt===""||moreTxt===moreToken){overflowState=1;
break
}else{if(!first||first&&wordsLen==1||first&&!forceMore){moreTxt=_truncString(moreTxt)
}if(more){jqEl.text(moreTxt+moreToken)
}else{jqEl.text(moreTxt)
}}if(first){first=false
}}while(ctxt.scrollHeight>ctxt.clientHeight);
concatTxt=moreTxt;
if(overflowState===0){overflowState=-1
}}if(overflowState!==0){break
}txt=concatTxt
}}return overflowState
}if(!initialised){_initialise();
initialised=true
}if(measureOk){cfg=(cfg!==undefined)?cfg:{};
var minHyphLen=(cfg.minHyphLen!==undefined)?cfg.minHyphLen:4,minHyphChunkLen=(cfg.minHyphChunkLen!==undefined)?cfg.minHyphChunkLen:4,mustSplitLen=minHyphChunkLen+minHyphLen,more=(cfg.more!==undefined)?(!!cfg.more):true,moreToken=(cfg.moreToken!==undefined)?cfg.moreToken:ELLIPSIS,moreTrimRx=(moreToken==ELLIPSIS)?new RegExp("("+moreToken+"|\\.\\.\\.)$"):new RegExp(moreToken+"$"),moreTokenLen=moreToken.length,moreTrim=(cfg.trimMoreToken!==undefined)?(!!cfg.trimMoreToken):true,sel=(cfg.sel!==undefined)?cfg.sel:null,jqCtxt=this;
jqCtxt.each(function(){var overflowState,ofCtxt=$(this),jqSel,jqSelLen,displayState=[],i=0,forceMore;
if(!!sel){jqSel=ofCtxt.find(sel).each(function(){displayState.push($(this).css("display"))
}).css("display","none");
jqSelLen=jqSel.length;
forceMore=false
}else{jqSel=ofCtxt;
jqSelLen=1
}ofCtxt.css({zoom:1,"word-wrap":"break-word"});
i=0;
do{if(!!sel){overflowState=_flowText(ofCtxt,jqSel[i],displayState[i],forceMore)
}else{overflowState=_flowText(ofCtxt,jqSel[i])
}if(overflowState===0){i++
}else{if(overflowState===1){forceMore=true;
$(jqSel[i]).css({display:"none"});
jqSelLen=i;
i--
}else{i=-1
}}}while(i>0&&i<jqSelLen)
})
}return this
}
})(jQuery);